dashboard_ctrl.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. ///<reference path="../../headers/common.d.ts" />
  2. import config from 'app/core/config';
  3. import angular from 'angular';
  4. import moment from 'moment';
  5. import _ from 'lodash';
  6. import coreModule from 'app/core/core_module';
  7. export class DashboardCtrl {
  8. /** @ngInject */
  9. constructor(
  10. private $scope,
  11. private $rootScope,
  12. keybindingSrv,
  13. timeSrv,
  14. variableSrv,
  15. alertingSrv,
  16. dashboardSrv,
  17. unsavedChangesSrv,
  18. dynamicDashboardSrv,
  19. dashboardViewStateSrv,
  20. contextSrv,
  21. alertSrv,
  22. $timeout) {
  23. $scope.editor = { index: 0 };
  24. var resizeEventTimeout;
  25. $scope.setupDashboard = function(data) {
  26. try {
  27. $scope.setupDashboardInternal(data);
  28. } catch (err) {
  29. $scope.onInitFailed(err, 'Dashboard init failed', true);
  30. }
  31. };
  32. $scope.setupDashboardInternal = function(data) {
  33. var dashboard = dashboardSrv.create(data.dashboard, data.meta);
  34. dashboardSrv.setCurrent(dashboard);
  35. // init services
  36. timeSrv.init(dashboard);
  37. alertingSrv.init(dashboard, data.alerts);
  38. // template values service needs to initialize completely before
  39. // the rest of the dashboard can load
  40. variableSrv.init(dashboard)
  41. // template values failes are non fatal
  42. .catch($scope.onInitFailed.bind(this, 'Templating init failed', false))
  43. // continue
  44. .finally(function() {
  45. dynamicDashboardSrv.init(dashboard);
  46. dynamicDashboardSrv.process();
  47. unsavedChangesSrv.init(dashboard, $scope);
  48. $scope.dashboard = dashboard;
  49. $scope.dashboardMeta = dashboard.meta;
  50. $scope.dashboardViewState = dashboardViewStateSrv.create($scope);
  51. keybindingSrv.setupDashboardBindings($scope, dashboard);
  52. $scope.dashboard.updateSubmenuVisibility();
  53. $scope.setWindowTitleAndTheme();
  54. $scope.appEvent("dashboard-initialized", $scope.dashboard);
  55. })
  56. .catch($scope.onInitFailed.bind(this, 'Dashboard init failed', true));
  57. };
  58. $scope.onInitFailed = function(msg, fatal, err) {
  59. console.log(msg, err);
  60. if (err.data && err.data.message) {
  61. err.message = err.data.message;
  62. } else if (!err.message) {
  63. err = {message: err.toString()};
  64. }
  65. $scope.appEvent("alert-error", [msg, err.message]);
  66. // protect against recursive fallbacks
  67. if (fatal && !$scope.loadedFallbackDashboard) {
  68. $scope.loadedFallbackDashboard = true;
  69. $scope.setupDashboard({dashboard: {title: 'Dashboard Init failed'}});
  70. }
  71. };
  72. $scope.templateVariableUpdated = function() {
  73. dynamicDashboardSrv.process();
  74. };
  75. $scope.setWindowTitleAndTheme = function() {
  76. window.document.title = config.window_title_prefix + $scope.dashboard.title;
  77. };
  78. $scope.broadcastRefresh = function() {
  79. $rootScope.$broadcast('refresh');
  80. };
  81. $scope.addRowDefault = function() {
  82. $scope.dashboard.addEmptyRow();
  83. };
  84. $scope.showJsonEditor = function(evt, options) {
  85. var editScope = $rootScope.$new();
  86. editScope.object = options.object;
  87. editScope.updateHandler = options.updateHandler;
  88. $scope.appEvent('show-dash-editor', { src: 'public/app/partials/edit_json.html', scope: editScope });
  89. };
  90. $scope.registerWindowResizeEvent = function() {
  91. angular.element(window).bind('resize', function() {
  92. $timeout.cancel(resizeEventTimeout);
  93. resizeEventTimeout = $timeout(function() { $scope.$broadcast('render'); }, 200);
  94. });
  95. $scope.$on('$destroy', function() {
  96. angular.element(window).unbind('resize');
  97. $scope.dashboard.destroy();
  98. });
  99. };
  100. $scope.timezoneChanged = function() {
  101. $rootScope.$broadcast("refresh");
  102. };
  103. }
  104. init(dashboard) {
  105. this.$scope.onAppEvent('show-json-editor', this.$scope.showJsonEditor);
  106. this.$scope.onAppEvent('template-variable-value-updated', this.$scope.templateVariableUpdated);
  107. this.$scope.setupDashboard(dashboard);
  108. this.$scope.registerWindowResizeEvent();
  109. }
  110. }
  111. coreModule.controller('DashboardCtrl', DashboardCtrl);