dashboard_ctrl.ts 4.3 KB

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