panel_ctrl.ts 6.2 KB

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