1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { ContextMenuAction } from './context-menu-action-set';
6 import { ContextMenuActionNames } from 'views-components/context-menu/context-menu-action-set';
7 import { sortByProperty } from 'common/array-utils';
8 import { horizontalMenuDivider, verticalMenuDivider } from './actions/context-menu-divider';
9 import { MultiSelectMenuAction } from 'views-components/multiselect-toolbar/ms-menu-actions';
11 export enum ContextMenuKind {
12 API_CLIENT_AUTHORIZATION = "ApiClientAuthorization",
13 ROOT_PROJECT = "RootProject",
14 ROOT_PROJECT_ADMIN = "RootProjectAdmin",
16 FILTER_GROUP = "FilterGroup",
17 READONLY_PROJECT = "ReadOnlyProject",
18 FROZEN_PROJECT = "FrozenProject",
19 FROZEN_PROJECT_ADMIN = "FrozenProjectAdmin",
20 PROJECT_ADMIN = "ProjectAdmin",
21 FILTER_GROUP_ADMIN = "FilterGroupAdmin",
22 RESOURCE = "Resource",
23 FAVORITE = "Favorite",
25 COLLECTION_FILES = "CollectionFiles",
26 COLLECTION_FILES_MULTIPLE = "CollectionFilesMultiple",
27 READONLY_COLLECTION_FILES = "ReadOnlyCollectionFiles",
28 READONLY_COLLECTION_FILES_MULTIPLE = "ReadOnlyCollectionFilesMultiple",
29 COLLECTION_FILES_NOT_SELECTED = "CollectionFilesNotSelected",
30 COLLECTION_FILE_ITEM = "CollectionFileItem",
31 COLLECTION_DIRECTORY_ITEM = "CollectionDirectoryItem",
32 READONLY_COLLECTION_FILE_ITEM = "ReadOnlyCollectionFileItem",
33 READONLY_COLLECTION_DIRECTORY_ITEM = "ReadOnlyCollectionDirectoryItem",
34 COLLECTION = "Collection",
35 COLLECTION_ADMIN = "CollectionAdmin",
36 READONLY_COLLECTION = "ReadOnlyCollection",
37 OLD_VERSION_COLLECTION = "OldVersionCollection",
38 TRASHED_COLLECTION = "TrashedCollection",
40 RUNNING_PROCESS_ADMIN = "RunningProcessAdmin",
41 PROCESS_ADMIN = "ProcessAdmin",
42 RUNNING_PROCESS_RESOURCE = "RunningProcessResource",
43 PROCESS_RESOURCE = "ProcessResource",
44 READONLY_PROCESS_RESOURCE = "ReadOnlyProcessResource",
45 PROCESS_LOGS = "ProcessLogs",
46 REPOSITORY = "Repository",
48 VIRTUAL_MACHINE = "VirtualMachine",
49 KEEP_SERVICE = "KeepService",
51 USER_DETAILS = "UserDetails",
53 GROUP_MEMBER = "GroupMember",
54 PERMISSION_EDIT = "PermissionEdit",
56 WORKFLOW = "Workflow",
57 READONLY_WORKFLOW = "ReadOnlyWorkflow",
58 SEARCH_RESULTS = "SearchResults",
62 const processOrder = [
63 ContextMenuActionNames.VIEW_DETAILS,
64 ContextMenuActionNames.OPEN_IN_NEW_TAB,
65 ContextMenuActionNames.COPY_UUID,
66 ContextMenuActionNames.DIVIDER,
67 ContextMenuActionNames.COPY_AND_RERUN_PROCESS,
68 ContextMenuActionNames.CANCEL,
69 ContextMenuActionNames.EDIT_PROCESS,
70 ContextMenuActionNames.REMOVE,
71 ContextMenuActionNames.DIVIDER,
72 ContextMenuActionNames.OUTPUTS,
73 ContextMenuActionNames.ADD_TO_FAVORITES,
74 ContextMenuActionNames.ADD_TO_PUBLIC_FAVORITES,
75 ContextMenuActionNames.DIVIDER,
76 ContextMenuActionNames.API_DETAILS,
79 const projectOrder = [
80 ContextMenuActionNames.VIEW_DETAILS,
81 ContextMenuActionNames.OPEN_IN_NEW_TAB,
82 ContextMenuActionNames.COPY_UUID,
83 ContextMenuActionNames.DIVIDER,
84 ContextMenuActionNames.SHARE,
85 ContextMenuActionNames.EDIT_PROJECT,
86 ContextMenuActionNames.MOVE_TO_TRASH,
87 ContextMenuActionNames.DIVIDER,
88 ContextMenuActionNames.NEW_PROJECT,
89 ContextMenuActionNames.MOVE_TO,
90 ContextMenuActionNames.FREEZE_PROJECT,
91 ContextMenuActionNames.ADD_TO_FAVORITES,
92 ContextMenuActionNames.ADD_TO_PUBLIC_FAVORITES,
93 ContextMenuActionNames.DIVIDER,
94 ContextMenuActionNames.COPY_LINK_TO_CLIPBOARD,
95 ContextMenuActionNames.OPEN_WITH_3RD_PARTY_CLIENT,
96 ContextMenuActionNames.API_DETAILS,
99 const collectionOrder = [
100 ContextMenuActionNames.VIEW_DETAILS,
101 ContextMenuActionNames.OPEN_IN_NEW_TAB,
102 ContextMenuActionNames.COPY_UUID,
103 ContextMenuActionNames.DIVIDER,
104 ContextMenuActionNames.SHARE,
105 ContextMenuActionNames.EDIT_COLLECTION,
106 ContextMenuActionNames.MOVE_TO_TRASH,
107 ContextMenuActionNames.DIVIDER,
108 ContextMenuActionNames.MAKE_A_COPY,
109 ContextMenuActionNames.MOVE_TO,
110 ContextMenuActionNames.ADD_TO_FAVORITES,
111 ContextMenuActionNames.ADD_TO_PUBLIC_FAVORITES,
112 ContextMenuActionNames.DIVIDER,
113 ContextMenuActionNames.COPY_LINK_TO_CLIPBOARD,
114 ContextMenuActionNames.OPEN_WITH_3RD_PARTY_CLIENT,
115 ContextMenuActionNames.API_DETAILS,
118 const workflowOrder = [
119 ContextMenuActionNames.VIEW_DETAILS,
120 ContextMenuActionNames.OPEN_IN_NEW_TAB,
121 ContextMenuActionNames.COPY_UUID,
122 ContextMenuActionNames.DIVIDER,
123 ContextMenuActionNames.RUN_WORKFLOW,
124 ContextMenuActionNames.DELETE_WORKFLOW,
125 ContextMenuActionNames.DIVIDER,
126 ContextMenuActionNames.COPY_LINK_TO_CLIPBOARD,
127 ContextMenuActionNames.API_DETAILS,
130 const rootProjectOrder = [
131 ContextMenuActionNames.VIEW_DETAILS,
132 ContextMenuActionNames.USER_ACCOUNT,
133 ContextMenuActionNames.API_DETAILS,
136 const defaultMultiOrder = [
137 ContextMenuActionNames.MOVE_TO,
138 ContextMenuActionNames.MAKE_A_COPY,
139 ContextMenuActionNames.MOVE_TO_TRASH,
142 const kindToOrder: Record<string, ContextMenuActionNames[]> = {
143 [ContextMenuKind.MULTI]: defaultMultiOrder,
145 [ContextMenuKind.PROCESS]: processOrder,
146 [ContextMenuKind.PROCESS_ADMIN]: processOrder,
147 [ContextMenuKind.PROCESS_RESOURCE]: processOrder,
148 [ContextMenuKind.RUNNING_PROCESS_ADMIN]: processOrder,
149 [ContextMenuKind.RUNNING_PROCESS_RESOURCE]: processOrder,
151 [ContextMenuKind.PROJECT]: projectOrder,
152 [ContextMenuKind.PROJECT_ADMIN]: projectOrder,
153 [ContextMenuKind.FROZEN_PROJECT]: projectOrder,
154 [ContextMenuKind.FROZEN_PROJECT_ADMIN]: projectOrder,
156 [ContextMenuKind.COLLECTION]: collectionOrder,
157 [ContextMenuKind.COLLECTION_ADMIN]: collectionOrder,
158 [ContextMenuKind.READONLY_COLLECTION]: collectionOrder,
159 [ContextMenuKind.OLD_VERSION_COLLECTION]: collectionOrder,
161 [ContextMenuKind.WORKFLOW]: workflowOrder,
162 [ContextMenuKind.READONLY_WORKFLOW]: workflowOrder,
164 [ContextMenuKind.GROUPS]: projectOrder,
166 [ContextMenuKind.FILTER_GROUP]: projectOrder,
167 [ContextMenuKind.FILTER_GROUP_ADMIN]: projectOrder,
169 [ContextMenuKind.ROOT_PROJECT]: rootProjectOrder,
170 [ContextMenuKind.ROOT_PROJECT_ADMIN]: rootProjectOrder,
173 export const menuDirection = {
174 VERTICAL: 'vertical',
175 HORIZONTAL: 'horizontal'
178 export const sortMenuItems = (menuKind: ContextMenuKind, menuItems: ContextMenuAction[], orthagonality: string): ContextMenuAction[] | MultiSelectMenuAction[] => {
180 const preferredOrder = kindToOrder[menuKind];
181 //if no specified order, sort by name
182 if (!preferredOrder) return menuItems.sort(sortByProperty("name"));
184 const bucketMap = new Map();
185 const leftovers: ContextMenuAction[] = [];
187 // if we have multiple dividers, we need each of them to have a different "name" property
190 preferredOrder.forEach((name) => {
191 if (name === ContextMenuActionNames.DIVIDER) {
193 bucketMap.set(`${name}-${count}`, orthagonality === menuDirection.VERTICAL ? verticalMenuDivider : horizontalMenuDivider)
195 bucketMap.set(name, null)
198 [...menuItems].forEach((item) => {
199 if (bucketMap.has(item.name)) bucketMap.set(item.name, item);
200 else leftovers.push(item);
203 return Array.from(bucketMap.values()).concat(leftovers).filter((item) => item !== null).reduce((acc, val)=>{
204 return acc.at(-1)?.name === "Divider" && val.name === "Divider" ? acc : acc.concat(val)