panel_ctrl.ts 7.5 KB

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