panel_ctrl.ts 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 {CELL_HEIGHT, CELL_VMARGIN} from '../dashboard/model';
  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. let extendedMenu = this.getExtendedMenu();
  130. menu.push({text: 'More ...', click: 'ctrl.removePanel();', icon: "fa fa-fw fa-cube", submenu: extendedMenu});
  131. menu.push({divider: true, role: 'Editor'});
  132. menu.push({text: 'Remove', click: 'ctrl.removePanel();', role: 'Editor', icon: "fa fa-fw fa-trash", shortcut: "p r"});
  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 * CELL_HEIGHT + ((this.panel.height-1) * CELL_VMARGIN);
  151. }
  152. this.height = this.containerHeight - (PANEL_BORDER + PANEL_PADDING + (this.panel.title ? TITLE_HEIGHT : EMPTY_TITLE_HEIGHT));
  153. }
  154. render(payload?) {
  155. // ignore if other panel is in fullscreen mode
  156. if (this.otherPanelInFullscreenMode()) {
  157. return;
  158. }
  159. this.calculatePanelHeight();
  160. this.timing.renderStart = new Date().getTime();
  161. this.events.emit('render', payload);
  162. }
  163. toggleEditorHelp(index) {
  164. if (this.editorHelpIndex === index) {
  165. this.editorHelpIndex = null;
  166. return;
  167. }
  168. this.editorHelpIndex = index;
  169. }
  170. duplicate() {
  171. this.dashboard.duplicatePanel(this.panel, this.row);
  172. this.$timeout(() => {
  173. this.$scope.$root.$broadcast('render');
  174. });
  175. }
  176. updateColumnSpan(span) {
  177. this.panel.span = Math.min(Math.max(Math.floor(this.panel.span + span), 1), 12);
  178. this.row.panelSpanChanged();
  179. this.$timeout(() => {
  180. this.render();
  181. });
  182. }
  183. removePanel() {
  184. this.dashboard.removePanel(this.panel);
  185. }
  186. editPanelJson() {
  187. this.publishAppEvent('show-json-editor', {
  188. object: this.panel,
  189. updateHandler: this.replacePanel.bind(this)
  190. });
  191. }
  192. replacePanel(newPanel, oldPanel) {
  193. var row = this.row;
  194. var index = _.indexOf(this.row.panels, oldPanel);
  195. this.row.panels.splice(index, 1);
  196. // adding it back needs to be done in next digest
  197. this.$timeout(() => {
  198. newPanel.id = oldPanel.id;
  199. newPanel.span = oldPanel.span;
  200. this.row.panels.splice(index, 0, newPanel);
  201. });
  202. }
  203. sharePanel() {
  204. var shareScope = this.$scope.$new();
  205. shareScope.panel = this.panel;
  206. shareScope.dashboard = this.dashboard;
  207. this.publishAppEvent('show-modal', {
  208. src: 'public/app/features/dashboard/partials/shareModal.html',
  209. scope: shareScope
  210. });
  211. }
  212. getInfoMode() {
  213. if (this.error) {
  214. return 'error';
  215. }
  216. if (!!this.panel.description) {
  217. return 'info';
  218. }
  219. if (this.panel.links && this.panel.links.length) {
  220. return 'links';
  221. }
  222. return '';
  223. }
  224. getInfoContent(options) {
  225. var markdown = this.panel.description;
  226. if (options.mode === 'tooltip') {
  227. markdown = this.error || this.panel.description;
  228. }
  229. var linkSrv = this.$injector.get('linkSrv');
  230. var templateSrv = this.$injector.get('templateSrv');
  231. var interpolatedMarkdown = templateSrv.replace(markdown, this.panel.scopedVars);
  232. var html = '<div class="markdown-html">';
  233. html += new Remarkable().render(interpolatedMarkdown);
  234. if (this.panel.links && this.panel.links.length > 0) {
  235. html += '<ul>';
  236. for (let link of this.panel.links) {
  237. var info = linkSrv.getPanelLinkAnchorInfo(link, this.panel.scopedVars);
  238. html += '<li><a class="panel-menu-link" href="' + info.href + '" target="' + info.target + '">' + info.title + '</a></li>';
  239. }
  240. html += '</ul>';
  241. }
  242. return html + '</div>';
  243. }
  244. openInspector() {
  245. var modalScope = this.$scope.$new();
  246. modalScope.panel = this.panel;
  247. modalScope.dashboard = this.dashboard;
  248. modalScope.panelInfoHtml = this.getInfoContent({mode: 'inspector'});
  249. modalScope.inspector = $.extend(true, {}, this.inspector);
  250. this.publishAppEvent('show-modal', {
  251. src: 'public/app/features/dashboard/partials/inspector.html',
  252. scope: modalScope
  253. });
  254. }
  255. }