panel_ctrl.ts 5.4 KB

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