panel_ctrl.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. this.refresh();
  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. }
  79. addEditorTab(title, directiveFn, index?) {
  80. var editorTab = {title, directiveFn};
  81. if (_.isString(directiveFn)) {
  82. editorTab.directiveFn = function() {
  83. return {templateUrl: directiveFn};
  84. };
  85. }
  86. if (index) {
  87. this.editorTabs.splice(index, 0, editorTab);
  88. } else {
  89. this.editorTabs.push(editorTab);
  90. }
  91. }
  92. getMenu() {
  93. let menu = [];
  94. menu.push({text: 'View', click: 'ctrl.viewPanel(); dismiss();'});
  95. menu.push({text: 'Edit', click: 'ctrl.editPanel(); dismiss();', role: 'Editor'});
  96. if (!this.fullscreen) { // duplication is not supported in fullscreen mode
  97. menu.push({ text: 'Duplicate', click: 'ctrl.duplicate()', role: 'Editor' });
  98. }
  99. menu.push({text: 'Share', click: 'ctrl.sharePanel(); dismiss();'});
  100. return menu;
  101. }
  102. getExtendedMenu() {
  103. var actions = [{text: 'Panel JSON', click: 'ctrl.editPanelJson(); dismiss();'}];
  104. this.events.emit('init-panel-actions', actions);
  105. return actions;
  106. }
  107. otherPanelInFullscreenMode() {
  108. return this.dashboard.meta.fullscreen && !this.fullscreen;
  109. }
  110. calculatePanelHeight() {
  111. if (this.fullscreen) {
  112. var docHeight = $(window).height();
  113. var editHeight = Math.floor(docHeight * 0.3);
  114. var fullscreenHeight = Math.floor(docHeight * 0.7);
  115. this.containerHeight = this.editMode ? editHeight : fullscreenHeight;
  116. } else {
  117. this.containerHeight = this.panel.height || this.row.height;
  118. if (_.isString(this.containerHeight)) {
  119. this.containerHeight = parseInt(this.containerHeight.replace('px', ''), 10);
  120. }
  121. }
  122. this.height = this.containerHeight - (PANEL_PADDING + (this.panel.title ? TITLE_HEIGHT : EMPTY_TITLE_HEIGHT));
  123. }
  124. render(payload?) {
  125. // ignore if other panel is in fullscreen mode
  126. if (this.otherPanelInFullscreenMode()) {
  127. return;
  128. }
  129. this.calculatePanelHeight();
  130. this.events.emit('render', payload);
  131. }
  132. toggleEditorHelp(index) {
  133. if (this.editorHelpIndex === index) {
  134. this.editorHelpIndex = null;
  135. return;
  136. }
  137. this.editorHelpIndex = index;
  138. }
  139. duplicate() {
  140. this.dashboard.duplicatePanel(this.panel, this.row);
  141. }
  142. updateColumnSpan(span) {
  143. this.panel.span = Math.min(Math.max(Math.floor(this.panel.span + span), 1), 12);
  144. this.$timeout(() => {
  145. this.render();
  146. });
  147. }
  148. removePanel() {
  149. this.publishAppEvent('confirm-modal', {
  150. title: 'Remove Panel',
  151. text: 'Are you sure you want to remove this panel?',
  152. icon: 'fa-trash',
  153. yesText: 'Remove',
  154. onConfirm: () => {
  155. this.row.panels = _.without(this.row.panels, this.panel);
  156. }
  157. });
  158. }
  159. editPanelJson() {
  160. this.publishAppEvent('show-json-editor', {
  161. object: this.panel,
  162. updateHandler: this.replacePanel.bind(this)
  163. });
  164. }
  165. replacePanel(newPanel, oldPanel) {
  166. var row = this.row;
  167. var index = _.indexOf(this.row.panels, oldPanel);
  168. this.row.panels.splice(index, 1);
  169. // adding it back needs to be done in next digest
  170. this.$timeout(() => {
  171. newPanel.id = oldPanel.id;
  172. newPanel.span = oldPanel.span;
  173. this.row.panels.splice(index, 0, newPanel);
  174. });
  175. }
  176. sharePanel() {
  177. var shareScope = this.$scope.$new();
  178. shareScope.panel = this.panel;
  179. shareScope.dashboard = this.dashboard;
  180. this.publishAppEvent('show-modal', {
  181. src: 'public/app/features/dashboard/partials/shareModal.html',
  182. scope: shareScope
  183. });
  184. }
  185. openInspector() {
  186. var modalScope = this.$scope.$new();
  187. modalScope.panel = this.panel;
  188. modalScope.dashboard = this.dashboard;
  189. modalScope.inspector = angular.copy(this.inspector);
  190. this.publishAppEvent('show-modal', {
  191. src: 'public/app/partials/inspector.html',
  192. scope: modalScope
  193. });
  194. }
  195. }