panel_ctrl.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 Remarkable from 'remarkable';
  6. import {GRID_CELL_HEIGHT, GRID_CELL_VMARGIN} from 'app/core/constants';
  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.publishAppEvent('panel-change-view', {
  65. fullscreen: fullscreen, edit: edit, panelId: this.panel.id
  66. });
  67. }
  68. viewPanel() {
  69. this.changeView(true, false);
  70. }
  71. editPanel() {
  72. this.changeView(true, true);
  73. }
  74. exitFullscreen() {
  75. this.changeView(false, false);
  76. }
  77. initEditMode() {
  78. this.editorTabs = [];
  79. this.addEditorTab('General', 'public/app/partials/panelgeneral.html');
  80. this.editModeInitiated = true;
  81. this.events.emit('init-edit-mode', null);
  82. var urlTab = (this.$injector.get('$routeParams').tab || '').toLowerCase();
  83. if (urlTab) {
  84. this.editorTabs.forEach((tab, i) => {
  85. if (tab.title.toLowerCase() === urlTab) {
  86. this.editorTabIndex = i;
  87. }
  88. });
  89. }
  90. }
  91. changeTab(newIndex) {
  92. this.editorTabIndex = newIndex;
  93. var route = this.$injector.get('$route');
  94. route.current.params.tab = this.editorTabs[newIndex].title.toLowerCase();
  95. route.updateParams();
  96. }
  97. addEditorTab(title, directiveFn, index?) {
  98. var editorTab = {title, directiveFn};
  99. if (_.isString(directiveFn)) {
  100. editorTab.directiveFn = function() {
  101. return {templateUrl: directiveFn};
  102. };
  103. }
  104. if (index) {
  105. this.editorTabs.splice(index, 0, editorTab);
  106. } else {
  107. this.editorTabs.push(editorTab);
  108. }
  109. }
  110. getMenu() {
  111. let menu = [];
  112. menu.push({text: 'View', click: 'ctrl.viewPanel();', icon: "fa fa-fw fa-eye", shortcut: "v"});
  113. menu.push({text: 'Edit', click: 'ctrl.editPanel();', role: 'Editor', icon: "fa fa-fw fa-edit", shortcut: "e"});
  114. menu.push({text: 'Share', click: 'ctrl.sharePanel();', icon: "fa fa-fw fa-share", shortcut: "p s"});
  115. let extendedMenu = this.getExtendedMenu();
  116. menu.push({text: 'More ...', click: 'ctrl.removePanel();', icon: "fa fa-fw fa-cube", submenu: extendedMenu});
  117. menu.push({divider: true, role: 'Editor'});
  118. menu.push({text: 'Remove', click: 'ctrl.removePanel();', role: 'Editor', icon: "fa fa-fw fa-trash", shortcut: "p r"});
  119. return menu;
  120. }
  121. getExtendedMenu() {
  122. let menu = [];
  123. if (!this.fullscreen) {
  124. menu.push({ text: 'Duplicate', click: 'ctrl.duplicate()', role: 'Editor' });
  125. }
  126. menu.push({text: 'Panel JSON', click: 'ctrl.editPanelJson(); dismiss();' });
  127. this.events.emit('init-panel-actions', menu);
  128. return menu;
  129. }
  130. otherPanelInFullscreenMode() {
  131. return this.dashboard.meta.fullscreen && !this.fullscreen;
  132. }
  133. calculatePanelHeight() {
  134. if (this.fullscreen) {
  135. var docHeight = $(window).height();
  136. var editHeight = Math.floor(docHeight * 0.4);
  137. var fullscreenHeight = Math.floor(docHeight * 0.8);
  138. this.containerHeight = this.editMode ? editHeight : fullscreenHeight;
  139. } else {
  140. this.containerHeight = this.panel.gridPos.h * GRID_CELL_HEIGHT + ((this.panel.gridPos.h-1) * GRID_CELL_VMARGIN);
  141. }
  142. this.height = this.containerHeight - (PANEL_BORDER + PANEL_PADDING + (this.panel.title ? TITLE_HEIGHT : EMPTY_TITLE_HEIGHT));
  143. }
  144. render(payload?) {
  145. this.timing.renderStart = new Date().getTime();
  146. this.events.emit('render', payload);
  147. }
  148. private onSizeChanged() {
  149. this.calculatePanelHeight();
  150. this.$timeout(() => {
  151. this.render();
  152. }, 100);
  153. }
  154. duplicate() {
  155. this.dashboard.duplicatePanel(this.panel);
  156. this.$timeout(() => {
  157. this.$scope.$root.$broadcast('render');
  158. });
  159. }
  160. removePanel(ask: boolean) {
  161. // confirm deletion
  162. if (ask !== false) {
  163. var text2, confirmText;
  164. if (this.panel.alert) {
  165. text2 = "Panel includes an alert rule, removing panel will also remove alert rule";
  166. confirmText = "YES";
  167. }
  168. appEvents.emit('confirm-modal', {
  169. title: 'Remove Panel',
  170. text: 'Are you sure you want to remove this panel?',
  171. text2: text2,
  172. icon: 'fa-trash',
  173. confirmText: confirmText,
  174. yesText: 'Remove',
  175. onConfirm: () => {
  176. this.removePanel(false);
  177. }
  178. });
  179. return;
  180. }
  181. this.dashboard.removePanel(this.panel);
  182. }
  183. editPanelJson() {
  184. this.publishAppEvent('show-json-editor', {
  185. object: this.panel.getSaveModel(),
  186. updateHandler: this.replacePanel.bind(this)
  187. });
  188. }
  189. replacePanel(newPanel, oldPanel) {
  190. var index = _.indexOf(this.dashboard.panels, oldPanel);
  191. this.dashboard.panels.splice(index, 1);
  192. // adding it back needs to be done in next digest
  193. this.$timeout(() => {
  194. newPanel.id = oldPanel.id;
  195. newPanel.width = oldPanel.width;
  196. this.dashboard.panels.splice(index, 0, newPanel);
  197. });
  198. }
  199. sharePanel() {
  200. var shareScope = this.$scope.$new();
  201. shareScope.panel = this.panel;
  202. shareScope.dashboard = this.dashboard;
  203. this.publishAppEvent('show-modal', {
  204. src: 'public/app/features/dashboard/partials/shareModal.html',
  205. scope: shareScope
  206. });
  207. }
  208. getInfoMode() {
  209. if (this.error) {
  210. return 'error';
  211. }
  212. if (!!this.panel.description) {
  213. return 'info';
  214. }
  215. if (this.panel.links && this.panel.links.length) {
  216. return 'links';
  217. }
  218. return '';
  219. }
  220. getInfoContent(options) {
  221. var markdown = this.panel.description;
  222. if (options.mode === 'tooltip') {
  223. markdown = this.error || this.panel.description;
  224. }
  225. var linkSrv = this.$injector.get('linkSrv');
  226. var templateSrv = this.$injector.get('templateSrv');
  227. var interpolatedMarkdown = templateSrv.replace(markdown, this.panel.scopedVars);
  228. var html = '<div class="markdown-html">';
  229. html += new Remarkable().render(interpolatedMarkdown);
  230. if (this.panel.links && this.panel.links.length > 0) {
  231. html += '<ul>';
  232. for (let link of this.panel.links) {
  233. var info = linkSrv.getPanelLinkAnchorInfo(link, this.panel.scopedVars);
  234. html += '<li><a class="panel-menu-link" href="' + info.href + '" target="' + info.target + '">' + info.title + '</a></li>';
  235. }
  236. html += '</ul>';
  237. }
  238. return html + '</div>';
  239. }
  240. openInspector() {
  241. var modalScope = this.$scope.$new();
  242. modalScope.panel = this.panel;
  243. modalScope.dashboard = this.dashboard;
  244. modalScope.panelInfoHtml = this.getInfoContent({mode: 'inspector'});
  245. modalScope.inspector = $.extend(true, {}, this.inspector);
  246. this.publishAppEvent('show-modal', {
  247. src: 'public/app/features/dashboard/partials/inspector.html',
  248. scope: modalScope
  249. });
  250. }
  251. }