2021-08-21 23:23:35 +02:00
|
|
|
<template>
|
2022-07-27 01:54:32 +02:00
|
|
|
<div class="sm:w-80 w-full relative">
|
2021-09-24 23:14:33 +02:00
|
|
|
<form @submit.prevent="submitSearch">
|
2022-11-08 01:27:17 +01:00
|
|
|
<ui-text-input ref="input" v-model="search" :placeholder="$strings.PlaceholderSearch" @input="inputUpdate" @focus="focussed" @blur="blurred" class="w-full h-8 text-sm" />
|
2021-09-24 23:14:33 +02:00
|
|
|
</form>
|
2021-08-21 23:23:35 +02:00
|
|
|
<div class="absolute top-0 right-0 bottom-0 h-full flex items-center px-2 text-gray-400 cursor-pointer" @click="clickClear">
|
|
|
|
<span v-if="!search" class="material-icons" style="font-size: 1.2rem">search</span>
|
|
|
|
<span v-else class="material-icons" style="font-size: 1.2rem">close</span>
|
|
|
|
</div>
|
2022-06-17 16:48:42 +02:00
|
|
|
<div v-show="showMenu && (lastSearch || isTyping)" class="absolute z-40 -mt-px w-40 sm:w-full bg-bg border border-black-200 shadow-lg rounded-md py-1 px-2 text-base ring-1 ring-black ring-opacity-5 overflow-auto focus:outline-none sm:text-sm globalSearchMenu">
|
2021-08-21 23:23:35 +02:00
|
|
|
<ul class="h-full w-full" role="listbox" aria-labelledby="listbox-label">
|
|
|
|
<li v-if="isTyping" class="py-2 px-2">
|
2022-11-09 00:10:08 +01:00
|
|
|
<p>{{ $strings.MessageThinking }}</p>
|
2021-08-21 23:23:35 +02:00
|
|
|
</li>
|
|
|
|
<li v-else-if="isFetching" class="py-2 px-2">
|
2022-11-09 00:10:08 +01:00
|
|
|
<p>{{ $strings.MessageFetching }}</p>
|
2021-08-21 23:23:35 +02:00
|
|
|
</li>
|
2021-10-09 18:09:06 +02:00
|
|
|
<li v-else-if="!totalResults" class="py-2 px-2">
|
2022-11-09 00:10:08 +01:00
|
|
|
<p>{{ $strings.MessageNoResults }}</p>
|
2021-08-21 23:23:35 +02:00
|
|
|
</li>
|
|
|
|
<template v-else>
|
2022-11-09 00:10:08 +01:00
|
|
|
<p v-if="bookResults.length" class="uppercase text-xs text-gray-400 my-1 px-1 font-semibold">{{ $strings.LabelBooks }}</p>
|
2022-03-13 18:39:12 +01:00
|
|
|
<template v-for="item in bookResults">
|
2022-06-15 19:35:50 +02:00
|
|
|
<li :key="item.libraryItem.id" class="text-gray-50 select-none relative cursor-pointer hover:bg-black-400 py-1" role="option" @click="clickOption">
|
2022-04-23 00:58:43 +02:00
|
|
|
<nuxt-link :to="`/item/${item.libraryItem.id}`">
|
2022-04-12 23:54:52 +02:00
|
|
|
<cards-item-search-card :library-item="item.libraryItem" :match-key="item.matchKey" :match-text="item.matchText" :search="lastSearch" />
|
|
|
|
</nuxt-link>
|
|
|
|
</li>
|
|
|
|
</template>
|
|
|
|
|
2022-11-09 00:10:08 +01:00
|
|
|
<p v-if="podcastResults.length" class="uppercase text-xs text-gray-400 my-1 px-1 font-semibold">{{ $strings.LabelPodcasts }}</p>
|
2022-04-12 23:54:52 +02:00
|
|
|
<template v-for="item in podcastResults">
|
2022-06-15 19:35:50 +02:00
|
|
|
<li :key="item.libraryItem.id" class="text-gray-50 select-none relative cursor-pointer hover:bg-black-400 py-1" role="option" @click="clickOption">
|
2022-04-23 00:58:43 +02:00
|
|
|
<nuxt-link :to="`/item/${item.libraryItem.id}`">
|
2022-04-12 23:54:52 +02:00
|
|
|
<cards-item-search-card :library-item="item.libraryItem" :match-key="item.matchKey" :match-text="item.matchText" :search="lastSearch" />
|
2021-10-09 18:09:06 +02:00
|
|
|
</nuxt-link>
|
|
|
|
</li>
|
|
|
|
</template>
|
|
|
|
|
2022-11-09 00:10:08 +01:00
|
|
|
<p v-if="authorResults.length" class="uppercase text-xs text-gray-400 mb-1 mt-3 px-1 font-semibold">{{ $strings.LabelAuthors }}</p>
|
2021-10-09 18:09:06 +02:00
|
|
|
<template v-for="item in authorResults">
|
2022-06-15 19:35:50 +02:00
|
|
|
<li :key="item.id" class="text-gray-50 select-none relative cursor-pointer hover:bg-black-400 py-1" role="option" @click="clickOption">
|
2022-04-09 01:34:30 +02:00
|
|
|
<nuxt-link :to="`/library/${currentLibraryId}/bookshelf?filter=authors.${$encode(item.id)}`">
|
2022-03-13 18:39:12 +01:00
|
|
|
<cards-author-search-card :author="item" />
|
2021-10-09 18:09:06 +02:00
|
|
|
</nuxt-link>
|
|
|
|
</li>
|
|
|
|
</template>
|
|
|
|
|
2022-11-09 00:10:08 +01:00
|
|
|
<p v-if="seriesResults.length" class="uppercase text-xs text-gray-400 mb-1 mt-3 px-1 font-semibold">{{ $strings.LabelSeries }}</p>
|
2021-10-09 18:09:06 +02:00
|
|
|
<template v-for="item in seriesResults">
|
2022-06-15 19:35:50 +02:00
|
|
|
<li :key="item.series.id" class="text-gray-50 select-none relative cursor-pointer hover:bg-black-400 py-1" role="option" @click="clickOption">
|
2022-03-13 18:39:12 +01:00
|
|
|
<nuxt-link :to="`/library/${currentLibraryId}/series/${item.series.id}`">
|
|
|
|
<cards-series-search-card :series="item.series" :book-items="item.books" />
|
2021-10-09 18:09:06 +02:00
|
|
|
</nuxt-link>
|
2021-08-21 23:23:35 +02:00
|
|
|
</li>
|
|
|
|
</template>
|
2021-10-17 18:29:52 +02:00
|
|
|
|
2022-11-09 00:10:08 +01:00
|
|
|
<p v-if="tagResults.length" class="uppercase text-xs text-gray-400 mb-1 mt-3 px-1 font-semibold">{{ $strings.LabelTags }}</p>
|
2021-10-17 18:29:52 +02:00
|
|
|
<template v-for="item in tagResults">
|
2022-06-15 19:35:50 +02:00
|
|
|
<li :key="item.name" class="text-gray-50 select-none relative cursor-pointer hover:bg-black-400 py-1" role="option" @click="clickOption">
|
2022-03-13 18:39:12 +01:00
|
|
|
<nuxt-link :to="`/library/${currentLibraryId}/bookshelf?filter=tags.${$encode(item.name)}`">
|
|
|
|
<cards-tag-search-card :tag="item.name" />
|
2021-10-17 18:29:52 +02:00
|
|
|
</nuxt-link>
|
|
|
|
</li>
|
|
|
|
</template>
|
2023-04-25 01:25:30 +02:00
|
|
|
|
|
|
|
<p v-if="narratorResults.length" class="uppercase text-xs text-gray-400 mb-1 mt-3 px-1 font-semibold">{{ $strings.LabelNarrators }}</p>
|
|
|
|
<template v-for="narrator in narratorResults">
|
|
|
|
<li :key="narrator.name" class="text-gray-50 select-none relative cursor-pointer hover:bg-black-400 py-1" role="option" @click="clickOption">
|
|
|
|
<nuxt-link :to="`/library/${currentLibraryId}/bookshelf?filter=narrators.${$encode(narrator.name)}`">
|
|
|
|
<cards-narrator-search-card :narrator="narrator.name" />
|
|
|
|
</nuxt-link>
|
|
|
|
</li>
|
|
|
|
</template>
|
2021-08-21 23:23:35 +02:00
|
|
|
</template>
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
export default {
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
showMenu: false,
|
|
|
|
isFocused: false,
|
|
|
|
focusTimeout: null,
|
|
|
|
isTyping: false,
|
|
|
|
isFetching: false,
|
|
|
|
search: null,
|
2022-04-12 23:54:52 +02:00
|
|
|
podcastResults: [],
|
2022-03-13 18:39:12 +01:00
|
|
|
bookResults: [],
|
2021-10-09 18:09:06 +02:00
|
|
|
authorResults: [],
|
|
|
|
seriesResults: [],
|
2021-10-17 18:29:52 +02:00
|
|
|
tagResults: [],
|
2023-04-25 01:25:30 +02:00
|
|
|
narratorResults: [],
|
2021-08-21 23:23:35 +02:00
|
|
|
searchTimeout: null,
|
|
|
|
lastSearch: null
|
|
|
|
}
|
|
|
|
},
|
|
|
|
computed: {
|
2021-10-06 04:10:49 +02:00
|
|
|
currentLibraryId() {
|
|
|
|
return this.$store.state.libraries.currentLibraryId
|
2021-10-09 18:09:06 +02:00
|
|
|
},
|
|
|
|
totalResults() {
|
2023-08-19 20:59:22 +02:00
|
|
|
return this.bookResults.length + this.seriesResults.length + this.authorResults.length + this.tagResults.length + this.podcastResults.length + this.narratorResults.length
|
2021-08-21 23:23:35 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
2022-06-15 19:35:50 +02:00
|
|
|
clickOption() {
|
|
|
|
this.clearResults()
|
|
|
|
},
|
2021-09-24 23:14:33 +02:00
|
|
|
submitSearch() {
|
|
|
|
if (!this.search) return
|
2021-10-17 18:29:52 +02:00
|
|
|
var search = this.search
|
|
|
|
this.clearResults()
|
2021-12-01 03:02:40 +01:00
|
|
|
this.$router.push(`/library/${this.currentLibraryId}/search?q=${search}`)
|
2021-10-17 18:29:52 +02:00
|
|
|
},
|
|
|
|
clearResults() {
|
2021-09-24 23:14:33 +02:00
|
|
|
this.search = null
|
2021-10-17 18:29:52 +02:00
|
|
|
this.lastSearch = null
|
2022-04-12 23:54:52 +02:00
|
|
|
this.podcastResults = []
|
2022-03-13 18:39:12 +01:00
|
|
|
this.bookResults = []
|
2021-10-09 18:09:06 +02:00
|
|
|
this.authorResults = []
|
|
|
|
this.seriesResults = []
|
2021-10-17 18:29:52 +02:00
|
|
|
this.tagResults = []
|
2023-04-25 01:25:30 +02:00
|
|
|
this.narratorResults = []
|
2021-09-24 23:14:33 +02:00
|
|
|
this.showMenu = false
|
2021-10-17 18:29:52 +02:00
|
|
|
this.isFetching = false
|
|
|
|
this.isTyping = false
|
|
|
|
clearTimeout(this.searchTimeout)
|
2021-09-24 23:14:33 +02:00
|
|
|
this.$nextTick(() => {
|
|
|
|
if (this.$refs.input) {
|
|
|
|
this.$refs.input.blur()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
2021-08-21 23:23:35 +02:00
|
|
|
focussed() {
|
|
|
|
this.isFocused = true
|
|
|
|
this.showMenu = true
|
|
|
|
},
|
|
|
|
blurred() {
|
|
|
|
this.isFocused = false
|
|
|
|
clearTimeout(this.focusTimeout)
|
|
|
|
this.focusTimeout = setTimeout(() => {
|
|
|
|
this.showMenu = false
|
|
|
|
}, 200)
|
|
|
|
},
|
|
|
|
async runSearch(value) {
|
|
|
|
this.lastSearch = value
|
|
|
|
if (!this.lastSearch) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
this.isFetching = true
|
2021-10-09 18:09:06 +02:00
|
|
|
|
2023-04-25 01:25:30 +02:00
|
|
|
const searchResults = await this.$axios.$get(`/api/libraries/${this.currentLibraryId}/search?q=${value}&limit=3`).catch((error) => {
|
2021-08-21 23:23:35 +02:00
|
|
|
console.error('Search error', error)
|
|
|
|
return []
|
|
|
|
})
|
2021-10-17 18:29:52 +02:00
|
|
|
|
|
|
|
// Search was canceled
|
|
|
|
if (!this.isFetching) return
|
|
|
|
|
2022-04-12 23:54:52 +02:00
|
|
|
this.podcastResults = searchResults.podcast || []
|
2022-03-13 18:39:12 +01:00
|
|
|
this.bookResults = searchResults.book || []
|
2021-10-09 18:09:06 +02:00
|
|
|
this.authorResults = searchResults.authors || []
|
|
|
|
this.seriesResults = searchResults.series || []
|
2021-10-17 18:29:52 +02:00
|
|
|
this.tagResults = searchResults.tags || []
|
2023-04-25 01:25:30 +02:00
|
|
|
this.narratorResults = searchResults.narrators || []
|
2021-10-09 18:09:06 +02:00
|
|
|
|
2021-08-21 23:23:35 +02:00
|
|
|
this.isFetching = false
|
2021-09-24 23:14:33 +02:00
|
|
|
if (!this.showMenu) {
|
|
|
|
return
|
|
|
|
}
|
2021-08-21 23:23:35 +02:00
|
|
|
},
|
|
|
|
inputUpdate(val) {
|
|
|
|
clearTimeout(this.searchTimeout)
|
|
|
|
if (!val) {
|
|
|
|
this.lastSearch = ''
|
|
|
|
this.isTyping = false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
this.isTyping = true
|
|
|
|
this.searchTimeout = setTimeout(() => {
|
2021-10-17 18:29:52 +02:00
|
|
|
// Canceled search
|
|
|
|
if (!this.isTyping) return
|
|
|
|
|
2021-08-21 23:23:35 +02:00
|
|
|
this.isTyping = false
|
|
|
|
this.runSearch(val)
|
2021-10-09 18:09:06 +02:00
|
|
|
}, 750)
|
2021-08-21 23:23:35 +02:00
|
|
|
},
|
|
|
|
clickClear() {
|
2021-10-17 18:29:52 +02:00
|
|
|
this.clearResults()
|
2021-08-21 23:23:35 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
mounted() {}
|
|
|
|
}
|
2021-10-09 18:09:06 +02:00
|
|
|
</script>
|
|
|
|
|
2023-05-30 23:55:52 +02:00
|
|
|
<style scoped>
|
2021-10-09 18:09:06 +02:00
|
|
|
.globalSearchMenu {
|
2023-05-30 23:55:52 +02:00
|
|
|
max-height: calc(100vh - 75px);
|
2021-10-09 18:09:06 +02:00
|
|
|
}
|
|
|
|
</style>
|