panel_ctrl.ts 6.2 KB

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