panel_ctrl.ts 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. $timeout: any;
  23. fullscreen: boolean;
  24. inspector: any;
  25. editModeInitiated: boolean;
  26. editMode: any;
  27. height: any;
  28. containerHeight: any;
  29. events: Emitter;
  30. timing: any;
  31. loading: boolean;
  32. constructor($scope, $injector) {
  33. this.$injector = $injector;
  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. let extendedMenu = this.getExtendedMenu();
  140. menu.push({
  141. text: 'More ...',
  142. click: '',
  143. icon: 'fa fa-fw fa-cube',
  144. submenu: extendedMenu,
  145. });
  146. if (this.dashboard.meta.canEdit) {
  147. menu.push({ divider: true, role: 'Editor' });
  148. menu.push({
  149. text: 'Remove',
  150. click: 'ctrl.removePanel();',
  151. role: 'Editor',
  152. icon: 'fa fa-fw fa-trash',
  153. shortcut: 'p r',
  154. });
  155. }
  156. return menu;
  157. }
  158. getExtendedMenu() {
  159. let menu = [];
  160. if (!this.fullscreen && this.dashboard.meta.canEdit) {
  161. menu.push({
  162. text: 'Duplicate',
  163. click: 'ctrl.duplicate()',
  164. role: 'Editor',
  165. });
  166. menu.push({
  167. text: 'Add to Panel List',
  168. click: 'ctrl.addToPanelList()',
  169. role: 'Editor',
  170. });
  171. }
  172. menu.push({
  173. text: 'Panel JSON',
  174. click: 'ctrl.editPanelJson(); dismiss();',
  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() {
  207. this.publishAppEvent('panel-remove', {
  208. panelId: this.panel.id,
  209. });
  210. }
  211. editPanelJson() {
  212. let editScope = this.$scope.$root.$new();
  213. editScope.object = this.panel.getSaveModel();
  214. editScope.updateHandler = this.replacePanel.bind(this);
  215. editScope.enableCopy = true;
  216. this.publishAppEvent('show-modal', {
  217. src: 'public/app/partials/edit_json.html',
  218. scope: editScope,
  219. });
  220. }
  221. addToPanelList() {
  222. store.set(LS_PANEL_COPY_KEY, JSON.stringify(this.panel.getSaveModel()));
  223. appEvents.emit('alert-success', ['Panel temporarily added to panel list']);
  224. }
  225. replacePanel(newPanel, oldPanel) {
  226. let dashboard = this.dashboard;
  227. let index = _.findIndex(dashboard.panels, panel => {
  228. return panel.id === oldPanel.id;
  229. });
  230. let deletedPanel = dashboard.panels.splice(index, 1);
  231. this.dashboard.events.emit('panel-removed', deletedPanel);
  232. newPanel = new PanelModel(newPanel);
  233. newPanel.id = oldPanel.id;
  234. dashboard.panels.splice(index, 0, newPanel);
  235. dashboard.sortPanelsByGridPos();
  236. dashboard.events.emit('panel-added', newPanel);
  237. }
  238. sharePanel() {
  239. var shareScope = this.$scope.$new();
  240. shareScope.panel = this.panel;
  241. shareScope.dashboard = this.dashboard;
  242. this.publishAppEvent('show-modal', {
  243. src: 'public/app/features/dashboard/partials/shareModal.html',
  244. scope: shareScope,
  245. });
  246. }
  247. getInfoMode() {
  248. if (this.error) {
  249. return 'error';
  250. }
  251. if (!!this.panel.description) {
  252. return 'info';
  253. }
  254. if (this.panel.links && this.panel.links.length) {
  255. return 'links';
  256. }
  257. return '';
  258. }
  259. getInfoContent(options) {
  260. var markdown = this.panel.description;
  261. if (options.mode === 'tooltip') {
  262. markdown = this.error || this.panel.description;
  263. }
  264. var linkSrv = this.$injector.get('linkSrv');
  265. var templateSrv = this.$injector.get('templateSrv');
  266. var interpolatedMarkdown = templateSrv.replace(markdown, this.panel.scopedVars);
  267. var html = '<div class="markdown-html">';
  268. html += new Remarkable().render(interpolatedMarkdown);
  269. if (this.panel.links && this.panel.links.length > 0) {
  270. html += '<ul>';
  271. for (let link of this.panel.links) {
  272. var info = linkSrv.getPanelLinkAnchorInfo(link, this.panel.scopedVars);
  273. html +=
  274. '<li><a class="panel-menu-link" href="' +
  275. info.href +
  276. '" target="' +
  277. info.target +
  278. '">' +
  279. info.title +
  280. '</a></li>';
  281. }
  282. html += '</ul>';
  283. }
  284. return html + '</div>';
  285. }
  286. openInspector() {
  287. var modalScope = this.$scope.$new();
  288. modalScope.panel = this.panel;
  289. modalScope.dashboard = this.dashboard;
  290. modalScope.panelInfoHtml = this.getInfoContent({ mode: 'inspector' });
  291. modalScope.inspector = $.extend(true, {}, this.inspector);
  292. this.publishAppEvent('show-modal', {
  293. src: 'public/app/features/dashboard/partials/inspector.html',
  294. scope: modalScope,
  295. });
  296. }
  297. }