dashboard_ctrl.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import config from 'app/core/config';
  2. import coreModule from 'app/core/core_module';
  3. import { DashboardModel } from './dashboard_model';
  4. import { PanelModel } from './panel_model';
  5. export class DashboardCtrl {
  6. dashboard: DashboardModel;
  7. dashboardViewState: any;
  8. loadedFallbackDashboard: boolean;
  9. editTab: number;
  10. /** @ngInject */
  11. constructor(
  12. private $scope,
  13. private $rootScope,
  14. private keybindingSrv,
  15. private timeSrv,
  16. private variableSrv,
  17. private alertingSrv,
  18. private dashboardSrv,
  19. private unsavedChangesSrv,
  20. private dashboardViewStateSrv,
  21. public playlistSrv
  22. ) {
  23. // temp hack due to way dashboards are loaded
  24. // can't use controllerAs on route yet
  25. $scope.ctrl = this;
  26. // TODO: break out settings view to separate view & controller
  27. this.editTab = 0;
  28. // funcs called from React component bindings and needs this binding
  29. this.getPanelContainer = this.getPanelContainer.bind(this);
  30. }
  31. setupDashboard(data) {
  32. try {
  33. this.setupDashboardInternal(data);
  34. } catch (err) {
  35. this.onInitFailed(err, 'Dashboard init failed', true);
  36. }
  37. }
  38. setupDashboardInternal(data) {
  39. const dashboard = this.dashboardSrv.create(data.dashboard, data.meta);
  40. this.dashboardSrv.setCurrent(dashboard);
  41. // init services
  42. this.timeSrv.init(dashboard);
  43. this.alertingSrv.init(dashboard, data.alerts);
  44. // template values service needs to initialize completely before
  45. // the rest of the dashboard can load
  46. this.variableSrv
  47. .init(dashboard)
  48. // template values failes are non fatal
  49. .catch(this.onInitFailed.bind(this, 'Templating init failed', false))
  50. // continue
  51. .finally(() => {
  52. this.dashboard = dashboard;
  53. this.dashboard.processRepeats();
  54. this.unsavedChangesSrv.init(dashboard, this.$scope);
  55. // TODO refactor ViewStateSrv
  56. this.$scope.dashboard = dashboard;
  57. this.dashboardViewState = this.dashboardViewStateSrv.create(this.$scope);
  58. this.keybindingSrv.setupDashboardBindings(this.$scope, dashboard);
  59. this.dashboard.updateSubmenuVisibility();
  60. this.setWindowTitleAndTheme();
  61. this.$scope.appEvent('dashboard-initialized', dashboard);
  62. })
  63. .catch(this.onInitFailed.bind(this, 'Dashboard init failed', true));
  64. }
  65. onInitFailed(msg, fatal, err) {
  66. console.log(msg, err);
  67. if (err.data && err.data.message) {
  68. err.message = err.data.message;
  69. } else if (!err.message) {
  70. err = { message: err.toString() };
  71. }
  72. this.$scope.appEvent('alert-error', [msg, err.message]);
  73. // protect against recursive fallbacks
  74. if (fatal && !this.loadedFallbackDashboard) {
  75. this.loadedFallbackDashboard = true;
  76. this.setupDashboard({ dashboard: { title: 'Dashboard Init failed' } });
  77. }
  78. }
  79. templateVariableUpdated() {
  80. this.dashboard.processRepeats();
  81. }
  82. setWindowTitleAndTheme() {
  83. window.document.title = config.window_title_prefix + this.dashboard.title;
  84. }
  85. showJsonEditor(evt, options) {
  86. var editScope = this.$rootScope.$new();
  87. editScope.object = options.object;
  88. editScope.updateHandler = options.updateHandler;
  89. this.$scope.appEvent('show-dash-editor', {
  90. src: 'public/app/partials/edit_json.html',
  91. scope: editScope,
  92. });
  93. }
  94. getDashboard() {
  95. return this.dashboard;
  96. }
  97. timezoneChanged() {
  98. this.$rootScope.$broadcast('refresh');
  99. }
  100. getPanelContainer() {
  101. return this;
  102. }
  103. onRemovingPanel(evt, options) {
  104. options = options || {};
  105. if (!options.panelId) {
  106. return;
  107. }
  108. var panelInfo = this.dashboard.getPanelInfoById(options.panelId);
  109. this.removePanel(panelInfo.panel, true);
  110. }
  111. removePanel(panel: PanelModel, ask: boolean) {
  112. // confirm deletion
  113. if (ask !== false) {
  114. var text2, confirmText;
  115. if (panel.alert) {
  116. text2 = 'Panel includes an alert rule, removing panel will also remove alert rule';
  117. confirmText = 'YES';
  118. }
  119. this.$scope.appEvent('confirm-modal', {
  120. title: 'Remove Panel',
  121. text: 'Are you sure you want to remove this panel?',
  122. text2: text2,
  123. icon: 'fa-trash',
  124. confirmText: confirmText,
  125. yesText: 'Remove',
  126. onConfirm: () => {
  127. this.removePanel(panel, false);
  128. },
  129. });
  130. return;
  131. }
  132. this.dashboard.removePanel(panel);
  133. }
  134. init(dashboard) {
  135. this.$scope.onAppEvent('show-json-editor', this.showJsonEditor.bind(this));
  136. this.$scope.onAppEvent('template-variable-value-updated', this.templateVariableUpdated.bind(this));
  137. this.$scope.onAppEvent('panel-remove', this.onRemovingPanel.bind(this));
  138. this.setupDashboard(dashboard);
  139. }
  140. }
  141. coreModule.controller('DashboardCtrl', DashboardCtrl);