2021-04-01 00:44:21 +05:30
|
|
|
<template>
|
2021-04-06 15:40:17 +05:30
|
|
|
<div class="uk-position-absolute uk-panel uk-box-shadow-large suggestions-container">
|
2021-04-01 03:39:39 +05:30
|
|
|
<ul class="uk-list uk-margin-remove uk-text-secondary">
|
2021-04-06 15:40:17 +05:30
|
|
|
<li
|
|
|
|
v-for="(suggestion, i) in searchSuggestions"
|
|
|
|
:key="i"
|
|
|
|
:class="{ selected: selected === i }"
|
2021-04-07 14:52:55 +05:30
|
|
|
@mouseover="onMouseOver(i)"
|
|
|
|
@mousedown.stop="onClick(i)"
|
2021-04-06 15:40:17 +05:30
|
|
|
class="uk-margin-remove suggestion"
|
|
|
|
>
|
2021-04-01 03:39:39 +05:30
|
|
|
{{ suggestion }}
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
</div>
|
2021-04-01 00:44:21 +05:30
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2021-04-06 15:40:17 +05:30
|
|
|
import Constants from "@/Constants.js";
|
|
|
|
|
2021-04-01 00:44:21 +05:30
|
|
|
export default {
|
|
|
|
props: {
|
2021-04-06 15:40:17 +05:30
|
|
|
searchText: String
|
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
selected: 0,
|
|
|
|
searchSuggestions: []
|
|
|
|
};
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
onKeyUp(e) {
|
|
|
|
if (e.key === "ArrowUp") {
|
|
|
|
if (this.selected <= 0) {
|
2021-04-07 14:52:55 +05:30
|
|
|
this.setSelected(this.searchSuggestions.length - 1);
|
2021-04-06 15:40:17 +05:30
|
|
|
} else {
|
2021-04-07 14:52:55 +05:30
|
|
|
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) {
|
2021-04-07 14:52:55 +05:30
|
|
|
this.setSelected(0);
|
2021-04-06 15:40:17 +05:30
|
|
|
} else {
|
2021-04-07 14:52:55 +05:30
|
|
|
this.setSelected(this.selected + 1);
|
2021-04-06 15:40:17 +05:30
|
|
|
}
|
|
|
|
e.preventDefault();
|
|
|
|
} else {
|
|
|
|
this.refreshSuggestions();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
async refreshSuggestions() {
|
|
|
|
this.searchSuggestions = await this.fetchJson(
|
|
|
|
Constants.BASE_URL + "/suggestions?query=" + encodeURI(this.searchText)
|
|
|
|
);
|
|
|
|
this.searchSuggestions.unshift(this.searchText);
|
2021-04-07 14:52:55 +05:30
|
|
|
this.setSelected(0);
|
|
|
|
},
|
|
|
|
onMouseOver(i) {
|
|
|
|
if (i !== this.selected) {
|
|
|
|
this.selected = i;
|
2021-04-06 15:40:17 +05:30
|
|
|
}
|
2021-04-07 14:52:55 +05:30
|
|
|
},
|
|
|
|
onClick(i) {
|
|
|
|
this.setSelected(i);
|
|
|
|
this.$router.push({
|
|
|
|
name: "SearchResults",
|
|
|
|
query: { search_query: this.searchText }
|
|
|
|
});
|
|
|
|
},
|
|
|
|
setSelected(val) {
|
|
|
|
this.selected = val;
|
|
|
|
this.$emit("searchchange", this.searchSuggestions[this.selected]);
|
2021-04-06 15:40:17 +05:30
|
|
|
}
|
2021-04-01 00:44:21 +05:30
|
|
|
}
|
|
|
|
};
|
|
|
|
</script>
|
|
|
|
|
2021-04-01 03:39:39 +05:30
|
|
|
<style>
|
|
|
|
.suggestions-container {
|
|
|
|
background-color: #242727;
|
|
|
|
left: 50%;
|
|
|
|
transform: translateX(-50%);
|
|
|
|
max-width: 640px;
|
|
|
|
width: 100%;
|
|
|
|
box-sizing: border-box;
|
2021-04-06 15:40:17 +05:30
|
|
|
padding: 5px 0;
|
|
|
|
}
|
|
|
|
.suggestion {
|
|
|
|
padding: 4px 15px;
|
|
|
|
}
|
|
|
|
.suggestion.selected {
|
|
|
|
background-color: #393d3d;
|
2021-04-01 03:39:39 +05:30
|
|
|
}
|
|
|
|
@media screen and (max-width: 959px) {
|
|
|
|
.suggestions-container {
|
|
|
|
max-width: calc(100% - 60px);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@media screen and (max-width: 639px) {
|
|
|
|
.suggestions-container {
|
|
|
|
max-width: calc(100% - 30px);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</style>
|