forked from MapComplete/MapComplete
Add the possibility to define a postfix and prefix for opening hours
This commit is contained in:
parent
9c147b6ec6
commit
a2306c2c6f
10 changed files with 281 additions and 81 deletions
|
@ -23,11 +23,39 @@ export default class OpeningHoursInput extends InputElement<string> {
|
|||
private readonly _value: UIEventSource<string>;
|
||||
private readonly _element: BaseUIElement;
|
||||
|
||||
constructor(value: UIEventSource<string> = new UIEventSource<string>("")) {
|
||||
constructor(value: UIEventSource<string> = new UIEventSource<string>(""), prefix = "", postfix = "") {
|
||||
super();
|
||||
this._value = value;
|
||||
let valueWithoutPrefix = value
|
||||
if (prefix !== "" && postfix !== "") {
|
||||
|
||||
valueWithoutPrefix = value.map(str => {
|
||||
if (str === undefined) {
|
||||
return undefined;
|
||||
}
|
||||
if(str === ""){
|
||||
return ""
|
||||
}
|
||||
if (str.startsWith(prefix) && str.endsWith(postfix)) {
|
||||
return str.substring(prefix.length, str.length - postfix.length)
|
||||
}
|
||||
return str
|
||||
}, [], noPrefix => {
|
||||
if (noPrefix === undefined) {
|
||||
return undefined;
|
||||
}
|
||||
if(noPrefix === ""){
|
||||
return ""
|
||||
}
|
||||
if (noPrefix.startsWith(prefix) && noPrefix.endsWith(postfix)) {
|
||||
return noPrefix
|
||||
}
|
||||
|
||||
const leftoverRules = value.map<string[]>(str => {
|
||||
return prefix + noPrefix + postfix
|
||||
})
|
||||
}
|
||||
|
||||
const leftoverRules = valueWithoutPrefix.map<string[]>(str => {
|
||||
if (str === undefined) {
|
||||
return []
|
||||
}
|
||||
|
@ -45,9 +73,9 @@ export default class OpeningHoursInput extends InputElement<string> {
|
|||
return leftOvers;
|
||||
})
|
||||
// Note: MUST be bound AFTER the leftover rules!
|
||||
const rulesFromOhPicker = value.map(OH.Parse);
|
||||
const rulesFromOhPicker = valueWithoutPrefix.map(OH.Parse);
|
||||
|
||||
const ph = value.map<string>(str => {
|
||||
const ph = valueWithoutPrefix.map<string>(str => {
|
||||
if (str === undefined) {
|
||||
return ""
|
||||
}
|
||||
|
@ -68,7 +96,7 @@ export default class OpeningHoursInput extends InputElement<string> {
|
|||
...leftoverRules.data,
|
||||
ph.data
|
||||
]
|
||||
value.setData(Utils.NoEmpty(rules).join(";"));
|
||||
valueWithoutPrefix.setData(Utils.NoEmpty(rules).join(";"));
|
||||
}
|
||||
|
||||
rulesFromOhPicker.addCallback(update);
|
||||
|
|
|
@ -23,10 +23,16 @@ export default class OpeningHoursVisualization extends Toggle {
|
|||
Translations.t.general.weekdays.abbreviations.sunday,
|
||||
]
|
||||
|
||||
constructor(tags: UIEventSource<any>, key: string) {
|
||||
constructor(tags: UIEventSource<any>, key: string, prefix = "", postfix = "") {
|
||||
const tagsDirect = tags.data;
|
||||
const ohTable = new VariableUiElement(tags
|
||||
.map(tags => tags[key]) // This mapping will absorb all other changes to tags in order to prevent regeneration
|
||||
.map(tags => {
|
||||
const value : string = tags[key];
|
||||
if(value.startsWith(prefix) && value.endsWith(postfix)){
|
||||
return value.substring(prefix.length, value.length - postfix.length)
|
||||
}
|
||||
return value;
|
||||
}) // This mapping will absorb all other changes to tags in order to prevent regeneration
|
||||
.map(ohtext => {
|
||||
try {
|
||||
// noinspection JSPotentiallyInvalidConstructorUsage
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue