panel_ctrl.ts 9.1 KB

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