panel_ctrl.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. menu.push({
  122. text: 'Share',
  123. click: 'ctrl.sharePanel();',
  124. icon: 'fa fa-fw fa-share',
  125. shortcut: 'p s',
  126. });
  127. // Additional items from sub-class
  128. menu.push(...(await this.getAdditionalMenuItems()));
  129. const extendedMenu = this.getExtendedMenu();
  130. menu.push({
  131. text: 'More ...',
  132. click: '',
  133. icon: 'fa fa-fw fa-cube',
  134. submenu: extendedMenu,
  135. });
  136. if (this.dashboard.meta.canEdit) {
  137. menu.push({ divider: true, role: 'Editor' });
  138. menu.push({
  139. text: 'Remove',
  140. click: 'ctrl.removePanel();',
  141. role: 'Editor',
  142. icon: 'fa fa-fw fa-trash',
  143. shortcut: 'p r',
  144. });
  145. }
  146. return menu;
  147. }
  148. getExtendedMenu() {
  149. const menu = [];
  150. if (!this.panel.fullscreen && this.dashboard.meta.canEdit) {
  151. menu.push({
  152. text: 'Duplicate',
  153. click: 'ctrl.duplicate()',
  154. role: 'Editor',
  155. shortcut: 'p d',
  156. });
  157. menu.push({
  158. text: 'Copy',
  159. click: 'ctrl.copyPanel()',
  160. role: 'Editor',
  161. });
  162. }
  163. menu.push({
  164. text: 'Panel JSON',
  165. click: 'ctrl.editPanelJson(); dismiss();',
  166. });
  167. this.events.emit('init-panel-actions', menu);
  168. return menu;
  169. }
  170. // Override in sub-class to add items before extended menu
  171. async getAdditionalMenuItems(): Promise<any[]> {
  172. return [];
  173. }
  174. otherPanelInFullscreenMode() {
  175. return this.dashboard.meta.fullscreen && !this.panel.fullscreen;
  176. }
  177. calculatePanelHeight(containerHeight: number) {
  178. this.containerHeight = containerHeight;
  179. this.height = calculateInnerPanelHeight(this.panel, containerHeight);
  180. }
  181. render(payload?: any) {
  182. this.events.emit('render', payload);
  183. }
  184. duplicate() {
  185. duplicatePanel(this.dashboard, this.panel);
  186. }
  187. removePanel() {
  188. removePanel(this.dashboard, this.panel, true);
  189. }
  190. editPanelJson() {
  191. editPanelJsonUtil(this.dashboard, this.panel);
  192. }
  193. copyPanel() {
  194. copyPanelUtil(this.panel);
  195. }
  196. sharePanel() {
  197. sharePanelUtil(this.dashboard, this.panel);
  198. }
  199. getInfoMode() {
  200. if (this.error) {
  201. return 'error';
  202. }
  203. if (!!this.panel.description) {
  204. return 'info';
  205. }
  206. if (this.panel.links && this.panel.links.length) {
  207. return 'links';
  208. }
  209. return '';
  210. }
  211. getInfoContent(options: { mode: string }) {
  212. const { panel } = this;
  213. let markdown = panel.description || '';
  214. if (options.mode === 'tooltip') {
  215. markdown = this.error || panel.description || '';
  216. }
  217. const templateSrv: TemplateSrv = this.$injector.get('templateSrv');
  218. const interpolatedMarkdown = templateSrv.replace(markdown, panel.scopedVars);
  219. let html = '<div class="markdown-html panel-info-content">';
  220. const md = renderMarkdown(interpolatedMarkdown);
  221. html += config.disableSanitizeHtml ? md : sanitize(md);
  222. if (panel.links && panel.links.length > 0) {
  223. const interpolatedLinks = getPanelLinksSupplier(panel).getLinks();
  224. html += '<ul class="panel-info-corner-links">';
  225. for (const link of interpolatedLinks) {
  226. html +=
  227. '<li><a class="panel-menu-link" href="' +
  228. escapeHtml(link.href) +
  229. '" target="' +
  230. escapeHtml(link.target) +
  231. '">' +
  232. escapeHtml(link.title) +
  233. '</a></li>';
  234. }
  235. html += '</ul>';
  236. }
  237. html += '</div>';
  238. return html;
  239. }
  240. // overriden from react
  241. onPluginTypeChange = (plugin: PanelPluginMeta) => {};
  242. }