panel_ctrl.ts 7.7 KB

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