Balance search pages to have view mode-aware max search results (#858)

* Balance search pages to have view mode-aware max search results

* Remove console.log

* Implement #829 to prevent invalid pages when changing the max results

* dammit
This commit is contained in:
Prospector
2023-01-11 12:49:44 -08:00
committed by GitHub
parent 7536322e53
commit a6aa78e3b6

View File

@@ -307,12 +307,14 @@
v-model="maxResults" v-model="maxResults"
placeholder="Select one" placeholder="Select one"
class="labeled-control__control" class="labeled-control__control"
:options="[5, 10, 15, 20, 50, 100]" :options="
maxResultsForView[$cosmetics.searchDisplayMode[projectType.id]]
"
:searchable="false" :searchable="false"
:close-on-select="true" :close-on-select="true"
:show-labels="false" :show-labels="false"
:allow-empty="false" :allow-empty="false"
@input="onSearchChange(currentPage)" @input="onMaxResultsChange(currentPage)"
/> />
</div> </div>
<button <button
@@ -472,6 +474,13 @@ export default {
sortType: { display: 'Relevance', name: 'relevance' }, sortType: { display: 'Relevance', name: 'relevance' },
maxResults: 20, maxResults: 20,
previousMaxResults: 20,
maxResultsForView: {
list: [5, 10, 15, 20, 50, 100],
grid: [6, 12, 18, 24, 48, 96],
gallery: [6, 10, 16, 20, 50, 100],
},
sidebarMenuOpen: false, sidebarMenuOpen: false,
showAllLoaders: false, showAllLoaders: false,
@@ -511,7 +520,7 @@ export default {
this.sortType.display = 'Downloads' this.sortType.display = 'Downloads'
break break
case 'newest': case 'newest':
this.sortType.display = 'Recently created' this.sortType.display = 'Recently published'
break break
case 'updated': case 'updated':
this.sortType.display = 'Recently updated' this.sortType.display = 'Recently updated'
@@ -521,11 +530,13 @@ export default {
break break
} }
} }
if (this.$route.query.m) { if (this.$route.query.m) {
this.maxResults = this.$route.query.m this.maxResults = this.$route.query.m
} }
if (this.$route.query.o) if (this.$route.query.o) {
this.currentPage = Math.ceil(this.$route.query.o / this.maxResults) + 1 this.currentPage = Math.ceil(this.$route.query.o / this.maxResults) + 1
}
this.projectType = this.$tag.projectTypes.find( this.projectType = this.$tag.projectTypes.find(
(x) => x.id === this.$route.name.substring(0, this.$route.name.length - 1) (x) => x.id === this.$route.name.substring(0, this.$route.name.length - 1)
@@ -600,6 +611,8 @@ export default {
await this.clearFilters() await this.clearFilters()
this.isLoading = false this.isLoading = false
this.setClosestMaxResults()
}, },
}, },
}, },
@@ -679,6 +692,19 @@ export default {
await this.onSearchChange(newPageNumber) await this.onSearchChange(newPageNumber)
}, },
async onMaxResultsChange(newPageNumber) {
newPageNumber = Math.max(
1,
Math.min(
Math.floor(
newPageNumber / (this.maxResults / this.previousMaxResults)
),
this.pageCount
)
)
this.previousMaxResults = this.maxResults
await this.onSearchChange(newPageNumber)
},
async onSearchChange(newPageNumber) { async onSearchChange(newPageNumber) {
this.currentPage = newPageNumber this.currentPage = newPageNumber
@@ -841,6 +867,19 @@ export default {
mode: newValue, mode: newValue,
$cookies: this.$cookies, $cookies: this.$cookies,
}) })
this.setClosestMaxResults()
},
setClosestMaxResults() {
const view = this.$cosmetics.searchDisplayMode[this.projectType.id]
const maxResultsOptions = this.maxResultsForView[view] ?? [20]
const currentMax = this.maxResults
if (!maxResultsOptions.includes(currentMax)) {
this.maxResults = maxResultsOptions.reduce(function (prev, curr) {
return Math.abs(curr - currentMax) <= Math.abs(prev - currentMax)
? curr
: prev
})
}
}, },
}, },
} }