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