panel_menu.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import { PanelHeaderMenuItemTypes, PanelHeaderMenuItemProps } from 'app/types/panel';
  2. import { store } from 'app/store/configureStore';
  3. import { updateLocation } from 'app/core/actions';
  4. import { PanelModel } from 'app/features/dashboard/panel_model';
  5. import { DashboardModel } from 'app/features/dashboard/dashboard_model';
  6. import { removePanel, duplicatePanel, copyPanel, editPanelJson, sharePanel } from 'app/features/dashboard/utils/panel';
  7. export const getPanelMenu = (
  8. dashboard: DashboardModel,
  9. panel: PanelModel,
  10. additionalMenuItems: PanelHeaderMenuItemProps[] = [],
  11. additionalSubMenuItems: PanelHeaderMenuItemProps[] = []
  12. ) => {
  13. const onViewPanel = () => {
  14. store.dispatch(
  15. updateLocation({
  16. query: {
  17. panelId: panel.id,
  18. edit: false,
  19. fullscreen: true,
  20. },
  21. })
  22. );
  23. };
  24. const onEditPanel = () => {
  25. store.dispatch(
  26. updateLocation({
  27. query: {
  28. panelId: panel.id,
  29. edit: true,
  30. fullscreen: true,
  31. },
  32. })
  33. );
  34. };
  35. const onSharePanel = () => {
  36. sharePanel(dashboard, panel);
  37. };
  38. const onDuplicatePanel = () => {
  39. duplicatePanel(dashboard, panel);
  40. };
  41. const onCopyPanel = () => {
  42. copyPanel(panel);
  43. };
  44. const onEditPanelJson = () => {
  45. editPanelJson(dashboard, panel);
  46. };
  47. const onRemovePanel = () => {
  48. removePanel(dashboard, panel, true);
  49. };
  50. const getSubMenu = () => {
  51. const menu: PanelHeaderMenuItemProps[] = [];
  52. if (!panel.fullscreen && dashboard.meta.canEdit) {
  53. menu.push({
  54. type: PanelHeaderMenuItemTypes.Link,
  55. text: 'Duplicate',
  56. handleClick: onDuplicatePanel,
  57. shortcut: 'p d',
  58. role: 'Editor',
  59. });
  60. menu.push({
  61. type: PanelHeaderMenuItemTypes.Link,
  62. text: 'Copy',
  63. handleClick: onCopyPanel,
  64. role: 'Editor',
  65. });
  66. }
  67. menu.push({
  68. type: PanelHeaderMenuItemTypes.Link,
  69. text: 'Panel JSON',
  70. handleClick: onEditPanelJson,
  71. });
  72. if (additionalSubMenuItems) {
  73. additionalSubMenuItems.forEach(item => {
  74. menu.push(item);
  75. });
  76. }
  77. return menu;
  78. };
  79. const menu: PanelHeaderMenuItemProps[] = [];
  80. menu.push({
  81. type: PanelHeaderMenuItemTypes.Link,
  82. text: 'View',
  83. iconClassName: 'fa fa-fw fa-eye',
  84. handleClick: onViewPanel,
  85. shortcut: 'v',
  86. });
  87. if (dashboard.meta.canEdit) {
  88. menu.push({
  89. type: PanelHeaderMenuItemTypes.Link,
  90. text: 'Edit',
  91. iconClassName: 'fa fa-fw fa-edit',
  92. handleClick: onEditPanel,
  93. shortcut: 'e',
  94. role: 'Editor',
  95. });
  96. }
  97. menu.push({
  98. type: PanelHeaderMenuItemTypes.Link,
  99. text: 'Share',
  100. iconClassName: 'fa fa-fw fa-share',
  101. handleClick: onSharePanel,
  102. shortcut: 'p s',
  103. });
  104. if (additionalMenuItems) {
  105. additionalMenuItems.forEach(item => {
  106. menu.push(item);
  107. });
  108. }
  109. const subMenu: PanelHeaderMenuItemProps[] = getSubMenu();
  110. menu.push({
  111. type: PanelHeaderMenuItemTypes.SubMenu,
  112. text: 'More...',
  113. iconClassName: 'fa fa-fw fa-cube',
  114. handleClick: null,
  115. subMenu: subMenu,
  116. });
  117. if (dashboard.meta.canEdit) {
  118. menu.push({
  119. type: PanelHeaderMenuItemTypes.Divider,
  120. role: 'Editor',
  121. });
  122. menu.push({
  123. type: PanelHeaderMenuItemTypes.Link,
  124. text: 'Remove',
  125. iconClassName: 'fa fa-fw fa-trash',
  126. handleClick: onRemovePanel,
  127. shortcut: 'p r',
  128. role: 'Editor',
  129. });
  130. }
  131. // Additional items from sub-class
  132. // menu.push(...this.getAdditionalMenuItems());
  133. return menu;
  134. };