panel.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import appEvents from 'app/core/app_events';
  2. import { DashboardModel } from 'app/features/dashboard/dashboard_model';
  3. import { PanelModel } from 'app/features/dashboard/panel_model';
  4. import store from 'app/core/store';
  5. import { LS_PANEL_COPY_KEY } from 'app/core/constants';
  6. export const removePanel = (dashboard: DashboardModel, panel: PanelModel, ask: boolean) => {
  7. // confirm deletion
  8. if (ask !== false) {
  9. const text2 = panel.alert ? 'Panel includes an alert rule, removing panel will also remove alert rule' : null;
  10. const confirmText = panel.alert ? 'YES' : null;
  11. appEvents.emit('confirm-modal', {
  12. title: 'Remove Panel',
  13. text: 'Are you sure you want to remove this panel?',
  14. text2: text2,
  15. icon: 'fa-trash',
  16. confirmText: confirmText,
  17. yesText: 'Remove',
  18. onConfirm: () => removePanel(dashboard, panel, false),
  19. });
  20. return;
  21. }
  22. dashboard.removePanel(panel);
  23. };
  24. export const duplicatePanel = (dashboard: DashboardModel, panel: PanelModel) => {
  25. dashboard.duplicatePanel(panel);
  26. };
  27. export const copyPanel = (panel: PanelModel) => {
  28. store.set(LS_PANEL_COPY_KEY, JSON.stringify(panel.getSaveModel()));
  29. appEvents.emit('alert-success', ['Panel copied. Open Add Panel to paste']);
  30. };
  31. const replacePanel = (dashboard: DashboardModel, newPanel: PanelModel, oldPanel: PanelModel) => {
  32. const index = dashboard.panels.findIndex(panel => {
  33. return panel.id === oldPanel.id;
  34. });
  35. const deletedPanel = dashboard.panels.splice(index, 1);
  36. dashboard.events.emit('panel-removed', deletedPanel);
  37. newPanel = new PanelModel(newPanel);
  38. newPanel.id = oldPanel.id;
  39. dashboard.panels.splice(index, 0, newPanel);
  40. dashboard.sortPanelsByGridPos();
  41. dashboard.events.emit('panel-added', newPanel);
  42. };
  43. export const editPanelJson = (dashboard: DashboardModel, panel: PanelModel) => {
  44. const model = {
  45. object: panel.getSaveModel(),
  46. updateHandler: (newPanel: PanelModel, oldPanel: PanelModel) => {
  47. replacePanel(dashboard, newPanel, oldPanel);
  48. },
  49. enableCopy: true,
  50. };
  51. appEvents.emit('show-modal', {
  52. src: 'public/app/partials/edit_json.html',
  53. model: model,
  54. });
  55. };
  56. export const sharePanel = (dashboard: DashboardModel, panel: PanelModel) => {
  57. appEvents.emit('show-modal', {
  58. src: 'public/app/features/dashboard/partials/shareModal.html',
  59. model: {
  60. dashboard: dashboard,
  61. panel: panel,
  62. },
  63. });
  64. };
  65. export const refreshPanel = (panel: PanelModel) => {
  66. panel.refresh();
  67. };
  68. export const toggleLegend = (panel: PanelModel) => {
  69. console.log('Toggle legend is not implemented yet');
  70. // We need to set panel.legend defaults first
  71. // panel.legend.show = !panel.legend.show;
  72. refreshPanel(panel);
  73. };