panel_ctrl.ts 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. $scope.$on('$destroy', () => {
  46. this.events.emit('panel-teardown');
  47. this.events.removeAllListeners();
  48. });
  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. this.dashboard.duplicatePanel(this.panel);
  209. }
  210. removePanel() {
  211. this.publishAppEvent('panel-remove', {
  212. panelId: this.panel.id,
  213. });
  214. }
  215. editPanelJson() {
  216. const editScope = this.$scope.$root.$new();
  217. editScope.object = this.panel.getSaveModel();
  218. editScope.updateHandler = this.replacePanel.bind(this);
  219. editScope.enableCopy = true;
  220. this.publishAppEvent('show-modal', {
  221. src: 'public/app/partials/edit_json.html',
  222. scope: editScope,
  223. });
  224. }
  225. copyPanel() {
  226. store.set(LS_PANEL_COPY_KEY, JSON.stringify(this.panel.getSaveModel()));
  227. appEvents.emit('alert-success', ['Panel copied. Open Add Panel to paste']);
  228. }
  229. replacePanel(newPanel, oldPanel) {
  230. const dashboard = this.dashboard;
  231. const index = _.findIndex(dashboard.panels, panel => {
  232. return panel.id === oldPanel.id;
  233. });
  234. const deletedPanel = dashboard.panels.splice(index, 1);
  235. this.dashboard.events.emit('panel-removed', deletedPanel);
  236. newPanel = new PanelModel(newPanel);
  237. newPanel.id = oldPanel.id;
  238. dashboard.panels.splice(index, 0, newPanel);
  239. dashboard.sortPanelsByGridPos();
  240. dashboard.events.emit('panel-added', newPanel);
  241. }
  242. sharePanel() {
  243. const shareScope = this.$scope.$new();
  244. shareScope.panel = this.panel;
  245. shareScope.dashboard = this.dashboard;
  246. this.publishAppEvent('show-modal', {
  247. src: 'public/app/features/dashboard/partials/shareModal.html',
  248. scope: shareScope,
  249. });
  250. }
  251. getInfoMode() {
  252. if (this.error) {
  253. return 'error';
  254. }
  255. if (!!this.panel.description) {
  256. return 'info';
  257. }
  258. if (this.panel.links && this.panel.links.length) {
  259. return 'links';
  260. }
  261. return '';
  262. }
  263. getInfoContent(options) {
  264. let markdown = this.panel.description;
  265. if (options.mode === 'tooltip') {
  266. markdown = this.error || this.panel.description;
  267. }
  268. const linkSrv = this.$injector.get('linkSrv');
  269. const sanitize = this.$injector.get('$sanitize');
  270. const templateSrv = this.$injector.get('templateSrv');
  271. const interpolatedMarkdown = templateSrv.replace(markdown, this.panel.scopedVars);
  272. let html = '<div class="markdown-html">';
  273. html += new Remarkable().render(interpolatedMarkdown);
  274. if (this.panel.links && this.panel.links.length > 0) {
  275. html += '<ul>';
  276. for (const link of this.panel.links) {
  277. const info = linkSrv.getPanelLinkAnchorInfo(link, this.panel.scopedVars);
  278. html +=
  279. '<li><a class="panel-menu-link" href="' +
  280. info.href +
  281. '" target="' +
  282. info.target +
  283. '">' +
  284. info.title +
  285. '</a></li>';
  286. }
  287. html += '</ul>';
  288. }
  289. html += '</div>';
  290. return sanitize(html);
  291. }
  292. openInspector() {
  293. const modalScope = this.$scope.$new();
  294. modalScope.panel = this.panel;
  295. modalScope.dashboard = this.dashboard;
  296. modalScope.panelInfoHtml = this.getInfoContent({ mode: 'inspector' });
  297. modalScope.inspector = $.extend(true, {}, this.inspector);
  298. this.publishAppEvent('show-modal', {
  299. src: 'public/app/features/dashboard/partials/inspector.html',
  300. scope: modalScope,
  301. });
  302. }
  303. }