panel_ctrl.ts 8.1 KB

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