Handle sorting when collapsing by series and filtering by series on library page

This commit is contained in:
advplyr 2023-09-08 13:42:19 -05:00
parent 826963bf00
commit 9e13c64408
2 changed files with 50 additions and 11 deletions

View File

@ -259,6 +259,12 @@ class LibraryController {
return res.json(libraryJson)
}
/**
* GET /api/libraries/:id/items
*
* @param {import('express').Request} req
* @param {import('express').Response} res
*/
async getLibraryItems(req, res) {
const include = (req.query.include || '').split(',').map(v => v.trim().toLowerCase()).filter(v => !!v)

View File

@ -130,20 +130,53 @@ module.exports = {
payload.total = libraryItems.length
}
const sortArray = [
{
asc: (li) => li.media.metadata.getSeries(seriesId).sequence
},
{ // If no series sequence then fallback to sorting by title (or collapsed series name for sub-series)
asc: (li) => {
if (Database.serverSettings.sortingIgnorePrefix) {
return li.collapsedSeries?.nameIgnorePrefix || li.media.metadata.titleIgnorePrefix
} else {
return li.collapsedSeries?.name || li.media.metadata.title
const sortingIgnorePrefix = Database.serverSettings.sortingIgnorePrefix
let sortArray = []
const direction = payload.sortDesc ? 'desc' : 'asc'
if (!payload.sortBy || payload.sortBy === 'sequence') {
sortArray = [
{
[direction]: (li) => li.media.metadata.getSeries(seriesId).sequence
},
{ // If no series sequence then fallback to sorting by title (or collapsed series name for sub-series)
[direction]: (li) => {
if (sortingIgnorePrefix) {
return li.collapsedSeries?.nameIgnorePrefix || li.media.metadata.titleIgnorePrefix
} else {
return li.collapsedSeries?.name || li.media.metadata.title
}
}
}
]
} else {
// If series are collapsed and not sorting by title or sequence,
// sort all collapsed series to the end in alphabetical order
if (payload.sortBy !== 'media.metadata.title') {
sortArray.push({
asc: (li) => {
if (li.collapsedSeries) {
return sortingIgnorePrefix ? li.collapsedSeries.nameIgnorePrefix : li.collapsedSeries.name
} else {
return ''
}
}
})
}
]
sortArray.push({
[direction]: (li) => {
if (payload.sortBy === 'media.metadata.title') {
if (sortingIgnorePrefix) {
return li.collapsedSeries?.nameIgnorePrefix || li.media.metadata.titleIgnorePrefix
} else {
return li.collapsedSeries?.name || li.media.metadata.title
}
} else {
return payload.sortBy.split('.').reduce((a, b) => a[b], li)
}
}
})
}
libraryItems = naturalSort(libraryItems).by(sortArray)