panel_ctrl.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. ///<reference path="../../headers/common.d.ts" />
  2. import config from 'app/core/config';
  3. import _ from 'lodash';
  4. import angular from 'angular';
  5. import $ from 'jquery';
  6. import {profiler} from 'app/core/profiler';
  7. const TITLE_HEIGHT = 25;
  8. const EMPTY_TITLE_HEIGHT = 9;
  9. const PANEL_PADDING = 5;
  10. const PANEL_BORDER = 2;
  11. import {Emitter} from 'app/core/core';
  12. export class PanelCtrl {
  13. panel: any;
  14. row: any;
  15. dashboard: any;
  16. editorTabIndex: number;
  17. pluginName: string;
  18. pluginId: string;
  19. editorTabs: any;
  20. $scope: any;
  21. $injector: any;
  22. $timeout: any;
  23. fullscreen: boolean;
  24. inspector: any;
  25. editModeInitiated: boolean;
  26. editorHelpIndex: number;
  27. editMode: any;
  28. height: any;
  29. containerHeight: any;
  30. events: Emitter;
  31. timing: any;
  32. constructor($scope, $injector) {
  33. this.$injector = $injector;
  34. this.$scope = $scope;
  35. this.$timeout = $injector.get('$timeout');
  36. this.editorTabIndex = 0;
  37. this.events = new Emitter();
  38. this.timing = {};
  39. var plugin = config.panels[this.panel.type];
  40. if (plugin) {
  41. this.pluginId = plugin.id;
  42. this.pluginName = plugin.name;
  43. }
  44. $scope.$on("refresh", () => this.refresh());
  45. $scope.$on("render", () => this.render());
  46. $scope.$on("$destroy", () => {
  47. this.events.emit('panel-teardown');
  48. this.events.removeAllListeners();
  49. });
  50. }
  51. init() {
  52. this.calculatePanelHeight();
  53. this.publishAppEvent('panel-initialized', {scope: this.$scope});
  54. this.events.emit('panel-initialized');
  55. }
  56. renderingCompleted() {
  57. profiler.renderingCompleted(this.panel.id, this.timing);
  58. }
  59. refresh() {
  60. this.events.emit('refresh', null);
  61. }
  62. publishAppEvent(evtName, evt) {
  63. this.$scope.$root.appEvent(evtName, evt);
  64. }
  65. changeView(fullscreen, edit) {
  66. this.publishAppEvent('panel-change-view', {
  67. fullscreen: fullscreen, edit: edit, panelId: this.panel.id
  68. });
  69. }
  70. viewPanel() {
  71. this.changeView(true, false);
  72. }
  73. editPanel() {
  74. this.changeView(true, true);
  75. }
  76. exitFullscreen() {
  77. this.changeView(false, false);
  78. }
  79. initEditMode() {
  80. this.editorTabs = [];
  81. this.addEditorTab('General', 'public/app/partials/panelgeneral.html');
  82. this.editModeInitiated = true;
  83. this.events.emit('init-edit-mode', null);
  84. var urlTab = (this.$injector.get('$routeParams').tab || '').toLowerCase();
  85. if (urlTab) {
  86. this.editorTabs.forEach((tab, i) => {
  87. if (tab.title.toLowerCase() === urlTab) {
  88. this.editorTabIndex = i;
  89. }
  90. });
  91. }
  92. }
  93. changeTab(newIndex) {
  94. this.editorTabIndex = newIndex;
  95. var route = this.$injector.get('$route');
  96. route.current.params.tab = this.editorTabs[newIndex].title.toLowerCase();
  97. route.updateParams();
  98. }
  99. addEditorTab(title, directiveFn, index?) {
  100. var editorTab = {title, directiveFn};
  101. if (_.isString(directiveFn)) {
  102. editorTab.directiveFn = function() {
  103. return {templateUrl: directiveFn};
  104. };
  105. }
  106. if (index) {
  107. this.editorTabs.splice(index, 0, editorTab);
  108. } else {
  109. this.editorTabs.push(editorTab);
  110. }
  111. }
  112. getMenu() {
  113. let menu = [];
  114. menu.push({text: 'View', click: 'ctrl.viewPanel(); dismiss();'});
  115. menu.push({text: 'Edit', click: 'ctrl.editPanel(); dismiss();', role: 'Editor'});
  116. if (!this.fullscreen) { // duplication is not supported in fullscreen mode
  117. menu.push({ text: 'Duplicate', click: 'ctrl.duplicate()', role: 'Editor' });
  118. }
  119. menu.push({text: 'Share', click: 'ctrl.sharePanel(); dismiss();'});
  120. return menu;
  121. }
  122. getExtendedMenu() {
  123. var actions = [{text: 'Panel JSON', click: 'ctrl.editPanelJson(); dismiss();'}];
  124. this.events.emit('init-panel-actions', actions);
  125. return actions;
  126. }
  127. otherPanelInFullscreenMode() {
  128. return this.dashboard.meta.fullscreen && !this.fullscreen;
  129. }
  130. calculatePanelHeight() {
  131. if (this.fullscreen) {
  132. var docHeight = $(window).height();
  133. var editHeight = Math.floor(docHeight * 0.4);
  134. var fullscreenHeight = Math.floor(docHeight * 0.8);
  135. this.containerHeight = this.editMode ? editHeight : fullscreenHeight;
  136. } else {
  137. this.containerHeight = this.panel.height || this.row.height;
  138. if (_.isString(this.containerHeight)) {
  139. this.containerHeight = parseInt(this.containerHeight.replace('px', ''), 10);
  140. }
  141. }
  142. this.height = this.containerHeight - (PANEL_BORDER + PANEL_PADDING + (this.panel.title ? TITLE_HEIGHT : EMPTY_TITLE_HEIGHT));
  143. }
  144. render(payload?) {
  145. // ignore if other panel is in fullscreen mode
  146. if (this.otherPanelInFullscreenMode()) {
  147. return;
  148. }
  149. this.calculatePanelHeight();
  150. this.timing.renderStart = new Date().getTime();
  151. this.events.emit('render', payload);
  152. }
  153. toggleEditorHelp(index) {
  154. if (this.editorHelpIndex === index) {
  155. this.editorHelpIndex = null;
  156. return;
  157. }
  158. this.editorHelpIndex = index;
  159. }
  160. duplicate() {
  161. this.dashboard.duplicatePanel(this.panel, this.row);
  162. this.$timeout(() => {
  163. this.$scope.$root.$broadcast('render');
  164. });
  165. }
  166. updateColumnSpan(span) {
  167. this.panel.span = Math.min(Math.max(Math.floor(this.panel.span + span), 1), 12);
  168. this.row.panelSpanChanged();
  169. this.$timeout(() => {
  170. this.render();
  171. });
  172. }
  173. removePanel() {
  174. this.row.removePanel(this.panel);
  175. }
  176. editPanelJson() {
  177. this.publishAppEvent('show-json-editor', {
  178. object: this.panel,
  179. updateHandler: this.replacePanel.bind(this)
  180. });
  181. }
  182. replacePanel(newPanel, oldPanel) {
  183. var row = this.row;
  184. var index = _.indexOf(this.row.panels, oldPanel);
  185. this.row.panels.splice(index, 1);
  186. // adding it back needs to be done in next digest
  187. this.$timeout(() => {
  188. newPanel.id = oldPanel.id;
  189. newPanel.span = oldPanel.span;
  190. this.row.panels.splice(index, 0, newPanel);
  191. });
  192. }
  193. sharePanel() {
  194. var shareScope = this.$scope.$new();
  195. shareScope.panel = this.panel;
  196. shareScope.dashboard = this.dashboard;
  197. this.publishAppEvent('show-modal', {
  198. src: 'public/app/features/dashboard/partials/shareModal.html',
  199. scope: shareScope
  200. });
  201. }
  202. openInspector() {
  203. var modalScope = this.$scope.$new();
  204. modalScope.panel = this.panel;
  205. modalScope.dashboard = this.dashboard;
  206. modalScope.inspector = $.extend(true, {}, this.inspector);
  207. this.publishAppEvent('show-modal', {
  208. src: 'public/app/partials/inspector.html',
  209. scope: modalScope
  210. });
  211. }
  212. }