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