panel_ctrl.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. import config from 'app/core/config';
  2. import _ from 'lodash';
  3. import $ from 'jquery';
  4. import {profiler} from 'app/core/profiler';
  5. import Remarkable from 'remarkable';
  6. import {CELL_HEIGHT, CELL_VMARGIN} from '../dashboard/DashboardModel';
  7. const TITLE_HEIGHT = 25;
  8. const EMPTY_TITLE_HEIGHT = 9;
  9. const PANEL_PADDING = 5;
  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. $timeout: any;
  23. fullscreen: boolean;
  24. inspector: any;
  25. editModeInitiated: boolean;
  26. editMode: any;
  27. height: any;
  28. containerHeight: any;
  29. events: Emitter;
  30. timing: any;
  31. constructor($scope, $injector) {
  32. this.$injector = $injector;
  33. this.$scope = $scope;
  34. this.$timeout = $injector.get('$timeout');
  35. this.editorTabIndex = 0;
  36. this.events = this.panel.events;
  37. this.timing = {};
  38. var plugin = config.panels[this.panel.type];
  39. if (plugin) {
  40. this.pluginId = plugin.id;
  41. this.pluginName = plugin.name;
  42. }
  43. $scope.$on("refresh", () => this.refresh());
  44. $scope.$on("$destroy", () => {
  45. this.events.emit('panel-teardown');
  46. this.events.removeAllListeners();
  47. });
  48. }
  49. init() {
  50. this.events.on('panel-size-changed', this.onSizeChanged.bind(this));
  51. this.publishAppEvent('panel-initialized', {scope: this.$scope});
  52. this.events.emit('panel-initialized');
  53. }
  54. renderingCompleted() {
  55. profiler.renderingCompleted(this.panel.id, this.timing);
  56. }
  57. refresh() {
  58. this.events.emit('refresh', null);
  59. }
  60. publishAppEvent(evtName, evt) {
  61. this.$scope.$root.appEvent(evtName, evt);
  62. }
  63. changeView(fullscreen, edit) {
  64. this.dashboard.setViewMode(this.panel, fullscreen, edit);
  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. var 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. var route = this.$injector.get('$route');
  92. route.current.params.tab = this.editorTabs[newIndex].title.toLowerCase();
  93. route.updateParams();
  94. }
  95. addEditorTab(title, directiveFn, index?) {
  96. var editorTab = {title, directiveFn};
  97. if (_.isString(directiveFn)) {
  98. editorTab.directiveFn = function() {
  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. let menu = [];
  110. menu.push({text: 'View', click: 'ctrl.viewPanel();', icon: "fa fa-fw fa-eye", shortcut: "v"});
  111. menu.push({text: 'Edit', click: 'ctrl.editPanel();', role: 'Editor', icon: "fa fa-fw fa-edit", shortcut: "e"});
  112. menu.push({text: 'Share', click: 'ctrl.sharePanel();', icon: "fa fa-fw fa-share", shortcut: "p s"});
  113. if (!this.fullscreen) {
  114. menu.push({ text: 'Duplicate', click: 'ctrl.duplicate()', role: 'Editor', icon: "fa fa-fw fa-copy" });
  115. }
  116. menu.push({divider: true});
  117. let extendedMenu = this.getExtendedMenu();
  118. menu.push({text: 'More ...', click: 'ctrl.removePanel();', icon: "fa fa-fw fa-cube", submenu: extendedMenu});
  119. menu.push({divider: true, role: 'Editor'});
  120. menu.push({text: 'Remove', click: 'ctrl.removePanel();', role: 'Editor', icon: "fa fa-fw fa-trash", shortcut: "p r"});
  121. return menu;
  122. }
  123. getExtendedMenu() {
  124. var actions = [{text: 'Panel JSON', click: 'ctrl.editPanelJson(); dismiss();'}];
  125. this.events.emit('init-panel-actions', actions);
  126. return actions;
  127. }
  128. otherPanelInFullscreenMode() {
  129. return this.dashboard.meta.fullscreen && !this.fullscreen;
  130. }
  131. calculatePanelHeight() {
  132. if (this.fullscreen) {
  133. var docHeight = $(window).height();
  134. var editHeight = Math.floor(docHeight * 0.4);
  135. var fullscreenHeight = Math.floor(docHeight * 0.8);
  136. this.containerHeight = this.editMode ? editHeight : fullscreenHeight;
  137. } else {
  138. this.containerHeight = this.panel.gridPos.h * CELL_HEIGHT + ((this.panel.gridPos.h-1) * CELL_VMARGIN);
  139. }
  140. this.height = this.containerHeight - (PANEL_BORDER + PANEL_PADDING + (this.panel.title ? TITLE_HEIGHT : EMPTY_TITLE_HEIGHT));
  141. }
  142. render(payload?) {
  143. this.timing.renderStart = new Date().getTime();
  144. this.events.emit('render', payload);
  145. }
  146. private onSizeChanged() {
  147. this.calculatePanelHeight();
  148. this.$timeout(() => {
  149. this.render();
  150. }, 100);
  151. }
  152. duplicate() {
  153. this.dashboard.duplicatePanel(this.panel);
  154. this.$timeout(() => {
  155. this.$scope.$root.$broadcast('render');
  156. });
  157. }
  158. removePanel() {
  159. this.dashboard.removePanel(this.panel);
  160. }
  161. editPanelJson() {
  162. this.publishAppEvent('show-json-editor', {
  163. object: this.panel,
  164. updateHandler: this.replacePanel.bind(this)
  165. });
  166. }
  167. replacePanel(newPanel, oldPanel) {
  168. var index = _.indexOf(this.dashboard.panels, oldPanel);
  169. this.dashboard.panels.splice(index, 1);
  170. // adding it back needs to be done in next digest
  171. this.$timeout(() => {
  172. newPanel.id = oldPanel.id;
  173. newPanel.width = oldPanel.width;
  174. this.dashboard.panels.splice(index, 0, newPanel);
  175. });
  176. }
  177. sharePanel() {
  178. var shareScope = this.$scope.$new();
  179. shareScope.panel = this.panel;
  180. shareScope.dashboard = this.dashboard;
  181. this.publishAppEvent('show-modal', {
  182. src: 'public/app/features/dashboard/partials/shareModal.html',
  183. scope: shareScope
  184. });
  185. }
  186. getInfoMode() {
  187. if (this.error) {
  188. return 'error';
  189. }
  190. if (!!this.panel.description) {
  191. return 'info';
  192. }
  193. if (this.panel.links && this.panel.links.length) {
  194. return 'links';
  195. }
  196. return '';
  197. }
  198. getInfoContent(options) {
  199. var markdown = this.panel.description;
  200. if (options.mode === 'tooltip') {
  201. markdown = this.error || this.panel.description;
  202. }
  203. var linkSrv = this.$injector.get('linkSrv');
  204. var templateSrv = this.$injector.get('templateSrv');
  205. var interpolatedMarkdown = templateSrv.replace(markdown, this.panel.scopedVars);
  206. var html = '<div class="markdown-html">';
  207. html += new Remarkable().render(interpolatedMarkdown);
  208. if (this.panel.links && this.panel.links.length > 0) {
  209. html += '<ul>';
  210. for (let link of this.panel.links) {
  211. var info = linkSrv.getPanelLinkAnchorInfo(link, this.panel.scopedVars);
  212. html += '<li><a class="panel-menu-link" href="' + info.href + '" target="' + info.target + '">' + info.title + '</a></li>';
  213. }
  214. html += '</ul>';
  215. }
  216. return html + '</div>';
  217. }
  218. openInspector() {
  219. var modalScope = this.$scope.$new();
  220. modalScope.panel = this.panel;
  221. modalScope.dashboard = this.dashboard;
  222. modalScope.panelInfoHtml = this.getInfoContent({mode: 'inspector'});
  223. modalScope.inspector = $.extend(true, {}, this.inspector);
  224. this.publishAppEvent('show-modal', {
  225. src: 'public/app/features/dashboard/partials/inspector.html',
  226. scope: modalScope
  227. });
  228. }
  229. }