Skip to content

Commit

Permalink
Facet selection with combo box: translate labels via output function …
Browse files Browse the repository at this point in the history
…in config if configured
  • Loading branch information
wolfgangmm committed Dec 31, 2023
1 parent 0abd942 commit 6414828
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 17 deletions.
7 changes: 4 additions & 3 deletions modules/config.xqm
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,10 @@ declare variable $config:facets := [
map {
"dimension": "language",
"heading": "facets.language",
"max": 10,
"source": "api/search/facets/language",
"max": 5,
"hierarchical": false(),
"output": function($label) {
"output": function($label, $language) {
switch($label)
case "de" return "German"
case "es" return "Spanish"
Expand All @@ -157,7 +158,7 @@ declare variable $config:facets := [
"dimension": "feature",
"heading": "facets.feature",
"source": "api/search/facets/feature",
"max": 15,
"max": 5,
"hierarchical": false()
},
map {
Expand Down
26 changes: 16 additions & 10 deletions modules/facets.xql
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ declare function facets:sort($facets as map(*)?) {

declare function facets:print-table($config as map(*), $nodes as element()+, $values as xs:string*, $params as xs:string*) {
let $all := exists($config?max) and facets:get-parameter("all-" || $config?dimension)
let $lang := tokenize(facets:get-parameter("language"), '-')[1]
let $count := if ($all) then 50 else $config?max
let $facets :=
if (exists($values)) then
Expand All @@ -50,11 +51,7 @@ declare function facets:print-table($config as map(*), $nodes as element()+, $va
{
array:for-each(facets:sort($facets), function($entry) {
map:for-each($entry, function($label, $freq) {
let $content :=
if (exists($config?output)) then
$config?output($label)
else
$label
let $content := facets:translate($config, $lang, $label)
return
<tr>
<td>
Expand Down Expand Up @@ -88,6 +85,7 @@ declare function facets:print-table($config as map(*), $nodes as element()+, $va

declare function facets:display($config as map(*), $nodes as element()+) {
let $params := facets:get-parameter("facet-" || $config?dimension)
let $lang := tokenize(facets:get-parameter("language"), '-')[1]
let $table := facets:print-table($config, $nodes, (), $params)

let $maxcount := 50
Expand Down Expand Up @@ -127,11 +125,7 @@ declare function facets:display($config as map(*), $nodes as element()+) {
<select multiple="">
{
for $param in facets:get-parameter("facet-" || $config?dimension)
let $label :=
if (map:contains($config, "output")) then
$config?output($param)
else
$param
let $label := facets:translate($config, $lang, $param)
return
<option value="{$param}" data-i18n="{$label}" selected="">{$label}</option>
}
Expand All @@ -156,4 +150,16 @@ declare function facets:get-parameter($name as xs:string) {
$fromSession?($name)
else
()
};

declare function facets:translate($config as map(*)?, $language as xs:string?, $label as xs:string) {
if (exists($config) and map:contains($config, "output")) then
let $fn := $config?output
return
if (function-arity($fn) = 2) then
$fn($label, $language)
else
$fn($label)
else
$label
};
9 changes: 5 additions & 4 deletions modules/lib/api/search.xql
Original file line number Diff line number Diff line change
Expand Up @@ -125,19 +125,20 @@ declare function sapi:facets($request as map(*)) {
};

declare function sapi:list-facets($request as map(*)) {
let $value := $request?parameters?value
let $query := $request?parameters?query
let $type := $request?parameters?type
let $lang := tokenize($request?parameters?language, '-')[1]

let $facetConfig := filter($config:facets?*, function($facetCfg) {
$facetCfg?dimension = $type
})
let $hits := session:get-attribute($config:session-prefix || ".hits")
let $facets := ft:facets($hits, $type, ())

let $matches :=
for $key in if (exists($request?parameters?value)) then $request?parameters?value else map:keys($facets)
let $label := facets:translate($facetConfig, $lang, $key)
return
map {
"text": $key,
"text": $label,
"freq": $facets($key),
"value": $key
}
Expand Down

0 comments on commit 6414828

Please sign in to comment.