1
0
mirror of https://github.com/TeamPiped/Piped.git synced 2024-12-16 23:30:27 +05:30
Piped/src/components/SearchSuggestions.vue

103 lines
2.9 KiB
Vue
Raw Normal View History

2021-04-01 00:44:21 +05:30
<template>
<div class="suggestions-container absolute">
<ul>
<li v-for="(suggestion, i) in searchSuggestions" :key="i" @mouseover="onMouseOver(i)">
<router-link
class="suggestion"
:class="{ 'suggestion-selected': selected === i }"
:to="`/results?search_query=${encodeURIComponent(suggestion)}`"
>{{ suggestion }}</router-link
>
</li>
2021-04-01 03:39:39 +05:30
</ul>
</div>
2021-04-01 00:44:21 +05:30
</template>
<script>
export default {
props: {
searchText: { type: String, default: "" },
2021-04-06 15:40:17 +05:30
},
emits: ["searchchange"],
2021-04-06 15:40:17 +05:30
data() {
return {
selected: 0,
2021-05-10 23:44:28 +05:30
searchSuggestions: [],
2021-04-06 15:40:17 +05:30
};
},
methods: {
onKeyUp(e) {
if (e.key === "ArrowUp") {
if (this.selected <= 0) {
this.setSelected(this.searchSuggestions.length - 1);
2021-04-06 15:40:17 +05:30
} else {
this.setSelected(this.selected - 1);
2021-04-06 15:40:17 +05:30
}
e.preventDefault();
} else if (e.key === "ArrowDown") {
if (this.selected >= this.searchSuggestions.length - 1) {
this.setSelected(0);
2021-04-06 15:40:17 +05:30
} else {
this.setSelected(this.selected + 1);
2021-04-06 15:40:17 +05:30
}
e.preventDefault();
} else {
this.refreshSuggestions();
}
},
async refreshSuggestions() {
2022-09-11 23:31:58 +05:30
if (!this.searchText) {
if (this.getPreferenceBoolean("searchHistory", false))
this.searchSuggestions = JSON.parse(localStorage.getItem("search_history")) ?? [];
2023-06-13 13:54:29 +05:30
} else if (this.getPreferenceBoolean("searchSuggestions", true)) {
this.searchSuggestions =
(
await this.fetchJson(this.apiUrl() + "/opensearch/suggestions", {
query: this.searchText,
})
)?.[1] ?? [];
2023-06-13 13:54:29 +05:30
} else {
this.searchSuggestions = [];
return;
2022-09-11 23:31:58 +05:30
}
2021-04-06 15:40:17 +05:30
this.searchSuggestions.unshift(this.searchText);
this.setSelected(0);
},
onMouseOver(i) {
if (i !== this.selected) {
this.selected = i;
2021-04-06 15:40:17 +05:30
}
},
onClick(i) {
this.setSelected(i);
},
setSelected(val) {
this.selected = val;
this.$emit("searchchange", this.searchSuggestions[this.selected]);
2021-05-10 23:44:28 +05:30
},
},
2021-04-01 00:44:21 +05:30
};
</script>
2021-04-01 03:39:39 +05:30
<style>
.suggestions-container {
2023-05-26 14:22:28 +05:30
@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;
}
.dark .suggestions-container {
@apply bg-dark-400;
}
.suggestion-selected {
@apply bg-gray-200;
}
.dark .suggestion-selected {
@apply bg-dark-100;
}
2021-04-06 15:40:17 +05:30
.suggestion {
@apply block w-full p-1;
2021-04-01 03:39:39 +05:30
}
</style>