panel_ctrl.ts 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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} from 'app/core/constants';
  8. const TITLE_HEIGHT = 27;
  9. const PANEL_BORDER = 2;
  10. import {Emitter} from 'app/core/core';
  11. export class PanelCtrl {
  12. panel: any;
  13. error: any;
  14. dashboard: any;
  15. editorTabIndex: number;
  16. pluginName: string;
  17. pluginId: string;
  18. editorTabs: any;
  19. $scope: any;
  20. $injector: any;
  21. $timeout: any;
  22. fullscreen: boolean;
  23. inspector: any;
  24. editModeInitiated: boolean;
  25. editMode: any;
  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.$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("component-did-mount", () => this.panelDidMount());
  45. $scope.$on("$destroy", () => {
  46. this.events.emit('panel-teardown');
  47. this.events.removeAllListeners();
  48. });
  49. }
  50. init() {
  51. this.events.emit('panel-initialized');
  52. this.publishAppEvent('panel-initialized', {scope: this.$scope});
  53. }
  54. panelDidMount() {
  55. this.events.emit('component-did-mount');
  56. }
  57. renderingCompleted() {
  58. profiler.renderingCompleted(this.panel.id, this.timing);
  59. }
  60. refresh() {
  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.publishAppEvent('panel-change-view', {
  68. fullscreen: fullscreen, edit: edit, panelId: this.panel.id
  69. });
  70. }
  71. viewPanel() {
  72. this.changeView(true, false);
  73. }
  74. editPanel() {
  75. this.changeView(true, true);
  76. }
  77. exitFullscreen() {
  78. this.changeView(false, false);
  79. }
  80. initEditMode() {
  81. this.editorTabs = [];
  82. this.addEditorTab('General', 'public/app/partials/panelgeneral.html');
  83. this.editModeInitiated = true;
  84. this.events.emit('init-edit-mode', null);
  85. var urlTab = (this.$injector.get('$routeParams').tab || '').toLowerCase();
  86. if (urlTab) {
  87. this.editorTabs.forEach((tab, i) => {
  88. if (tab.title.toLowerCase() === urlTab) {
  89. this.editorTabIndex = i;
  90. }
  91. });
  92. }
  93. }
  94. changeTab(newIndex) {
  95. this.editorTabIndex = newIndex;
  96. var route = this.$injector.get('$route');
  97. route.current.params.tab = this.editorTabs[newIndex].title.toLowerCase();
  98. route.updateParams();
  99. }
  100. addEditorTab(title, directiveFn, index?) {
  101. var editorTab = {title, directiveFn};
  102. if (_.isString(directiveFn)) {
  103. editorTab.directiveFn = function() {
  104. return {templateUrl: directiveFn};
  105. };
  106. }
  107. if (index) {
  108. this.editorTabs.splice(index, 0, editorTab);
  109. } else {
  110. this.editorTabs.push(editorTab);
  111. }
  112. }
  113. getMenu() {
  114. let menu = [];
  115. menu.push({text: 'View', click: 'ctrl.viewPanel();', icon: "fa fa-fw fa-eye", shortcut: "v"});
  116. if (this.dashboard.meta.canEdit) {
  117. menu.push({text: 'Edit', click: 'ctrl.editPanel();', role: 'Editor', icon: "fa fa-fw fa-edit", shortcut: "e"});
  118. }
  119. menu.push({text: 'Share', click: 'ctrl.sharePanel();', icon: "fa fa-fw fa-share", shortcut: "p s"});
  120. let extendedMenu = this.getExtendedMenu();
  121. menu.push({text: 'More ...', click: 'ctrl.removePanel();', icon: "fa fa-fw fa-cube", submenu: extendedMenu});
  122. if (this.dashboard.meta.canEdit) {
  123. menu.push({divider: true, role: 'Editor'});
  124. menu.push({text: 'Remove', click: 'ctrl.removePanel();', role: 'Editor', icon: "fa fa-fw fa-trash", shortcut: "p r"});
  125. }
  126. return menu;
  127. }
  128. getExtendedMenu() {
  129. let menu = [];
  130. if (!this.fullscreen && this.dashboard.meta.canEdit) {
  131. menu.push({ text: 'Duplicate', click: 'ctrl.duplicate()', role: 'Editor' });
  132. }
  133. menu.push({text: 'Panel JSON', click: 'ctrl.editPanelJson(); dismiss();' });
  134. this.events.emit('init-panel-actions', menu);
  135. return menu;
  136. }
  137. otherPanelInFullscreenMode() {
  138. return this.dashboard.meta.fullscreen && !this.fullscreen;
  139. }
  140. calculatePanelHeight() {
  141. if (this.fullscreen) {
  142. var docHeight = $(window).height();
  143. var editHeight = Math.floor(docHeight * 0.4);
  144. var fullscreenHeight = Math.floor(docHeight * 0.8);
  145. this.containerHeight = this.editMode ? editHeight : fullscreenHeight;
  146. } else {
  147. this.containerHeight = this.panel.gridPos.h * GRID_CELL_HEIGHT + ((this.panel.gridPos.h-1) * GRID_CELL_VMARGIN);
  148. }
  149. if (this.panel.soloMode) {
  150. this.containerHeight = $(window).height();
  151. }
  152. this.height = this.containerHeight - (PANEL_BORDER + TITLE_HEIGHT);
  153. }
  154. render(payload?) {
  155. this.timing.renderStart = new Date().getTime();
  156. this.events.emit('render', payload);
  157. }
  158. duplicate() {
  159. this.dashboard.duplicatePanel(this.panel);
  160. this.$timeout(() => {
  161. this.$scope.$root.$broadcast('render');
  162. });
  163. }
  164. removePanel(ask: boolean) {
  165. // confirm deletion
  166. if (ask !== false) {
  167. var text2, confirmText;
  168. if (this.panel.alert) {
  169. text2 = "Panel includes an alert rule, removing panel will also remove alert rule";
  170. confirmText = "YES";
  171. }
  172. appEvents.emit('confirm-modal', {
  173. title: 'Remove Panel',
  174. text: 'Are you sure you want to remove this panel?',
  175. text2: text2,
  176. icon: 'fa-trash',
  177. confirmText: confirmText,
  178. yesText: 'Remove',
  179. onConfirm: () => {
  180. this.removePanel(false);
  181. }
  182. });
  183. return;
  184. }
  185. this.dashboard.removePanel(this.panel);
  186. }
  187. editPanelJson() {
  188. let editScope = this.$scope.$root.$new();
  189. editScope.object = this.panel.getSaveModel();
  190. editScope.updateHandler = this.replacePanel.bind(this);
  191. this.publishAppEvent('show-modal', {
  192. src: 'public/app/partials/edit_json.html',
  193. scope: editScope
  194. });
  195. }
  196. replacePanel(newPanel, oldPanel) {
  197. let dashboard = this.dashboard;
  198. let index = _.findIndex(dashboard.panels, (panel) => {
  199. return panel.id === oldPanel.id;
  200. });
  201. let deletedPanel = dashboard.panels.splice(index, 1);
  202. this.dashboard.events.emit('panel-removed', deletedPanel);
  203. newPanel = new PanelModel(newPanel);
  204. newPanel.id = oldPanel.id;
  205. dashboard.panels.splice(index, 0, newPanel);
  206. dashboard.sortPanelsByGridPos();
  207. dashboard.events.emit('panel-added', newPanel);
  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. }