panel_ctrl.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. const TITLE_HEIGHT = 25;
  7. const EMPTY_TITLE_HEIGHT = 9;
  8. const PANEL_PADDING = 5;
  9. const PANEL_BORDER = 2;
  10. import {Emitter} from 'app/core/core';
  11. export class PanelCtrl {
  12. panel: any;
  13. error: any;
  14. row: 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 = new Emitter();
  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("render", () => this.render());
  45. $scope.$on("$destroy", () => {
  46. this.events.emit('panel-teardown');
  47. this.events.removeAllListeners();
  48. });
  49. // we should do something interesting
  50. // with newly added panels
  51. if (this.panel.isNew) {
  52. delete this.panel.isNew;
  53. }
  54. }
  55. init() {
  56. this.calculatePanelHeight();
  57. this.publishAppEvent('panel-initialized', {scope: this.$scope});
  58. this.events.emit('panel-initialized');
  59. }
  60. renderingCompleted() {
  61. profiler.renderingCompleted(this.panel.id, this.timing);
  62. }
  63. refresh() {
  64. this.events.emit('refresh', null);
  65. }
  66. publishAppEvent(evtName, evt) {
  67. this.$scope.$root.appEvent(evtName, evt);
  68. }
  69. changeView(fullscreen, edit) {
  70. this.publishAppEvent('panel-change-view', {
  71. fullscreen: fullscreen, edit: edit, panelId: this.panel.id
  72. });
  73. }
  74. viewPanel() {
  75. this.changeView(true, false);
  76. }
  77. editPanel() {
  78. this.changeView(true, true);
  79. }
  80. exitFullscreen() {
  81. this.changeView(false, false);
  82. }
  83. initEditMode() {
  84. this.editorTabs = [];
  85. this.addEditorTab('General', 'public/app/partials/panelgeneral.html');
  86. this.editModeInitiated = true;
  87. this.events.emit('init-edit-mode', null);
  88. var urlTab = (this.$injector.get('$routeParams').tab || '').toLowerCase();
  89. if (urlTab) {
  90. this.editorTabs.forEach((tab, i) => {
  91. if (tab.title.toLowerCase() === urlTab) {
  92. this.editorTabIndex = i;
  93. }
  94. });
  95. }
  96. }
  97. changeTab(newIndex) {
  98. this.editorTabIndex = newIndex;
  99. var route = this.$injector.get('$route');
  100. route.current.params.tab = this.editorTabs[newIndex].title.toLowerCase();
  101. route.updateParams();
  102. }
  103. addEditorTab(title, directiveFn, index?) {
  104. var editorTab = {title, directiveFn};
  105. if (_.isString(directiveFn)) {
  106. editorTab.directiveFn = function() {
  107. return {templateUrl: directiveFn};
  108. };
  109. }
  110. if (index) {
  111. this.editorTabs.splice(index, 0, editorTab);
  112. } else {
  113. this.editorTabs.push(editorTab);
  114. }
  115. }
  116. getMenu() {
  117. let menu = [];
  118. menu.push({text: 'View', click: 'ctrl.viewPanel(); dismiss();'});
  119. menu.push({text: 'Edit', click: 'ctrl.editPanel(); dismiss();', role: 'Editor'});
  120. if (!this.fullscreen) { // duplication is not supported in fullscreen mode
  121. menu.push({ text: 'Duplicate', click: 'ctrl.duplicate()', role: 'Editor' });
  122. }
  123. menu.push({text: 'Share', click: 'ctrl.sharePanel(); dismiss();'});
  124. return menu;
  125. }
  126. getExtendedMenu() {
  127. var actions = [{text: 'Panel JSON', click: 'ctrl.editPanelJson(); dismiss();'}];
  128. this.events.emit('init-panel-actions', actions);
  129. return actions;
  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.height || this.row.height;
  142. if (_.isString(this.containerHeight)) {
  143. this.containerHeight = parseInt(this.containerHeight.replace('px', ''), 10);
  144. }
  145. }
  146. this.height = this.containerHeight - (PANEL_BORDER + PANEL_PADDING + (this.panel.title ? TITLE_HEIGHT : EMPTY_TITLE_HEIGHT));
  147. }
  148. render(payload?) {
  149. // ignore if other panel is in fullscreen mode
  150. if (this.otherPanelInFullscreenMode()) {
  151. return;
  152. }
  153. this.calculatePanelHeight();
  154. this.timing.renderStart = new Date().getTime();
  155. this.events.emit('render', payload);
  156. }
  157. duplicate() {
  158. this.dashboard.duplicatePanel(this.panel, this.row);
  159. this.$timeout(() => {
  160. this.$scope.$root.$broadcast('render');
  161. });
  162. }
  163. updateColumnSpan(span) {
  164. this.panel.span = Math.min(Math.max(Math.floor(this.panel.span + span), 1), 12);
  165. this.row.panelSpanChanged();
  166. this.$timeout(() => {
  167. this.render();
  168. });
  169. }
  170. removePanel() {
  171. this.row.removePanel(this.panel);
  172. }
  173. editPanelJson() {
  174. this.publishAppEvent('show-json-editor', {
  175. object: this.panel,
  176. updateHandler: this.replacePanel.bind(this)
  177. });
  178. }
  179. replacePanel(newPanel, oldPanel) {
  180. var index = _.indexOf(this.row.panels, oldPanel);
  181. this.row.panels.splice(index, 1);
  182. // adding it back needs to be done in next digest
  183. this.$timeout(() => {
  184. newPanel.id = oldPanel.id;
  185. newPanel.span = oldPanel.span;
  186. this.row.panels.splice(index, 0, newPanel);
  187. });
  188. }
  189. sharePanel() {
  190. var shareScope = this.$scope.$new();
  191. shareScope.panel = this.panel;
  192. shareScope.dashboard = this.dashboard;
  193. this.publishAppEvent('show-modal', {
  194. src: 'public/app/features/dashboard/partials/shareModal.html',
  195. scope: shareScope
  196. });
  197. }
  198. getInfoMode() {
  199. if (this.error) {
  200. return 'error';
  201. }
  202. if (!!this.panel.description) {
  203. return 'info';
  204. }
  205. if (this.panel.links && this.panel.links.length) {
  206. return 'links';
  207. }
  208. return '';
  209. }
  210. getInfoContent(options) {
  211. var markdown = this.panel.description;
  212. if (options.mode === 'tooltip') {
  213. markdown = this.error || this.panel.description;
  214. }
  215. var linkSrv = this.$injector.get('linkSrv');
  216. var templateSrv = this.$injector.get('templateSrv');
  217. var interpolatedMarkdown = templateSrv.replace(markdown, this.panel.scopedVars);
  218. var html = '<div class="markdown-html">';
  219. html += new Remarkable().render(interpolatedMarkdown);
  220. if (this.panel.links && this.panel.links.length > 0) {
  221. html += '<ul>';
  222. for (let link of this.panel.links) {
  223. var info = linkSrv.getPanelLinkAnchorInfo(link, this.panel.scopedVars);
  224. html += '<li><a class="panel-menu-link" href="' + info.href + '" target="' + info.target + '">' + info.title + '</a></li>';
  225. }
  226. html += '</ul>';
  227. }
  228. return html + '</div>';
  229. }
  230. openInspector() {
  231. var modalScope = this.$scope.$new();
  232. modalScope.panel = this.panel;
  233. modalScope.dashboard = this.dashboard;
  234. modalScope.panelInfoHtml = this.getInfoContent({mode: 'inspector'});
  235. modalScope.inspector = $.extend(true, {}, this.inspector);
  236. this.publishAppEvent('show-modal', {
  237. src: 'public/app/features/dashboard/partials/inspector.html',
  238. scope: modalScope
  239. });
  240. }
  241. }