panel_ctrl.ts 8.9 KB

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