panel_ctrl.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. editorTabIndex: number;
  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. timing: any;
  32. loading: boolean;
  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.editorTabIndex = 0;
  39. this.events = this.panel.events;
  40. this.timing = {};
  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, this.timing);
  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. this.editorTabs = [];
  79. this.editModeInitiated = true;
  80. this.events.emit('init-edit-mode', null);
  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.fullscreen) {
  170. const docHeight = $('.react-grid-layout').height();
  171. const editHeight = Math.floor(docHeight * 0.35);
  172. const fullscreenHeight = Math.floor(docHeight * 0.8);
  173. this.containerHeight = this.panel.isEditing ? editHeight : fullscreenHeight;
  174. } else {
  175. this.containerHeight = this.panel.gridPos.h * GRID_CELL_HEIGHT + (this.panel.gridPos.h - 1) * GRID_CELL_VMARGIN;
  176. }
  177. if (this.panel.soloMode) {
  178. this.containerHeight = $(window).height();
  179. }
  180. // hacky solution
  181. if (this.panel.isEditing && !this.editModeInitiated) {
  182. this.initEditMode();
  183. }
  184. this.height = this.containerHeight - (PANEL_BORDER + PANEL_HEADER_HEIGHT);
  185. }
  186. render(payload?) {
  187. this.timing.renderStart = new Date().getTime();
  188. this.events.emit('render', payload);
  189. }
  190. duplicate() {
  191. duplicatePanel(this.dashboard, this.panel);
  192. }
  193. removePanel() {
  194. this.publishAppEvent('panel-remove', {
  195. panelId: this.panel.id,
  196. });
  197. }
  198. editPanelJson() {
  199. editPanelJsonUtil(this.dashboard, this.panel);
  200. }
  201. copyPanel() {
  202. copyPanelUtil(this.panel);
  203. }
  204. sharePanel() {
  205. sharePanelUtil(this.dashboard, this.panel);
  206. }
  207. getInfoMode() {
  208. if (this.error) {
  209. return 'error';
  210. }
  211. if (!!this.panel.description) {
  212. return 'info';
  213. }
  214. if (this.panel.links && this.panel.links.length) {
  215. return 'links';
  216. }
  217. return '';
  218. }
  219. getInfoContent(options) {
  220. let markdown = this.panel.description;
  221. if (options.mode === 'tooltip') {
  222. markdown = this.error || this.panel.description;
  223. }
  224. const linkSrv = this.$injector.get('linkSrv');
  225. const sanitize = this.$injector.get('$sanitize');
  226. const templateSrv = this.$injector.get('templateSrv');
  227. const interpolatedMarkdown = templateSrv.replace(markdown, this.panel.scopedVars);
  228. let html = '<div class="markdown-html">';
  229. html += new Remarkable().render(interpolatedMarkdown);
  230. if (this.panel.links && this.panel.links.length > 0) {
  231. html += '<ul>';
  232. for (const link of this.panel.links) {
  233. const info = linkSrv.getPanelLinkAnchorInfo(link, this.panel.scopedVars);
  234. html +=
  235. '<li><a class="panel-menu-link" href="' +
  236. info.href +
  237. '" target="' +
  238. info.target +
  239. '">' +
  240. info.title +
  241. '</a></li>';
  242. }
  243. html += '</ul>';
  244. }
  245. html += '</div>';
  246. return sanitize(html);
  247. }
  248. openInspector() {
  249. const modalScope = this.$scope.$new();
  250. modalScope.panel = this.panel;
  251. modalScope.dashboard = this.dashboard;
  252. modalScope.panelInfoHtml = this.getInfoContent({ mode: 'inspector' });
  253. modalScope.inspector = $.extend(true, {}, this.inspector);
  254. this.publishAppEvent('show-modal', {
  255. src: 'public/app/features/dashboard/partials/inspector.html',
  256. scope: modalScope,
  257. });
  258. }
  259. }