panel_ctrl.ts 5.8 KB

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