dashboard_ctrl.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. export class DashboardCtrl implements PanelContainer {
  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. private panelLoader
  23. ) {
  24. // temp hack due to way dashboards are loaded
  25. // can't use controllerAs on route yet
  26. $scope.ctrl = this;
  27. // TODO: break out settings view to separate view & controller
  28. this.editTab = 0;
  29. // funcs called from React component bindings and needs this binding
  30. this.getPanelContainer = this.getPanelContainer.bind(this);
  31. }
  32. setupDashboard(data) {
  33. try {
  34. this.setupDashboardInternal(data);
  35. } catch (err) {
  36. this.onInitFailed(err, 'Dashboard init failed', true);
  37. }
  38. }
  39. setupDashboardInternal(data) {
  40. const dashboard = this.dashboardSrv.create(data.dashboard, data.meta);
  41. this.dashboardSrv.setCurrent(dashboard);
  42. // init services
  43. this.timeSrv.init(dashboard);
  44. this.alertingSrv.init(dashboard, data.alerts);
  45. // template values service needs to initialize completely before
  46. // the rest of the dashboard can load
  47. this.variableSrv
  48. .init(dashboard)
  49. // template values failes are non fatal
  50. .catch(this.onInitFailed.bind(this, 'Templating init failed', false))
  51. // continue
  52. .finally(() => {
  53. this.dashboard = dashboard;
  54. this.dashboard.processRepeats();
  55. this.unsavedChangesSrv.init(dashboard, this.$scope);
  56. // TODO refactor ViewStateSrv
  57. this.$scope.dashboard = dashboard;
  58. this.dashboardViewState = this.dashboardViewStateSrv.create(
  59. this.$scope
  60. );
  61. this.keybindingSrv.setupDashboardBindings(this.$scope, dashboard);
  62. this.dashboard.updateSubmenuVisibility();
  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.window_title_prefix + this.dashboard.title;
  87. }
  88. showJsonEditor(evt, options) {
  89. var 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. init(dashboard) {
  110. this.$scope.onAppEvent('show-json-editor', this.showJsonEditor.bind(this));
  111. this.$scope.onAppEvent(
  112. 'template-variable-value-updated',
  113. this.templateVariableUpdated.bind(this)
  114. );
  115. this.setupDashboard(dashboard);
  116. }
  117. }
  118. coreModule.controller('DashboardCtrl', DashboardCtrl);