panel_ctrl.ts 7.4 KB

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