panel_ctrl.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. import _ from 'lodash';
  2. import { sanitize, escapeHtml } from 'app/core/utils/text';
  3. import { renderMarkdown } from '@grafana/data';
  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. removePanel,
  11. copyPanel as copyPanelUtil,
  12. editPanelJson as editPanelJsonUtil,
  13. sharePanel as sharePanelUtil,
  14. calculateInnerPanelHeight,
  15. } from 'app/features/dashboard/utils/panel';
  16. import { GRID_COLUMN_COUNT } from 'app/core/constants';
  17. import { auto } from 'angular';
  18. import { TemplateSrv } from '../templating/template_srv';
  19. import { PanelPluginMeta } from '@grafana/ui/src/types/panel';
  20. import { getPanelLinksSupplier } from './panellinks/linkSuppliers';
  21. export class PanelCtrl {
  22. panel: any;
  23. error: any;
  24. dashboard: any;
  25. pluginName: string;
  26. pluginId: string;
  27. editorTabs: any;
  28. $scope: any;
  29. $injector: auto.IInjectorService;
  30. $location: any;
  31. $timeout: any;
  32. inspector: any;
  33. editModeInitiated: boolean;
  34. height: any;
  35. containerHeight: any;
  36. events: Emitter;
  37. loading: boolean;
  38. timing: any;
  39. maxPanelsPerRowOptions: number[];
  40. constructor($scope: any, $injector: auto.IInjectorService) {
  41. this.$injector = $injector;
  42. this.$location = $injector.get('$location');
  43. this.$scope = $scope;
  44. this.$timeout = $injector.get('$timeout');
  45. this.editorTabs = [];
  46. this.events = this.panel.events;
  47. this.timing = {}; // not used but here to not break plugins
  48. const plugin = config.panels[this.panel.type];
  49. if (plugin) {
  50. this.pluginId = plugin.id;
  51. this.pluginName = plugin.name;
  52. }
  53. $scope.$on('component-did-mount', () => this.panelDidMount());
  54. }
  55. panelDidMount() {
  56. this.events.emit('component-did-mount');
  57. this.dashboard.panelInitialized(this.panel);
  58. }
  59. renderingCompleted() {
  60. profiler.renderingCompleted();
  61. }
  62. refresh() {
  63. this.panel.refresh();
  64. }
  65. publishAppEvent(evtName: string, evt: any) {
  66. this.$scope.$root.appEvent(evtName, evt);
  67. }
  68. changeView(fullscreen: boolean, edit: boolean) {
  69. this.publishAppEvent('panel-change-view', {
  70. fullscreen,
  71. edit,
  72. panelId: this.panel.id,
  73. });
  74. }
  75. viewPanel() {
  76. this.changeView(true, false);
  77. }
  78. editPanel() {
  79. this.changeView(true, true);
  80. }
  81. exitFullscreen() {
  82. this.changeView(false, false);
  83. }
  84. initEditMode() {
  85. if (!this.editModeInitiated) {
  86. this.editModeInitiated = true;
  87. this.events.emit('init-edit-mode', null);
  88. this.maxPanelsPerRowOptions = getFactors(GRID_COLUMN_COUNT);
  89. }
  90. }
  91. addEditorTab(title: string, directiveFn: any, index?: number, icon?: any) {
  92. const editorTab = { title, directiveFn, icon };
  93. if (_.isString(directiveFn)) {
  94. editorTab.directiveFn = () => {
  95. return { templateUrl: directiveFn };
  96. };
  97. }
  98. if (index) {
  99. this.editorTabs.splice(index, 0, editorTab);
  100. } else {
  101. this.editorTabs.push(editorTab);
  102. }
  103. }
  104. async getMenu() {
  105. const menu = [];
  106. menu.push({
  107. text: 'View',
  108. click: 'ctrl.viewPanel();',
  109. icon: 'gicon gicon-viewer',
  110. shortcut: 'v',
  111. });
  112. if (this.dashboard.meta.canEdit) {
  113. menu.push({
  114. text: 'Edit',
  115. click: 'ctrl.editPanel();',
  116. role: 'Editor',
  117. icon: 'gicon gicon-editor',
  118. shortcut: 'e',
  119. });
  120. }
  121. // se elimina la opción Share del menu dropdown - Oscar Leiva 03/12/2019
  122. // menu.push({
  123. // text: 'Share',
  124. // click: 'ctrl.sharePanel();',
  125. // icon: 'fa fa-fw fa-share',
  126. // shortcut: 'p s',
  127. // });
  128. // Additional items from sub-class
  129. menu.push(...(await this.getAdditionalMenuItems()));
  130. const extendedMenu = this.getExtendedMenu();
  131. menu.push({
  132. text: 'More ...',
  133. click: '',
  134. icon: 'fa fa-fw fa-cube',
  135. submenu: extendedMenu,
  136. });
  137. if (this.dashboard.meta.canEdit) {
  138. menu.push({ divider: true, role: 'Editor' });
  139. menu.push({
  140. text: 'Remove',
  141. click: 'ctrl.removePanel();',
  142. role: 'Editor',
  143. icon: 'fa fa-fw fa-trash',
  144. shortcut: 'p r',
  145. });
  146. }
  147. return menu;
  148. }
  149. getExtendedMenu() {
  150. const menu = [];
  151. if (!this.panel.fullscreen && this.dashboard.meta.canEdit) {
  152. menu.push({
  153. text: 'Duplicate',
  154. click: 'ctrl.duplicate()',
  155. role: 'Editor',
  156. shortcut: 'p d',
  157. });
  158. menu.push({
  159. text: 'Copy',
  160. click: 'ctrl.copyPanel()',
  161. role: 'Editor',
  162. });
  163. }
  164. // se elimina el elemento 'Panel Json' del dropdown - Oscar leiva 03/12/2019
  165. // menu.push({
  166. // text: 'Panel JSON',
  167. // click: 'ctrl.editPanelJson(); dismiss();',
  168. // });
  169. this.events.emit('init-panel-actions', menu);
  170. return menu;
  171. }
  172. // Override in sub-class to add items before extended menu
  173. async getAdditionalMenuItems(): Promise<any[]> {
  174. return [];
  175. }
  176. otherPanelInFullscreenMode() {
  177. return this.dashboard.meta.fullscreen && !this.panel.fullscreen;
  178. }
  179. calculatePanelHeight(containerHeight: number) {
  180. this.containerHeight = containerHeight;
  181. this.height = calculateInnerPanelHeight(this.panel, containerHeight);
  182. }
  183. render(payload?: any) {
  184. this.events.emit('render', payload);
  185. }
  186. duplicate() {
  187. duplicatePanel(this.dashboard, this.panel);
  188. }
  189. removePanel() {
  190. removePanel(this.dashboard, this.panel, true);
  191. }
  192. editPanelJson() {
  193. editPanelJsonUtil(this.dashboard, this.panel);
  194. }
  195. copyPanel() {
  196. copyPanelUtil(this.panel);
  197. }
  198. sharePanel() {
  199. sharePanelUtil(this.dashboard, this.panel);
  200. }
  201. getInfoMode() {
  202. if (this.error) {
  203. return 'error';
  204. }
  205. if (!!this.panel.description) {
  206. return 'info';
  207. }
  208. if (this.panel.links && this.panel.links.length) {
  209. return 'links';
  210. }
  211. return '';
  212. }
  213. getInfoContent(options: { mode: string }) {
  214. const { panel } = this;
  215. let markdown = panel.description || '';
  216. if (options.mode === 'tooltip') {
  217. markdown = this.error || panel.description || '';
  218. }
  219. const templateSrv: TemplateSrv = this.$injector.get('templateSrv');
  220. const interpolatedMarkdown = templateSrv.replace(markdown, panel.scopedVars);
  221. let html = '<div class="markdown-html panel-info-content">';
  222. const md = renderMarkdown(interpolatedMarkdown);
  223. html += config.disableSanitizeHtml ? md : sanitize(md);
  224. if (panel.links && panel.links.length > 0) {
  225. const interpolatedLinks = getPanelLinksSupplier(panel).getLinks();
  226. html += '<ul class="panel-info-corner-links">';
  227. for (const link of interpolatedLinks) {
  228. html +=
  229. '<li><a class="panel-menu-link" href="' +
  230. escapeHtml(link.href) +
  231. '" target="' +
  232. escapeHtml(link.target) +
  233. '">' +
  234. escapeHtml(link.title) +
  235. '</a></li>';
  236. }
  237. html += '</ul>';
  238. }
  239. html += '</div>';
  240. return html;
  241. }
  242. // overriden from react
  243. onPluginTypeChange = (plugin: PanelPluginMeta) => {};
  244. }