panel_ctrl.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. ///<reference path="../../headers/common.d.ts" />
  2. import config from 'app/core/config';
  3. import _ from 'lodash';
  4. import $ from 'jquery';
  5. import {profiler} from 'app/core/profiler';
  6. import Remarkable from 'remarkable';
  7. import {CELL_HEIGHT, CELL_VMARGIN} from '../dashboard/model';
  8. const TITLE_HEIGHT = 25;
  9. const EMPTY_TITLE_HEIGHT = 9;
  10. const PANEL_PADDING = 5;
  11. const PANEL_BORDER = 2;
  12. import {Emitter} from 'app/core/core';
  13. export class PanelCtrl {
  14. panel: any;
  15. error: any;
  16. dashboard: any;
  17. editorTabIndex: number;
  18. pluginName: string;
  19. pluginId: string;
  20. editorTabs: any;
  21. $scope: any;
  22. $injector: any;
  23. $timeout: any;
  24. fullscreen: boolean;
  25. inspector: any;
  26. editModeInitiated: boolean;
  27. editMode: any;
  28. height: any;
  29. containerHeight: any;
  30. events: Emitter;
  31. timing: any;
  32. constructor($scope, $injector) {
  33. this.$injector = $injector;
  34. this.$scope = $scope;
  35. this.$timeout = $injector.get('$timeout');
  36. this.editorTabIndex = 0;
  37. this.events = new Emitter();
  38. this.timing = {};
  39. var plugin = config.panels[this.panel.type];
  40. if (plugin) {
  41. this.pluginId = plugin.id;
  42. this.pluginName = plugin.name;
  43. }
  44. $scope.$on("refresh", () => this.refresh());
  45. $scope.$on("render", () => this.render());
  46. $scope.$on("$destroy", () => {
  47. this.events.emit('panel-teardown');
  48. this.events.removeAllListeners();
  49. });
  50. // we should do something interesting
  51. // with newly added panels
  52. if (this.panel.isNew) {
  53. delete this.panel.isNew;
  54. }
  55. }
  56. init() {
  57. this.calculatePanelHeight();
  58. this.publishAppEvent('panel-initialized', {scope: this.$scope});
  59. this.events.emit('panel-initialized');
  60. }
  61. renderingCompleted() {
  62. profiler.renderingCompleted(this.panel.id, this.timing);
  63. }
  64. refresh() {
  65. this.events.emit('refresh', null);
  66. }
  67. publishAppEvent(evtName, evt) {
  68. this.$scope.$root.appEvent(evtName, evt);
  69. }
  70. changeView(fullscreen, edit) {
  71. this.publishAppEvent('panel-change-view', {
  72. fullscreen: fullscreen, edit: edit, panelId: this.panel.id
  73. });
  74. }
  75. viewPanel() {
  76. this.changeView(true, false);
  77. }
  78. editPanel() {
  79. this.changeView(true, true);
  80. }
  81. exitFullscreen() {
  82. this.changeView(false, false);
  83. }
  84. initEditMode() {
  85. this.editorTabs = [];
  86. this.addEditorTab('General', 'public/app/partials/panelgeneral.html');
  87. this.editModeInitiated = true;
  88. this.events.emit('init-edit-mode', null);
  89. var urlTab = (this.$injector.get('$routeParams').tab || '').toLowerCase();
  90. if (urlTab) {
  91. this.editorTabs.forEach((tab, i) => {
  92. if (tab.title.toLowerCase() === urlTab) {
  93. this.editorTabIndex = i;
  94. }
  95. });
  96. }
  97. }
  98. changeTab(newIndex) {
  99. this.editorTabIndex = newIndex;
  100. var route = this.$injector.get('$route');
  101. route.current.params.tab = this.editorTabs[newIndex].title.toLowerCase();
  102. route.updateParams();
  103. }
  104. addEditorTab(title, directiveFn, index?) {
  105. var editorTab = {title, directiveFn};
  106. if (_.isString(directiveFn)) {
  107. editorTab.directiveFn = function() {
  108. return {templateUrl: directiveFn};
  109. };
  110. }
  111. if (index) {
  112. this.editorTabs.splice(index, 0, editorTab);
  113. } else {
  114. this.editorTabs.push(editorTab);
  115. }
  116. }
  117. getMenu() {
  118. let menu = [];
  119. menu.push({text: 'View', click: 'ctrl.viewPanel();', icon: "fa fa-fw fa-eye", shortcut: "v"});
  120. menu.push({text: 'Edit', click: 'ctrl.editPanel();', role: 'Editor', icon: "fa fa-fw fa-edit", shortcut: "e"});
  121. menu.push({text: 'Share', click: 'ctrl.sharePanel();', icon: "fa fa-fw fa-share", shortcut: "p s"});
  122. if (!this.fullscreen) {
  123. menu.push({ text: 'Duplicate', click: 'ctrl.duplicate()', role: 'Editor', icon: "fa fa-fw fa-copy" });
  124. }
  125. menu.push({divider: true});
  126. let extendedMenu = this.getExtendedMenu();
  127. menu.push({text: 'More ...', click: 'ctrl.removePanel();', icon: "fa fa-fw fa-cube", submenu: extendedMenu});
  128. menu.push({divider: true, role: 'Editor'});
  129. menu.push({text: 'Remove', click: 'ctrl.removePanel();', role: 'Editor', icon: "fa fa-fw fa-trash", shortcut: "p r"});
  130. return menu;
  131. }
  132. getExtendedMenu() {
  133. var actions = [{text: 'Panel JSON', click: 'ctrl.editPanelJson(); dismiss();'}];
  134. this.events.emit('init-panel-actions', actions);
  135. return actions;
  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.height * CELL_HEIGHT + ((this.panel.height-1) * CELL_VMARGIN);
  148. }
  149. this.height = this.containerHeight - (PANEL_BORDER + PANEL_PADDING + (this.panel.title ? TITLE_HEIGHT : EMPTY_TITLE_HEIGHT));
  150. }
  151. render(payload?) {
  152. // ignore if other panel is in fullscreen mode
  153. if (this.otherPanelInFullscreenMode()) {
  154. return;
  155. }
  156. this.calculatePanelHeight();
  157. this.timing.renderStart = new Date().getTime();
  158. this.events.emit('render', payload);
  159. }
  160. duplicate() {
  161. this.dashboard.duplicatePanel(this.panel);
  162. this.$timeout(() => {
  163. this.$scope.$root.$broadcast('render');
  164. });
  165. }
  166. removePanel() {
  167. this.dashboard.removePanel(this.panel);
  168. }
  169. editPanelJson() {
  170. this.publishAppEvent('show-json-editor', {
  171. object: this.panel,
  172. updateHandler: this.replacePanel.bind(this)
  173. });
  174. }
  175. replacePanel(newPanel, oldPanel) {
  176. var index = _.indexOf(this.dashboard.panels, oldPanel);
  177. this.dashboard.panels.splice(index, 1);
  178. // adding it back needs to be done in next digest
  179. this.$timeout(() => {
  180. newPanel.id = oldPanel.id;
  181. newPanel.width = oldPanel.width;
  182. this.dashboard.panels.splice(index, 0, newPanel);
  183. });
  184. }
  185. sharePanel() {
  186. var shareScope = this.$scope.$new();
  187. shareScope.panel = this.panel;
  188. shareScope.dashboard = this.dashboard;
  189. this.publishAppEvent('show-modal', {
  190. src: 'public/app/features/dashboard/partials/shareModal.html',
  191. scope: shareScope
  192. });
  193. }
  194. getInfoMode() {
  195. if (this.error) {
  196. return 'error';
  197. }
  198. if (!!this.panel.description) {
  199. return 'info';
  200. }
  201. if (this.panel.links && this.panel.links.length) {
  202. return 'links';
  203. }
  204. return '';
  205. }
  206. getInfoContent(options) {
  207. var markdown = this.panel.description;
  208. if (options.mode === 'tooltip') {
  209. markdown = this.error || this.panel.description;
  210. }
  211. var linkSrv = this.$injector.get('linkSrv');
  212. var templateSrv = this.$injector.get('templateSrv');
  213. var interpolatedMarkdown = templateSrv.replace(markdown, this.panel.scopedVars);
  214. var html = '<div class="markdown-html">';
  215. html += new Remarkable().render(interpolatedMarkdown);
  216. if (this.panel.links && this.panel.links.length > 0) {
  217. html += '<ul>';
  218. for (let link of this.panel.links) {
  219. var info = linkSrv.getPanelLinkAnchorInfo(link, this.panel.scopedVars);
  220. html += '<li><a class="panel-menu-link" href="' + info.href + '" target="' + info.target + '">' + info.title + '</a></li>';
  221. }
  222. html += '</ul>';
  223. }
  224. return html + '</div>';
  225. }
  226. openInspector() {
  227. var modalScope = this.$scope.$new();
  228. modalScope.panel = this.panel;
  229. modalScope.dashboard = this.dashboard;
  230. modalScope.panelInfoHtml = this.getInfoContent({mode: 'inspector'});
  231. modalScope.inspector = $.extend(true, {}, this.inspector);
  232. this.publishAppEvent('show-modal', {
  233. src: 'public/app/features/dashboard/partials/inspector.html',
  234. scope: modalScope
  235. });
  236. }
  237. }