dashboard_ctrl.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. import { PanelModel } from './panel_model';
  6. import { GRID_CELL_HEIGHT, GRID_CELL_VMARGIN } from 'app/core/constants';
  7. export class DashboardCtrl implements PanelContainer {
  8. dashboard: DashboardModel;
  9. dashboardViewState: any;
  10. loadedFallbackDashboard: boolean;
  11. editTab: number;
  12. /** @ngInject */
  13. constructor(
  14. private $scope,
  15. private $rootScope,
  16. private keybindingSrv,
  17. private timeSrv,
  18. private variableSrv,
  19. private alertingSrv,
  20. private dashboardSrv,
  21. private unsavedChangesSrv,
  22. private dashboardViewStateSrv,
  23. public playlistSrv,
  24. private panelLoader,
  25. private $location
  26. ) {
  27. // temp hack due to way dashboards are loaded
  28. // can't use controllerAs on route yet
  29. $scope.ctrl = this;
  30. // TODO: break out settings view to separate view & controller
  31. this.editTab = 0;
  32. // funcs called from React component bindings and needs this binding
  33. this.getPanelContainer = this.getPanelContainer.bind(this);
  34. }
  35. setupDashboard(data) {
  36. try {
  37. this.setupDashboardInternal(data);
  38. } catch (err) {
  39. this.onInitFailed(err, 'Dashboard init failed', true);
  40. }
  41. }
  42. setupDashboardInternal(data) {
  43. const dashboard = this.dashboardSrv.create(data.dashboard, data.meta);
  44. this.dashboardSrv.setCurrent(dashboard);
  45. // init services
  46. this.timeSrv.init(dashboard);
  47. this.alertingSrv.init(dashboard, data.alerts);
  48. // template values service needs to initialize completely before
  49. // the rest of the dashboard can load
  50. this.variableSrv
  51. .init(dashboard)
  52. // template values failes are non fatal
  53. .catch(this.onInitFailed.bind(this, 'Templating init failed', false))
  54. // continue
  55. .finally(() => {
  56. this.dashboard = dashboard;
  57. this.dashboard.processRepeats();
  58. this.autofitPanels();
  59. this.unsavedChangesSrv.init(dashboard, this.$scope);
  60. // TODO refactor ViewStateSrv
  61. this.$scope.dashboard = dashboard;
  62. this.dashboardViewState = this.dashboardViewStateSrv.create(this.$scope);
  63. this.keybindingSrv.setupDashboardBindings(this.$scope, dashboard);
  64. this.dashboard.updateSubmenuVisibility();
  65. this.setWindowTitleAndTheme();
  66. this.$scope.appEvent('dashboard-initialized', dashboard);
  67. })
  68. .catch(this.onInitFailed.bind(this, 'Dashboard init failed', true));
  69. }
  70. autofitPanels() {
  71. if (this.$location.search().autofitpanels) {
  72. let maxRows = Math.max(
  73. ...this.dashboard.panels.map(panel => {
  74. return panel.gridPos.h + panel.gridPos.y;
  75. })
  76. );
  77. //Consider navbar and submenu controls, padding and margin
  78. let availableHeight = window.innerHeight - 40;
  79. let availableRows = Math.floor(availableHeight / (GRID_CELL_HEIGHT + GRID_CELL_VMARGIN));
  80. let scaleFactor = maxRows / availableRows;
  81. this.dashboard.panels.forEach((panel, i) => {
  82. panel.gridPos.y = Math.round(panel.gridPos.y / scaleFactor) || 1;
  83. panel.gridPos.h = Math.round(panel.gridPos.h / scaleFactor) || 1;
  84. });
  85. this.dashboard.meta.autofitpanels = true;
  86. console.log(this.dashboard);
  87. }
  88. }
  89. onInitFailed(msg, fatal, err) {
  90. console.log(msg, err);
  91. if (err.data && err.data.message) {
  92. err.message = err.data.message;
  93. } else if (!err.message) {
  94. err = { message: err.toString() };
  95. }
  96. this.$scope.appEvent('alert-error', [msg, err.message]);
  97. // protect against recursive fallbacks
  98. if (fatal && !this.loadedFallbackDashboard) {
  99. this.loadedFallbackDashboard = true;
  100. this.setupDashboard({ dashboard: { title: 'Dashboard Init failed' } });
  101. }
  102. }
  103. templateVariableUpdated() {
  104. this.dashboard.processRepeats();
  105. }
  106. setWindowTitleAndTheme() {
  107. window.document.title = config.window_title_prefix + this.dashboard.title;
  108. }
  109. showJsonEditor(evt, options) {
  110. var editScope = this.$rootScope.$new();
  111. editScope.object = options.object;
  112. editScope.updateHandler = options.updateHandler;
  113. this.$scope.appEvent('show-dash-editor', {
  114. src: 'public/app/partials/edit_json.html',
  115. scope: editScope,
  116. });
  117. }
  118. getDashboard() {
  119. return this.dashboard;
  120. }
  121. getPanelLoader() {
  122. return this.panelLoader;
  123. }
  124. timezoneChanged() {
  125. this.$rootScope.$broadcast('refresh');
  126. }
  127. getPanelContainer() {
  128. return this;
  129. }
  130. onRemovingPanel(evt, options) {
  131. options = options || {};
  132. if (!options.panelId) {
  133. return;
  134. }
  135. var panelInfo = this.dashboard.getPanelInfoById(options.panelId);
  136. this.removePanel(panelInfo.panel, true);
  137. }
  138. removePanel(panel: PanelModel, ask: boolean) {
  139. // confirm deletion
  140. if (ask !== false) {
  141. var text2, confirmText;
  142. if (panel.alert) {
  143. text2 = 'Panel includes an alert rule, removing panel will also remove alert rule';
  144. confirmText = 'YES';
  145. }
  146. this.$scope.appEvent('confirm-modal', {
  147. title: 'Remove Panel',
  148. text: 'Are you sure you want to remove this panel?',
  149. text2: text2,
  150. icon: 'fa-trash',
  151. confirmText: confirmText,
  152. yesText: 'Remove',
  153. onConfirm: () => {
  154. this.removePanel(panel, false);
  155. },
  156. });
  157. return;
  158. }
  159. this.dashboard.removePanel(panel);
  160. }
  161. init(dashboard) {
  162. this.$scope.onAppEvent('show-json-editor', this.showJsonEditor.bind(this));
  163. this.$scope.onAppEvent('template-variable-value-updated', this.templateVariableUpdated.bind(this));
  164. this.$scope.onAppEvent('panel-remove', this.onRemovingPanel.bind(this));
  165. this.setupDashboard(dashboard);
  166. }
  167. }
  168. coreModule.controller('DashboardCtrl', DashboardCtrl);