panel_ctrl.ts 6.5 KB

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