panel_ctrl.ts 8.1 KB

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