dashboard_ctrl.ts 4.8 KB

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