Skip to content

Commit

Permalink
feat: Filter abbreviation by current locale
Browse files Browse the repository at this point in the history
  • Loading branch information
ptbrowne committed Mar 2, 2023
1 parent 3b73aae commit 00f1417
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 8 deletions.
18 changes: 14 additions & 4 deletions app/rdf/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,14 +355,24 @@ export const getCubeDimensionValuesWithMetadata = async ({
predicates: {
identifier:
scaleType === "Ordinal" || scaleType === "Nominal"
? schema.identifier
? {
predicate: schema.identifier,
}
: null,
position:
scaleType === "Ordinal"
? {
predicate: schema.position,
}
: null,
position: scaleType === "Ordinal" ? schema.position : null,
color:
scaleType === "Nominal" || scaleType === "Ordinal"
? schema.color
? { predicate: schema.color }
: null,
alternateName: schema.alternateName,
alternateName: {
predicate: schema.alternateName,
locale: locale,
},
},
cache,
}),
Expand Down
24 changes: 20 additions & 4 deletions app/rdf/query-literals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ import { LRUCache } from "typescript-lru-cache";
import batchLoad from "./batch-load";
import { pragmas } from "./create-source";

type PredicateOption = Record<string, NamedNode<string> | null>;
type PredicateOption = Record<
string,
null | {
predicate: NamedNode<string>;
locale?: string;
}
>;

type ResourceLiteral<T extends PredicateOption> = {
iri: NamedNode;
Expand All @@ -30,9 +36,19 @@ const buildResourceLiteralQuery = ({
}
${Object.entries(predicates)
.filter((x) => x[1])
.map(([attr, namedNode]) => {
return sparql`OPTIONAL { ?iri ${namedNode} ?${attr}. }`;
.filter((x): x is [typeof x[0], Exclude<typeof x[1], null>] => !!x[1])
.map(([attr, option]) => {
const { predicate, locale } = option;
return sparql`
OPTIONAL {
?iri ${predicate} ?${attr}.
${
locale
? sparql`FILTER (langMatches(lang(?${attr}), "${locale}") || lang(?${attr}) = "")`
: ""
}
}
`;
})}
`.prologue`${pragmas}`;

Expand Down
31 changes: 31 additions & 0 deletions e2e/abbreviations.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,34 @@ test("hierarchies: it should be possible to enable abbreviations for colors", as

expect(legendItems).toEqual(["CH4", "CO2", "N2O"]);
});

test("localized abbreviations", async ({ actions, selectors }) => {
await actions.chart.createFrom(
"https://environment.ld.admin.ch/foen/gefahren-waldbrand-praeventionsmassnahmen-kantone/1",
"Int"
);

await actions.editor.changeChartType("Map");
await actions.editor.selectActiveField("Warning region");

const checkbox = await selectors.edition.useAbbreviationsCheckbox();

await checkbox.click();

// Wait for the data to load.
await selectors.chart.loaded();
await selectors.edition.filtersLoaded();
await selectors.chart.colorLegend(undefined, { setTimeout: 5_000 });

const legendItems = await (
await selectors.chart.colorLegendItems()
).allInnerTexts();

expect(legendItems).toEqual([
"No measures",
"Warning",
"Conditional ban on fires",
"Fire ban in the forest",
"Ban on fires",
]);
});

0 comments on commit 00f1417

Please sign in to comment.