panel_ctrl.ts 6.9 KB

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