panel_ctrl.ts 5.4 KB

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