2020-10-14 12:15:09 +02:00
|
|
|
import {TagDependantUIElement, TagDependantUIElementConstructor} from "./UIElementConstructor";
|
2020-08-30 01:13:18 +02:00
|
|
|
import {TagsFilter, TagUtils} from "../Logic/Tags";
|
2020-07-31 17:38:03 +02:00
|
|
|
import {OnlyShowIfConstructor} from "./OnlyShowIf";
|
2020-08-17 17:23:15 +02:00
|
|
|
import {UIEventSource} from "../Logic/UIEventSource";
|
2020-08-22 02:12:46 +02:00
|
|
|
import Translation from "../UI/i18n/Translation";
|
2020-08-30 01:13:18 +02:00
|
|
|
import Translations from "../UI/i18n/Translations";
|
2020-07-31 17:38:03 +02:00
|
|
|
|
|
|
|
export class TagRenderingOptions implements TagDependantUIElementConstructor {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notes: by not giving a 'question', one disables the question form alltogether
|
|
|
|
*/
|
|
|
|
public options: {
|
2020-08-22 02:12:46 +02:00
|
|
|
question?: string | Translation;
|
2020-07-31 17:38:03 +02:00
|
|
|
freeform?: {
|
|
|
|
key: string;
|
|
|
|
tagsPreprocessor?: (tags: any) => any;
|
2020-08-22 02:12:46 +02:00
|
|
|
template: string | Translation;
|
|
|
|
renderTemplate: string | Translation;
|
|
|
|
placeholder?: string | Translation;
|
2020-07-31 17:38:03 +02:00
|
|
|
extraTags?: TagsFilter
|
|
|
|
};
|
2020-09-10 19:33:06 +02:00
|
|
|
multiAnswer?: boolean,
|
2020-09-17 13:13:02 +02:00
|
|
|
mappings?: { k: TagsFilter; txt: string | Translation; substitute?: boolean, hideInAnwser?: boolean }[]
|
2020-07-31 17:38:03 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
constructor(options: {
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is the string that is shown in the popup if this tag is missing.
|
|
|
|
*
|
|
|
|
* If 'question' is undefined, then the question is never asked at all
|
|
|
|
* If the question is "" (empty string) then the question is
|
|
|
|
*/
|
2020-08-22 02:12:46 +02:00
|
|
|
question?: Translation | string,
|
2020-07-31 17:38:03 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* What is the priority of the question.
|
|
|
|
* By default, in the popup of a feature, only one question is shown at the same time. If multiple questions are unanswered, the question with the highest priority is asked first
|
|
|
|
*/
|
|
|
|
priority?: number,
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mappings convert a well-known tag combination into a user friendly text.
|
|
|
|
* It converts e.g. 'access=yes' into 'this area can be accessed'
|
|
|
|
*
|
|
|
|
* If there are multiple tags that should be matched, And can be used. All tags in AND will be added when the question is picked (and the corresponding text will only be shown if all tags are present).
|
|
|
|
* If AND is used, it is best practice to make sure every used tag is in every option (with empty string) to erase extra tags.
|
|
|
|
*
|
|
|
|
* If a 'k' is null, then this one is shown by default. It can be used to force a default value, e.g. to show that the name of a POI is not (yet) known .
|
|
|
|
* A mapping where 'k' is null will not be shown as option in the radio buttons.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
2020-09-10 19:33:06 +02:00
|
|
|
mappings?: { k: TagsFilter, txt: Translation | string, priority?: number, substitute?: boolean, hideInAnswer?: boolean }[],
|
2020-07-31 17:38:03 +02:00
|
|
|
|
2020-09-10 19:33:06 +02:00
|
|
|
/**
|
|
|
|
* If true, use checkboxes to answer instead of radiobuttons
|
|
|
|
*/
|
|
|
|
multiAnswer?: boolean,
|
2020-07-31 17:38:03 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* If one wants to render a freeform tag (thus no predefined key/values) or if there are a few well-known tags with a freeform object,
|
|
|
|
* use this.
|
|
|
|
* In the question, it'll offer a textfield
|
|
|
|
*/
|
|
|
|
freeform?: {
|
|
|
|
key: string,
|
2020-08-22 02:12:46 +02:00
|
|
|
template: string | Translation,
|
|
|
|
renderTemplate: string | Translation
|
|
|
|
placeholder?: string | Translation,
|
2020-07-31 17:38:03 +02:00
|
|
|
extraTags?: TagsFilter,
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* In some very rare cases, tags have to be rewritten before displaying
|
|
|
|
* This function can be used for that.
|
|
|
|
* This function is ran on a _copy_ of the original properties
|
|
|
|
*/
|
|
|
|
tagsPreprocessor?: ((tags: any) => void)
|
|
|
|
}) {
|
|
|
|
this.options = options;
|
|
|
|
}
|
|
|
|
|
2020-08-31 02:59:47 +02:00
|
|
|
OnlyShowIf(tagsFilter: TagsFilter): TagDependantUIElementConstructor {
|
|
|
|
return new OnlyShowIfConstructor(tagsFilter, this);
|
2020-07-31 17:38:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
IsQuestioning(tags: any): boolean {
|
|
|
|
const tagsKV = TagUtils.proprtiesToKV(tags);
|
|
|
|
|
|
|
|
for (const oneOnOneElement of this.options.mappings ?? []) {
|
|
|
|
if (oneOnOneElement.k === null || oneOnOneElement.k.matches(tagsKV)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (this.options.freeform !== undefined && tags[this.options.freeform.key] !== undefined) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-08-30 01:13:18 +02:00
|
|
|
return this.options.question !== undefined;
|
2020-07-31 17:38:03 +02:00
|
|
|
}
|
|
|
|
|
2020-08-30 01:13:18 +02:00
|
|
|
GetContent(tags: any): Translation {
|
2020-08-22 02:12:46 +02:00
|
|
|
const tagsKV = TagUtils.proprtiesToKV(tags);
|
|
|
|
|
|
|
|
for (const oneOnOneElement of this.options.mappings ?? []) {
|
|
|
|
if (oneOnOneElement.k === null || oneOnOneElement.k.matches(tagsKV)) {
|
2020-08-30 01:13:18 +02:00
|
|
|
return Translations.WT(oneOnOneElement.txt);
|
2020-08-22 02:12:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (this.options.freeform !== undefined) {
|
2020-08-30 01:13:18 +02:00
|
|
|
let template = Translations.WT(this.options.freeform.renderTemplate);
|
|
|
|
return template.Subs(tags);
|
2020-08-22 02:12:46 +02:00
|
|
|
}
|
|
|
|
|
2020-09-10 19:33:06 +02:00
|
|
|
console.warn("No content defined for", tags, " with mapping", this);
|
2020-08-22 02:12:46 +02:00
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-10 19:33:06 +02:00
|
|
|
public static tagRendering: (tags: UIEventSource<any>,
|
|
|
|
options: {
|
|
|
|
priority?: number;
|
|
|
|
question?: string | Translation;
|
|
|
|
freeform?: {
|
|
|
|
key: string;
|
|
|
|
tagsPreprocessor?: (tags: any) => any;
|
|
|
|
template: string | Translation;
|
|
|
|
renderTemplate: string | Translation;
|
|
|
|
placeholder?: string | Translation; extraTags?: TagsFilter
|
|
|
|
},
|
|
|
|
multiAnswer?: boolean,
|
|
|
|
mappings?: { k: TagsFilter; txt: string | Translation; priority?: number; substitute?: boolean, hideInAnswer?: boolean }[]
|
|
|
|
}) => TagDependantUIElement;
|
2020-07-31 17:38:03 +02:00
|
|
|
|
2020-10-14 12:15:09 +02:00
|
|
|
construct(tags: UIEventSource<any>): TagDependantUIElement {
|
|
|
|
return TagRenderingOptions.tagRendering(tags, this.options);
|
2020-07-31 17:38:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
IsKnown(properties: any): boolean {
|
|
|
|
return !this.IsQuestioning(properties);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|