dashboard_ctrl.ts 4.3 KB

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