Better tag rewriting, add icons, add bicycle rental theme

This commit is contained in:
Pieter Vander Vennet 2022-01-29 02:45:59 +01:00
parent 1dcb3897e4
commit 9594868e83
23 changed files with 389 additions and 117 deletions

View file

@ -26,7 +26,6 @@ export default class TableOfContents extends Combine {
let els: { level: number, content: BaseUIElement }[] = []
for (const title of titles) {
let content: BaseUIElement
console.log("Constructing content for ", title)
if (title.title instanceof Translation) {
content = title.title.Clone()
} else if (title.title instanceof FixedUiElement) {

View file

@ -5,6 +5,8 @@ import {VariableUiElement} from "../Base/VariableUIElement";
import List from "../Base/List";
import {SubstitutedTranslation} from "../SubstitutedTranslation";
import TagRenderingConfig from "../../Models/ThemeConfig/TagRenderingConfig";
import Combine from "../Base/Combine";
import Img from "../Base/Img";
/***
* Displays the correct value for a known tagrendering
@ -38,11 +40,17 @@ export default class TagRenderingAnswer extends VariableUiElement {
return undefined;
}
const valuesToRender: BaseUIElement[] = trs.map(tr => new SubstitutedTranslation(tr, tagsSource, state, options?.specialViz))
const valuesToRender: BaseUIElement[] = trs.map(tr => {
const text = new SubstitutedTranslation(tr.then, tagsSource, state, options?.specialViz);
if(tr.icon === undefined){
return text
}
return new Combine([new Img(tr.icon).SetClass("w-6 max-h-6 pr-2"), text]).SetClass("flex")
})
if (valuesToRender.length === 1) {
return valuesToRender[0];
} else if (valuesToRender.length > 1) {
return new List(valuesToRender)
return new Combine(valuesToRender).SetClass("flex flex-col")
}
return undefined;
}).map((element: BaseUIElement) => element?.SetClass(contentClasses)?.SetStyle(contentStyle)))

View file

@ -26,6 +26,10 @@ import TagRenderingConfig from "../../Models/ThemeConfig/TagRenderingConfig";
import {Unit} from "../../Models/Unit";
import VariableInputElement from "../Input/VariableInputElement";
import Toggle from "../Input/Toggle";
import Img from "../Base/Img";
import {flattenEach, tag} from "@turf/turf";
import FeaturePipeline from "../../Logic/FeatureSource/FeaturePipeline";
import FeaturePipelineState from "../../Logic/State/FeaturePipelineState";
/**
* Shows the question element.
@ -343,7 +347,8 @@ export default class TagRenderingQuestion extends Combine {
mapping: {
if: TagsFilter,
then: Translation,
addExtraTags: Tag[]
addExtraTags: Tag[],
img?: string
}, ifNot?: TagsFilter[]): InputElement<TagsFilter> {
let tagging: TagsFilter = mapping.if;
@ -354,11 +359,23 @@ export default class TagRenderingQuestion extends Combine {
tagging = new And([tagging, ...mapping.addExtraTags])
}
return new FixedInputElement(
new SubstitutedTranslation(mapping.then, tagsSource, state),
TagRenderingQuestion.GenerateMappingContent(mapping, tagsSource, state) ,
tagging,
(t0, t1) => t1.isEquivalent(t0));
}
private static GenerateMappingContent( mapping: {
then: Translation,
icon?: string
}, tagsSource: UIEventSource<any>, state: FeaturePipelineState): BaseUIElement{
const text = new SubstitutedTranslation(mapping.then, tagsSource, state)
if (mapping.icon === undefined) {
return text;
}
return new Combine([new Img(mapping.icon).SetClass("w-6 max-h-6 pr-2"), text]).SetClass("flex")
}
private static GenerateFreeform(state, configuration: TagRenderingConfig, applicableUnit: Unit, tags: UIEventSource<any>): InputElement<TagsFilter> {
const freeform = configuration.freeform;

View file

@ -142,6 +142,33 @@ export class Translation extends BaseUIElement {
}
/**
*
* Given a translation such as `{en: "How much of bicycle_types are rented here}` (which is this translation)
* and a translation object `{ en: "electrical bikes" }`, plus the translation specification `bicycle_types`, will return
* a new translation:
* `{en: "How much electrical bikes are rented here?"}`
*
* @param translationObject
* @param stringToReplace
* @constructor
*/
public Fuse(translationObject: Translation, stringToReplace: string): Translation{
const translations = this.translations
const newTranslations = {}
for (const lang in translations) {
const target = translationObject.textFor(lang)
if(target === undefined){
continue
}
if(typeof target !== "string"){
throw "Invalid object in Translation.fuse: translationObject['"+lang+"'] is not a string, it is: "+JSON.stringify(target)
}
newTranslations[lang] = this.translations[lang].replaceAll(stringToReplace, target)
}
return new Translation(newTranslations)
}
public replace(a: string, b: string) {
if (a.startsWith("{") && a.endsWith("}")) {
a = a.substr(1, a.length - 2);

View file

@ -2,11 +2,11 @@ import {FixedUiElement} from "../Base/FixedUiElement";
import AllTranslationAssets from "../../AllTranslationAssets";
import {Translation} from "./Translation";
import BaseUIElement from "../BaseUIElement";
import * as known_languages from "../../assets/generated/used_languages.json"
export default class Translations {
static t = AllTranslationAssets.t;
private static knownLanguages = new Set(known_languages.languages)
constructor() {
throw "Translations is static. If you want to intitialize a new translation, use the singular form"
}
@ -89,4 +89,11 @@ export default class Translations {
}
static isProbablyATranslation(transl: any) {
if(typeof transl !== "object"){
return false;
}
// is a weird key found?
return !Object.keys(transl).some(key => !this.knownLanguages.has(key))
}
}