panel_ctrl.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 { generalTab } from './GeneralTabCtrl';
  14. import { GRID_CELL_HEIGHT, GRID_CELL_VMARGIN, PANEL_HEADER_HEIGHT, PANEL_BORDER } from 'app/core/constants';
  15. export class PanelCtrl {
  16. panel: any;
  17. error: any;
  18. dashboard: any;
  19. editorTabIndex: number;
  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. timing: any;
  33. loading: boolean;
  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.editorTabIndex = 0;
  40. this.events = this.panel.events;
  41. this.timing = {};
  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, this.timing);
  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. this.editorTabs = [];
  80. this.addEditorTab('General', generalTab);
  81. this.editModeInitiated = true;
  82. this.events.emit('init-edit-mode', null);
  83. const urlTab = (this.$injector.get('$routeParams').tab || '').toLowerCase();
  84. if (urlTab) {
  85. this.editorTabs.forEach((tab, i) => {
  86. if (tab.title.toLowerCase() === urlTab) {
  87. this.editorTabIndex = i;
  88. }
  89. });
  90. }
  91. }
  92. changeTab(newIndex) {
  93. this.editorTabIndex = newIndex;
  94. const route = this.$injector.get('$route');
  95. route.current.params.tab = this.editorTabs[newIndex].title.toLowerCase();
  96. route.updateParams();
  97. }
  98. addEditorTab(title, directiveFn, index?, icon?) {
  99. const editorTab = { title, directiveFn, icon };
  100. if (_.isString(directiveFn)) {
  101. editorTab.directiveFn = () => {
  102. return { templateUrl: directiveFn };
  103. };
  104. }
  105. if (index) {
  106. this.editorTabs.splice(index, 0, editorTab);
  107. } else {
  108. this.editorTabs.push(editorTab);
  109. }
  110. }
  111. getMenu() {
  112. const menu = [];
  113. menu.push({
  114. text: 'View',
  115. click: 'ctrl.viewPanel();',
  116. icon: 'fa fa-fw fa-eye',
  117. shortcut: 'v',
  118. });
  119. if (this.dashboard.meta.canEdit) {
  120. menu.push({
  121. text: 'Edit',
  122. click: 'ctrl.editPanel();',
  123. role: 'Editor',
  124. icon: 'fa fa-fw fa-edit',
  125. shortcut: 'e',
  126. });
  127. }
  128. menu.push({
  129. text: 'Share',
  130. click: 'ctrl.sharePanel();',
  131. icon: 'fa fa-fw fa-share',
  132. shortcut: 'p s',
  133. });
  134. // Additional items from sub-class
  135. menu.push(...this.getAdditionalMenuItems());
  136. const extendedMenu = this.getExtendedMenu();
  137. menu.push({
  138. text: 'More ...',
  139. click: '',
  140. icon: 'fa fa-fw fa-cube',
  141. submenu: extendedMenu,
  142. });
  143. if (this.dashboard.meta.canEdit) {
  144. menu.push({ divider: true, role: 'Editor' });
  145. menu.push({
  146. text: 'Remove',
  147. click: 'ctrl.removePanel();',
  148. role: 'Editor',
  149. icon: 'fa fa-fw fa-trash',
  150. shortcut: 'p r',
  151. });
  152. }
  153. return menu;
  154. }
  155. getExtendedMenu() {
  156. const menu = [];
  157. if (!this.panel.fullscreen && this.dashboard.meta.canEdit) {
  158. menu.push({
  159. text: 'Duplicate',
  160. click: 'ctrl.duplicate()',
  161. role: 'Editor',
  162. shortcut: 'p d',
  163. });
  164. menu.push({
  165. text: 'Copy',
  166. click: 'ctrl.copyPanel()',
  167. role: 'Editor',
  168. });
  169. }
  170. menu.push({
  171. text: 'Panel JSON',
  172. click: 'ctrl.editPanelJson(); dismiss();',
  173. });
  174. this.events.emit('init-panel-actions', menu);
  175. return menu;
  176. }
  177. // Override in sub-class to add items before extended menu
  178. getAdditionalMenuItems() {
  179. return [];
  180. }
  181. otherPanelInFullscreenMode() {
  182. return this.dashboard.meta.fullscreen && !this.panel.fullscreen;
  183. }
  184. calculatePanelHeight() {
  185. if (this.panel.fullscreen) {
  186. const docHeight = $('.react-grid-layout').height();
  187. const editHeight = Math.floor(docHeight * 0.35);
  188. const fullscreenHeight = Math.floor(docHeight * 0.8);
  189. this.containerHeight = this.panel.isEditing ? editHeight : fullscreenHeight;
  190. } else {
  191. this.containerHeight = this.panel.gridPos.h * GRID_CELL_HEIGHT + (this.panel.gridPos.h - 1) * GRID_CELL_VMARGIN;
  192. }
  193. if (this.panel.soloMode) {
  194. this.containerHeight = $(window).height();
  195. }
  196. // hacky solution
  197. if (this.panel.isEditing && !this.editModeInitiated) {
  198. this.initEditMode();
  199. }
  200. this.height = this.containerHeight - (PANEL_BORDER + PANEL_HEADER_HEIGHT);
  201. }
  202. render(payload?) {
  203. this.timing.renderStart = new Date().getTime();
  204. this.events.emit('render', payload);
  205. }
  206. duplicate() {
  207. duplicatePanel(this.dashboard, this.panel);
  208. }
  209. removePanel() {
  210. this.publishAppEvent('panel-remove', {
  211. panelId: this.panel.id,
  212. });
  213. }
  214. editPanelJson() {
  215. editPanelJsonUtil(this.dashboard, this.panel);
  216. }
  217. copyPanel() {
  218. copyPanelUtil(this.panel);
  219. }
  220. sharePanel() {
  221. sharePanelUtil(this.dashboard, this.panel);
  222. }
  223. getInfoMode() {
  224. if (this.error) {
  225. return 'error';
  226. }
  227. if (!!this.panel.description) {
  228. return 'info';
  229. }
  230. if (this.panel.links && this.panel.links.length) {
  231. return 'links';
  232. }
  233. return '';
  234. }
  235. getInfoContent(options) {
  236. let markdown = this.panel.description;
  237. if (options.mode === 'tooltip') {
  238. markdown = this.error || this.panel.description;
  239. }
  240. const linkSrv = this.$injector.get('linkSrv');
  241. const sanitize = this.$injector.get('$sanitize');
  242. const templateSrv = this.$injector.get('templateSrv');
  243. const interpolatedMarkdown = templateSrv.replace(markdown, this.panel.scopedVars);
  244. let html = '<div class="markdown-html">';
  245. html += new Remarkable().render(interpolatedMarkdown);
  246. if (this.panel.links && this.panel.links.length > 0) {
  247. html += '<ul>';
  248. for (const link of this.panel.links) {
  249. const info = linkSrv.getPanelLinkAnchorInfo(link, this.panel.scopedVars);
  250. html +=
  251. '<li><a class="panel-menu-link" href="' +
  252. info.href +
  253. '" target="' +
  254. info.target +
  255. '">' +
  256. info.title +
  257. '</a></li>';
  258. }
  259. html += '</ul>';
  260. }
  261. html += '</div>';
  262. return sanitize(html);
  263. }
  264. openInspector() {
  265. const modalScope = this.$scope.$new();
  266. modalScope.panel = this.panel;
  267. modalScope.dashboard = this.dashboard;
  268. modalScope.panelInfoHtml = this.getInfoContent({ mode: 'inspector' });
  269. modalScope.inspector = $.extend(true, {}, this.inspector);
  270. this.publishAppEvent('show-modal', {
  271. src: 'public/app/features/dashboard/partials/inspector.html',
  272. scope: modalScope,
  273. });
  274. }
  275. }