panel_ctrl.ts 6.2 KB

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