panel_ctrl.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. import _ from 'lodash';
  2. import $ from 'jquery';
  3. import Remarkable from 'remarkable';
  4. import config from 'app/core/config';
  5. import { profiler } from 'app/core/core';
  6. import { Emitter } from 'app/core/core';
  7. import getFactors from 'app/core/utils/factors';
  8. import {
  9. duplicatePanel,
  10. copyPanel as copyPanelUtil,
  11. editPanelJson as editPanelJsonUtil,
  12. sharePanel as sharePanelUtil,
  13. } from 'app/features/dashboard/utils/panel';
  14. import { GRID_CELL_HEIGHT, GRID_CELL_VMARGIN, GRID_COLUMN_COUNT, PANEL_HEADER_HEIGHT, PANEL_BORDER } from 'app/core/constants';
  15. export class PanelCtrl {
  16. panel: any;
  17. error: any;
  18. dashboard: any;
  19. pluginName: string;
  20. pluginId: string;
  21. editorTabs: any;
  22. $scope: any;
  23. $injector: any;
  24. $location: any;
  25. $timeout: any;
  26. inspector: any;
  27. editModeInitiated: boolean;
  28. height: any;
  29. containerHeight: any;
  30. events: Emitter;
  31. timing: any;
  32. loading: boolean;
  33. maxPanelsPerRowOptions: number[];
  34. constructor($scope, $injector) {
  35. this.$injector = $injector;
  36. this.$location = $injector.get('$location');
  37. this.$scope = $scope;
  38. this.$timeout = $injector.get('$timeout');
  39. this.editorTabs = [];
  40. this.events = this.panel.events;
  41. this.timing = {};
  42. const plugin = config.panels[this.panel.type];
  43. if (plugin) {
  44. this.pluginId = plugin.id;
  45. this.pluginName = plugin.name;
  46. }
  47. $scope.$on('component-did-mount', () => this.panelDidMount());
  48. }
  49. panelDidMount() {
  50. this.events.emit('component-did-mount');
  51. this.dashboard.panelInitialized(this.panel);
  52. }
  53. renderingCompleted() {
  54. profiler.renderingCompleted(this.panel.id, this.timing);
  55. }
  56. refresh() {
  57. this.panel.refresh();
  58. }
  59. publishAppEvent(evtName, evt) {
  60. this.$scope.$root.appEvent(evtName, evt);
  61. }
  62. changeView(fullscreen, edit) {
  63. this.publishAppEvent('panel-change-view', {
  64. fullscreen: fullscreen,
  65. edit: edit,
  66. panelId: this.panel.id,
  67. });
  68. }
  69. viewPanel() {
  70. this.changeView(true, false);
  71. }
  72. editPanel() {
  73. this.changeView(true, true);
  74. }
  75. exitFullscreen() {
  76. this.changeView(false, false);
  77. }
  78. initEditMode() {
  79. if (!this.editModeInitiated) {
  80. this.editModeInitiated = true;
  81. this.events.emit('init-edit-mode', null);
  82. this.maxPanelsPerRowOptions = getFactors(GRID_COLUMN_COUNT);
  83. }
  84. }
  85. addEditorTab(title, directiveFn, index?, icon?) {
  86. const editorTab = { title, directiveFn, icon };
  87. if (_.isString(directiveFn)) {
  88. editorTab.directiveFn = () => {
  89. return { templateUrl: directiveFn };
  90. };
  91. }
  92. if (index) {
  93. this.editorTabs.splice(index, 0, editorTab);
  94. } else {
  95. this.editorTabs.push(editorTab);
  96. }
  97. }
  98. getMenu() {
  99. const menu = [];
  100. menu.push({
  101. text: 'View',
  102. click: 'ctrl.viewPanel();',
  103. icon: 'fa fa-fw fa-eye',
  104. shortcut: 'v',
  105. });
  106. if (this.dashboard.meta.canEdit) {
  107. menu.push({
  108. text: 'Edit',
  109. click: 'ctrl.editPanel();',
  110. role: 'Editor',
  111. icon: 'fa fa-fw fa-edit',
  112. shortcut: 'e',
  113. });
  114. }
  115. menu.push({
  116. text: 'Share',
  117. click: 'ctrl.sharePanel();',
  118. icon: 'fa fa-fw fa-share',
  119. shortcut: 'p s',
  120. });
  121. // Additional items from sub-class
  122. menu.push(...this.getAdditionalMenuItems());
  123. const extendedMenu = this.getExtendedMenu();
  124. menu.push({
  125. text: 'More ...',
  126. click: '',
  127. icon: 'fa fa-fw fa-cube',
  128. submenu: extendedMenu,
  129. });
  130. if (this.dashboard.meta.canEdit) {
  131. menu.push({ divider: true, role: 'Editor' });
  132. menu.push({
  133. text: 'Remove',
  134. click: 'ctrl.removePanel();',
  135. role: 'Editor',
  136. icon: 'fa fa-fw fa-trash',
  137. shortcut: 'p r',
  138. });
  139. }
  140. return menu;
  141. }
  142. getExtendedMenu() {
  143. const menu = [];
  144. if (!this.panel.fullscreen && this.dashboard.meta.canEdit) {
  145. menu.push({
  146. text: 'Duplicate',
  147. click: 'ctrl.duplicate()',
  148. role: 'Editor',
  149. shortcut: 'p d',
  150. });
  151. menu.push({
  152. text: 'Copy',
  153. click: 'ctrl.copyPanel()',
  154. role: 'Editor',
  155. });
  156. }
  157. menu.push({
  158. text: 'Panel JSON',
  159. click: 'ctrl.editPanelJson(); dismiss();',
  160. });
  161. this.events.emit('init-panel-actions', menu);
  162. return menu;
  163. }
  164. // Override in sub-class to add items before extended menu
  165. getAdditionalMenuItems() {
  166. return [];
  167. }
  168. otherPanelInFullscreenMode() {
  169. return this.dashboard.meta.fullscreen && !this.panel.fullscreen;
  170. }
  171. calculatePanelHeight() {
  172. if (this.panel.isEditing) {
  173. this.containerHeight = $('.panel-wrapper--edit').height();
  174. } else if (this.panel.fullscreen) {
  175. this.containerHeight = $('.panel-wrapper--view').height();
  176. } else {
  177. this.containerHeight = this.panel.gridPos.h * GRID_CELL_HEIGHT + (this.panel.gridPos.h - 1) * GRID_CELL_VMARGIN;
  178. }
  179. if (this.panel.soloMode) {
  180. this.containerHeight = $(window).height();
  181. }
  182. this.height = this.containerHeight - (PANEL_BORDER + PANEL_HEADER_HEIGHT);
  183. }
  184. render(payload?) {
  185. this.timing.renderStart = new Date().getTime();
  186. this.events.emit('render', payload);
  187. }
  188. duplicate() {
  189. duplicatePanel(this.dashboard, this.panel);
  190. }
  191. removePanel() {
  192. this.publishAppEvent('panel-remove', {
  193. panelId: this.panel.id,
  194. });
  195. }
  196. editPanelJson() {
  197. editPanelJsonUtil(this.dashboard, this.panel);
  198. }
  199. copyPanel() {
  200. copyPanelUtil(this.panel);
  201. }
  202. sharePanel() {
  203. sharePanelUtil(this.dashboard, this.panel);
  204. }
  205. getInfoMode() {
  206. if (this.error) {
  207. return 'error';
  208. }
  209. if (!!this.panel.description) {
  210. return 'info';
  211. }
  212. if (this.panel.links && this.panel.links.length) {
  213. return 'links';
  214. }
  215. return '';
  216. }
  217. getInfoContent(options) {
  218. let markdown = this.panel.description;
  219. if (options.mode === 'tooltip') {
  220. markdown = this.error || this.panel.description;
  221. }
  222. const linkSrv = this.$injector.get('linkSrv');
  223. const sanitize = this.$injector.get('$sanitize');
  224. const templateSrv = this.$injector.get('templateSrv');
  225. const interpolatedMarkdown = templateSrv.replace(markdown, this.panel.scopedVars);
  226. let html = '<div class="markdown-html">';
  227. html += new Remarkable().render(interpolatedMarkdown);
  228. if (this.panel.links && this.panel.links.length > 0) {
  229. html += '<ul>';
  230. for (const link of this.panel.links) {
  231. const info = linkSrv.getPanelLinkAnchorInfo(link, this.panel.scopedVars);
  232. html +=
  233. '<li><a class="panel-menu-link" href="' +
  234. info.href +
  235. '" target="' +
  236. info.target +
  237. '">' +
  238. info.title +
  239. '</a></li>';
  240. }
  241. html += '</ul>';
  242. }
  243. html += '</div>';
  244. return sanitize(html);
  245. }
  246. openInspector() {
  247. const modalScope = this.$scope.$new();
  248. modalScope.panel = this.panel;
  249. modalScope.dashboard = this.dashboard;
  250. modalScope.panelInfoHtml = this.getInfoContent({ mode: 'inspector' });
  251. modalScope.inspector = $.extend(true, {}, this.inspector);
  252. this.publishAppEvent('show-modal', {
  253. src: 'public/app/features/dashboard/partials/inspector.html',
  254. scope: modalScope,
  255. });
  256. }
  257. }