mirror of
				https://github.com/advplyr/audiobookshelf.git
				synced 2025-10-27 11:18:14 +01:00 
			
		
		
		
	Handle sorting when collapsing by series and filtering by series on library page
This commit is contained in:
		
							parent
							
								
									826963bf00
								
							
						
					
					
						commit
						9e13c64408
					
				@ -259,6 +259,12 @@ class LibraryController {
 | 
				
			|||||||
    return res.json(libraryJson)
 | 
					    return res.json(libraryJson)
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  /**
 | 
				
			||||||
 | 
					   * GET /api/libraries/:id/items
 | 
				
			||||||
 | 
					   * 
 | 
				
			||||||
 | 
					   * @param {import('express').Request} req 
 | 
				
			||||||
 | 
					   * @param {import('express').Response} res 
 | 
				
			||||||
 | 
					   */
 | 
				
			||||||
  async getLibraryItems(req, res) {
 | 
					  async getLibraryItems(req, res) {
 | 
				
			||||||
    const include = (req.query.include || '').split(',').map(v => v.trim().toLowerCase()).filter(v => !!v)
 | 
					    const include = (req.query.include || '').split(',').map(v => v.trim().toLowerCase()).filter(v => !!v)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -130,13 +130,18 @@ module.exports = {
 | 
				
			|||||||
      payload.total = libraryItems.length
 | 
					      payload.total = libraryItems.length
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const sortArray = [
 | 
					    const sortingIgnorePrefix = Database.serverSettings.sortingIgnorePrefix
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    let sortArray = []
 | 
				
			||||||
 | 
					    const direction = payload.sortDesc ? 'desc' : 'asc'
 | 
				
			||||||
 | 
					    if (!payload.sortBy || payload.sortBy === 'sequence') {
 | 
				
			||||||
 | 
					      sortArray = [
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
        asc: (li) => li.media.metadata.getSeries(seriesId).sequence
 | 
					          [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)
 | 
					        { // If no series sequence then fallback to sorting by title (or collapsed series name for sub-series)
 | 
				
			||||||
        asc: (li) => {
 | 
					          [direction]: (li) => {
 | 
				
			||||||
          if (Database.serverSettings.sortingIgnorePrefix) {
 | 
					            if (sortingIgnorePrefix) {
 | 
				
			||||||
              return li.collapsedSeries?.nameIgnorePrefix || li.media.metadata.titleIgnorePrefix
 | 
					              return li.collapsedSeries?.nameIgnorePrefix || li.media.metadata.titleIgnorePrefix
 | 
				
			||||||
            } else {
 | 
					            } else {
 | 
				
			||||||
              return li.collapsedSeries?.name || li.media.metadata.title
 | 
					              return li.collapsedSeries?.name || li.media.metadata.title
 | 
				
			||||||
@ -144,6 +149,34 @@ module.exports = {
 | 
				
			|||||||
          }
 | 
					          }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
      ]
 | 
					      ]
 | 
				
			||||||
 | 
					    } 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)
 | 
					    libraryItems = naturalSort(libraryItems).by(sortArray)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user