panel_ctrl.ts 7.7 KB

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