dashboard_ctrl.ts 3.9 KB

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