panel_ctrl.ts 8.0 KB

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