dashboard_ctrl.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 './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 dynamicDashboardSrv,
  21. private dashboardViewStateSrv,
  22. private panelLoader) {
  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.init(dashboard)
  47. // template values failes are non fatal
  48. .catch(this.onInitFailed.bind(this, 'Templating init failed', false))
  49. // continue
  50. .finally(() => {
  51. this.dashboard = dashboard;
  52. this.dynamicDashboardSrv.init(dashboard);
  53. this.dynamicDashboardSrv.process();
  54. this.unsavedChangesSrv.init(dashboard, this.$scope);
  55. // TODO refactor ViewStateSrv
  56. this.$scope.dashboard = dashboard;
  57. this.dashboardViewState = this.dashboardViewStateSrv.create(this.$scope);
  58. this.keybindingSrv.setupDashboardBindings(this.$scope, dashboard);
  59. this.dashboard.updateSubmenuVisibility();
  60. this.setWindowTitleAndTheme();
  61. this.$scope.appEvent("dashboard-initialized", dashboard);
  62. })
  63. .catch(this.onInitFailed.bind(this, 'Dashboard init failed', true));
  64. }
  65. onInitFailed(msg, fatal, err) {
  66. console.log(msg, err);
  67. if (err.data && err.data.message) {
  68. err.message = err.data.message;
  69. } else if (!err.message) {
  70. err = {message: err.toString()};
  71. }
  72. this.$scope.appEvent("alert-error", [msg, err.message]);
  73. // protect against recursive fallbacks
  74. if (fatal && !this.loadedFallbackDashboard) {
  75. this.loadedFallbackDashboard = true;
  76. this.setupDashboard({dashboard: {title: 'Dashboard Init failed'}});
  77. }
  78. }
  79. templateVariableUpdated() {
  80. this.dynamicDashboardSrv.process();
  81. }
  82. setWindowTitleAndTheme() {
  83. window.document.title = config.window_title_prefix + this.dashboard.title;
  84. }
  85. showJsonEditor(evt, options) {
  86. var editScope = this.$rootScope.$new();
  87. editScope.object = options.object;
  88. editScope.updateHandler = options.updateHandler;
  89. this.$scope.appEvent('show-dash-editor', { src: 'public/app/partials/edit_json.html', scope: editScope });
  90. }
  91. getDashboard() {
  92. return this.dashboard;
  93. }
  94. getPanelLoader() {
  95. return this.panelLoader;
  96. }
  97. timezoneChanged() {
  98. this.$rootScope.$broadcast("refresh");
  99. }
  100. onFolderChange(folder) {
  101. this.dashboard.folderId = folder.id;
  102. this.dashboard.meta.folderId = folder.id;
  103. this.dashboard.meta.folderTitle= folder.title;
  104. }
  105. getPanelContainer() {
  106. console.log('DashboardCtrl:getPanelContainer()');
  107. return this;
  108. }
  109. init(dashboard) {
  110. this.$scope.onAppEvent('show-json-editor', this.$scope.showJsonEditor);
  111. this.$scope.onAppEvent('template-variable-value-updated', this.$scope.templateVariableUpdated);
  112. this.setupDashboard(dashboard);
  113. }
  114. }
  115. coreModule.controller('DashboardCtrl', DashboardCtrl);