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.dashboard.updateSubmenuVisibility();
  57. this.dashboard.autoFitPanels(window.innerHeight);
  58. this.unsavedChangesSrv.init(dashboard, this.$scope);
  59. // TODO refactor ViewStateSrv
  60. this.$scope.dashboard = dashboard;
  61. this.dashboardViewState = this.dashboardViewStateSrv.create(this.$scope);
  62. this.keybindingSrv.setupDashboardBindings(this.$scope, dashboard);
  63. this.setWindowTitleAndTheme();
  64. this.$scope.appEvent('dashboard-initialized', dashboard);
  65. })
  66. .catch(this.onInitFailed.bind(this, 'Dashboard init failed', true));
  67. }
  68. onInitFailed(msg, fatal, err) {
  69. console.log(msg, err);
  70. if (err.data && err.data.message) {
  71. err.message = err.data.message;
  72. } else if (!err.message) {
  73. err = { message: err.toString() };
  74. }
  75. this.$scope.appEvent('alert-error', [msg, err.message]);
  76. // protect against recursive fallbacks
  77. if (fatal && !this.loadedFallbackDashboard) {
  78. this.loadedFallbackDashboard = true;
  79. this.setupDashboard({ dashboard: { title: 'Dashboard Init failed' } });
  80. }
  81. }
  82. templateVariableUpdated() {
  83. this.dashboard.processRepeats();
  84. }
  85. setWindowTitleAndTheme() {
  86. window.document.title = config.windowTitlePrefix + this.dashboard.title;
  87. }
  88. showJsonEditor(evt, options) {
  89. const editScope = this.$rootScope.$new();
  90. editScope.object = options.object;
  91. editScope.updateHandler = options.updateHandler;
  92. this.$scope.appEvent('show-dash-editor', {
  93. src: 'public/app/partials/edit_json.html',
  94. scope: editScope,
  95. });
  96. }
  97. getDashboard() {
  98. return this.dashboard;
  99. }
  100. getPanelLoader() {
  101. return this.panelLoader;
  102. }
  103. timezoneChanged() {
  104. this.$rootScope.$broadcast('refresh');
  105. }
  106. getPanelContainer() {
  107. return this;
  108. }
  109. onRemovingPanel(evt, options) {
  110. options = options || {};
  111. if (!options.panelId) {
  112. return;
  113. }
  114. const panelInfo = this.dashboard.getPanelInfoById(options.panelId);
  115. this.removePanel(panelInfo.panel, true);
  116. }
  117. removePanel(panel: PanelModel, ask: boolean) {
  118. // confirm deletion
  119. if (ask !== false) {
  120. let text2, confirmText;
  121. if (panel.alert) {
  122. text2 = 'Panel includes an alert rule, removing panel will also remove alert rule';
  123. confirmText = 'YES';
  124. }
  125. this.$scope.appEvent('confirm-modal', {
  126. title: 'Remove Panel',
  127. text: 'Are you sure you want to remove this panel?',
  128. text2: text2,
  129. icon: 'fa-trash',
  130. confirmText: confirmText,
  131. yesText: 'Remove',
  132. onConfirm: () => {
  133. this.removePanel(panel, false);
  134. },
  135. });
  136. return;
  137. }
  138. this.dashboard.removePanel(panel);
  139. }
  140. init(dashboard) {
  141. this.$scope.onAppEvent('show-json-editor', this.showJsonEditor.bind(this));
  142. this.$scope.onAppEvent('template-variable-value-updated', this.templateVariableUpdated.bind(this));
  143. this.$scope.onAppEvent('panel-remove', this.onRemovingPanel.bind(this));
  144. this.setupDashboard(dashboard);
  145. }
  146. }
  147. coreModule.controller('DashboardCtrl', DashboardCtrl);