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 = 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. this.calculatePanelHeight();
  49. }
  50. init() {
  51. this.events.on('panel-size-changed', this.onSizeChanged.bind(this));
  52. this.events.emit('panel-initialized');
  53. this.publishAppEvent('panel-initialized', {scope: this.$scope});
  54. }
  55. panelDidMount() {
  56. this.events.emit('component-did-mount');
  57. }
  58. renderingCompleted() {
  59. profiler.renderingCompleted(this.panel.id, this.timing);
  60. }
  61. refresh() {
  62. this.events.emit('refresh', null);
  63. }
  64. publishAppEvent(evtName, evt) {
  65. this.$scope.$root.appEvent(evtName, evt);
  66. }
  67. changeView(fullscreen, edit) {
  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. if (this.panel.soloMode) {
  147. this.containerHeight = $(window).height();
  148. }
  149. this.height = this.containerHeight - (PANEL_BORDER + TITLE_HEIGHT);
  150. }
  151. render(payload?) {
  152. this.timing.renderStart = new Date().getTime();
  153. this.events.emit('render', payload);
  154. }
  155. private onSizeChanged() {
  156. this.calculatePanelHeight();
  157. this.$timeout(() => {
  158. this.render();
  159. }, 100);
  160. }
  161. duplicate() {
  162. this.dashboard.duplicatePanel(this.panel);
  163. this.$timeout(() => {
  164. this.$scope.$root.$broadcast('render');
  165. });
  166. }
  167. removePanel(ask: boolean) {
  168. // confirm deletion
  169. if (ask !== false) {
  170. var text2, confirmText;
  171. if (this.panel.alert) {
  172. text2 = "Panel includes an alert rule, removing panel will also remove alert rule";
  173. confirmText = "YES";
  174. }
  175. appEvents.emit('confirm-modal', {
  176. title: 'Remove Panel',
  177. text: 'Are you sure you want to remove this panel?',
  178. text2: text2,
  179. icon: 'fa-trash',
  180. confirmText: confirmText,
  181. yesText: 'Remove',
  182. onConfirm: () => {
  183. this.removePanel(false);
  184. }
  185. });
  186. return;
  187. }
  188. this.dashboard.removePanel(this.panel);
  189. }
  190. editPanelJson() {
  191. this.publishAppEvent('show-json-editor', {
  192. object: this.panel.getSaveModel(),
  193. updateHandler: this.replacePanel.bind(this)
  194. });
  195. }
  196. replacePanel(newPanel, oldPanel) {
  197. var index = _.indexOf(this.dashboard.panels, oldPanel);
  198. this.dashboard.panels.splice(index, 1);
  199. // adding it back needs to be done in next digest
  200. this.$timeout(() => {
  201. newPanel.id = oldPanel.id;
  202. newPanel.width = oldPanel.width;
  203. this.dashboard.panels.splice(index, 0, newPanel);
  204. });
  205. }
  206. sharePanel() {
  207. var shareScope = this.$scope.$new();
  208. shareScope.panel = this.panel;
  209. shareScope.dashboard = this.dashboard;
  210. this.publishAppEvent('show-modal', {
  211. src: 'public/app/features/dashboard/partials/shareModal.html',
  212. scope: shareScope
  213. });
  214. }
  215. getInfoMode() {
  216. if (this.error) {
  217. return 'error';
  218. }
  219. if (!!this.panel.description) {
  220. return 'info';
  221. }
  222. if (this.panel.links && this.panel.links.length) {
  223. return 'links';
  224. }
  225. return '';
  226. }
  227. getInfoContent(options) {
  228. var markdown = this.panel.description;
  229. if (options.mode === 'tooltip') {
  230. markdown = this.error || this.panel.description;
  231. }
  232. var linkSrv = this.$injector.get('linkSrv');
  233. var templateSrv = this.$injector.get('templateSrv');
  234. var interpolatedMarkdown = templateSrv.replace(markdown, this.panel.scopedVars);
  235. var html = '<div class="markdown-html">';
  236. html += new Remarkable().render(interpolatedMarkdown);
  237. if (this.panel.links && this.panel.links.length > 0) {
  238. html += '<ul>';
  239. for (let link of this.panel.links) {
  240. var info = linkSrv.getPanelLinkAnchorInfo(link, this.panel.scopedVars);
  241. html += '<li><a class="panel-menu-link" href="' + info.href + '" target="' + info.target + '">' + info.title + '</a></li>';
  242. }
  243. html += '</ul>';
  244. }
  245. return html + '</div>';
  246. }
  247. openInspector() {
  248. var modalScope = this.$scope.$new();
  249. modalScope.panel = this.panel;
  250. modalScope.dashboard = this.dashboard;
  251. modalScope.panelInfoHtml = this.getInfoContent({mode: 'inspector'});
  252. modalScope.inspector = $.extend(true, {}, this.inspector);
  253. this.publishAppEvent('show-modal', {
  254. src: 'public/app/features/dashboard/partials/inspector.html',
  255. scope: modalScope
  256. });
  257. }
  258. }