panel_ctrl.ts 8.2 KB

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