Search: autocomplete terms add up as labels, all their records exact (Q7)

A chosen suggestion becomes a removable label in the field; the search
starts with Enter or the button, so several terms and free text combine.
The request sends the whole phrase (titles, model) and the term IDs.
Terms are ANDed in the same filter as the countries of Q6; every record
of the chosen terms is exact, ordered by the other words then by the
model, then close results from the whole catalogue. The message names
what makes a result exact (terms, countries as typed). Wider field.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-01 11:34:25 +01:00
co-authored by Claude Opus 5.5
parent 030fe5ee86
commit 91d03b44ce
15 changed files with 403 additions and 69 deletions
@@ -2,7 +2,10 @@
<!-- One field for a phrase in natural language, one button (F1), with
suggestions of tags and thesaurus terms on the first two words (F7).
Same ids and classes as the former Drupal form, so main.scss applies.
Suggestions follow the ARIA combobox pattern (list autocomplete). -->
Suggestions follow the ARIA combobox pattern (list autocomplete).
A suggestion chosen becomes a label in the field, removable, and the
search starts with Enter or the button only (Q7): several terms add
up, and the phrase sent keeps their names next to the text typed. -->
<form
id="materio-sapi-search-form"
class="materio-sapi-search-form"
@@ -10,7 +13,18 @@
@submit.prevent="submit"
@keydown.esc="closeHelp"
>
<div class="form-item form-item-search">
<div class="form-item form-item-search" :class="{ 'has-terms': chosen.length }">
<ul v-if="chosen.length" class="search-terms" :aria-label="text.termsLabel">
<li v-for="(term, i) in chosen" :key="term.value">
<span class="search-term-label">{{ term.label }}</span>
<button
type="button"
class="search-term-remove"
:title="text.removeTerm(term.label)"
@click="removeTerm(i)"
><span aria-hidden="true">×</span><span class="visually-hidden">{{ text.removeTerm(term.label) }}</span></button>
</li>
</ul>
<input
id="edit-search"
ref="input"
@@ -24,7 +38,7 @@
:aria-controls="listId"
:aria-activedescendant="active >= 0 ? optionId(active) : null"
:maxlength="maxLength"
:placeholder="text.placeholder"
:placeholder="chosen.length ? '' : text.placeholder"
:aria-label="$t('default.Search')"
enterkeyhint="search"
autocomplete="off"
@@ -104,6 +118,8 @@ export default {
data () {
return {
typed: '',
// Terms chosen in the suggestions, { value: tid, label: name } (Q7).
chosen: [],
// Same limit as the backend (Base::MAX_PHRASE_LENGTH, H1).
maxLength: 150,
suggestions: [],
@@ -119,7 +135,8 @@ export default {
},
computed: {
...mapState({
keys: state => state.Search.keys
keys: state => state.Search.keys,
terms: state => state.Search.terms
}),
text: searchTexts
},
@@ -152,7 +169,9 @@ export default {
if (id !== this.requestId || !this.suggestible(this.typed)) {
return
}
this.suggestions = Array.isArray(data) ? data : []
// Terms already chosen are not suggested again.
const chosen = this.chosen.map(t => String(t.value))
this.suggestions = (Array.isArray(data) ? data : []).filter(s => !chosen.includes(String(s.value)))
this.active = -1
this.open = this.suggestions.length > 0
const n = this.suggestions.length
@@ -163,6 +182,12 @@ export default {
})
},
onKeydown (e) {
// Backspace at the start of the field removes the last term (Q7).
if (e.key === 'Backspace' && this.chosen.length && e.target.selectionStart === 0 && e.target.selectionEnd === 0) {
e.preventDefault()
this.removeTerm(this.chosen.length - 1)
return
}
if (!this.open) {
return
}
@@ -199,25 +224,53 @@ export default {
this.suggestions = []
this.announce = ''
},
// A suggestion searches that term alone, nothing added (F7).
// A suggestion becomes a label in place of the words typed for it; the
// search waits for Enter or the button, so that terms add up (Q7).
choose (item) {
clearTimeout(this.timer)
this.requestId++
this.close()
this.typed = item.label
this.go({ keys: item.label, terms: JSON.stringify([{ value: item.value, label: item.label }]) })
if (!this.chosen.some(t => String(t.value) === String(item.value))) {
this.chosen.push({ value: item.value, label: item.label })
}
this.typed = ''
this.$refs.input.focus()
},
removeTerm (i) {
this.chosen.splice(i, 1)
this.$refs.input.focus()
},
// The phrase: names of the terms, then the text typed. The IDs of the
// terms go along, for the exact conditions (Q7).
submit () {
clearTimeout(this.timer)
this.requestId++
this.close()
const phrase = this.typed.replace(/\s+/g, ' ').trim()
// Only the phrase, no terms.
this.go(phrase ? { keys: phrase } : {})
const phrase = [...this.chosen.map(t => t.label), this.typed].join(' ').replace(/\s+/g, ' ').trim()
if (!phrase) {
this.go({})
return
}
this.go(this.chosen.length ? { keys: phrase, terms: JSON.stringify(this.chosen) } : { keys: phrase })
},
// An example of the help runs as a typed phrase.
// Labels and text from the search of the URL or of another link: the
// text is the phrase without the names of the terms.
fromStore () {
const terms = Array.isArray(this.terms) ? this.terms.filter(t => t && t.value && t.label) : []
let phrase = this.phraseFromKeys(this.keys)
for (const t of terms) {
const i = phrase.toLowerCase().indexOf(String(t.label).toLowerCase())
if (i >= 0) {
phrase = phrase.slice(0, i) + phrase.slice(i + String(t.label).length)
}
}
this.chosen = terms.map(t => ({ value: t.value, label: t.label }))
this.typed = phrase.replace(/\s+/g, ' ').replace(/^[\s,]+|[\s,]+$/g, '')
},
// An example of the help runs as a typed phrase, without terms.
searchExample (example) {
this.helpOpen = false
this.chosen = []
this.typed = example
this.submit()
},
@@ -242,13 +295,16 @@ export default {
},
watch: {
// The store changes when a search starts from the URL or another link.
keys (n) {
this.typed = this.phraseFromKeys(n)
keys () {
this.fromStore()
},
terms () {
this.fromStore()
}
},
created () {
// Direct page load: the store already holds the keys of the URL.
this.typed = this.phraseFromKeys(this.keys)
this.fromStore()
},
mounted () {
document.addEventListener('click', this.onDocumentClick)
@@ -20,7 +20,10 @@ const TEXTS = {
],
suggestionsLabel: 'Suggestions',
suggestionsOne: '1 suggestion, flèches haut et bas pour choisir',
suggestionsMany: n => `${n} suggestions, flèches haut et bas pour choisir`
suggestionsMany: n => `${n} suggestions, flèches haut et bas pour choisir`,
// Terms chosen in the suggestions (Q7).
termsLabel: 'Termes choisis',
removeTerm: label => `Retirer ${label}`
},
en: {
placeholder: 'Describe the material you are looking for',
@@ -37,7 +40,9 @@ const TEXTS = {
],
suggestionsLabel: 'Suggestions',
suggestionsOne: '1 suggestion, use up and down arrows to choose',
suggestionsMany: n => `${n} suggestions, use up and down arrows to choose`
suggestionsMany: n => `${n} suggestions, use up and down arrows to choose`,
termsLabel: 'Chosen terms',
removeTerm: label => `Remove ${label}`
}
}