panel_ctrl.ts 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. this.events.emit('init-panel-actions', menu);
  171. return menu;
  172. }
  173. otherPanelInFullscreenMode() {
  174. return this.dashboard.meta.fullscreen && !this.fullscreen;
  175. }
  176. calculatePanelHeight() {
  177. if (this.fullscreen) {
  178. var docHeight = $(window).height();
  179. var editHeight = Math.floor(docHeight * 0.4);
  180. var fullscreenHeight = Math.floor(docHeight * 0.8);
  181. this.containerHeight = this.editMode ? editHeight : fullscreenHeight;
  182. } else {
  183. this.containerHeight = this.panel.gridPos.h * GRID_CELL_HEIGHT + (this.panel.gridPos.h - 1) * GRID_CELL_VMARGIN;
  184. }
  185. if (this.panel.soloMode) {
  186. this.containerHeight = $(window).height();
  187. }
  188. this.height = this.containerHeight - (PANEL_BORDER + TITLE_HEIGHT);
  189. }
  190. render(payload?) {
  191. this.timing.renderStart = new Date().getTime();
  192. this.events.emit('render', payload);
  193. }
  194. duplicate() {
  195. this.dashboard.duplicatePanel(this.panel);
  196. this.$timeout(() => {
  197. this.$scope.$root.$broadcast('render');
  198. });
  199. }
  200. removePanel(ask: boolean) {
  201. // confirm deletion
  202. if (ask !== false) {
  203. var text2, confirmText;
  204. if (this.panel.alert) {
  205. text2 = 'Panel includes an alert rule, removing panel will also remove alert rule';
  206. confirmText = 'YES';
  207. }
  208. appEvents.emit('confirm-modal', {
  209. title: 'Remove Panel',
  210. text: 'Are you sure you want to remove this panel?',
  211. text2: text2,
  212. icon: 'fa-trash',
  213. confirmText: confirmText,
  214. yesText: 'Remove',
  215. onConfirm: () => {
  216. this.removePanel(false);
  217. },
  218. });
  219. return;
  220. }
  221. this.dashboard.removePanel(this.panel);
  222. }
  223. editPanelJson() {
  224. let editScope = this.$scope.$root.$new();
  225. editScope.object = this.panel.getSaveModel();
  226. editScope.updateHandler = this.replacePanel.bind(this);
  227. this.publishAppEvent('show-modal', {
  228. src: 'public/app/partials/edit_json.html',
  229. scope: editScope,
  230. });
  231. }
  232. replacePanel(newPanel, oldPanel) {
  233. let dashboard = this.dashboard;
  234. let index = _.findIndex(dashboard.panels, panel => {
  235. return panel.id === oldPanel.id;
  236. });
  237. let deletedPanel = dashboard.panels.splice(index, 1);
  238. this.dashboard.events.emit('panel-removed', deletedPanel);
  239. newPanel = new PanelModel(newPanel);
  240. newPanel.id = oldPanel.id;
  241. dashboard.panels.splice(index, 0, newPanel);
  242. dashboard.sortPanelsByGridPos();
  243. dashboard.events.emit('panel-added', newPanel);
  244. }
  245. sharePanel() {
  246. var shareScope = this.$scope.$new();
  247. shareScope.panel = this.panel;
  248. shareScope.dashboard = this.dashboard;
  249. this.publishAppEvent('show-modal', {
  250. src: 'public/app/features/dashboard/partials/shareModal.html',
  251. scope: shareScope,
  252. });
  253. }
  254. getInfoMode() {
  255. if (this.error) {
  256. return 'error';
  257. }
  258. if (!!this.panel.description) {
  259. return 'info';
  260. }
  261. if (this.panel.links && this.panel.links.length) {
  262. return 'links';
  263. }
  264. return '';
  265. }
  266. getInfoContent(options) {
  267. var markdown = this.panel.description;
  268. if (options.mode === 'tooltip') {
  269. markdown = this.error || this.panel.description;
  270. }
  271. var linkSrv = this.$injector.get('linkSrv');
  272. var templateSrv = this.$injector.get('templateSrv');
  273. var interpolatedMarkdown = templateSrv.replace(markdown, this.panel.scopedVars);
  274. var html = '<div class="markdown-html">';
  275. html += new Remarkable().render(interpolatedMarkdown);
  276. if (this.panel.links && this.panel.links.length > 0) {
  277. html += '<ul>';
  278. for (let link of this.panel.links) {
  279. var info = linkSrv.getPanelLinkAnchorInfo(link, this.panel.scopedVars);
  280. html +=
  281. '<li><a class="panel-menu-link" href="' +
  282. info.href +
  283. '" target="' +
  284. info.target +
  285. '">' +
  286. info.title +
  287. '</a></li>';
  288. }
  289. html += '</ul>';
  290. }
  291. return html + '</div>';
  292. }
  293. openInspector() {
  294. var modalScope = this.$scope.$new();
  295. modalScope.panel = this.panel;
  296. modalScope.dashboard = this.dashboard;
  297. modalScope.panelInfoHtml = this.getInfoContent({ mode: 'inspector' });
  298. modalScope.inspector = $.extend(true, {}, this.inspector);
  299. this.publishAppEvent('show-modal', {
  300. src: 'public/app/features/dashboard/partials/inspector.html',
  301. scope: modalScope,
  302. });
  303. }
  304. }