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