panel_ctrl.ts 9.4 KB

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