panel_ctrl.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. 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. row: any;
  17. dashboard: any;
  18. editorTabIndex: number;
  19. pluginName: string;
  20. pluginId: string;
  21. editorTabs: any;
  22. $scope: any;
  23. $injector: any;
  24. $timeout: any;
  25. fullscreen: boolean;
  26. inspector: any;
  27. editModeInitiated: boolean;
  28. editorHelpIndex: number;
  29. editMode: any;
  30. height: any;
  31. containerHeight: any;
  32. events: Emitter;
  33. timing: any;
  34. constructor($scope, $injector) {
  35. this.$injector = $injector;
  36. this.$scope = $scope;
  37. this.$timeout = $injector.get('$timeout');
  38. this.editorTabIndex = 0;
  39. this.events = new Emitter();
  40. this.timing = {};
  41. var plugin = config.panels[this.panel.type];
  42. if (plugin) {
  43. this.pluginId = plugin.id;
  44. this.pluginName = plugin.name;
  45. }
  46. $scope.$on("refresh", () => this.refresh());
  47. $scope.$on("render", () => this.render());
  48. $scope.$on("$destroy", () => {
  49. this.events.emit('panel-teardown');
  50. this.events.removeAllListeners();
  51. });
  52. // we should do something interesting
  53. // with newly added panels
  54. if (this.panel.isNew) {
  55. delete this.panel.isNew;
  56. }
  57. }
  58. init() {
  59. this.calculatePanelHeight();
  60. this.publishAppEvent('panel-initialized', {scope: this.$scope});
  61. this.events.emit('panel-initialized');
  62. }
  63. renderingCompleted() {
  64. profiler.renderingCompleted(this.panel.id, this.timing);
  65. }
  66. refresh() {
  67. this.events.emit('refresh', null);
  68. }
  69. publishAppEvent(evtName, evt) {
  70. this.$scope.$root.appEvent(evtName, evt);
  71. }
  72. changeView(fullscreen, edit) {
  73. this.publishAppEvent('panel-change-view', {
  74. fullscreen: fullscreen, edit: edit, panelId: this.panel.id
  75. });
  76. }
  77. viewPanel() {
  78. this.changeView(true, false);
  79. }
  80. editPanel() {
  81. this.changeView(true, true);
  82. }
  83. exitFullscreen() {
  84. this.changeView(false, false);
  85. }
  86. initEditMode() {
  87. this.editorTabs = [];
  88. this.addEditorTab('General', 'public/app/partials/panelgeneral.html');
  89. this.editModeInitiated = true;
  90. this.events.emit('init-edit-mode', null);
  91. var urlTab = (this.$injector.get('$routeParams').tab || '').toLowerCase();
  92. if (urlTab) {
  93. this.editorTabs.forEach((tab, i) => {
  94. if (tab.title.toLowerCase() === urlTab) {
  95. this.editorTabIndex = i;
  96. }
  97. });
  98. }
  99. }
  100. changeTab(newIndex) {
  101. this.editorTabIndex = newIndex;
  102. var route = this.$injector.get('$route');
  103. route.current.params.tab = this.editorTabs[newIndex].title.toLowerCase();
  104. route.updateParams();
  105. }
  106. addEditorTab(title, directiveFn, index?) {
  107. var editorTab = {title, directiveFn};
  108. if (_.isString(directiveFn)) {
  109. editorTab.directiveFn = function() {
  110. return {templateUrl: directiveFn};
  111. };
  112. }
  113. if (index) {
  114. this.editorTabs.splice(index, 0, editorTab);
  115. } else {
  116. this.editorTabs.push(editorTab);
  117. }
  118. }
  119. getMenu() {
  120. let menu = [];
  121. menu.push({text: 'View', click: 'ctrl.viewPanel(); dismiss();'});
  122. menu.push({text: 'Edit', click: 'ctrl.editPanel(); dismiss();', role: 'Editor'});
  123. if (!this.fullscreen) { // duplication is not supported in fullscreen mode
  124. menu.push({ text: 'Duplicate', click: 'ctrl.duplicate()', role: 'Editor' });
  125. }
  126. menu.push({text: 'Share', click: 'ctrl.sharePanel(); dismiss();'});
  127. return menu;
  128. }
  129. getExtendedMenu() {
  130. var actions = [{text: 'Panel JSON', click: 'ctrl.editPanelJson(); dismiss();'}];
  131. this.events.emit('init-panel-actions', actions);
  132. return actions;
  133. }
  134. otherPanelInFullscreenMode() {
  135. return this.dashboard.meta.fullscreen && !this.fullscreen;
  136. }
  137. calculatePanelHeight() {
  138. if (this.fullscreen) {
  139. var docHeight = $(window).height();
  140. var editHeight = Math.floor(docHeight * 0.4);
  141. var fullscreenHeight = Math.floor(docHeight * 0.8);
  142. this.containerHeight = this.editMode ? editHeight : fullscreenHeight;
  143. } else {
  144. this.containerHeight = this.panel.height || this.row.height;
  145. if (_.isString(this.containerHeight)) {
  146. this.containerHeight = parseInt(this.containerHeight.replace('px', ''), 10);
  147. }
  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. toggleEditorHelp(index) {
  161. if (this.editorHelpIndex === index) {
  162. this.editorHelpIndex = null;
  163. return;
  164. }
  165. this.editorHelpIndex = index;
  166. }
  167. duplicate() {
  168. this.dashboard.duplicatePanel(this.panel, this.row);
  169. this.$timeout(() => {
  170. this.$scope.$root.$broadcast('render');
  171. });
  172. }
  173. updateColumnSpan(span) {
  174. this.panel.span = Math.min(Math.max(Math.floor(this.panel.span + span), 1), 12);
  175. this.row.panelSpanChanged();
  176. this.$timeout(() => {
  177. this.render();
  178. });
  179. }
  180. removePanel() {
  181. this.row.removePanel(this.panel);
  182. }
  183. editPanelJson() {
  184. this.publishAppEvent('show-json-editor', {
  185. object: this.panel,
  186. updateHandler: this.replacePanel.bind(this)
  187. });
  188. }
  189. replacePanel(newPanel, oldPanel) {
  190. var row = this.row;
  191. var index = _.indexOf(this.row.panels, oldPanel);
  192. this.row.panels.splice(index, 1);
  193. // adding it back needs to be done in next digest
  194. this.$timeout(() => {
  195. newPanel.id = oldPanel.id;
  196. newPanel.span = oldPanel.span;
  197. this.row.panels.splice(index, 0, newPanel);
  198. });
  199. }
  200. sharePanel() {
  201. var shareScope = this.$scope.$new();
  202. shareScope.panel = this.panel;
  203. shareScope.dashboard = this.dashboard;
  204. this.publishAppEvent('show-modal', {
  205. src: 'public/app/features/dashboard/partials/shareModal.html',
  206. scope: shareScope
  207. });
  208. }
  209. getInfoMode() {
  210. if (this.error) {
  211. return 'error';
  212. }
  213. if (!!this.panel.description) {
  214. return 'info';
  215. }
  216. if (this.panel.links && this.panel.links.length) {
  217. return 'links';
  218. }
  219. return '';
  220. }
  221. getInfoContent(options) {
  222. var markdown = this.panel.description;
  223. if (options.mode === 'tooltip') {
  224. markdown = this.error || this.panel.description;
  225. }
  226. var linkSrv = this.$injector.get('linkSrv');
  227. var templateSrv = this.$injector.get('templateSrv');
  228. var interpolatedMarkdown = templateSrv.replace(markdown, this.panel.scopedVars);
  229. var html = '<div class="markdown-html">';
  230. html += new Remarkable().render(interpolatedMarkdown);
  231. if (this.panel.links && this.panel.links.length > 0) {
  232. html += '<ul>';
  233. for (let link of this.panel.links) {
  234. var info = linkSrv.getPanelLinkAnchorInfo(link, this.panel.scopedVars);
  235. html += '<li><a class="panel-menu-link" href="' + info.href + '" target="' + info.target + '">' + info.title + '</a></li>';
  236. }
  237. html += '</ul>';
  238. }
  239. return html + '</div>';
  240. }
  241. openInspector() {
  242. var modalScope = this.$scope.$new();
  243. modalScope.panel = this.panel;
  244. modalScope.dashboard = this.dashboard;
  245. modalScope.panelInfoHtml = this.getInfoContent({mode: 'inspector'});
  246. modalScope.inspector = $.extend(true, {}, this.inspector);
  247. this.publishAppEvent('show-modal', {
  248. src: 'public/app/features/dashboard/partials/inspector.html',
  249. scope: modalScope
  250. });
  251. }
  252. }