dashboard_ctrl.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. private panelLoader) {
  22. // temp hack due to way dashboards are loaded
  23. // can't use controllerAs on route yet
  24. $scope.ctrl = this;
  25. // TODO: break out settings view to separate view & controller
  26. this.editTab = 0;
  27. // funcs called from React component bindings and needs this binding
  28. this.getPanelContainer = this.getPanelContainer.bind(this);
  29. }
  30. setupDashboard(data) {
  31. try {
  32. this.setupDashboardInternal(data);
  33. } catch (err) {
  34. this.onInitFailed(err, 'Dashboard init failed', true);
  35. }
  36. }
  37. setupDashboardInternal(data) {
  38. const dashboard = this.dashboardSrv.create(data.dashboard, data.meta);
  39. this.dashboardSrv.setCurrent(dashboard);
  40. // init services
  41. this.timeSrv.init(dashboard);
  42. this.alertingSrv.init(dashboard, data.alerts);
  43. // template values service needs to initialize completely before
  44. // the rest of the dashboard can load
  45. this.variableSrv.init(dashboard)
  46. // template values failes are non fatal
  47. .catch(this.onInitFailed.bind(this, 'Templating init failed', false))
  48. // continue
  49. .finally(() => {
  50. this.dashboard = dashboard;
  51. this.dashboard.processRepeats();
  52. this.unsavedChangesSrv.init(dashboard, this.$scope);
  53. // TODO refactor ViewStateSrv
  54. this.$scope.dashboard = dashboard;
  55. this.dashboardViewState = this.dashboardViewStateSrv.create(this.$scope);
  56. this.keybindingSrv.setupDashboardBindings(this.$scope, dashboard);
  57. this.dashboard.updateSubmenuVisibility();
  58. this.setWindowTitleAndTheme();
  59. this.$scope.appEvent("dashboard-initialized", dashboard);
  60. })
  61. .catch(this.onInitFailed.bind(this, 'Dashboard init failed', true));
  62. }
  63. onInitFailed(msg, fatal, err) {
  64. console.log(msg, err);
  65. if (err.data && err.data.message) {
  66. err.message = err.data.message;
  67. } else if (!err.message) {
  68. err = {message: err.toString()};
  69. }
  70. this.$scope.appEvent("alert-error", [msg, err.message]);
  71. // protect against recursive fallbacks
  72. if (fatal && !this.loadedFallbackDashboard) {
  73. this.loadedFallbackDashboard = true;
  74. this.setupDashboard({dashboard: {title: 'Dashboard Init failed'}});
  75. }
  76. }
  77. templateVariableUpdated() {
  78. this.dashboard.processRepeats();
  79. }
  80. setWindowTitleAndTheme() {
  81. window.document.title = config.window_title_prefix + this.dashboard.title;
  82. }
  83. showJsonEditor(evt, options) {
  84. var editScope = this.$rootScope.$new();
  85. editScope.object = options.object;
  86. editScope.updateHandler = options.updateHandler;
  87. this.$scope.appEvent('show-dash-editor', { src: 'public/app/partials/edit_json.html', scope: editScope });
  88. }
  89. getDashboard() {
  90. return this.dashboard;
  91. }
  92. getPanelLoader() {
  93. return this.panelLoader;
  94. }
  95. timezoneChanged() {
  96. this.$rootScope.$broadcast("refresh");
  97. }
  98. onFolderChange(folder) {
  99. this.dashboard.folderId = folder.id;
  100. this.dashboard.meta.folderId = folder.id;
  101. this.dashboard.meta.folderTitle= folder.title;
  102. }
  103. getPanelContainer() {
  104. console.log('DashboardCtrl: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);