| 
									
										
										
										
											2020-10-08 19:03:00 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * The full opening hours element, including the table, opening hours picker. | 
					
						
							|  |  |  |  * Keeps track of unparsed rules | 
					
						
							|  |  |  |  * Exports everything conventiently as a string, for direct use | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2021-01-02 16:04:16 +01:00
										 |  |  | import OpeningHoursPicker from "./OpeningHoursPicker"; | 
					
						
							| 
									
										
										
										
											2022-06-05 02:24:14 +02:00
										 |  |  | import {Store, UIEventSource} from "../../Logic/UIEventSource"; | 
					
						
							| 
									
										
										
										
											2021-01-02 16:04:16 +01:00
										 |  |  | import {VariableUiElement} from "../Base/VariableUIElement"; | 
					
						
							|  |  |  | import Combine from "../Base/Combine"; | 
					
						
							|  |  |  | import {FixedUiElement} from "../Base/FixedUiElement"; | 
					
						
							| 
									
										
										
										
											2022-06-05 02:24:14 +02:00
										 |  |  | import {OH, OpeningHour} from "./OpeningHours"; | 
					
						
							| 
									
										
										
										
											2021-01-02 16:04:16 +01:00
										 |  |  | import {InputElement} from "../Input/InputElement"; | 
					
						
							|  |  |  | import PublicHolidayInput from "./PublicHolidayInput"; | 
					
						
							|  |  |  | import Translations from "../i18n/Translations"; | 
					
						
							| 
									
										
										
										
											2021-06-10 01:36:20 +02:00
										 |  |  | import BaseUIElement from "../BaseUIElement"; | 
					
						
							| 
									
										
										
										
											2021-01-02 16:04:16 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-08 19:03:00 +02:00
										 |  |  | export default class OpeningHoursInput extends InputElement<string> { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-10 01:36:20 +02:00
										 |  |  |     public readonly IsSelected: UIEventSource<boolean> = new UIEventSource<boolean>(false); | 
					
						
							| 
									
										
										
										
											2020-10-08 19:03:00 +02:00
										 |  |  |     private readonly _value: UIEventSource<string>; | 
					
						
							| 
									
										
										
										
											2021-06-10 01:36:20 +02:00
										 |  |  |     private readonly _element: BaseUIElement; | 
					
						
							| 
									
										
										
										
											2020-10-08 19:03:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-29 03:42:33 +02:00
										 |  |  |     constructor(value: UIEventSource<string> = new UIEventSource<string>(""), prefix = "", postfix = "") { | 
					
						
							| 
									
										
										
										
											2020-10-08 19:03:00 +02:00
										 |  |  |         super(); | 
					
						
							| 
									
										
										
										
											2021-06-24 02:33:43 +02:00
										 |  |  |         this._value = value; | 
					
						
							| 
									
										
										
										
											2021-10-29 03:42:33 +02:00
										 |  |  |         let valueWithoutPrefix = value | 
					
						
							|  |  |  |         if (prefix !== "" && postfix !== "") { | 
					
						
							| 
									
										
										
										
											2022-06-05 02:24:14 +02:00
										 |  |  |             valueWithoutPrefix = value.sync(str => { | 
					
						
							| 
									
										
										
										
											2021-10-29 03:42:33 +02:00
										 |  |  |                 if (str === undefined) { | 
					
						
							|  |  |  |                     return undefined; | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |                 if (str === "") { | 
					
						
							| 
									
										
										
										
											2021-10-29 03:42:33 +02:00
										 |  |  |                     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; | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |                 if (noPrefix === "") { | 
					
						
							| 
									
										
										
										
											2021-10-29 03:42:33 +02:00
										 |  |  |                     return "" | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 if (noPrefix.startsWith(prefix) && noPrefix.endsWith(postfix)) { | 
					
						
							|  |  |  |                     return noPrefix | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 return prefix + noPrefix + postfix | 
					
						
							|  |  |  |             }) | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-06-10 01:36:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-05 02:24:14 +02:00
										 |  |  |         const leftoverRules: Store<string[]> = valueWithoutPrefix.map(str => { | 
					
						
							| 
									
										
										
										
											2020-10-08 19:03:00 +02:00
										 |  |  |             if (str === undefined) { | 
					
						
							|  |  |  |                 return [] | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             const leftOvers: string[] = []; | 
					
						
							|  |  |  |             const rules = str.split(";"); | 
					
						
							|  |  |  |             for (const rule of rules) { | 
					
						
							|  |  |  |                 if (OH.ParseRule(rule) !== null) { | 
					
						
							|  |  |  |                     continue; | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2021-06-16 16:39:48 +02:00
										 |  |  |                 if (OH.ParsePHRule(rule) !== null) { | 
					
						
							| 
									
										
										
										
											2020-10-08 19:03:00 +02:00
										 |  |  |                     continue; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 leftOvers.push(rule); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             return leftOvers; | 
					
						
							|  |  |  |         }) | 
					
						
							| 
									
										
										
										
											2022-06-05 02:24:14 +02:00
										 |  |  |         | 
					
						
							|  |  |  |         let ph = ""; | 
					
						
							|  |  |  |         const rules = valueWithoutPrefix.data?.split(";") ?? []; | 
					
						
							|  |  |  |         for (const rule of rules) { | 
					
						
							|  |  |  |             if (OH.ParsePHRule(rule) !== null) { | 
					
						
							|  |  |  |                 ph = rule; | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         const phSelector = new PublicHolidayInput(new UIEventSource<string>(ph)); | 
					
						
							|  |  |  |          | 
					
						
							|  |  |  |          | 
					
						
							| 
									
										
										
										
											2021-06-16 16:39:48 +02:00
										 |  |  |         // Note: MUST be bound AFTER the leftover rules!
 | 
					
						
							| 
									
										
										
										
											2022-06-05 02:24:14 +02:00
										 |  |  |         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); | 
					
						
							|  |  |  |             const ph = phSelector.GetValue().data; | 
					
						
							|  |  |  |             if(ph){ | 
					
						
							|  |  |  |                str += "; "+ph  | 
					
						
							| 
									
										
										
										
											2020-10-08 19:03:00 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-06-05 02:24:14 +02:00
										 |  |  |              | 
					
						
							|  |  |  |             str += leftoverRules.data.join("; ") | 
					
						
							|  |  |  |             if(!str.endsWith(";")){ | 
					
						
							|  |  |  |                 str += ";" | 
					
						
							| 
									
										
										
										
											2020-10-08 19:03:00 +02:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2022-06-05 02:24:14 +02:00
										 |  |  |             if(str === oldString){ | 
					
						
							|  |  |  |                 return oldString; // We pass a reference to the old string to stabilize the EventSource
 | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             console.log("Reconstructed '"+ str+"'") | 
					
						
							|  |  |  |             return str; | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2020-10-08 19:03:00 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-10 01:36:20 +02:00
										 |  |  |         const leftoverWarning = new VariableUiElement(leftoverRules.map((leftovers: string[]) => { | 
					
						
							| 
									
										
										
										
											2020-10-08 19:03:00 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if (leftovers.length == 0) { | 
					
						
							|  |  |  |                 return ""; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             return new Combine([ | 
					
						
							|  |  |  |                 Translations.t.general.opening_hours.not_all_rules_parsed, | 
					
						
							| 
									
										
										
										
											2021-06-10 01:36:20 +02:00
										 |  |  |                 new FixedUiElement(leftovers.map(r => `${r}<br/>`).join("")).SetClass("subtle") | 
					
						
							|  |  |  |             ]); | 
					
						
							| 
									
										
										
										
											2020-10-08 19:03:00 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         })) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-10 01:36:20 +02:00
										 |  |  |         const ohPicker = new OpeningHoursPicker(rulesFromOhPicker); | 
					
						
							| 
									
										
										
										
											2020-10-08 19:03:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-10 01:36:20 +02:00
										 |  |  |         this._element = new Combine([ | 
					
						
							|  |  |  |             leftoverWarning, | 
					
						
							|  |  |  |             ohPicker, | 
					
						
							|  |  |  |             phSelector | 
					
						
							|  |  |  |         ]) | 
					
						
							| 
									
										
										
										
											2020-10-08 19:03:00 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     GetValue(): UIEventSource<string> { | 
					
						
							|  |  |  |         return this._value; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     IsValid(t: string): boolean { | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-09 00:05:51 +02:00
										 |  |  |     protected InnerConstructElement(): HTMLElement { | 
					
						
							|  |  |  |         return this._element.ConstructElement() | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-08 19:03:00 +02:00
										 |  |  | } |