| 
									
										
										
										
											2020-10-08 19:03:00 +02:00
										 |  |  | /** | 
					
						
							|  |  |  |  * The full opening hours element, including the table, opening hours picker. | 
					
						
							|  |  |  |  * Keeps track of unparsed rules | 
					
						
							| 
									
										
										
										
											2023-04-16 03:42:26 +02:00
										 |  |  |  * Exports everything conveniently as a string, for direct use | 
					
						
							| 
									
										
										
										
											2020-10-08 19:03:00 +02:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											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> { | 
					
						
							|  |  |  |     private readonly _value: UIEventSource<string> | 
					
						
							| 
									
										
										
										
											2021-06-10 01:36:20 +02:00
										 |  |  |     private readonly _element: BaseUIElement | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +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 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                     } | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |                     if (str === "") { | 
					
						
							| 
									
										
										
										
											2021-10-29 03:42:33 +02:00
										 |  |  |                         return "" | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                     } | 
					
						
							| 
									
										
										
										
											2021-10-29 03:42:33 +02:00
										 |  |  |                     if (str.startsWith(prefix) && str.endsWith(postfix)) { | 
					
						
							|  |  |  |                         return str.substring(prefix.length, str.length - postfix.length) | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                     } | 
					
						
							| 
									
										
										
										
											2021-10-29 03:42:33 +02:00
										 |  |  |                     return str | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 }, | 
					
						
							|  |  |  |                 [], | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |                 (noPrefix) => { | 
					
						
							| 
									
										
										
										
											2021-10-29 03:42:33 +02:00
										 |  |  |                     if (noPrefix === undefined) { | 
					
						
							|  |  |  |                         return undefined | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                     } | 
					
						
							| 
									
										
										
										
											2021-11-07 16:34:51 +01:00
										 |  |  |                     if (noPrefix === "") { | 
					
						
							| 
									
										
										
										
											2021-10-29 03:42:33 +02:00
										 |  |  |                         return "" | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                     } | 
					
						
							| 
									
										
										
										
											2021-10-29 03:42:33 +02:00
										 |  |  |                     if (noPrefix.startsWith(prefix) && noPrefix.endsWith(postfix)) { | 
					
						
							|  |  |  |                         return noPrefix | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-29 03:42:33 +02:00
										 |  |  |                     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-09-08 21:40:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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)) | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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) => { | 
					
						
							|  |  |  |                 return OH.Parse(str) | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |             [leftoverRules, phSelector.GetValue()], | 
					
						
							|  |  |  |             (rules, oldString) => { | 
					
						
							| 
									
										
										
										
											2022-06-29 17:27:23 +02:00
										 |  |  |                 // We always add a ';', to easily add new rules. We remove the ';' again at the end of the function
 | 
					
						
							| 
									
										
										
										
											2022-06-05 02:24:14 +02:00
										 |  |  |                 // Important: spaces are _not_ allowed after a ';' as it'll destabilize the parsing!
 | 
					
						
							|  |  |  |                 let str = OH.ToString(rules) + ";" | 
					
						
							|  |  |  |                 const ph = phSelector.GetValue().data | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 if (ph) { | 
					
						
							| 
									
										
										
										
											2022-06-05 02:24:14 +02:00
										 |  |  |                     str += ph + ";" | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2020-10-08 19:03:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-29 17:27:23 +02:00
										 |  |  |                 str += leftoverRules.data.join(";") + ";" | 
					
						
							| 
									
										
										
										
											2020-10-08 19:03:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-10 01:36:20 +02:00
										 |  |  |                 str = str.trim() | 
					
						
							|  |  |  |                 if (str.endsWith(";")) { | 
					
						
							|  |  |  |                     str = str.substring(0, str.length - 1) | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2021-06-10 01:36:20 +02:00
										 |  |  |                 if (str.startsWith(";")) { | 
					
						
							|  |  |  |                     str = str.substring(1) | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2022-06-29 17:27:23 +02:00
										 |  |  |                 str.trim() | 
					
						
							| 
									
										
										
										
											2020-10-08 19:03:00 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 if (str === oldString) { | 
					
						
							|  |  |  |                     return oldString // We pass a reference to the old string to stabilize the EventSource
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2020-10-08 19:03:00 +02:00
										 |  |  |                 return str | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-06-10 01:36:20 +02:00
										 |  |  |         ) | 
					
						
							| 
									
										
										
										
											2020-10-08 19:03:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-10 01:36:20 +02:00
										 |  |  |         const leftoverWarning = new VariableUiElement( | 
					
						
							|  |  |  |             leftoverRules.map((leftovers: string[]) => { | 
					
						
							|  |  |  |                 if (leftovers.length == 0) { | 
					
						
							| 
									
										
										
										
											2021-10-29 03:42:33 +02:00
										 |  |  |                     return "" | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2020-10-08 19:03:00 +02:00
										 |  |  |                 return new Combine([ | 
					
						
							|  |  |  |                     Translations.t.general.opening_hours.not_all_rules_parsed, | 
					
						
							|  |  |  |                     new FixedUiElement(leftovers.map((r) => `${r}<br/>`).join("")).SetClass( | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                         "subtle" | 
					
						
							|  |  |  |                     ), | 
					
						
							|  |  |  |                 ]) | 
					
						
							| 
									
										
										
										
											2020-10-08 19:03:00 +02:00
										 |  |  |             }) | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         ) | 
					
						
							| 
									
										
										
										
											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
										 |  |  | } |