panel_ctrl.ts 6.4 KB

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