panel_ctrl.ts 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. ///<reference path="../../headers/common.d.ts" />
  2. import config from 'app/core/config';
  3. import _ from 'lodash';
  4. import angular from 'angular';
  5. import $ from 'jquery';
  6. import {profiler} from 'app/core/profiler';
  7. import Remarkable from 'remarkable';
  8. import {contextSrv} from 'app/core/core';
  9. const TITLE_HEIGHT = 25;
  10. const EMPTY_TITLE_HEIGHT = 9;
  11. const PANEL_PADDING = 5;
  12. const PANEL_BORDER = 2;
  13. import {Emitter} from 'app/core/core';
  14. export class PanelCtrl {
  15. panel: any;
  16. error: any;
  17. row: any;
  18. dashboard: any;
  19. editorTabIndex: number;
  20. pluginName: string;
  21. pluginId: string;
  22. editorTabs: any;
  23. $scope: any;
  24. $injector: any;
  25. $timeout: any;
  26. fullscreen: boolean;
  27. inspector: any;
  28. editModeInitiated: boolean;
  29. editorHelpIndex: number;
  30. editMode: any;
  31. height: any;
  32. containerHeight: any;
  33. events: Emitter;
  34. timing: any;
  35. constructor($scope, $injector) {
  36. this.$injector = $injector;
  37. this.$scope = $scope;
  38. this.$timeout = $injector.get('$timeout');
  39. this.editorTabIndex = 0;
  40. this.events = new Emitter();
  41. this.timing = {};
  42. var plugin = config.panels[this.panel.type];
  43. if (plugin) {
  44. this.pluginId = plugin.id;
  45. this.pluginName = plugin.name;
  46. }
  47. $scope.$on("refresh", () => this.refresh());
  48. $scope.$on("render", () => this.render());
  49. $scope.$on("$destroy", () => {
  50. this.events.emit('panel-teardown');
  51. this.events.removeAllListeners();
  52. });
  53. // we should do something interesting
  54. // with newly added panels
  55. if (this.panel.isNew) {
  56. delete this.panel.isNew;
  57. }
  58. }
  59. init() {
  60. this.calculatePanelHeight();
  61. this.publishAppEvent('panel-initialized', {scope: this.$scope});
  62. this.events.emit('panel-initialized');
  63. }
  64. renderingCompleted() {
  65. profiler.renderingCompleted(this.panel.id, this.timing);
  66. }
  67. refresh() {
  68. this.events.emit('refresh', null);
  69. }
  70. publishAppEvent(evtName, evt) {
  71. this.$scope.$root.appEvent(evtName, evt);
  72. }
  73. changeView(fullscreen, edit) {
  74. this.publishAppEvent('panel-change-view', {
  75. fullscreen: fullscreen, edit: edit, panelId: this.panel.id
  76. });
  77. }
  78. viewPanel() {
  79. this.changeView(true, false);
  80. }
  81. editPanel() {
  82. this.changeView(true, true);
  83. }
  84. exitFullscreen() {
  85. this.changeView(false, false);
  86. }
  87. initEditMode() {
  88. this.editorTabs = [];
  89. this.addEditorTab('General', 'public/app/partials/panelgeneral.html');
  90. this.editModeInitiated = true;
  91. this.events.emit('init-edit-mode', null);
  92. var urlTab = (this.$injector.get('$routeParams').tab || '').toLowerCase();
  93. if (urlTab) {
  94. this.editorTabs.forEach((tab, i) => {
  95. if (tab.title.toLowerCase() === urlTab) {
  96. this.editorTabIndex = i;
  97. }
  98. });
  99. }
  100. }
  101. changeTab(newIndex) {
  102. this.editorTabIndex = newIndex;
  103. var route = this.$injector.get('$route');
  104. route.current.params.tab = this.editorTabs[newIndex].title.toLowerCase();
  105. route.updateParams();
  106. }
  107. addEditorTab(title, directiveFn, index?) {
  108. var editorTab = {title, directiveFn};
  109. if (_.isString(directiveFn)) {
  110. editorTab.directiveFn = function() {
  111. return {templateUrl: directiveFn};
  112. };
  113. }
  114. if (index) {
  115. this.editorTabs.splice(index, 0, editorTab);
  116. } else {
  117. this.editorTabs.push(editorTab);
  118. }
  119. }
  120. getMenu() {
  121. let menu = [];
  122. menu.push({text: 'View', click: 'ctrl.viewPanel();', icon: "fa fa-fw fa-eye", shortcut: "v"});
  123. menu.push({text: 'Edit', click: 'ctrl.editPanel();', role: 'Editor', icon: "fa fa-fw fa-edit", shortcut: "e"});
  124. menu.push({text: 'Share', click: 'ctrl.sharePanel();', icon: "fa fa-fw fa-share", shortcut: "p s"});
  125. if (!this.fullscreen) {
  126. menu.push({ text: 'Duplicate', click: 'ctrl.duplicate()', role: 'Editor', icon: "fa fa-fw fa-copy" });
  127. }
  128. menu.push({divider: true});
  129. menu.push({text: 'Remove', click: 'ctrl.removePanel();', role: 'Editor', icon: "fa fa-fw fa-trash", shortcut: "p r"});
  130. menu.push({divider: true, role: 'Editor'});
  131. let extendedMenu = this.getExtendedMenu();
  132. menu.push({text: 'More ...', click: 'ctrl.removePanel();', icon: "fa fa-fw fa-cube", submenu: extendedMenu});
  133. return menu;
  134. }
  135. getExtendedMenu() {
  136. var actions = [{text: 'Panel JSON', click: 'ctrl.editPanelJson(); dismiss();'}];
  137. this.events.emit('init-panel-actions', actions);
  138. return actions;
  139. }
  140. otherPanelInFullscreenMode() {
  141. return this.dashboard.meta.fullscreen && !this.fullscreen;
  142. }
  143. calculatePanelHeight() {
  144. if (this.fullscreen) {
  145. var docHeight = $(window).height();
  146. var editHeight = Math.floor(docHeight * 0.4);
  147. var fullscreenHeight = Math.floor(docHeight * 0.8);
  148. this.containerHeight = this.editMode ? editHeight : fullscreenHeight;
  149. } else {
  150. this.containerHeight = this.panel.height || this.row.height;
  151. if (_.isString(this.containerHeight)) {
  152. this.containerHeight = parseInt(this.containerHeight.replace('px', ''), 10);
  153. }
  154. }
  155. this.height = this.containerHeight - (PANEL_BORDER + PANEL_PADDING + (this.panel.title ? TITLE_HEIGHT : EMPTY_TITLE_HEIGHT));
  156. }
  157. render(payload?) {
  158. // ignore if other panel is in fullscreen mode
  159. if (this.otherPanelInFullscreenMode()) {
  160. return;
  161. }
  162. this.calculatePanelHeight();
  163. this.timing.renderStart = new Date().getTime();
  164. this.events.emit('render', payload);
  165. }
  166. toggleEditorHelp(index) {
  167. if (this.editorHelpIndex === index) {
  168. this.editorHelpIndex = null;
  169. return;
  170. }
  171. this.editorHelpIndex = index;
  172. }
  173. duplicate() {
  174. this.dashboard.duplicatePanel(this.panel, this.row);
  175. this.$timeout(() => {
  176. this.$scope.$root.$broadcast('render');
  177. });
  178. }
  179. updateColumnSpan(span) {
  180. this.panel.span = Math.min(Math.max(Math.floor(this.panel.span + span), 1), 12);
  181. this.row.panelSpanChanged();
  182. this.$timeout(() => {
  183. this.render();
  184. });
  185. }
  186. removePanel() {
  187. this.row.removePanel(this.panel);
  188. }
  189. editPanelJson() {
  190. this.publishAppEvent('show-json-editor', {
  191. object: this.panel,
  192. updateHandler: this.replacePanel.bind(this)
  193. });
  194. }
  195. replacePanel(newPanel, oldPanel) {
  196. var row = this.row;
  197. var index = _.indexOf(this.row.panels, oldPanel);
  198. this.row.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.span = oldPanel.span;
  203. this.row.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. }