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:
@@ -62,6 +62,13 @@ function materio_sapi_search_messages_strings(): array {
|
||||
=> '1 résultat exact pour « @keys »',
|
||||
$plural('@n exact result out of @total relevant results for “@keys”', '@n exact results out of @total relevant results for “@keys”')
|
||||
=> $plural('@n résultat exact sur @total résultats pertinents pour « @keys »', '@n résultats exacts sur @total résultats pertinents pour « @keys »'),
|
||||
// What makes a result exact, named (Q6, Q7).
|
||||
$plural('No exact result for @filters, @n close result for “@keys”', 'No exact result for @filters, @n close results for “@keys”')
|
||||
=> $plural('Aucun résultat exact pour @filters, @n résultat proche pour « @keys »', 'Aucun résultat exact pour @filters, @n résultats proches pour « @keys »'),
|
||||
'1 exact result for @filters: “@keys”'
|
||||
=> '1 résultat exact pour @filters : « @keys »',
|
||||
$plural('@n exact result for @filters, out of @total relevant results for “@keys”', '@n exact results for @filters, out of @total relevant results for “@keys”')
|
||||
=> $plural('@n résultat exact pour @filters, sur @total résultats pertinents pour « @keys »', '@n résultats exacts pour @filters, sur @total résultats pertinents pour « @keys »'),
|
||||
// Former text, French corrected ("le champs").
|
||||
'Please use the search form to search from our @count materials.'
|
||||
=> 'Utilisez le champ de recherche pour naviguer dans nos @count matériaux référencés.',
|
||||
@@ -93,3 +100,10 @@ function materio_sapi_update_11002(): string {
|
||||
_locale_refresh_translations(['fr'], $lids);
|
||||
return 'French translation of the search messages added.';
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the French translation of the messages naming the filters (Q6, Q7).
|
||||
*/
|
||||
function materio_sapi_update_11003(): string {
|
||||
return materio_sapi_update_11002();
|
||||
}
|
||||
|
||||
@@ -111,7 +111,7 @@ class Base extends ControllerBase {
|
||||
*
|
||||
* Off on trial since 17/11 (H6): on a phrase it matched about 40 % of the
|
||||
* catalogue. Kept: it still runs when there are no neighbours (model
|
||||
* stopped, slow or switched off, term chosen in the autocomplete), so that
|
||||
* stopped, slow or switched off), so that
|
||||
* the search answers as before.
|
||||
*/
|
||||
public const OR_WITH_KNN = FALSE;
|
||||
@@ -138,10 +138,31 @@ class Base extends ControllerBase {
|
||||
private ?array $countryTermIds = NULL;
|
||||
|
||||
/**
|
||||
* Whether the queries are restricted to the countries found (Q6).
|
||||
* Whether the queries are restricted to the countries found (Q6) and to the
|
||||
* terms chosen in the autocomplete (Q7).
|
||||
*/
|
||||
private bool $countryFilter = FALSE;
|
||||
|
||||
/**
|
||||
* Terms chosen in the autocomplete, as IDs (Q7).
|
||||
*
|
||||
* @var int[]
|
||||
*/
|
||||
private array $termIds = [];
|
||||
|
||||
/**
|
||||
* What makes a result exact, for the message: names of the chosen terms,
|
||||
* countries as typed (Q6, Q7).
|
||||
*
|
||||
* @var string[]
|
||||
*/
|
||||
private array $filterNames = [];
|
||||
|
||||
/**
|
||||
* Most terms taken from a request (Q7).
|
||||
*/
|
||||
public const MAX_TERMS = 10;
|
||||
|
||||
/**
|
||||
* The reference query, run before the others.
|
||||
*
|
||||
@@ -180,11 +201,11 @@ class Base extends ControllerBase {
|
||||
]);
|
||||
$cache = \Drupal::cache('materio_sapi');
|
||||
if ($cached = $cache->get($cid)) {
|
||||
[$this->results, $this->exactematch_count, $this->knn_count] = $cached->data;
|
||||
[$this->results, $this->exactematch_count, $this->knn_count, $this->filterNames] = $cached->data + [3 => []];
|
||||
return;
|
||||
}
|
||||
$this->sapiQuery();
|
||||
$cache->set($cid, [$this->results, $this->exactematch_count, $this->knn_count], \Drupal::time()->getRequestTime() + self::RANKING_LIFETIME, ['node_list']);
|
||||
$cache->set($cid, [$this->results, $this->exactematch_count, $this->knn_count, $this->filterNames], \Drupal::time()->getRequestTime() + self::RANKING_LIFETIME, ['node_list']);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -213,11 +234,17 @@ class Base extends ControllerBase {
|
||||
// Only stop words: search the phrase as typed, as before.
|
||||
$keys = $words ? implode(' ', $words) : implode(' ', $this->keys);
|
||||
|
||||
// A country named in the phrase is a condition, not a keyword (Q6):
|
||||
// « pigment Italie » searches « pigment » among the records of Italian
|
||||
// companies or tagged Italie. Not for a term chosen in the autocomplete.
|
||||
$this->countries = $this->terms ? $this->countries : CountryDetector::detect($text);
|
||||
$this->countryFilter = (bool) $this->countries['codes'];
|
||||
// Terms chosen in the autocomplete (Q7) and a country named in the
|
||||
// phrase (Q6) are conditions, not keywords: « pigment Italie » searches
|
||||
// « pigment » among the records of Italian companies or tagged Italie;
|
||||
// [céramique] [translucide] japon searches « japon » among the records
|
||||
// that have both terms. The phrase keeps the names of the terms for the
|
||||
// titles and the model; the conditions use their IDs.
|
||||
$this->termIds = $this->chosenTermIds();
|
||||
$free = $this->termIds ? $this->withoutTermNames($text, $lang) : $text;
|
||||
$this->countries = CountryDetector::detect($free);
|
||||
$this->countryFilter = $this->termIds || $this->countries['codes'];
|
||||
$this->filterNames = [...$this->termNames($lang), ...$this->countries['forms']];
|
||||
$this->knn_count = 0;
|
||||
if (!$this->countryFilter) {
|
||||
[$nids, $uuids, $this->exactematch_count] = $this->rank($keys, $words, $lang, $exact, TRUE);
|
||||
@@ -225,28 +252,44 @@ class Base extends ControllerBase {
|
||||
else {
|
||||
$rest = KeywordText::words($this->countries['rest'], $lang);
|
||||
if (!$rest) {
|
||||
// The country alone (« italien »): all its records, no model.
|
||||
// The terms or the country alone (« italien »): all their records.
|
||||
$exact += $this->countryQuery($lang);
|
||||
$this->exactematch_count = count($exact);
|
||||
$this->results = ['uuids' => array_column($exact, 1), 'nids' => array_column($exact, 0)];
|
||||
return;
|
||||
$nids = array_keys($exact);
|
||||
$uuids = array_column($exact, 1, 0);
|
||||
}
|
||||
else {
|
||||
if (count($words) <= self::SHORT_QUERY_WORDS) {
|
||||
// A title may hold the country (« papier japonais »): whole phrase.
|
||||
$exact += $this->phraseQuery($keys, $lang);
|
||||
}
|
||||
$keys = implode(' ', $rest);
|
||||
// The country first: its exact matches, then its neighbours…
|
||||
[$nids, $uuids, $this->exactematch_count] = $this->rank($keys, $rest, $lang, $exact, FALSE);
|
||||
// …then the same search everywhere, as without a country, so that the
|
||||
// list does not stop after a handful of records (51 Portuguese records
|
||||
// in all, « céramique Portugal » gave 2 results, 30/11). Counted as
|
||||
// close results: only the records of the country are exact.
|
||||
if ($this->termIds) {
|
||||
// Terms chosen are a filter asked for (Q7): all their records are
|
||||
// exact, the rest of the phrase only orders them. « bois-résiné
|
||||
// qui sent bon » gave no exact match and 2 of the 31 records of
|
||||
// the term on top, the others scattered or cut (01/12).
|
||||
[$nids, $uuids, $this->exactematch_count] = $this->termRecords(implode(' ', $rest), $rest, $lang, $exact);
|
||||
}
|
||||
else {
|
||||
// The records of the country first: their exact matches on the
|
||||
// rest of the phrase, then their neighbours…
|
||||
[$nids, $uuids, $this->exactematch_count] = $this->rank(implode(' ', $rest), $rest, $lang, $exact, FALSE);
|
||||
}
|
||||
}
|
||||
// …then the same search everywhere, so that the list does not stop
|
||||
// after a handful of records (51 Portuguese records in all,
|
||||
// « céramique Portugal » gave 2 results, 30/11; 13 records have both
|
||||
// céramique and translucide, 01/12). Counted as close results: only
|
||||
// the records of the terms and the country are exact. The names of the
|
||||
// terms count again here, as words; nothing more for a country alone.
|
||||
$this->countryFilter = FALSE;
|
||||
[$more, $more_uuids] = $this->rank($keys, $rest, $lang, [], TRUE);
|
||||
$wide = KeywordText::words($this->countries['codes'] ? CountryDetector::detect($text)['rest'] : $text, $lang);
|
||||
if ($wide) {
|
||||
[$more, $more_uuids] = $this->rank(implode(' ', $wide), $wide, $lang, [], TRUE);
|
||||
$nids = array_merge($nids, array_values(array_diff($more, $nids)));
|
||||
$uuids += $more_uuids;
|
||||
}
|
||||
}
|
||||
|
||||
$this->results = [
|
||||
'uuids' => array_map(static fn ($nid) => $uuids[$nid], $nids),
|
||||
@@ -289,11 +332,47 @@ class Base extends ControllerBase {
|
||||
return [$nids, $uuids, count($exact)];
|
||||
}
|
||||
|
||||
/**
|
||||
* Every record of the chosen terms, in order (Q7).
|
||||
*
|
||||
* First the exact matches already found and the records whose tags hold
|
||||
* every other word, then the others by closeness to the phrase (no
|
||||
* ScoreCut: they are all kept), then those the model did not score.
|
||||
*
|
||||
* @param string $keys
|
||||
* Rest of the phrase, without stop words.
|
||||
* @param string[] $words
|
||||
* The same, as a list.
|
||||
* @param array<int, array{0: mixed, 1: string}> $exact
|
||||
* Exact matches already found (references, titles).
|
||||
*
|
||||
* @return array{0: array<int|string>, 1: array<int, string>, 2: int}
|
||||
* Node IDs in order, their UUIDs keyed by nid, number of exact matches.
|
||||
*/
|
||||
private function termRecords(string $keys, array $words, string $lang, array $exact): array {
|
||||
if (count($words) <= self::SHORT_QUERY_WORDS) {
|
||||
$exact += $this->andQuery($keys, $lang);
|
||||
}
|
||||
$all = $this->countryQuery($lang);
|
||||
// A title holding the phrase counts only with the terms.
|
||||
$exact = array_intersect_key($exact, $all);
|
||||
$neighbours = $this->neighbours($lang) ?? [];
|
||||
$this->knn_count += count($neighbours);
|
||||
$nids = array_keys($exact);
|
||||
foreach (array_keys($neighbours) as $nid) {
|
||||
if (isset($all[$nid])) {
|
||||
$nids[] = $nid;
|
||||
}
|
||||
}
|
||||
$nids = array_values(array_unique(array_merge($nids, array_keys($all))));
|
||||
$uuids = array_column($exact + $all, 1, 0);
|
||||
return [$nids, $uuids, count($nids)];
|
||||
}
|
||||
|
||||
/**
|
||||
* Nodes closest in meaning to the phrase (materio_ai, Solr kNN).
|
||||
*
|
||||
* Not for a term chosen in the autocomplete (direct search) nor without
|
||||
* the materio_ai service. On a model or Solr error, past the delay of
|
||||
* Not without the materio_ai service. On a model or Solr error, past the delay of
|
||||
* QueryEmbedder (1 s), or with materio_ai switched off, the keyword
|
||||
* results stand alone, with no message: the search answers as before H5.
|
||||
*
|
||||
@@ -302,12 +381,14 @@ class Base extends ControllerBase {
|
||||
* are no neighbours to use.
|
||||
*/
|
||||
private function neighbours(string $lang): ?array {
|
||||
if ($this->terms || !\Drupal::hasService('materio_ai.neighbour_search')) {
|
||||
if (!\Drupal::hasService('materio_ai.neighbour_search')) {
|
||||
return NULL;
|
||||
}
|
||||
// References are keywords: they mean nothing to the model. Neither do
|
||||
// countries (« italien » brought Milano, Veneto): they filter instead (Q6).
|
||||
$phrase = $this->countries['codes'] ? $this->countries['rest'] : $this->phrase;
|
||||
// The names of the chosen terms stay (Q7): « céramique translucide » means
|
||||
// more to the model than « translucide ».
|
||||
$phrase = $this->countries['codes'] ? CountryDetector::detect($this->phrase)['rest'] : $this->phrase;
|
||||
$phrase = trim(preg_replace(self::REFERENCE_PATTERN, ' ', $phrase) ?? '');
|
||||
if ($phrase === '') {
|
||||
return NULL;
|
||||
@@ -395,17 +476,6 @@ class Base extends ControllerBase {
|
||||
private function andQuery(string $keys, string $lang): array {
|
||||
$this->and_query = $this->keywordQuery('terms', 'AND', $keys, $lang, 'materio_sapi_search_and_query');
|
||||
|
||||
// A term chosen in the autocomplete restricts to the taxonomy fields.
|
||||
if ($this->terms && is_array($this->terms) && count($this->terms)) {
|
||||
$term_conditions = $this->and_query->createConditionGroup('OR');
|
||||
foreach ($this->terms as $term) {
|
||||
foreach (['tag_tid', 'thesaurus_tid'] as $field) {
|
||||
$term_conditions->addCondition($field, (int) $term->value);
|
||||
}
|
||||
}
|
||||
$this->and_query->addConditionGroup($term_conditions);
|
||||
}
|
||||
|
||||
// Thesaurus and tag fields only.
|
||||
$this->and_query->setFulltextFields([
|
||||
'thesaurus_name_0',
|
||||
@@ -428,7 +498,7 @@ class Base extends ControllerBase {
|
||||
}
|
||||
|
||||
/**
|
||||
* Every record of the countries named in the phrase (Q6).
|
||||
* Every record of the chosen terms and of the countries named (Q6, Q7).
|
||||
*
|
||||
* @return array<int, array{0: mixed, 1: string}>
|
||||
* [nid, uuid] keyed by nid, in result order.
|
||||
@@ -450,8 +520,19 @@ class Base extends ControllerBase {
|
||||
* The query, not executed yet.
|
||||
*/
|
||||
private function addCountryCondition($query): void {
|
||||
if (!$this->countryFilter) {
|
||||
return;
|
||||
}
|
||||
// Every chosen term, as a tag or a thesaurus term (Q7): AND between the
|
||||
// terms, so that choosing a second one narrows the list.
|
||||
foreach ($this->termIds as $tid) {
|
||||
$group = $query->createConditionGroup('OR');
|
||||
$group->addCondition('tag_tid', $tid);
|
||||
$group->addCondition('thesaurus_tid', $tid);
|
||||
$query->addConditionGroup($group);
|
||||
}
|
||||
$codes = $this->countries['codes'];
|
||||
if (!$this->countryFilter || !$codes) {
|
||||
if (!$codes) {
|
||||
return;
|
||||
}
|
||||
$group = $query->createConditionGroup('OR');
|
||||
@@ -464,6 +545,63 @@ class Base extends ControllerBase {
|
||||
$query->addConditionGroup($group);
|
||||
}
|
||||
|
||||
/**
|
||||
* IDs of the terms chosen in the autocomplete (Q7).
|
||||
*
|
||||
* The request sends [{"value": tid, "label": name}, …]; only the IDs are
|
||||
* kept, the names come from the terms themselves.
|
||||
*
|
||||
* @return int[]
|
||||
* Term IDs, at most MAX_TERMS.
|
||||
*/
|
||||
private function chosenTermIds(): array {
|
||||
$ids = [];
|
||||
foreach (is_array($this->terms) ? $this->terms : [] as $term) {
|
||||
$tid = is_object($term) ? ($term->value ?? NULL) : $term;
|
||||
if (is_numeric($tid) && (int) $tid > 0) {
|
||||
$ids[(int) $tid] = (int) $tid;
|
||||
}
|
||||
}
|
||||
return array_slice(array_values($ids), 0, self::MAX_TERMS);
|
||||
}
|
||||
|
||||
/**
|
||||
* Names of the chosen terms in the language, in the order chosen (Q7).
|
||||
*
|
||||
* @return string[]
|
||||
* Names.
|
||||
*/
|
||||
private function termNames(string $lang): array {
|
||||
$terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadMultiple($this->termIds);
|
||||
$names = [];
|
||||
foreach ($this->termIds as $tid) {
|
||||
if (isset($terms[$tid])) {
|
||||
$term = $terms[$tid];
|
||||
$names[] = $term->hasTranslation($lang) ? $term->getTranslation($lang)->label() : $term->label();
|
||||
}
|
||||
}
|
||||
return $names;
|
||||
}
|
||||
|
||||
/**
|
||||
* The phrase without the names of the chosen terms (Q7).
|
||||
*
|
||||
* What is left is searched among the records of the terms.
|
||||
*/
|
||||
private function withoutTermNames(string $text, string $lang): string {
|
||||
$names = $this->termNames($lang);
|
||||
foreach (\Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadMultiple($this->termIds) as $term) {
|
||||
$names[] = $term->label();
|
||||
}
|
||||
// Longest first (« bois flotté » before « bois »).
|
||||
$names = array_unique(array_filter($names));
|
||||
usort($names, static fn ($a, $b): int => mb_strlen($b) <=> mb_strlen($a));
|
||||
foreach ($names as $name) {
|
||||
$text = preg_replace('/(?<![\p{L}\p{N}])' . preg_quote($name, '/') . '(?![\p{L}\p{N}])/iu', ' ', $text) ?? $text;
|
||||
}
|
||||
return trim(preg_replace('/\s+/u', ' ', $text) ?? $text);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tags and thesaurus terms named like the countries found.
|
||||
*
|
||||
@@ -624,7 +762,7 @@ class Base extends ControllerBase {
|
||||
// count no longer depends on an arbitrary limit. Plurals and "close"
|
||||
// results without any exact match (F4).
|
||||
$resp['infos'] = (new SearchMessage($this->getStringTranslation()))
|
||||
->results($resp['exactematch_count'], $resp['count'], $this->phrase, $lang);
|
||||
->results($resp['exactematch_count'], $resp['count'], $this->phrase, $lang, $this->filterNames);
|
||||
// $resp['options'] = $this->query->getOptions();
|
||||
|
||||
// $uuids = [];
|
||||
|
||||
@@ -27,8 +27,12 @@ final class SearchMessage {
|
||||
|
||||
/**
|
||||
* Message of a search: exact matches out of all the results shown.
|
||||
*
|
||||
* @param string[] $filters
|
||||
* What makes a result exact, when it is not every word: terms chosen in
|
||||
* the autocomplete (Q7), countries as typed (Q6). Named in the message.
|
||||
*/
|
||||
public function results(int $exact, int $total, string $keys, string $lang): TranslatableMarkup {
|
||||
public function results(int $exact, int $total, string $keys, string $lang, array $filters = []): TranslatableMarkup {
|
||||
$args = [
|
||||
'@n' => self::number($exact, $lang),
|
||||
'@total' => self::number($total, $lang),
|
||||
@@ -37,6 +41,19 @@ final class SearchMessage {
|
||||
if ($total === 0) {
|
||||
return $this->translation->translate('No results for “@keys”. Try other words or a broader description.', $args);
|
||||
}
|
||||
$named = self::listing($filters, $lang);
|
||||
// « italien » alone: naming it twice says nothing more.
|
||||
if ($named !== '' && mb_strtolower($named) !== mb_strtolower($keys)) {
|
||||
$args['@filters'] = $named;
|
||||
if ($exact === 0) {
|
||||
$args['@n'] = $args['@total'];
|
||||
return $this->translation->formatPlural($total, 'No exact result for @filters, @n close result for “@keys”', 'No exact result for @filters, @n close results for “@keys”', $args);
|
||||
}
|
||||
if ($total === 1) {
|
||||
return $this->translation->translate('1 exact result for @filters: “@keys”', $args);
|
||||
}
|
||||
return $this->translation->formatPlural($exact, '@n exact result for @filters, out of @total relevant results for “@keys”', '@n exact results for @filters, out of @total relevant results for “@keys”', $args);
|
||||
}
|
||||
if ($exact === 0) {
|
||||
$args['@n'] = $args['@total'];
|
||||
return $this->translation->formatPlural($total, 'No exact result, @n close result for “@keys”', 'No exact result, @n close results for “@keys”', $args);
|
||||
@@ -59,6 +76,21 @@ final class SearchMessage {
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* « a », « a et b », « a, b et c » (English: and).
|
||||
*
|
||||
* @param string[] $items
|
||||
* Names.
|
||||
*/
|
||||
public static function listing(array $items, string $lang): string {
|
||||
$items = array_values(array_filter(array_map('trim', $items), 'strlen'));
|
||||
if (count($items) < 2) {
|
||||
return $items[0] ?? '';
|
||||
}
|
||||
$last = array_pop($items);
|
||||
return implode(', ', $items) . ($lang === 'fr' ? ' et ' : ' and ') . $last;
|
||||
}
|
||||
|
||||
/**
|
||||
* A number with the thousands separator of the language.
|
||||
*
|
||||
|
||||
@@ -41,6 +41,37 @@ final class SearchMessageTest extends UnitTestCase {
|
||||
$this->assertSame($expected, (string) $message->results($exact, $total, 'bois', 'en'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Cases with a filter: what makes a result exact is named (Q6, Q7).
|
||||
*/
|
||||
public static function filtersProvider(): array {
|
||||
return [
|
||||
'one term' => [30, 252, 'bois-résiné qui sent bon', ['bois-résiné'], '30 exact results for bois-résiné, out of 252 relevant results for “bois-résiné qui sent bon”'],
|
||||
'terms and country' => [3, 291, 'céramique translucide japon', ['céramique', 'translucide', 'japon'], '3 exact results for céramique, translucide and japon, out of 291 relevant results for “céramique translucide japon”'],
|
||||
'none exact' => [0, 40, 'bois-résiné bleu', ['bois-résiné'], 'No exact result for bois-résiné, 40 close results for “bois-résiné bleu”'],
|
||||
'one alone' => [1, 1, 'tissu Islande', ['Islande'], '1 exact result for Islande: “tissu Islande”'],
|
||||
'country alone' => [586, 586, 'italien', ['italien'], '586 exact results out of 586 relevant results for “italien”'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* The filters are named, unless they are the whole phrase.
|
||||
*/
|
||||
#[DataProvider('filtersProvider')]
|
||||
public function testFilters(int $exact, int $total, string $keys, array $filters, string $expected): void {
|
||||
$message = new SearchMessage($this->getStringTranslationStub());
|
||||
$this->assertSame($expected, (string) $message->results($exact, $total, $keys, 'en', $filters));
|
||||
}
|
||||
|
||||
/**
|
||||
* French list: « a, b et c ».
|
||||
*/
|
||||
public function testListing(): void {
|
||||
$this->assertSame('céramique, translucide et Japon', SearchMessage::listing(['céramique', 'translucide', 'Japon'], 'fr'));
|
||||
$this->assertSame('a and b', SearchMessage::listing(['a', 'b'], 'en'));
|
||||
$this->assertSame('', SearchMessage::listing([], 'fr'));
|
||||
}
|
||||
|
||||
/**
|
||||
* The help before any search gives the size of the catalogue.
|
||||
*/
|
||||
|
||||
+1
-1
File diff suppressed because one or more lines are too long
Binary file not shown.
+1
-1
File diff suppressed because one or more lines are too long
Binary file not shown.
+2
-2
File diff suppressed because one or more lines are too long
Binary file not shown.
+1
-1
File diff suppressed because one or more lines are too long
Binary file not shown.
@@ -520,9 +520,10 @@ header[role="banner"]{
|
||||
#block-materiosapisearchblock{
|
||||
align-self: flex-end;
|
||||
flex-shrink: 0;
|
||||
// Long enough for a phrase and for the placeholder (F4).
|
||||
width: 30em;
|
||||
max-width: 50%;
|
||||
// Long enough for a phrase and for the placeholder (F4), and for a
|
||||
// few chosen terms as labels next to the text (Q7).
|
||||
width: 40em;
|
||||
max-width: 60%;
|
||||
}
|
||||
// Tablet and mobile: the field on its own line, full width (F5).
|
||||
@include col-mediaquery-max(4){
|
||||
@@ -654,6 +655,32 @@ header[role="banner"]{
|
||||
width: 100%;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
// Terms chosen in the suggestions, as labels in the field (Q7):
|
||||
// the border goes to the whole line, labels then text.
|
||||
&.has-terms{
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
align-items: center;
|
||||
gap: 0.2em;
|
||||
border: 1px #BBB solid;
|
||||
border-radius: 14px;
|
||||
padding: 0.15em 0.3em;
|
||||
box-sizing: border-box;
|
||||
background-color: #fff;
|
||||
&:focus-within{
|
||||
outline: 2px solid #666;
|
||||
outline-offset: 1px;
|
||||
}
|
||||
#edit-search{
|
||||
flex: 1 1 6em;
|
||||
width: auto;
|
||||
min-width: 6em;
|
||||
border: 0;
|
||||
border-radius: 0;
|
||||
padding: 0.15em;
|
||||
outline: none;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.button.form-submit{
|
||||
@@ -744,6 +771,37 @@ header[role="banner"]{
|
||||
}
|
||||
|
||||
|
||||
#materio-sapi-search-form ul.search-terms {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 0.2em;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
list-style: none;
|
||||
li {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
border-radius: 10px;
|
||||
padding: 0 0.1em 0 0.5em;
|
||||
background-color: #333;
|
||||
color: #fff;
|
||||
font-size: 0.85em;
|
||||
line-height: 1.5;
|
||||
}
|
||||
.search-term-remove {
|
||||
border: 0;
|
||||
padding: 0 0.3em;
|
||||
background: transparent;
|
||||
color: #fff;
|
||||
font-size: 1.1em;
|
||||
line-height: 1;
|
||||
cursor: pointer;
|
||||
&:hover, &:focus-visible {
|
||||
color: #ccc;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Suggestions of the search field (SearchForm.vue, F7), same look as the
|
||||
// former jQuery UI autocomplete.
|
||||
#materio-sapi-search-form ul.search-suggestions {
|
||||
|
||||
@@ -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}`
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user