panel_ctrl.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. // we should do something interesting
  51. // with newly added panels
  52. if (this.panel.isNew) {
  53. delete this.panel.isNew;
  54. }
  55. }
  56. init() {
  57. this.calculatePanelHeight();
  58. this.publishAppEvent('panel-initialized', {scope: this.$scope});
  59. this.events.emit('panel-initialized');
  60. }
  61. renderingCompleted() {
  62. profiler.renderingCompleted(this.panel.id, this.timing);
  63. }
  64. refresh() {
  65. this.events.emit('refresh', null);
  66. }
  67. publishAppEvent(evtName, evt) {
  68. this.$scope.$root.appEvent(evtName, evt);
  69. }
  70. changeView(fullscreen, edit) {
  71. this.publishAppEvent('panel-change-view', {
  72. fullscreen: fullscreen, edit: edit, panelId: this.panel.id
  73. });
  74. }
  75. viewPanel() {
  76. this.changeView(true, false);
  77. }
  78. editPanel() {
  79. this.changeView(true, true);
  80. }
  81. exitFullscreen() {
  82. this.changeView(false, false);
  83. }
  84. initEditMode() {
  85. this.editorTabs = [];
  86. this.addEditorTab('General', 'public/app/partials/panelgeneral.html');
  87. this.editModeInitiated = true;
  88. this.events.emit('init-edit-mode', null);
  89. var urlTab = (this.$injector.get('$routeParams').tab || '').toLowerCase();
  90. if (urlTab) {
  91. this.editorTabs.forEach((tab, i) => {
  92. if (tab.title.toLowerCase() === urlTab) {
  93. this.editorTabIndex = i;
  94. }
  95. });
  96. }
  97. }
  98. changeTab(newIndex) {
  99. this.editorTabIndex = newIndex;
  100. var route = this.$injector.get('$route');
  101. route.current.params.tab = this.editorTabs[newIndex].title.toLowerCase();
  102. route.updateParams();
  103. }
  104. addEditorTab(title, directiveFn, index?) {
  105. var editorTab = {title, directiveFn};
  106. if (_.isString(directiveFn)) {
  107. editorTab.directiveFn = function() {
  108. return {templateUrl: directiveFn};
  109. };
  110. }
  111. if (index) {
  112. this.editorTabs.splice(index, 0, editorTab);
  113. } else {
  114. this.editorTabs.push(editorTab);
  115. }
  116. }
  117. getMenu() {
  118. let menu = [];
  119. menu.push({text: 'View', click: 'ctrl.viewPanel(); dismiss();'});
  120. menu.push({text: 'Edit', click: 'ctrl.editPanel(); dismiss();', role: 'Editor'});
  121. if (!this.fullscreen) { // duplication is not supported in fullscreen mode
  122. menu.push({ text: 'Duplicate', click: 'ctrl.duplicate()', role: 'Editor' });
  123. }
  124. menu.push({text: 'Share', click: 'ctrl.sharePanel(); dismiss();'});
  125. return menu;
  126. }
  127. getExtendedMenu() {
  128. var actions = [{text: 'Panel JSON', click: 'ctrl.editPanelJson(); dismiss();'}];
  129. this.events.emit('init-panel-actions', actions);
  130. return actions;
  131. }
  132. otherPanelInFullscreenMode() {
  133. return this.dashboard.meta.fullscreen && !this.fullscreen;
  134. }
  135. calculatePanelHeight() {
  136. if (this.fullscreen) {
  137. var docHeight = $(window).height();
  138. var editHeight = Math.floor(docHeight * 0.4);
  139. var fullscreenHeight = Math.floor(docHeight * 0.8);
  140. this.containerHeight = this.editMode ? editHeight : fullscreenHeight;
  141. } else {
  142. this.containerHeight = this.panel.height || this.row.height;
  143. if (_.isString(this.containerHeight)) {
  144. this.containerHeight = parseInt(this.containerHeight.replace('px', ''), 10);
  145. }
  146. }
  147. this.height = this.containerHeight - (PANEL_BORDER + PANEL_PADDING + (this.panel.title ? TITLE_HEIGHT : EMPTY_TITLE_HEIGHT));
  148. }
  149. render(payload?) {
  150. // ignore if other panel is in fullscreen mode
  151. if (this.otherPanelInFullscreenMode()) {
  152. return;
  153. }
  154. this.calculatePanelHeight();
  155. this.timing.renderStart = new Date().getTime();
  156. this.events.emit('render', payload);
  157. }
  158. toggleEditorHelp(index) {
  159. if (this.editorHelpIndex === index) {
  160. this.editorHelpIndex = null;
  161. return;
  162. }
  163. this.editorHelpIndex = index;
  164. }
  165. duplicate() {
  166. this.dashboard.duplicatePanel(this.panel, this.row);
  167. this.$timeout(() => {
  168. this.$scope.$root.$broadcast('render');
  169. });
  170. }
  171. updateColumnSpan(span) {
  172. this.panel.span = Math.min(Math.max(Math.floor(this.panel.span + span), 1), 12);
  173. this.row.panelSpanChanged();
  174. this.$timeout(() => {
  175. this.render();
  176. });
  177. }
  178. removePanel() {
  179. this.row.removePanel(this.panel);
  180. }
  181. editPanelJson() {
  182. this.publishAppEvent('show-json-editor', {
  183. object: this.panel,
  184. updateHandler: this.replacePanel.bind(this)
  185. });
  186. }
  187. replacePanel(newPanel, oldPanel) {
  188. var row = this.row;
  189. var index = _.indexOf(this.row.panels, oldPanel);
  190. this.row.panels.splice(index, 1);
  191. // adding it back needs to be done in next digest
  192. this.$timeout(() => {
  193. newPanel.id = oldPanel.id;
  194. newPanel.span = oldPanel.span;
  195. this.row.panels.splice(index, 0, newPanel);
  196. });
  197. }
  198. sharePanel() {
  199. var shareScope = this.$scope.$new();
  200. shareScope.panel = this.panel;
  201. shareScope.dashboard = this.dashboard;
  202. this.publishAppEvent('show-modal', {
  203. src: 'public/app/features/dashboard/partials/shareModal.html',
  204. scope: shareScope
  205. });
  206. }
  207. openInspector() {
  208. var modalScope = this.$scope.$new();
  209. modalScope.panel = this.panel;
  210. modalScope.dashboard = this.dashboard;
  211. modalScope.inspector = $.extend(true, {}, this.inspector);
  212. this.publishAppEvent('show-modal', {
  213. src: 'public/app/partials/inspector.html',
  214. scope: modalScope
  215. });
  216. }
  217. }