| 
									
										
										
										
											2021-01-02 16:04:16 +01: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"; | 
					
						
							| 
									
										
										
										
											2021-06-16 16:39:48 +02:00
										 |  |  | import Toggle from "../Input/Toggle"; | 
					
						
							| 
									
										
										
										
											2020-10-08 19:03:00 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | export default class PublicHolidayInput extends InputElement<string> { | 
					
						
							|  |  |  |     IsSelected: UIEventSource<boolean> = new UIEventSource<boolean>(false); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private readonly _value: UIEventSource<string>; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     constructor(value: UIEventSource<string> = new UIEventSource<string>("")) { | 
					
						
							|  |  |  |         super(); | 
					
						
							|  |  |  |         this._value = value; | 
					
						
							| 
									
										
										
										
											2021-06-16 16:39:48 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-10-08 19:03:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-30 18:48:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-16 16:39:48 +02:00
										 |  |  |     GetValue(): UIEventSource<string> { | 
					
						
							|  |  |  |         return this._value; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-10-08 19:03:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-16 16:39:48 +02:00
										 |  |  |     IsValid(t: string): boolean { | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-06-10 14:05:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-16 16:39:48 +02:00
										 |  |  |     protected InnerConstructElement(): HTMLElement { | 
					
						
							|  |  |  |         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"}, | 
					
						
							| 
									
										
										
										
											2021-06-30 18:48:23 +02:00
										 |  |  |                 {shown: Translations.t.general.opening_hours.ph_open_as_usual.Clone(), value: "open"}, | 
					
						
							| 
									
										
										
										
											2021-06-16 16:39:48 +02:00
										 |  |  |                 {shown: Translations.t.general.opening_hours.ph_open.Clone(), value: " "}, | 
					
						
							|  |  |  |             ] | 
					
						
							|  |  |  |         ).SetClass("inline-block"); | 
					
						
							|  |  |  |         /* | 
					
						
							|  |  |  |         * 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", | 
					
						
							|  |  |  |             htmlType: "time" | 
					
						
							|  |  |  |         }).SetClass("inline-block"); | 
					
						
							|  |  |  |         const end = new TextField({ | 
					
						
							|  |  |  |             placeholder: "starthour", | 
					
						
							|  |  |  |             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(), | 
					
						
							|  |  |  |                 end | 
					
						
							|  |  |  |             ]), | 
					
						
							|  |  |  |             undefined, | 
					
						
							|  |  |  |             mode.map(mode => mode === " ") | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         this.SetupDataSync(mode, start.GetValue(), end.GetValue()) | 
					
						
							| 
									
										
										
										
											2021-06-30 18:48:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-16 16:39:48 +02:00
										 |  |  |         return new Combine([ | 
					
						
							|  |  |  |             dropdown, | 
					
						
							|  |  |  |             askHours | 
					
						
							|  |  |  |         ]).ConstructElement() | 
					
						
							| 
									
										
										
										
											2020-10-08 19:03:00 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-30 18:48:23 +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 => { | 
					
						
							|  |  |  |             mode.setData(parsed.mode) | 
					
						
							|  |  |  |             startTime.setData(parsed.start) | 
					
						
							|  |  |  |             endTime.setData(parsed.end) | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // We use this as a 'addCallbackAndRun'
 | 
					
						
							|  |  |  |         mode.map(mode => { | 
					
						
							|  |  |  |                 if (mode === undefined || mode === "") { | 
					
						
							|  |  |  |                     // not known
 | 
					
						
							|  |  |  |                     value.setData(undefined) | 
					
						
							|  |  |  |                     return | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 if (mode === "off") { | 
					
						
							|  |  |  |                     value.setData("PH off"); | 
					
						
							|  |  |  |                     return; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 if (mode === "open") { | 
					
						
							|  |  |  |                     value.setData("PH open"); | 
					
						
							|  |  |  |                     return; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 // 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) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             }, [startTime, endTime] | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-08 19:03:00 +02:00
										 |  |  | } |