dashboard_ctrl.ts 4.9 KB

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