dashboard_ctrl.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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(this.$scope);
  59. this.keybindingSrv.setupDashboardBindings(this.$scope, dashboard);
  60. this.dashboard.updateSubmenuVisibility();
  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.window_title_prefix + this.dashboard.title;
  85. }
  86. showJsonEditor(evt, options) {
  87. var 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. getPanelLoader() {
  99. return this.panelLoader;
  100. }
  101. timezoneChanged() {
  102. this.$rootScope.$broadcast('refresh');
  103. }
  104. getPanelContainer() {
  105. return this;
  106. }
  107. init(dashboard) {
  108. this.$scope.onAppEvent('show-json-editor', this.showJsonEditor.bind(this));
  109. this.$scope.onAppEvent('template-variable-value-updated', this.templateVariableUpdated.bind(this));
  110. this.setupDashboard(dashboard);
  111. }
  112. }
  113. coreModule.controller('DashboardCtrl', DashboardCtrl);