panel_ctrl.ts 8.0 KB

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