panel_ctrl.ts 6.6 KB

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