Skip to content

Commit f6ebe7a

Browse files
add selectedItems composable
Handles selections as well as keyboard navigation. Currently aims to replace `client/src/components/History/Content/SelectedItems.js`. Made it a reusable composable instead of just scoped to history items.
1 parent 53f9e21 commit f6ebe7a

6 files changed

Lines changed: 665 additions & 293 deletions

File tree

client/src/components/History/Content/ContentItem.vue

Lines changed: 14 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import { useRoute, useRouter } from "vue-router/composables";
1616
import type { ItemUrls } from "@/components/History/Content/Dataset/index";
1717
import { updateContentFields } from "@/components/History/model/queries";
1818
import { useEntryPointStore } from "@/stores/entryPointStore";
19-
import { useEventStore } from "@/stores/eventStore";
2019
import { clearDrag } from "@/utils/setDrag";
2120
2221
import { JobStateSummary } from "./Collection/JobStateSummary";
@@ -48,6 +47,8 @@ interface Props {
4847
filterable?: boolean;
4948
isPlaceholder?: boolean;
5049
isSubItem?: boolean;
50+
getItemKey?: (item: any) => string;
51+
selectClickHandler?: (item: any, event: Event) => boolean;
5152
}
5253
5354
const props = withDefaults(defineProps<Props>(), {
@@ -63,17 +64,18 @@ const props = withDefaults(defineProps<Props>(), {
6364
filterable: false,
6465
isPlaceholder: false,
6566
isSubItem: false,
67+
getItemKey: (item: any) => {
68+
return `${item.history_content_type}-${item.id}`;
69+
},
70+
selectClickHandler: (item: any, event: Event) => {
71+
return true;
72+
},
6673
});
6774
6875
const emit = defineEmits<{
6976
(e: "update:selected", selected: boolean): void;
7077
(e: "update:expand-dataset", expand: boolean): void;
71-
(e: "shift-arrow-select", direction: string): void;
7278
(e: "init-key-selection"): void;
73-
(e: "arrow-navigate", direction: string): void;
74-
(e: "hide-selection"): void;
75-
(e: "select-all"): void;
76-
(e: "selected-to"): void;
7779
(e: "delete", item: any, recursive: boolean): void;
7880
(e: "undelete"): void;
7981
(e: "unhide"): void;
@@ -82,10 +84,10 @@ const emit = defineEmits<{
8284
(e: "tag-change", item: any, newTags: Array<string>): void;
8385
(e: "tag-click", tag: string): void;
8486
(e: "toggleHighlights", item: any): void;
87+
(e: "on-key-down", event: KeyboardEvent): void;
8588
}>();
8689
8790
const entryPointStore = useEntryPointStore();
88-
const eventStore = useEventStore();
8991
9092
const contentItem = ref<HTMLElement | null>(null);
9193
const subItemsVisible = ref(false);
@@ -95,7 +97,7 @@ const jobState = computed(() => {
9597
});
9698
9799
const contentId = computed(() => {
98-
return `dataset-${props.item.id}`;
100+
return props.getItemKey(props.item);
99101
});
100102
101103
const contentCls = computed(() => {
@@ -181,13 +183,6 @@ const itemUrls = computed<ItemUrls>(() => {
181183
};
182184
});
183185
184-
/** Based on the user's keyboard platform, checks if it is the
185-
* typical key for selection (ctrl for windows/linux, cmd for mac)
186-
*/
187-
function isSelectKey(event: KeyboardEvent) {
188-
return eventStore.isMac ? event.metaKey : event.ctrlKey;
189-
}
190-
191186
function onKeyDown(event: KeyboardEvent) {
192187
const classList = (event.target as HTMLElement)?.classList;
193188
if (!classList.contains("content-item") || classList.contains("sub-item")) {
@@ -197,45 +192,15 @@ function onKeyDown(event: KeyboardEvent) {
197192
if (event.key === "Enter" || event.key === " ") {
198193
event.preventDefault();
199194
onClick();
200-
} else if ((event.key === "ArrowUp" || event.key === "ArrowDown") && !event.shiftKey) {
201-
event.preventDefault();
202-
emit("arrow-navigate", event.key);
203-
}
204-
205-
if (props.writable) {
206-
if (event.key === "Tab") {
207-
emit("init-key-selection");
208-
} else {
209-
event.preventDefault();
210-
if ((event.key === "ArrowUp" || event.key === "ArrowDown") && event.shiftKey) {
211-
emit("shift-arrow-select", event.key);
212-
} else if (event.key === "ArrowUp" || event.key === "ArrowDown") {
213-
emit("init-key-selection");
214-
} else if (event.key === "Delete" && !props.selected && !props.item.deleted) {
215-
onDelete(event.shiftKey);
216-
emit("arrow-navigate", "ArrowDown");
217-
} else if (event.key === "Escape") {
218-
emit("hide-selection");
219-
} else if (event.key === "a" && isSelectKey(event)) {
220-
emit("select-all");
221-
}
222-
}
195+
} else {
196+
emit("on-key-down", event);
223197
}
224198
}
225199
226200
function onClick(e?: Event) {
227201
const event = e as KeyboardEvent;
228-
if (event && props.writable) {
229-
if (isSelectKey(event)) {
230-
emit("init-key-selection");
231-
emit("update:selected", !props.selected);
232-
return;
233-
} else if (event.shiftKey) {
234-
emit("selected-to");
235-
return;
236-
} else {
237-
emit("init-key-selection");
238-
}
202+
if (event && props.writable && !props.selectClickHandler(props.item, event)) {
203+
return;
239204
}
240205
if (props.isPlaceholder) {
241206
return;

0 commit comments

Comments
 (0)