Fix instabilities with complicated opening hours, fix #902

This commit is contained in:
pietervdvn 2022-06-29 17:27:23 +02:00
parent f99685c47d
commit b5c48f65b0
3 changed files with 26 additions and 9 deletions

View file

@ -38,7 +38,8 @@ export default class OpeningHoursInput extends InputElement<string> {
return str.substring(prefix.length, str.length - postfix.length)
}
return str
}, [], noPrefix => {
}, [],
noPrefix => {
if (noPrefix === undefined) {
return undefined;
}
@ -84,23 +85,30 @@ export default class OpeningHoursInput extends InputElement<string> {
// Note: MUST be bound AFTER the leftover rules!
const rulesFromOhPicker: UIEventSource<OpeningHour[]> = valueWithoutPrefix.sync(str => {
console.log(">> Parsing '"+ str+"'")
return OH.Parse(str);
}, [leftoverRules, phSelector.GetValue()], (rules, oldString) => {
let str = OH.ToString(rules);
// We always add a ';', to easily add new rules. We remove the ';' again at the end of the function
// Important: spaces are _not_ allowed after a ';' as it'll destabilize the parsing!
let str = OH.ToString(rules) + ";"
const ph = phSelector.GetValue().data;
if(ph){
str += "; "+ph
str += ph + ";"
}
str += leftoverRules.data.join("; ")
if(!str.endsWith(";")){
str += ";"
str += leftoverRules.data.join(";") + ";"
str = str.trim()
if(str.endsWith(";")){
str = str.substring(0, str.length - 1)
}
if(str.startsWith(";")){
str = str.substring(1)
}
str.trim()
if(str === oldString){
return oldString; // We pass a reference to the old string to stabilize the EventSource
}
console.log("Reconstructed '"+ str+"'")
return str;
});

View file

@ -34,6 +34,14 @@ export default class OpeningHoursPicker extends InputElement<OpeningHour[]> {
return true;
}
/**
*
* const rules = OH.ParseRule("Jul-Aug Sa closed; Mo,Tu,Th,Fr,PH 12:00-22:30, We 17:00-22:30, Sa 14:00-19:00, Su 10:00-21:00; Dec 24,25,31 off; Jan 1 off")
* const v = new UIEventSource(rules)
* const ohpicker = new OpeningHoursPicker(v)
* const html = ohpicker.InnerConstructElement()
* html !== undefined // => true
*/
protected InnerConstructElement(): HTMLElement {
return this._backgroundTable.ConstructElement();
}

View file

@ -68,7 +68,8 @@ export default class OpeningHoursPickerTable extends InputElement<OpeningHour[]>
const ranges = new VariableUiElement(
this.source.map(ohs => ohs.filter((oh: OpeningHour) => oh.weekday === i))
this.source.map(ohs =>
(ohs ?? []).filter((oh: OpeningHour) => oh.weekday === i))
.map(ohsForToday => {
return new Combine(ohsForToday.map(oh => new OpeningHoursRange(oh, () => {
this.source.data.splice(this.source.data.indexOf(oh), 1)