| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | import { OH } from "./OpeningHours" | 
					
						
							|  |  |  | import { UIEventSource } from "../../Logic/UIEventSource" | 
					
						
							|  |  |  | import Combine from "../Base/Combine" | 
					
						
							|  |  |  | import { TextField } from "../Input/TextField" | 
					
						
							|  |  |  | import { DropDown } from "../Input/DropDown" | 
					
						
							|  |  |  | import { InputElement } from "../Input/InputElement" | 
					
						
							|  |  |  | import Translations from "../i18n/Translations" | 
					
						
							|  |  |  | import Toggle from "../Input/Toggle" | 
					
						
							| 
									
										
										
										
											2020-10-08 19:03:00 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | export default class PublicHolidayInput extends InputElement<string> { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     IsSelected: UIEventSource<boolean> = new UIEventSource<boolean>(false) | 
					
						
							| 
									
										
										
										
											2020-10-08 19:03:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     private readonly _value: UIEventSource<string> | 
					
						
							| 
									
										
										
										
											2020-10-08 19:03:00 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     constructor(value: UIEventSource<string> = new UIEventSource<string>("")) { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         super() | 
					
						
							|  |  |  |         this._value = value | 
					
						
							| 
									
										
										
										
											2021-06-16 16:39:48 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-10-08 19:03:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-16 16:39:48 +02:00
										 |  |  |     GetValue(): UIEventSource<string> { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         return this._value | 
					
						
							| 
									
										
										
										
											2021-06-16 16:39:48 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-10-08 19:03:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-16 16:39:48 +02:00
										 |  |  |     IsValid(t: string): boolean { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         return true | 
					
						
							| 
									
										
										
										
											2021-06-16 16:39:48 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-06-10 14:05:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-29 16:57:29 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * // should construct an element
 | 
					
						
							|  |  |  |      * const html = new PublicHolidayInput().InnerConstructElement() | 
					
						
							|  |  |  |      * html !== undefined // => true
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2022-06-29 16:57:29 +02:00
										 |  |  |      * // should construct an element despite having an invalid input
 | 
					
						
							|  |  |  |      * const html = new PublicHolidayInput(new UIEventSource("invalid")).InnerConstructElement() | 
					
						
							|  |  |  |      * html !== undefined // => true
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2022-06-29 16:57:29 +02:00
										 |  |  |      * // should construct an element despite having null as input
 | 
					
						
							|  |  |  |      * const html = new PublicHolidayInput(new UIEventSource(null)).InnerConstructElement() | 
					
						
							|  |  |  |      * html !== undefined // => true
 | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-06-16 16:39:48 +02:00
										 |  |  |     protected InnerConstructElement(): HTMLElement { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         const dropdown = new DropDown(Translations.t.general.opening_hours.open_during_ph.Clone(), [ | 
					
						
							|  |  |  |             { shown: Translations.t.general.opening_hours.ph_not_known.Clone(), value: "" }, | 
					
						
							|  |  |  |             { shown: Translations.t.general.opening_hours.ph_closed.Clone(), value: "off" }, | 
					
						
							|  |  |  |             { shown: Translations.t.general.opening_hours.ph_open_as_usual.Clone(), value: "open" }, | 
					
						
							|  |  |  |             { shown: Translations.t.general.opening_hours.ph_open.Clone(), value: " " }, | 
					
						
							|  |  |  |         ]).SetClass("inline-block") | 
					
						
							| 
									
										
										
										
											2021-06-16 16:39:48 +02:00
										 |  |  |         /* | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |          * Either "" (unknown), " " (opened) or "off" (closed) | 
					
						
							|  |  |  |          * */ | 
					
						
							|  |  |  |         const mode = dropdown.GetValue() | 
					
						
							| 
									
										
										
										
											2020-10-08 19:03:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-16 16:39:48 +02:00
										 |  |  |         const start = new TextField({ | 
					
						
							|  |  |  |             placeholder: "starthour", | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             htmlType: "time", | 
					
						
							|  |  |  |         }).SetClass("inline-block") | 
					
						
							| 
									
										
										
										
											2021-06-16 16:39:48 +02:00
										 |  |  |         const end = new TextField({ | 
					
						
							|  |  |  |             placeholder: "starthour", | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             htmlType: "time", | 
					
						
							|  |  |  |         }).SetClass("inline-block") | 
					
						
							| 
									
										
										
										
											2021-06-30 18:48:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-16 16:39:48 +02:00
										 |  |  |         const askHours = new Toggle( | 
					
						
							|  |  |  |             new Combine([ | 
					
						
							|  |  |  |                 Translations.t.general.opening_hours.opensAt.Clone(), | 
					
						
							|  |  |  |                 start, | 
					
						
							|  |  |  |                 Translations.t.general.opening_hours.openTill.Clone(), | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                 end, | 
					
						
							| 
									
										
										
										
											2021-06-16 16:39:48 +02:00
										 |  |  |             ]), | 
					
						
							|  |  |  |             undefined, | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             mode.map((mode) => mode === " ") | 
					
						
							| 
									
										
										
										
											2021-06-16 16:39:48 +02:00
										 |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         this.SetupDataSync(mode, start.GetValue(), end.GetValue()) | 
					
						
							| 
									
										
										
										
											2021-06-30 18:48:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         return new Combine([dropdown, askHours]).ConstructElement() | 
					
						
							| 
									
										
										
										
											2020-10-08 19:03:00 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |     private SetupDataSync( | 
					
						
							|  |  |  |         mode: UIEventSource<string>, | 
					
						
							|  |  |  |         startTime: UIEventSource<string>, | 
					
						
							|  |  |  |         endTime: UIEventSource<string> | 
					
						
							|  |  |  |     ) { | 
					
						
							|  |  |  |         const value = this._value | 
					
						
							|  |  |  |         value | 
					
						
							|  |  |  |             .map((ph) => OH.ParsePHRule(ph)) | 
					
						
							|  |  |  |             .addCallbackAndRunD((parsed) => { | 
					
						
							|  |  |  |                 if (parsed === null) { | 
					
						
							| 
									
										
										
										
											2022-06-29 16:57:29 +02:00
										 |  |  |                     return | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2021-09-09 00:05:51 +02:00
										 |  |  |                 mode.setData(parsed.mode) | 
					
						
							|  |  |  |                 startTime.setData(parsed.start) | 
					
						
							|  |  |  |                 endTime.setData(parsed.end) | 
					
						
							|  |  |  |             }) | 
					
						
							| 
									
										
										
										
											2021-06-30 18:48:23 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // We use this as a 'addCallbackAndRun'
 | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |         mode.map( | 
					
						
							|  |  |  |             (mode) => { | 
					
						
							| 
									
										
										
										
											2021-06-30 18:48:23 +02:00
										 |  |  |                 if (mode === undefined || mode === "") { | 
					
						
							|  |  |  |                     // not known
 | 
					
						
							|  |  |  |                     value.setData(undefined) | 
					
						
							|  |  |  |                     return | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 if (mode === "off") { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                     value.setData("PH off") | 
					
						
							|  |  |  |                     return | 
					
						
							| 
									
										
										
										
											2021-06-30 18:48:23 +02:00
										 |  |  |                 } | 
					
						
							|  |  |  |                 if (mode === "open") { | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |                     value.setData("PH open") | 
					
						
							|  |  |  |                     return | 
					
						
							| 
									
										
										
										
											2021-06-30 18:48:23 +02:00
										 |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 // Open during PH with special hours
 | 
					
						
							|  |  |  |                 if (startTime.data === undefined || endTime.data === undefined) { | 
					
						
							|  |  |  |                     // hours not filled in - not saveable
 | 
					
						
							|  |  |  |                     value.setData(undefined) | 
					
						
							|  |  |  |                     return | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 const oh = `PH ${startTime.data}-${endTime.data}` | 
					
						
							|  |  |  |                 value.setData(oh) | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  |             }, | 
					
						
							|  |  |  |             [startTime, endTime] | 
					
						
							| 
									
										
										
										
											2021-06-30 18:48:23 +02:00
										 |  |  |         ) | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-09-08 21:40:48 +02:00
										 |  |  | } |