panel_ctrl.ts 7.6 KB

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