panel_ctrl.ts 8.7 KB

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