Change to groups and exposure of groups, add sticky header to first group item, fix shared questions

This commit is contained in:
Pieter Vander Vennet 2021-10-23 00:31:41 +02:00
parent ff0ee35ec1
commit 688b991677
8 changed files with 101 additions and 72 deletions

View file

@ -196,9 +196,15 @@ export interface LayerConfigJson {
*
* A special value is 'questions', which indicates the location of the questions box. If not specified, it'll be appended to the bottom of the featureInfobox.
*
* At last, one can define a group of renderings where parts of all strings will be replaced by multiple other strings.
* This is mainly create questions for a 'left' and a 'right' side of the road.
* These will be grouped and questions will be asked together
*/
tagRenderings?: (string | {builtin: string, override: any} | TagRenderingConfigJson | {
leftRightKeys: string[],
rewrite: {
sourceString: string,
into: string[]
}[],
renderings: (string | {builtin: string, override: any} | TagRenderingConfigJson)[]
}) [],

View file

@ -13,8 +13,8 @@ export interface TagRenderingConfigJson {
id?: string,
/**
* Optional: this can group questions together in one question box.
* Written by 'left-right'-keys automatically
* If 'group' is defined on many tagRenderings, these are grouped together when shown. The questions are grouped together as well.
* The first tagRendering of a group will always be a sticky element.
*/
group?: string
@ -173,7 +173,5 @@ export interface TagRenderingConfigJson {
* If this is important to your usecase, consider using multiple radiobutton-fields without `multiAnswer`
*/
ifnot?: AndOrTagConfigJson | string
}[]
}

View file

@ -207,7 +207,7 @@ export default class LayerConfig extends WithContextLoader {
this.tagRenderings = this.ExtractLayerTagRenderings(json)
const missingIds = json.tagRenderings?.filter(tr => typeof tr !== "string" && tr["builtin"] === undefined && tr["id"] === undefined && tr["leftRightKeys"] === undefined) ?? [];
const missingIds = json.tagRenderings?.filter(tr => typeof tr !== "string" && tr["builtin"] === undefined && tr["id"] === undefined && tr["rewrite"] === undefined) ?? [];
if (missingIds.length > 0 && official) {
console.error("Some tagRenderings of", this.id, "are missing an id:", missingIds)
@ -277,39 +277,41 @@ export default class LayerConfig extends WithContextLoader {
}
const normalTagRenderings: (string | { builtin: string, override: any } | TagRenderingConfigJson)[] = []
const leftRightRenderings: ({ leftRightKeys: string[], renderings: (string | { builtin: string, override: any } | TagRenderingConfigJson)[] })[] = []
const renderingsToRewrite: ({ rewrite:{
sourceString: string,
into: string[]
}, renderings: (string | { builtin: string, override: any } | TagRenderingConfigJson)[] })[] = []
for (let i = 0; i < json.tagRenderings.length; i++) {
const tr = json.tagRenderings[i];
const lrkDefined = tr["leftRightKeys"] !== undefined
const rewriteDefined = tr["rewrite"] !== undefined
const renderingsDefined = tr["renderings"]
if (!lrkDefined && !renderingsDefined) {
if (!rewriteDefined && !renderingsDefined) {
// @ts-ignore
normalTagRenderings.push(tr)
continue
}
if (lrkDefined && renderingsDefined) {
if (rewriteDefined && renderingsDefined) {
// @ts-ignore
leftRightRenderings.push(tr)
renderingsToRewrite.push(tr)
continue
}
throw `Error in ${this._context}.tagrenderings[${i}]: got a value which defines either \`leftRightKeys\` or \`renderings\`, but not both. Either define both or move the \`renderings\` out of this scope`
throw `Error in ${this._context}.tagrenderings[${i}]: got a value which defines either \`rewrite\` or \`renderings\`, but not both. Either define both or move the \`renderings\` out of this scope`
}
const allRenderings = this.ParseTagRenderings(normalTagRenderings, false);
if(leftRightRenderings.length === 0){
if(renderingsToRewrite.length === 0){
return allRenderings
}
const leftRenderings : TagRenderingConfig[] = []
const rightRenderings : TagRenderingConfig[] = []
function prepConfig(target:string, tr: TagRenderingConfigJson){
function prepConfig(keyToRewrite: string, target:string, tr: TagRenderingConfigJson){
function replaceRecursive(transl: string | any){
if(typeof transl === "string"){
return transl.replace("left|right", target)
return transl.replace(keyToRewrite, target)
}
if(transl.map !== undefined){
return transl.map(o => replaceRecursive(o))
@ -328,33 +330,34 @@ export default class LayerConfig extends WithContextLoader {
tr.group = target
return tr
}
for (const leftRightRendering of leftRightRenderings) {
const keysToRewrite = leftRightRendering.leftRightKeys
const tagRenderings = leftRightRendering.renderings
const left = this.ParseTagRenderings(tagRenderings, false, tr => prepConfig("left", tr))
const right = this.ParseTagRenderings(tagRenderings, false, tr => prepConfig("right", tr))
leftRenderings.push(...left)
rightRenderings.push(...right)
const rewriteGroups: Map<string, TagRenderingConfig[]> = new Map<string, TagRenderingConfig[]>()
for (const rewriteGroup of renderingsToRewrite) {
const tagRenderings = rewriteGroup.renderings
const textToReplace = rewriteGroup.rewrite.sourceString
const targets = rewriteGroup.rewrite.into
for (const target of targets) {
const parsedRenderings = this.ParseTagRenderings(tagRenderings, false, tr => prepConfig(textToReplace, target, tr))
if(!rewriteGroups.has(target)){
rewriteGroups.set(target, [])
}
rewriteGroups.get(target).push(... parsedRenderings)
}
}
leftRenderings.push(new TagRenderingConfig(<TagRenderingConfigJson>{
id: "questions",
group:"left",
}, "layerConfig.ts.leftQuestionBox"))
rightRenderings.push(new TagRenderingConfig(<TagRenderingConfigJson>{
id: "questions",
group:"right",
}, "layerConfig.ts.rightQuestionBox"))
allRenderings.push(...leftRenderings)
allRenderings.push(...rightRenderings)
rewriteGroups.forEach((group, groupName) => {
group.push(new TagRenderingConfig({
id:"questions",
group:groupName
}))
})
rewriteGroups.forEach(group => {
allRenderings.push(...group)
})
return allRenderings;

View file

@ -47,7 +47,7 @@ export default class WithContextLoader {
tagRenderings?: (string | { builtin: string, override: any } | TagRenderingConfigJson)[],
readOnly = false,
prepConfig: ((config: TagRenderingConfigJson) => TagRenderingConfigJson) = undefined
) {
) : TagRenderingConfig[]{
if (tagRenderings === undefined) {
return [];
}
@ -77,18 +77,17 @@ export default class WithContextLoader {
continue;
}
if (renderingJson["override"] !== undefined) {
let sharedJson = SharedTagRenderings.SharedTagRenderingJson.get(renderingId)
if (sharedJson === undefined) {
const keys = Array.from(SharedTagRenderings.SharedTagRenderingJson.keys());
throw `Predefined tagRendering ${renderingId} not found in ${context}.\n Try one of ${keys.join(
", "
)}\n If you intent to output this text literally, use {\"render\": <your text>} instead"}`;
}
renderingJson = Utils.Merge(renderingJson["override"], sharedJson)
let sharedJson = SharedTagRenderings.SharedTagRenderingJson.get(renderingId)
if (sharedJson === undefined) {
const keys = Array.from(SharedTagRenderings.SharedTagRenderingJson.keys());
throw `Predefined tagRendering ${renderingId} not found in ${context}.\n Try one of ${keys.join(
", "
)}\n If you intent to output this text literally, use {\"render\": <your text>} instead"}`;
}
if (renderingJson["override"] !== undefined) {
sharedJson = Utils.Merge(renderingJson["override"], sharedJson)
}
renderingJson = sharedJson
}