dashboard_ctrl.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 } from 'app/core/constants';
  7. import { PanelLinksEditorCtrl } from '../panellinks/module';
  8. export class DashboardCtrl implements PanelContainer {
  9. dashboard: DashboardModel;
  10. dashboardViewState: any;
  11. loadedFallbackDashboard: boolean;
  12. editTab: number;
  13. /** @ngInject */
  14. constructor(
  15. private $scope,
  16. private $rootScope,
  17. private keybindingSrv,
  18. private timeSrv,
  19. private variableSrv,
  20. private alertingSrv,
  21. private dashboardSrv,
  22. private unsavedChangesSrv,
  23. private dashboardViewStateSrv,
  24. public playlistSrv,
  25. private panelLoader
  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. console.log(this.dashboard.panels);
  59. let maxRows = Math.max(
  60. ...this.dashboard.panels.map(panel => {
  61. return panel.gridPos.h + panel.gridPos.y;
  62. })
  63. );
  64. console.log('maxRows: ' + maxRows);
  65. //Consider navbar and submenu controls
  66. let availableHeight = window.innerHeight - 280;
  67. let availableRows = Math.floor(availableHeight / GRID_CELL_HEIGHT);
  68. console.log('availableRows: ' + availableRows);
  69. if (maxRows > availableRows) {
  70. let scaleFactor = maxRows / availableRows;
  71. console.log(scaleFactor);
  72. this.dashboard.panels.forEach((panel, i) => {
  73. console.log(i);
  74. console.log(panel.gridPos);
  75. panel.gridPos.y = Math.floor(panel.gridPos.y / scaleFactor) || 1;
  76. panel.gridPos.h = Math.floor(panel.gridPos.h / scaleFactor) || 1;
  77. console.log(panel.gridPos);
  78. });
  79. }
  80. console.log(this.dashboard.panels);
  81. this.unsavedChangesSrv.init(dashboard, this.$scope);
  82. // TODO refactor ViewStateSrv
  83. this.$scope.dashboard = dashboard;
  84. this.dashboardViewState = this.dashboardViewStateSrv.create(this.$scope);
  85. this.keybindingSrv.setupDashboardBindings(this.$scope, dashboard);
  86. this.dashboard.updateSubmenuVisibility();
  87. this.setWindowTitleAndTheme();
  88. this.$scope.appEvent('dashboard-initialized', dashboard);
  89. })
  90. .catch(this.onInitFailed.bind(this, 'Dashboard init failed', true));
  91. }
  92. onInitFailed(msg, fatal, err) {
  93. console.log(msg, err);
  94. if (err.data && err.data.message) {
  95. err.message = err.data.message;
  96. } else if (!err.message) {
  97. err = { message: err.toString() };
  98. }
  99. this.$scope.appEvent('alert-error', [msg, err.message]);
  100. // protect against recursive fallbacks
  101. if (fatal && !this.loadedFallbackDashboard) {
  102. this.loadedFallbackDashboard = true;
  103. this.setupDashboard({ dashboard: { title: 'Dashboard Init failed' } });
  104. }
  105. }
  106. templateVariableUpdated() {
  107. this.dashboard.processRepeats();
  108. }
  109. setWindowTitleAndTheme() {
  110. window.document.title = config.window_title_prefix + this.dashboard.title;
  111. }
  112. showJsonEditor(evt, options) {
  113. var editScope = this.$rootScope.$new();
  114. editScope.object = options.object;
  115. editScope.updateHandler = options.updateHandler;
  116. this.$scope.appEvent('show-dash-editor', {
  117. src: 'public/app/partials/edit_json.html',
  118. scope: editScope,
  119. });
  120. }
  121. getDashboard() {
  122. return this.dashboard;
  123. }
  124. getPanelLoader() {
  125. return this.panelLoader;
  126. }
  127. timezoneChanged() {
  128. this.$rootScope.$broadcast('refresh');
  129. }
  130. getPanelContainer() {
  131. return this;
  132. }
  133. onRemovingPanel(evt, options) {
  134. options = options || {};
  135. if (!options.panelId) {
  136. return;
  137. }
  138. var panelInfo = this.dashboard.getPanelInfoById(options.panelId);
  139. this.removePanel(panelInfo.panel, true);
  140. }
  141. removePanel(panel: PanelModel, ask: boolean) {
  142. // confirm deletion
  143. if (ask !== false) {
  144. var text2, confirmText;
  145. if (panel.alert) {
  146. text2 = 'Panel includes an alert rule, removing panel will also remove alert rule';
  147. confirmText = 'YES';
  148. }
  149. this.$scope.appEvent('confirm-modal', {
  150. title: 'Remove Panel',
  151. text: 'Are you sure you want to remove this panel?',
  152. text2: text2,
  153. icon: 'fa-trash',
  154. confirmText: confirmText,
  155. yesText: 'Remove',
  156. onConfirm: () => {
  157. this.removePanel(panel, false);
  158. },
  159. });
  160. return;
  161. }
  162. this.dashboard.removePanel(panel);
  163. }
  164. init(dashboard) {
  165. this.$scope.onAppEvent('show-json-editor', this.showJsonEditor.bind(this));
  166. this.$scope.onAppEvent('template-variable-value-updated', this.templateVariableUpdated.bind(this));
  167. this.$scope.onAppEvent('panel-remove', this.onRemovingPanel.bind(this));
  168. this.setupDashboard(dashboard);
  169. }
  170. }
  171. coreModule.controller('DashboardCtrl', DashboardCtrl);