Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
geoffroy-noel-ddh committed Dec 9, 2024
2 parents eb8a172 + 76a03f5 commit e27fbd0
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 19 deletions.
64 changes: 48 additions & 16 deletions app/assets/search.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ createApp({
annotationId: '',
object: null, // ?
image: null, // ?
searchPhrase: '',
// if null, &img in query string will be the phrase.
// if '' the phrase will be empty.
searchPhrase: null,
dateFrom: DATE_MIN,
dateTo: DATE_MAX,
facets: {},
Expand Down Expand Up @@ -98,7 +100,7 @@ createApp({
}

this.setSelectionFromAddressBar()
this.search(true)
this.search()
},
watch: {
'selection.searchPhrase'() {
Expand Down Expand Up @@ -337,12 +339,22 @@ createApp({
}
if (ret) {
this.selection.items.clear()
this.unselectAllTags()
}
return ret
},
unselectAllTags() {
Object.keys(this.definitions.tags).forEach(k => {
this.definitions.tags[k] = null
})
},
resetSearch() {
this.selection.searchPhrase = ''
this.selection.facets = {}
this.selection.items.clear()
this.selection.dateFrom = DATE_MIN
this.selection.dateTo = DATE_MAX
this.unselectAllTags()
this.search()
},
resetItemsjsconfig() {
Expand Down Expand Up @@ -407,17 +419,17 @@ createApp({
tag: {
title: 'Tags',
},
cxf: {
title: 'Component x Features',
// gh-56
sort: 'key'
},
com: {
title: 'Components',
},
fea: {
title: 'Features',
},
cxf: {
title: 'Component x Features',
// gh-56
sort: 'key'
},
mat: {
title: 'Material',
},
Expand Down Expand Up @@ -449,7 +461,7 @@ createApp({
per_page: this.selection.perPage,
page: this.selection.page,
sort: 'or1',
query: this.selection.searchPhrase,
query: (this.selection.searchPhrase || '').trim(),
filters: this.selection.facets
}
if (this.selection.dateFrom > DATE_MIN || this.selection.dateTo < DATE_MAX) {
Expand Down Expand Up @@ -686,7 +698,18 @@ createApp({
for (let facet of Object.keys(this.selection.facets)) {
searchParams[`f.${facet}`] = this.selection.facets[facet].join('|')
}
utils.setQueryString(searchParams)

let defaults = {
// make sure q is always in the query string, even if ''
q: 'DEFAULT',
pag: 1,
ppg: ITEMS_PER_PAGE,
daf: DATE_MIN,
dat: DATE_MAX,
sup: 0,
}

utils.setQueryString(searchParams, defaults)
},
setSelectionFromAddressBar() {
let searchParams = new URLSearchParams(window.location.search);
Expand All @@ -695,16 +718,19 @@ createApp({
this.selection.image = searchParams.get('img') || ''
this.selection.showSuppliedText = searchParams.get('sup') === '1'
this.selection.annotationId = searchParams.get('ann') || ''
this.selection.dateFrom = searchParams.get('daf') || DATE_MIN
this.selection.dateTo = searchParams.get('dat') || DATE_MAX
// this.description.script = searchParams.get('scr') || ''
this.selection.dateFrom = this._getNumberFromString(searchParams.get('daf'), DATE_MIN)
this.selection.dateTo = this._getNumberFromString(searchParams.get('dat'), DATE_MAX)
// this.description.script = searchParams.get('scr') || ''

this.selection.searchPhrase = searchParams.get('q') || ''
if (!this.selection.searchPhrase && this.selection.image) {
this.selection.searchPhrase = searchParams.get('q')
if (this.selection.searchPhrase === null && this.selection.image) {
this.selection.searchPhrase = this.selection.image.replace(/\.[^.]+$/, '')
} else {
this.selection.searchPhrase = (this.selection.searchPhrase || '').trim()
}
this.selection.page = parseInt(searchParams.get('pag') || '1')
this.selection.perPage = parseInt(searchParams.get('ppg') || ITEMS_PER_PAGE)

this.selection.page = this._getNumberFromString(searchParams.get('pag'), 1)
this.selection.perPage = this._getNumberFromString(searchParams.get('ppg'), ITEMS_PER_PAGE)

for (let facet of Object.keys(this.getFacetDefinitions())) {
let options = searchParams.get(`f.${facet}`)
Expand All @@ -713,5 +739,11 @@ createApp({
}
}
},
_getNumberFromString(stringValue, defaultValue=0) {
let res = parseInt(stringValue)
let ret = isNaN(res) ? defaultValue : res
console.log(stringValue, res, defaultValue, ret)
return ret
}
}
}).mount('#search');
9 changes: 6 additions & 3 deletions app/utils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
export const IS_BROWSER = (typeof window !== "undefined")
export const IS_BROWSER_LOCAL = IS_BROWSER && (window.location.hostname == 'localhost')
export const DEBUG_DONT_SAVE = false;
// export const DEBUG_DONT_SAVE = true;
// export const DEBUG_DONT_SAVE = IS_BROWSER;

async function mod(exports) {
Expand All @@ -17,15 +18,17 @@ async function mod(exports) {
return str.replace(/\W+/g, '-').toLowerCase()
}

exports.setQueryString = function(parameters) {
exports.setQueryString = function(parameters, defaults={}) {
let newRelativePathQuery = window.location.pathname
let qsKeys = Object.keys(parameters)
let qs = ''
if (qsKeys.length) {
for (let k of qsKeys) {
if (parameters[k]) {
let defaultValue = defaults[k] ?? ''
let valueStr = `${parameters[k]}`.trim()
if (valueStr != defaultValue) {
if (qs) qs += '&';
qs += `${k}=${parameters[k]}`
qs += `${k}=${encodeURIComponent(valueStr)}`
}
}
if (qs) {
Expand Down

0 comments on commit e27fbd0

Please sign in to comment.