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",
64 const processOrder = [
65 ContextMenuActionNames.VIEW_DETAILS,
66 ContextMenuActionNames.OPEN_IN_NEW_TAB,
67 ContextMenuActionNames.OUTPUTS,
68 ContextMenuActionNames.API_DETAILS,
69 ContextMenuActionNames.DIVIDER,
70 ContextMenuActionNames.EDIT_PROCESS,
71 ContextMenuActionNames.COPY_AND_RERUN_PROCESS,
72 ContextMenuActionNames.CANCEL,
73 ContextMenuActionNames.REMOVE,
74 ContextMenuActionNames.DIVIDER,
75 ContextMenuActionNames.ADD_TO_FAVORITES,
76 ContextMenuActionNames.ADD_TO_PUBLIC_FAVORITES,
79 const projectOrder = [
80 ContextMenuActionNames.VIEW_DETAILS,
81 ContextMenuActionNames.OPEN_IN_NEW_TAB,
82 ContextMenuActionNames.COPY_LINK_TO_CLIPBOARD,
83 ContextMenuActionNames.OPEN_WITH_3RD_PARTY_CLIENT,
84 ContextMenuActionNames.API_DETAILS,
85 ContextMenuActionNames.DIVIDER,
86 ContextMenuActionNames.SHARE,
87 ContextMenuActionNames.NEW_PROJECT,
88 ContextMenuActionNames.EDIT_PROJECT,
89 ContextMenuActionNames.MOVE_TO,
90 ContextMenuActionNames.MOVE_TO_TRASH,
91 ContextMenuActionNames.DIVIDER,
92 ContextMenuActionNames.FREEZE_PROJECT,
93 ContextMenuActionNames.ADD_TO_FAVORITES,
94 ContextMenuActionNames.ADD_TO_PUBLIC_FAVORITES,
97 const collectionOrder = [
98 ContextMenuActionNames.VIEW_DETAILS,
99 ContextMenuActionNames.OPEN_IN_NEW_TAB,
100 ContextMenuActionNames.COPY_LINK_TO_CLIPBOARD,
101 ContextMenuActionNames.OPEN_WITH_3RD_PARTY_CLIENT,
102 ContextMenuActionNames.API_DETAILS,
103 ContextMenuActionNames.DIVIDER,
104 ContextMenuActionNames.SHARE,
105 ContextMenuActionNames.EDIT_COLLECTION,
106 ContextMenuActionNames.MOVE_TO,
107 ContextMenuActionNames.MAKE_A_COPY,
108 ContextMenuActionNames.MOVE_TO_TRASH,
109 ContextMenuActionNames.DIVIDER,
110 ContextMenuActionNames.ADD_TO_FAVORITES,
111 ContextMenuActionNames.ADD_TO_PUBLIC_FAVORITES,
114 const workflowOrder = [
115 ContextMenuActionNames.VIEW_DETAILS,
116 ContextMenuActionNames.OPEN_IN_NEW_TAB,
117 ContextMenuActionNames.COPY_LINK_TO_CLIPBOARD,
118 ContextMenuActionNames.API_DETAILS,
119 ContextMenuActionNames.DIVIDER,
120 ContextMenuActionNames.RUN_WORKFLOW,
121 ContextMenuActionNames.DELETE_WORKFLOW,
124 const defaultMultiOrder = [
125 ContextMenuActionNames.MOVE_TO,
126 ContextMenuActionNames.MAKE_A_COPY,
127 ContextMenuActionNames.MOVE_TO_TRASH,
130 const kindToOrder: Record<string, ContextMenuActionNames[]> = {
131 [ContextMenuKind.MULTI]: defaultMultiOrder,
133 [ContextMenuKind.PROCESS]: processOrder,
134 [ContextMenuKind.PROCESS_ADMIN]: processOrder,
135 [ContextMenuKind.PROCESS_RESOURCE]: processOrder,
136 [ContextMenuKind.RUNNING_PROCESS_ADMIN]: processOrder,
137 [ContextMenuKind.RUNNING_PROCESS_RESOURCE]: processOrder,
139 [ContextMenuKind.PROJECT]: projectOrder,
140 [ContextMenuKind.PROJECT_ADMIN]: projectOrder,
141 [ContextMenuKind.FROZEN_PROJECT]: projectOrder,
142 [ContextMenuKind.FROZEN_PROJECT_ADMIN]: projectOrder,
144 [ContextMenuKind.COLLECTION]: collectionOrder,
145 [ContextMenuKind.COLLECTION_ADMIN]: collectionOrder,
146 [ContextMenuKind.READONLY_COLLECTION]: collectionOrder,
147 [ContextMenuKind.OLD_VERSION_COLLECTION]: collectionOrder,
149 [ContextMenuKind.WORKFLOW]: workflowOrder,
150 [ContextMenuKind.READONLY_WORKFLOW]: workflowOrder,
153 export const menuDirection = {
154 VERTICAL: 'vertical',
155 HORIZONTAL: 'horizontal'
158 export const sortMenuItems = (menuKind: ContextMenuKind, menuItems: ContextMenuAction[], orthagonality: string): ContextMenuAction[] | MultiSelectMenuAction[] => {
160 const preferredOrder = kindToOrder[menuKind];
161 //if no specified order, sort by name
162 if (!preferredOrder) return menuItems.sort(sortByProperty("name"));
164 const bucketMap = new Map();
165 const leftovers: ContextMenuAction[] = [];
167 // if we have multiple dividers, we need each of them to have a different "name" property
170 preferredOrder.forEach((name) => {
171 if (name === ContextMenuActionNames.DIVIDER) {
173 bucketMap.set(`${name}-${count}`, orthagonality === menuDirection.VERTICAL ? verticalMenuDivider : horizontalMenuDivider)
175 bucketMap.set(name, null)
178 [...menuItems].forEach((item) => {
179 if (bucketMap.has(item.name)) bucketMap.set(item.name, item);
180 else leftovers.push(item);
183 return Array.from(bucketMap.values()).concat(leftovers).filter((item) => item !== null);