panel_ctrl.ts 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. inspector: any;
  25. editModeInitiated: boolean;
  26. height: any;
  27. containerHeight: any;
  28. events: Emitter;
  29. timing: any;
  30. loading: boolean;
  31. constructor($scope, $injector) {
  32. this.$injector = $injector;
  33. this.$location = $injector.get('$location');
  34. this.$scope = $scope;
  35. this.$timeout = $injector.get('$timeout');
  36. this.editorTabIndex = 0;
  37. this.events = this.panel.events;
  38. this.timing = {};
  39. var plugin = config.panels[this.panel.type];
  40. if (plugin) {
  41. this.pluginId = plugin.id;
  42. this.pluginName = plugin.name;
  43. }
  44. $scope.$on('refresh', () => this.refresh());
  45. $scope.$on('component-did-mount', () => this.panelDidMount());
  46. $scope.$on('$destroy', () => {
  47. this.events.emit('panel-teardown');
  48. this.events.removeAllListeners();
  49. });
  50. }
  51. init() {
  52. this.events.emit('panel-initialized');
  53. this.publishAppEvent('panel-initialized', { scope: this.$scope });
  54. }
  55. panelDidMount() {
  56. this.events.emit('component-did-mount');
  57. }
  58. renderingCompleted() {
  59. profiler.renderingCompleted(this.panel.id, this.timing);
  60. }
  61. refresh() {
  62. this.events.emit('refresh', null);
  63. }
  64. publishAppEvent(evtName, evt) {
  65. this.$scope.$root.appEvent(evtName, evt);
  66. }
  67. changeView(fullscreen, edit) {
  68. this.publishAppEvent('panel-change-view', {
  69. fullscreen: fullscreen,
  70. edit: edit,
  71. panelId: this.panel.id,
  72. });
  73. }
  74. viewPanel() {
  75. this.changeView(true, false);
  76. }
  77. editPanel() {
  78. this.changeView(true, true);
  79. }
  80. exitFullscreen() {
  81. this.changeView(false, false);
  82. }
  83. initEditMode() {
  84. this.editorTabs = [];
  85. this.addEditorTab('General', 'public/app/partials/panelgeneral.html');
  86. this.editModeInitiated = true;
  87. this.events.emit('init-edit-mode', null);
  88. var urlTab = (this.$injector.get('$routeParams').tab || '').toLowerCase();
  89. if (urlTab) {
  90. this.editorTabs.forEach((tab, i) => {
  91. if (tab.title.toLowerCase() === urlTab) {
  92. this.editorTabIndex = i;
  93. }
  94. });
  95. }
  96. }
  97. changeTab(newIndex) {
  98. this.editorTabIndex = newIndex;
  99. var route = this.$injector.get('$route');
  100. route.current.params.tab = this.editorTabs[newIndex].title.toLowerCase();
  101. route.updateParams();
  102. }
  103. addEditorTab(title, directiveFn, index?) {
  104. var editorTab = { title, directiveFn };
  105. if (_.isString(directiveFn)) {
  106. editorTab.directiveFn = function() {
  107. return { templateUrl: directiveFn };
  108. };
  109. }
  110. if (index) {
  111. this.editorTabs.splice(index, 0, editorTab);
  112. } else {
  113. this.editorTabs.push(editorTab);
  114. }
  115. }
  116. getMenu() {
  117. let menu = [];
  118. menu.push({
  119. text: 'View',
  120. click: 'ctrl.viewPanel();',
  121. icon: 'fa fa-fw fa-eye',
  122. shortcut: 'v',
  123. });
  124. if (this.dashboard.meta.canEdit) {
  125. menu.push({
  126. text: 'Edit',
  127. click: 'ctrl.editPanel();',
  128. role: 'Editor',
  129. icon: 'fa fa-fw fa-edit',
  130. shortcut: 'e',
  131. });
  132. }
  133. menu.push({
  134. text: 'Share',
  135. click: 'ctrl.sharePanel();',
  136. icon: 'fa fa-fw fa-share',
  137. shortcut: 'p s',
  138. });
  139. // Additional items from sub-class
  140. menu.push(...this.getAdditionalMenuItems());
  141. let extendedMenu = this.getExtendedMenu();
  142. menu.push({
  143. text: 'More ...',
  144. click: '',
  145. icon: 'fa fa-fw fa-cube',
  146. submenu: extendedMenu,
  147. });
  148. if (this.dashboard.meta.canEdit) {
  149. menu.push({ divider: true, role: 'Editor' });
  150. menu.push({
  151. text: 'Remove',
  152. click: 'ctrl.removePanel();',
  153. role: 'Editor',
  154. icon: 'fa fa-fw fa-trash',
  155. shortcut: 'p r',
  156. });
  157. }
  158. return menu;
  159. }
  160. getExtendedMenu() {
  161. let menu = [];
  162. if (!this.panel.fullscreen && this.dashboard.meta.canEdit) {
  163. menu.push({
  164. text: 'Duplicate',
  165. click: 'ctrl.duplicate()',
  166. role: 'Editor',
  167. shortcut: 'p d',
  168. });
  169. menu.push({
  170. text: 'Copy',
  171. click: 'ctrl.copyPanel()',
  172. role: 'Editor',
  173. });
  174. }
  175. menu.push({
  176. text: 'Panel JSON',
  177. click: 'ctrl.editPanelJson(); dismiss();',
  178. });
  179. this.events.emit('init-panel-actions', menu);
  180. return menu;
  181. }
  182. // Override in sub-class to add items before extended menu
  183. getAdditionalMenuItems() {
  184. return [];
  185. }
  186. otherPanelInFullscreenMode() {
  187. return this.dashboard.meta.fullscreen && !this.panel.fullscreen;
  188. }
  189. calculatePanelHeight() {
  190. if (this.panel.fullscreen) {
  191. var docHeight = $(window).height();
  192. var editHeight = Math.floor(docHeight * 0.4);
  193. var fullscreenHeight = Math.floor(docHeight * 0.8);
  194. this.containerHeight = this.panel.isEditing ? editHeight : fullscreenHeight;
  195. } else {
  196. this.containerHeight = this.panel.gridPos.h * GRID_CELL_HEIGHT + (this.panel.gridPos.h - 1) * GRID_CELL_VMARGIN;
  197. }
  198. if (this.panel.soloMode) {
  199. this.containerHeight = $(window).height();
  200. }
  201. // hacky solution
  202. if (this.panel.isEditing && !this.editModeInitiated) {
  203. this.initEditMode();
  204. }
  205. this.height = this.containerHeight - (PANEL_BORDER + TITLE_HEIGHT);
  206. }
  207. render(payload?) {
  208. this.timing.renderStart = new Date().getTime();
  209. this.events.emit('render', payload);
  210. }
  211. duplicate() {
  212. this.dashboard.duplicatePanel(this.panel);
  213. }
  214. removePanel() {
  215. this.publishAppEvent('panel-remove', {
  216. panelId: this.panel.id,
  217. });
  218. }
  219. editPanelJson() {
  220. let editScope = this.$scope.$root.$new();
  221. editScope.object = this.panel.getSaveModel();
  222. editScope.updateHandler = this.replacePanel.bind(this);
  223. editScope.enableCopy = true;
  224. this.publishAppEvent('show-modal', {
  225. src: 'public/app/partials/edit_json.html',
  226. scope: editScope,
  227. });
  228. }
  229. copyPanel() {
  230. store.set(LS_PANEL_COPY_KEY, JSON.stringify(this.panel.getSaveModel()));
  231. appEvents.emit('alert-success', ['Panel copied. Open Add Panel to paste']);
  232. }
  233. replacePanel(newPanel, oldPanel) {
  234. let dashboard = this.dashboard;
  235. let index = _.findIndex(dashboard.panels, panel => {
  236. return panel.id === oldPanel.id;
  237. });
  238. let deletedPanel = dashboard.panels.splice(index, 1);
  239. this.dashboard.events.emit('panel-removed', deletedPanel);
  240. newPanel = new PanelModel(newPanel);
  241. newPanel.id = oldPanel.id;
  242. dashboard.panels.splice(index, 0, newPanel);
  243. dashboard.sortPanelsByGridPos();
  244. dashboard.events.emit('panel-added', newPanel);
  245. }
  246. sharePanel() {
  247. var shareScope = this.$scope.$new();
  248. shareScope.panel = this.panel;
  249. shareScope.dashboard = this.dashboard;
  250. this.publishAppEvent('show-modal', {
  251. src: 'public/app/features/dashboard/partials/shareModal.html',
  252. scope: shareScope,
  253. });
  254. }
  255. getInfoMode() {
  256. if (this.error) {
  257. return 'error';
  258. }
  259. if (!!this.panel.description) {
  260. return 'info';
  261. }
  262. if (this.panel.links && this.panel.links.length) {
  263. return 'links';
  264. }
  265. return '';
  266. }
  267. getInfoContent(options) {
  268. var markdown = this.panel.description;
  269. if (options.mode === 'tooltip') {
  270. markdown = this.error || this.panel.description;
  271. }
  272. var linkSrv = this.$injector.get('linkSrv');
  273. var sanitize = this.$injector.get('$sanitize');
  274. var templateSrv = this.$injector.get('templateSrv');
  275. var interpolatedMarkdown = templateSrv.replace(markdown, this.panel.scopedVars);
  276. var html = '<div class="markdown-html">';
  277. html += new Remarkable().render(interpolatedMarkdown);
  278. if (this.panel.links && this.panel.links.length > 0) {
  279. html += '<ul>';
  280. for (let link of this.panel.links) {
  281. var info = linkSrv.getPanelLinkAnchorInfo(link, this.panel.scopedVars);
  282. html +=
  283. '<li><a class="panel-menu-link" href="' +
  284. info.href +
  285. '" target="' +
  286. info.target +
  287. '">' +
  288. info.title +
  289. '</a></li>';
  290. }
  291. html += '</ul>';
  292. }
  293. html += '</div>';
  294. return sanitize(html);
  295. }
  296. openInspector() {
  297. var modalScope = this.$scope.$new();
  298. modalScope.panel = this.panel;
  299. modalScope.dashboard = this.dashboard;
  300. modalScope.panelInfoHtml = this.getInfoContent({ mode: 'inspector' });
  301. modalScope.inspector = $.extend(true, {}, this.inspector);
  302. this.publishAppEvent('show-modal', {
  303. src: 'public/app/features/dashboard/partials/inspector.html',
  304. scope: modalScope,
  305. });
  306. }
  307. }