panel_ctrl.ts 8.8 KB

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