Better tag rendering stealing capacity

This commit is contained in:
Pieter Vander Vennet 2021-11-14 18:01:48 +01:00
parent c940890eca
commit b5693304f2
4 changed files with 104 additions and 213 deletions

View file

@ -42,6 +42,9 @@ export default class TagRenderingConfig {
}[]
constructor(json: string | TagRenderingConfigJson, context?: string) {
if (json === undefined) {
throw "Initing a TagRenderingConfig with undefined in " + context;
}
if (json === "questions") {
// Very special value
@ -55,14 +58,11 @@ export default class TagRenderingConfig {
if (typeof json === "number") {
this.render = Translations.T("" + json, context + ".render")
return;
json = ""+json
}
if (json === undefined) {
throw "Initing a TagRenderingConfig with undefined in " + context;
}
if (typeof json === "string") {
this.render = Translations.T(json, context + ".render");
this.multiAnswer = false;
@ -75,6 +75,8 @@ export default class TagRenderingConfig {
throw "Invalid ID in "+context+": an id can only contain [a-zA-Z0-0_-] as characters. The offending id is: "+this.id
}
this.group = json.group ?? "";
this.render = Translations.T(json.render, context + ".render");
this.question = Translations.T(json.question, context + ".question");

View file

@ -7,8 +7,13 @@ export default class WithContextLoader {
protected readonly _context: string;
private readonly _json: any;
public static getKnownTagRenderings : ((id: string) => TagRenderingConfigJson)= function(id) {
return SharedTagRenderings.SharedTagRenderingJson.get(id)
public static getKnownTagRenderings : ((id: string) => TagRenderingConfigJson[])= function(id) {
const found = SharedTagRenderings.SharedTagRenderingJson.get(id)
if(found !== undefined){
return [found]
}else{
return []
}
}
constructor(json: any, context: string) {
@ -64,36 +69,54 @@ export default class WithContextLoader {
}
const context = this._context
const renderings: TagRenderingConfig[] = []
options = options ?? {}
if (options.prepConfig === undefined) {
options.prepConfig = c => c
}
const preparedConfigs : TagRenderingConfigJson[] = []
for (let i = 0; i < tagRenderings.length; i++) {
let renderingJson = tagRenderings[i]
if (typeof renderingJson === "string") {
renderingJson = {builtin: renderingJson, override: undefined}
}
if (renderingJson["builtin"] !== undefined) {
const renderingId = renderingJson["builtin"]
let sharedJson = WithContextLoader.getKnownTagRenderings(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["builtin"] === undefined) {
const patchedConfig = options.prepConfig(<TagRenderingConfigJson>renderingJson)
preparedConfigs.push(patchedConfig)
continue
}
const renderingId = renderingJson["builtin"]
let sharedJsons = []
if(typeof renderingId === "string"){
sharedJsons = WithContextLoader.getKnownTagRenderings(renderingId)
}else{
sharedJsons = [].concat( ...(<string[]>renderingId).map(id => WithContextLoader.getKnownTagRenderings(id) ) )
}
if (sharedJsons.length === 0) {
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"}`;
}
for (let sharedJson of sharedJsons) {
if (renderingJson["override"] !== undefined) {
sharedJson = Utils.Merge(renderingJson["override"], JSON.parse(JSON.stringify(sharedJson)))
}
renderingJson = sharedJson
const patchedConfig = options.prepConfig(<TagRenderingConfigJson>sharedJson)
preparedConfigs.push(patchedConfig)
}
}
const patchedConfig = options.prepConfig(<TagRenderingConfigJson>renderingJson)
const tr = new TagRenderingConfig(patchedConfig, `${context}.tagrendering[${i}]`);
const renderings: TagRenderingConfig[] = []
for (let i = 0; i < preparedConfigs.length; i++){
const preparedConfig = preparedConfigs[i];
const tr = new TagRenderingConfig(preparedConfig, `${context}.tagrendering[${i}]`);
if(options.readOnlyMode && tr.question !== undefined){
throw "A question is defined for "+`${context}.tagrendering[${i}], but this is not allowed at this position - probably because this rendering is an icon, badge or label`
}