2021-03-31 19:14:21 +00:00
|
|
|
<template>
|
2021-12-28 00:04:20 +00:00
|
|
|
<div class="absolute suggestions-container">
|
2021-12-27 15:36:18 +00:00
|
|
|
<ul>
|
2021-04-06 10:10:17 +00:00
|
|
|
<li
|
|
|
|
v-for="(suggestion, i) in searchSuggestions"
|
|
|
|
:key="i"
|
2021-12-27 15:36:18 +00:00
|
|
|
class="suggestion"
|
2021-12-28 00:04:20 +00:00
|
|
|
:class="{ 'suggestion-selected': selected === i }"
|
2021-04-07 09:22:55 +00:00
|
|
|
@mouseover="onMouseOver(i)"
|
|
|
|
@mousedown.stop="onClick(i)"
|
2021-12-27 16:29:25 +00:00
|
|
|
v-text="suggestion"
|
|
|
|
/>
|
2021-03-31 22:09:39 +00:00
|
|
|
</ul>
|
|
|
|
</div>
|
2021-03-31 19:14:21 +00:00
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
export default {
|
|
|
|
props: {
|
2021-10-08 18:52:51 +00:00
|
|
|
searchText: { type: String, default: "" },
|
2021-04-06 10:10:17 +00:00
|
|
|
},
|
2021-10-08 18:52:51 +00:00
|
|
|
emits: ["searchchange"],
|
2021-04-06 10:10:17 +00:00
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
selected: 0,
|
2021-05-10 18:14:28 +00:00
|
|
|
searchSuggestions: [],
|
2021-04-06 10:10:17 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
onKeyUp(e) {
|
|
|
|
if (e.key === "ArrowUp") {
|
|
|
|
if (this.selected <= 0) {
|
2021-04-07 09:22:55 +00:00
|
|
|
this.setSelected(this.searchSuggestions.length - 1);
|
2021-04-06 10:10:17 +00:00
|
|
|
} else {
|
2021-04-07 09:22:55 +00:00
|
|
|
this.setSelected(this.selected - 1);
|
2021-04-06 10:10:17 +00:00
|
|
|
}
|
|
|
|
e.preventDefault();
|
|
|
|
} else if (e.key === "ArrowDown") {
|
|
|
|
if (this.selected >= this.searchSuggestions.length - 1) {
|
2021-04-07 09:22:55 +00:00
|
|
|
this.setSelected(0);
|
2021-04-06 10:10:17 +00:00
|
|
|
} else {
|
2021-04-07 09:22:55 +00:00
|
|
|
this.setSelected(this.selected + 1);
|
2021-04-06 10:10:17 +00:00
|
|
|
}
|
|
|
|
e.preventDefault();
|
|
|
|
} else {
|
|
|
|
this.refreshSuggestions();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
async refreshSuggestions() {
|
2022-09-11 18:01:58 +00:00
|
|
|
if (!this.searchText) {
|
2022-10-02 13:27:25 +00:00
|
|
|
if (this.getPreferenceBoolean("searchHistory", false))
|
|
|
|
this.searchSuggestions = JSON.parse(localStorage.getItem("search_history")) ?? [];
|
2022-09-11 18:01:58 +00:00
|
|
|
} else {
|
2023-05-25 18:43:11 +00:00
|
|
|
this.searchSuggestions =
|
|
|
|
(
|
|
|
|
await this.fetchJson(this.apiUrl() + "/opensearch/suggestions", {
|
|
|
|
query: this.searchText,
|
|
|
|
})
|
|
|
|
)?.[1] ?? [];
|
2022-09-11 18:01:58 +00:00
|
|
|
}
|
2021-04-06 10:10:17 +00:00
|
|
|
this.searchSuggestions.unshift(this.searchText);
|
2021-04-07 09:22:55 +00:00
|
|
|
this.setSelected(0);
|
|
|
|
},
|
|
|
|
onMouseOver(i) {
|
|
|
|
if (i !== this.selected) {
|
|
|
|
this.selected = i;
|
2021-04-06 10:10:17 +00:00
|
|
|
}
|
2021-04-07 09:22:55 +00:00
|
|
|
},
|
|
|
|
onClick(i) {
|
|
|
|
this.setSelected(i);
|
|
|
|
this.$router.push({
|
|
|
|
name: "SearchResults",
|
2021-05-10 18:14:28 +00:00
|
|
|
query: { search_query: this.searchSuggestions[i] },
|
2021-04-07 09:22:55 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
setSelected(val) {
|
|
|
|
this.selected = val;
|
|
|
|
this.$emit("searchchange", this.searchSuggestions[this.selected]);
|
2021-05-10 18:14:28 +00:00
|
|
|
},
|
|
|
|
},
|
2021-03-31 19:14:21 +00:00
|
|
|
};
|
|
|
|
</script>
|
|
|
|
|
2021-03-31 22:09:39 +00:00
|
|
|
<style>
|
|
|
|
.suggestions-container {
|
2023-05-26 08:52:28 +00:00
|
|
|
@apply left-1/2 translate-x-[-50%] transform-gpu max-w-3xl w-full box-border z-10 lt-md:max-w-[calc(100%-0.5rem)] bg-gray-300;
|
2021-12-28 00:04:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
.dark .suggestions-container {
|
|
|
|
@apply bg-dark-400;
|
|
|
|
}
|
|
|
|
|
|
|
|
.suggestion-selected {
|
|
|
|
@apply bg-gray-200;
|
|
|
|
}
|
|
|
|
|
|
|
|
.dark .suggestion-selected {
|
|
|
|
@apply bg-dark-100;
|
|
|
|
}
|
|
|
|
|
2021-04-06 10:10:17 +00:00
|
|
|
.suggestion {
|
2023-05-26 08:52:28 +00:00
|
|
|
@apply p-1;
|
2021-03-31 22:09:39 +00:00
|
|
|
}
|
|
|
|
</style>
|