Skip to content

Commit 76a03f5

Browse files
feat(search): clear will now remove the inscription reference from the query string and it won't come back. Also fixed issues with numeric parameters in the query string.
1 parent 1ace139 commit 76a03f5

File tree

2 files changed

+38
-14
lines changed

2 files changed

+38
-14
lines changed

app/assets/search.mjs

+33-11
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ createApp({
3333
annotationId: '',
3434
object: null, // ?
3535
image: null, // ?
36-
searchPhrase: '',
36+
// if null, &img in query string will be the phrase.
37+
// if '' the phrase will be empty.
38+
searchPhrase: null,
3739
dateFrom: DATE_MIN,
3840
dateTo: DATE_MAX,
3941
facets: {},
@@ -98,7 +100,7 @@ createApp({
98100
}
99101

100102
this.setSelectionFromAddressBar()
101-
this.search(true)
103+
this.search()
102104
},
103105
watch: {
104106
'selection.searchPhrase'() {
@@ -459,7 +461,7 @@ createApp({
459461
per_page: this.selection.perPage,
460462
page: this.selection.page,
461463
sort: 'or1',
462-
query: this.selection.searchPhrase,
464+
query: (this.selection.searchPhrase || '').trim(),
463465
filters: this.selection.facets
464466
}
465467
if (this.selection.dateFrom > DATE_MIN || this.selection.dateTo < DATE_MAX) {
@@ -696,7 +698,18 @@ createApp({
696698
for (let facet of Object.keys(this.selection.facets)) {
697699
searchParams[`f.${facet}`] = this.selection.facets[facet].join('|')
698700
}
699-
utils.setQueryString(searchParams)
701+
702+
let defaults = {
703+
// make sure q is always in the query string, even if ''
704+
q: 'DEFAULT',
705+
pag: 1,
706+
ppg: ITEMS_PER_PAGE,
707+
daf: DATE_MIN,
708+
dat: DATE_MAX,
709+
sup: 0,
710+
}
711+
712+
utils.setQueryString(searchParams, defaults)
700713
},
701714
setSelectionFromAddressBar() {
702715
let searchParams = new URLSearchParams(window.location.search);
@@ -705,16 +718,19 @@ createApp({
705718
this.selection.image = searchParams.get('img') || ''
706719
this.selection.showSuppliedText = searchParams.get('sup') === '1'
707720
this.selection.annotationId = searchParams.get('ann') || ''
708-
this.selection.dateFrom = searchParams.get('daf') || DATE_MIN
709-
this.selection.dateTo = searchParams.get('dat') || DATE_MAX
710-
// this.description.script = searchParams.get('scr') || ''
721+
this.selection.dateFrom = this._getNumberFromString(searchParams.get('daf'), DATE_MIN)
722+
this.selection.dateTo = this._getNumberFromString(searchParams.get('dat'), DATE_MAX)
723+
// this.description.script = searchParams.get('scr') || ''
711724

712-
this.selection.searchPhrase = searchParams.get('q') || ''
713-
if (!this.selection.searchPhrase && this.selection.image) {
725+
this.selection.searchPhrase = searchParams.get('q')
726+
if (this.selection.searchPhrase === null && this.selection.image) {
714727
this.selection.searchPhrase = this.selection.image.replace(/\.[^.]+$/, '')
728+
} else {
729+
this.selection.searchPhrase = (this.selection.searchPhrase || '').trim()
715730
}
716-
this.selection.page = parseInt(searchParams.get('pag') || '1')
717-
this.selection.perPage = parseInt(searchParams.get('ppg') || ITEMS_PER_PAGE)
731+
732+
this.selection.page = this._getNumberFromString(searchParams.get('pag'), 1)
733+
this.selection.perPage = this._getNumberFromString(searchParams.get('ppg'), ITEMS_PER_PAGE)
718734

719735
for (let facet of Object.keys(this.getFacetDefinitions())) {
720736
let options = searchParams.get(`f.${facet}`)
@@ -723,5 +739,11 @@ createApp({
723739
}
724740
}
725741
},
742+
_getNumberFromString(stringValue, defaultValue=0) {
743+
let res = parseInt(stringValue)
744+
let ret = isNaN(res) ? defaultValue : res
745+
console.log(stringValue, res, defaultValue, ret)
746+
return ret
747+
}
726748
}
727749
}).mount('#search');

app/utils.mjs

+5-3
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,17 @@ async function mod(exports) {
1818
return str.replace(/\W+/g, '-').toLowerCase()
1919
}
2020

21-
exports.setQueryString = function(parameters) {
21+
exports.setQueryString = function(parameters, defaults={}) {
2222
let newRelativePathQuery = window.location.pathname
2323
let qsKeys = Object.keys(parameters)
2424
let qs = ''
2525
if (qsKeys.length) {
2626
for (let k of qsKeys) {
27-
if (parameters[k]) {
27+
let defaultValue = defaults[k] ?? ''
28+
let valueStr = `${parameters[k]}`.trim()
29+
if (valueStr != defaultValue) {
2830
if (qs) qs += '&';
29-
qs += `${k}=${parameters[k]}`
31+
qs += `${k}=${encodeURIComponent(valueStr)}`
3032
}
3133
}
3234
if (qs) {

0 commit comments

Comments
 (0)