panel_ctrl.ts 6.2 KB

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