panel_ctrl.ts 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. import config from 'app/core/config';
  2. import _ from 'lodash';
  3. import $ from 'jquery';
  4. import { appEvents, profiler } from 'app/core/core';
  5. import { PanelModel } from 'app/features/dashboard/panel_model';
  6. import Remarkable from 'remarkable';
  7. import { GRID_CELL_HEIGHT, GRID_CELL_VMARGIN, LS_PANEL_COPY_KEY } from 'app/core/constants';
  8. import store from 'app/core/store';
  9. const TITLE_HEIGHT = 27;
  10. const PANEL_BORDER = 2;
  11. import { Emitter } from 'app/core/core';
  12. export class PanelCtrl {
  13. panel: any;
  14. error: any;
  15. dashboard: any;
  16. editorTabIndex: number;
  17. pluginName: string;
  18. pluginId: string;
  19. editorTabs: any;
  20. $scope: any;
  21. $injector: any;
  22. $location: any;
  23. $timeout: any;
  24. fullscreen: boolean;
  25. inspector: any;
  26. editModeInitiated: boolean;
  27. editMode: any;
  28. height: any;
  29. containerHeight: any;
  30. events: Emitter;
  31. timing: any;
  32. loading: boolean;
  33. constructor($scope, $injector) {
  34. this.$injector = $injector;
  35. this.$location = $injector.get('$location');
  36. this.$scope = $scope;
  37. this.$timeout = $injector.get('$timeout');
  38. this.editorTabIndex = 0;
  39. this.events = this.panel.events;
  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('component-did-mount', () => this.panelDidMount());
  48. $scope.$on('$destroy', () => {
  49. this.events.emit('panel-teardown');
  50. this.events.removeAllListeners();
  51. });
  52. }
  53. init() {
  54. this.events.emit('panel-initialized');
  55. this.publishAppEvent('panel-initialized', { scope: this.$scope });
  56. }
  57. panelDidMount() {
  58. this.events.emit('component-did-mount');
  59. }
  60. renderingCompleted() {
  61. profiler.renderingCompleted(this.panel.id, this.timing);
  62. }
  63. refresh() {
  64. this.events.emit('refresh', null);
  65. }
  66. publishAppEvent(evtName, evt) {
  67. this.$scope.$root.appEvent(evtName, evt);
  68. }
  69. changeView(fullscreen, edit) {
  70. this.publishAppEvent('panel-change-view', {
  71. fullscreen: fullscreen,
  72. edit: edit,
  73. 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. const menu = [];
  120. menu.push({
  121. text: 'View',
  122. click: 'ctrl.viewPanel();',
  123. icon: 'fa fa-fw fa-eye',
  124. shortcut: 'v',
  125. });
  126. if (this.dashboard.meta.canEdit) {
  127. menu.push({
  128. text: 'Edit',
  129. click: 'ctrl.editPanel();',
  130. role: 'Editor',
  131. icon: 'fa fa-fw fa-edit',
  132. shortcut: 'e',
  133. });
  134. }
  135. menu.push({
  136. text: 'Share',
  137. click: 'ctrl.sharePanel();',
  138. icon: 'fa fa-fw fa-share',
  139. shortcut: 'p s',
  140. });
  141. // Additional items from sub-class
  142. menu.push(...this.getAdditionalMenuItems());
  143. const extendedMenu = this.getExtendedMenu();
  144. menu.push({
  145. text: 'More ...',
  146. click: '',
  147. icon: 'fa fa-fw fa-cube',
  148. submenu: extendedMenu,
  149. });
  150. if (this.dashboard.meta.canEdit) {
  151. menu.push({ divider: true, role: 'Editor' });
  152. menu.push({
  153. text: 'Remove',
  154. click: 'ctrl.removePanel();',
  155. role: 'Editor',
  156. icon: 'fa fa-fw fa-trash',
  157. shortcut: 'p r',
  158. });
  159. }
  160. return menu;
  161. }
  162. getExtendedMenu() {
  163. const menu = [];
  164. if (!this.fullscreen && this.dashboard.meta.canEdit) {
  165. menu.push({
  166. text: 'Duplicate',
  167. click: 'ctrl.duplicate()',
  168. role: 'Editor',
  169. shortcut: 'p d',
  170. });
  171. menu.push({
  172. text: 'Copy',
  173. click: 'ctrl.copyPanel()',
  174. role: 'Editor',
  175. });
  176. }
  177. menu.push({
  178. text: 'Panel JSON',
  179. click: 'ctrl.editPanelJson(); dismiss();',
  180. });
  181. this.events.emit('init-panel-actions', menu);
  182. return menu;
  183. }
  184. // Override in sub-class to add items before extended menu
  185. getAdditionalMenuItems() {
  186. return [];
  187. }
  188. otherPanelInFullscreenMode() {
  189. return this.dashboard.meta.fullscreen && !this.fullscreen;
  190. }
  191. calculatePanelHeight() {
  192. if (this.fullscreen) {
  193. var docHeight = $(window).height();
  194. var editHeight = Math.floor(docHeight * 0.4);
  195. var fullscreenHeight = Math.floor(docHeight * 0.8);
  196. this.containerHeight = this.editMode ? editHeight : fullscreenHeight;
  197. } else {
  198. this.containerHeight = this.panel.gridPos.h * GRID_CELL_HEIGHT + (this.panel.gridPos.h - 1) * GRID_CELL_VMARGIN;
  199. }
  200. if (this.panel.soloMode) {
  201. this.containerHeight = $(window).height();
  202. }
  203. this.height = this.containerHeight - (PANEL_BORDER + TITLE_HEIGHT);
  204. }
  205. render(payload?) {
  206. this.timing.renderStart = new Date().getTime();
  207. this.events.emit('render', payload);
  208. }
  209. duplicate() {
  210. this.dashboard.duplicatePanel(this.panel);
  211. this.$timeout(() => {
  212. this.$scope.$root.$broadcast('render');
  213. });
  214. }
  215. removePanel() {
  216. this.publishAppEvent('panel-remove', {
  217. panelId: this.panel.id,
  218. });
  219. }
  220. editPanelJson() {
  221. const editScope = this.$scope.$root.$new();
  222. editScope.object = this.panel.getSaveModel();
  223. editScope.updateHandler = this.replacePanel.bind(this);
  224. editScope.enableCopy = true;
  225. this.publishAppEvent('show-modal', {
  226. src: 'public/app/partials/edit_json.html',
  227. scope: editScope,
  228. });
  229. }
  230. copyPanel() {
  231. store.set(LS_PANEL_COPY_KEY, JSON.stringify(this.panel.getSaveModel()));
  232. appEvents.emit('alert-success', ['Panel copied. Open Add Panel to paste']);
  233. }
  234. replacePanel(newPanel, oldPanel) {
  235. const dashboard = this.dashboard;
  236. const index = _.findIndex(dashboard.panels, panel => {
  237. return panel.id === oldPanel.id;
  238. });
  239. const deletedPanel = dashboard.panels.splice(index, 1);
  240. this.dashboard.events.emit('panel-removed', deletedPanel);
  241. newPanel = new PanelModel(newPanel);
  242. newPanel.id = oldPanel.id;
  243. dashboard.panels.splice(index, 0, newPanel);
  244. dashboard.sortPanelsByGridPos();
  245. dashboard.events.emit('panel-added', newPanel);
  246. }
  247. sharePanel() {
  248. var shareScope = this.$scope.$new();
  249. shareScope.panel = this.panel;
  250. shareScope.dashboard = this.dashboard;
  251. this.publishAppEvent('show-modal', {
  252. src: 'public/app/features/dashboard/partials/shareModal.html',
  253. scope: shareScope,
  254. });
  255. }
  256. getInfoMode() {
  257. if (this.error) {
  258. return 'error';
  259. }
  260. if (!!this.panel.description) {
  261. return 'info';
  262. }
  263. if (this.panel.links && this.panel.links.length) {
  264. return 'links';
  265. }
  266. return '';
  267. }
  268. getInfoContent(options) {
  269. var markdown = this.panel.description;
  270. if (options.mode === 'tooltip') {
  271. markdown = this.error || this.panel.description;
  272. }
  273. var linkSrv = this.$injector.get('linkSrv');
  274. var sanitize = this.$injector.get('$sanitize');
  275. var templateSrv = this.$injector.get('templateSrv');
  276. var interpolatedMarkdown = templateSrv.replace(markdown, this.panel.scopedVars);
  277. var html = '<div class="markdown-html">';
  278. html += new Remarkable().render(interpolatedMarkdown);
  279. if (this.panel.links && this.panel.links.length > 0) {
  280. html += '<ul>';
  281. for (const link of this.panel.links) {
  282. var info = linkSrv.getPanelLinkAnchorInfo(link, this.panel.scopedVars);
  283. html +=
  284. '<li><a class="panel-menu-link" href="' +
  285. info.href +
  286. '" target="' +
  287. info.target +
  288. '">' +
  289. info.title +
  290. '</a></li>';
  291. }
  292. html += '</ul>';
  293. }
  294. html += '</div>';
  295. return sanitize(html);
  296. }
  297. openInspector() {
  298. var modalScope = this.$scope.$new();
  299. modalScope.panel = this.panel;
  300. modalScope.dashboard = this.dashboard;
  301. modalScope.panelInfoHtml = this.getInfoContent({ mode: 'inspector' });
  302. modalScope.inspector = $.extend(true, {}, this.inspector);
  303. this.publishAppEvent('show-modal', {
  304. src: 'public/app/features/dashboard/partials/inspector.html',
  305. scope: modalScope,
  306. });
  307. }
  308. }