audiobookshelf/client/components/controls/LibrarySortSelect.vue

194 lines
5.8 KiB
Vue
Raw Normal View History

2021-08-19 03:18:44 +02:00
<template>
<div ref="wrapper" class="relative" v-click-outside="clickOutside">
<button type="button" class="relative w-full h-full bg-fg border border-gray-500 hover:border-gray-400 rounded shadow-sm pl-3 pr-3 py-0 text-left focus:outline-none sm:text-sm cursor-pointer" aria-haspopup="menu" :aria-expanded="showMenu" @click.prevent="showMenu = !showMenu">
2021-08-19 03:18:44 +02:00
<span class="flex items-center justify-between">
<span class="block truncate text-xs" :class="!selectedText ? 'text-gray-300' : ''">{{ selectedText }}</span>
<span class="material-symbols text-lg text-yellow-400" :aria-label="descending ? $strings.LabelSortDescending : $strings.LabelSortAscending">{{ descending ? 'expand_more' : 'expand_less' }}</span>
2021-08-19 03:18:44 +02:00
</span>
</button>
<ul v-show="showMenu" class="absolute z-10 mt-1 w-full bg-bg border border-black-200 shadow-lg max-h-96 rounded-md py-1 ring-1 ring-black ring-opacity-5 overflow-auto focus:outline-none text-sm" role="menu">
<template v-for="item in selectItems">
<li :key="item.value" class="select-none relative py-2 pr-9 cursor-pointer hover:bg-white/5" :class="item.value === selected ? 'bg-white/5 text-yellow-400' : 'text-gray-200 hover:text-white'" role="menuitem" @click="clickedOption(item.value)">
2021-08-19 03:18:44 +02:00
<div class="flex items-center">
<span class="font-normal ml-3 block truncate">{{ item.text }}</span>
2021-08-19 03:18:44 +02:00
</div>
2022-11-09 00:10:08 +01:00
<span v-if="item.value === selected" class="text-yellow-400 absolute inset-y-0 right-0 flex items-center pr-4">
<span class="material-symbols text-xl" :aria-label="descending ? $strings.LabelSortDescending : $strings.LabelSortAscending">{{ descending ? 'expand_more' : 'expand_less' }}</span>
2021-08-19 03:18:44 +02:00
</span>
</li>
</template>
</ul>
</div>
</template>
<script>
export default {
props: {
value: String,
descending: Boolean
},
data() {
2022-11-09 00:10:08 +01:00
return {
showMenu: false
}
},
computed: {
selected: {
get() {
return this.value
},
2022-11-09 00:10:08 +01:00
set(val) {
this.$emit('input', val)
}
},
selectedDesc: {
get() {
return this.descending
},
2022-11-09 00:10:08 +01:00
set(val) {
this.$emit('update:descending', val)
}
2022-11-09 00:10:08 +01:00
},
2023-01-04 01:00:01 +01:00
libraryMediaType() {
return this.$store.getters['libraries/getCurrentLibraryMediaType']
},
2022-11-09 00:10:08 +01:00
isPodcast() {
2023-01-04 01:00:01 +01:00
return this.libraryMediaType === 'podcast'
},
2022-11-09 00:10:08 +01:00
podcastItems() {
return [
{
text: this.$strings.LabelTitle,
value: 'media.metadata.title'
},
{
text: this.$strings.LabelAuthor,
value: 'media.metadata.author'
},
{
text: this.$strings.LabelAddedAt,
value: 'addedAt'
},
2021-08-19 03:18:44 +02:00
{
text: this.$strings.LabelSize,
2021-08-19 03:18:44 +02:00
value: 'size'
},
{
text: this.$strings.LabelNumberOfEpisodes,
value: 'media.numTracks'
},
{
text: this.$strings.LabelFileBirthtime,
value: 'birthtimeMs'
},
{
text: this.$strings.LabelFileModified,
value: 'mtimeMs'
},
{
text: this.$strings.LabelRandomly,
value: 'random'
2021-08-19 03:18:44 +02:00
}
]
},
2022-11-09 00:10:08 +01:00
bookItems() {
return [
{
text: this.$strings.LabelTitle,
value: 'media.metadata.title'
},
{
text: this.$strings.LabelAuthorFirstLast,
2022-11-09 00:10:08 +01:00
value: 'media.metadata.authorName'
},
{
text: this.$strings.LabelAuthorLastFirst,
2022-11-09 00:10:08 +01:00
value: 'media.metadata.authorNameLF'
},
{
text: this.$strings.LabelPublishYear,
2022-11-09 00:10:08 +01:00
value: 'media.metadata.publishedYear'
},
{
text: this.$strings.LabelAddedAt,
2022-11-09 00:10:08 +01:00
value: 'addedAt'
},
{
text: this.$strings.LabelSize,
2022-11-09 00:10:08 +01:00
value: 'size'
},
{
text: this.$strings.LabelDuration,
2022-11-09 00:10:08 +01:00
value: 'media.duration'
},
{
text: this.$strings.LabelFileBirthtime,
2022-11-09 00:10:08 +01:00
value: 'birthtimeMs'
},
{
text: this.$strings.LabelFileModified,
2022-11-09 00:10:08 +01:00
value: 'mtimeMs'
},
{
text: this.$strings.LabelRandomly,
value: 'random'
2022-11-09 00:10:08 +01:00
}
]
2021-08-19 03:18:44 +02:00
},
2022-11-09 00:10:08 +01:00
seriesItems() {
return [
...this.bookItems,
{
text: this.$strings.LabelSequence,
2022-11-09 00:10:08 +01:00
value: 'sequence'
}
]
},
selectItems() {
let items = null
if (this.isPodcast) {
items = this.podcastItems
} else if (this.$store.getters['user/getUserSetting']('filterBy').startsWith('series.')) {
items = this.seriesItems
} else {
items = this.bookItems
}
2022-11-09 00:10:08 +01:00
if (!items.some((i) => i.value === this.selected)) {
this.selected = items[0].value
this.selectedDesc = !this.defaultsToAsc(items[0].value)
}
return items
},
2021-08-19 03:18:44 +02:00
selectedText() {
var _selected = this.selected
if (!_selected) return ''
if (this.selected.startsWith('book.')) _selected = _selected.replace('book.', 'media.metadata.')
var _sel = this.selectItems.find((i) => i.value === _selected)
2021-08-19 03:18:44 +02:00
if (!_sel) return ''
return _sel.text
}
},
methods: {
clickOutside() {
this.showMenu = false
},
clickedOption(val) {
if (this.selected === val) {
this.selectedDesc = !this.selectedDesc
} else {
this.selected = val
if (this.defaultsToAsc(val)) this.selectedDesc = false
2021-08-19 03:18:44 +02:00
}
this.showMenu = false
this.$nextTick(() => this.$emit('change', val))
},
defaultsToAsc(val) {
2022-11-09 00:10:08 +01:00
return val == 'media.metadata.title' || val == 'media.metadata.author' || val == 'media.metadata.authorName' || val == 'media.metadata.authorNameLF' || val == 'sequence'
2021-08-19 03:18:44 +02:00
}
}
}
</script>