diff --git a/src/UI/OpeningHours/OpeningHoursInput.ts b/src/UI/OpeningHours/OpeningHoursInput.ts index 7e824645c4..f6d9226174 100644 --- a/src/UI/OpeningHours/OpeningHoursInput.ts +++ b/src/UI/OpeningHours/OpeningHoursInput.ts @@ -10,9 +10,10 @@ import Combine from "../Base/Combine" import { FixedUiElement } from "../Base/FixedUiElement" import { OH, OpeningHour } from "./OpeningHours" import { InputElement } from "../Input/InputElement" -import PublicHolidayInput from "./PublicHolidayInput" import Translations from "../i18n/Translations" import BaseUIElement from "../BaseUIElement" +import SvelteUIElement from "../Base/SvelteUIElement" +import PublicHolidaySelector from "./PublicHolidaySelector.svelte" export default class OpeningHoursInput extends InputElement { private readonly _value: UIEventSource @@ -87,19 +88,19 @@ export default class OpeningHoursInput extends InputElement { break } } - const phSelector = new PublicHolidayInput(new UIEventSource(ph)) + const phSelectorValue = new UIEventSource(ph ?? "") // Note: MUST be bound AFTER the leftover rules! const rulesFromOhPicker: UIEventSource = valueWithoutPrefix.sync( (str) => { return OH.Parse(str) }, - [leftoverRules, phSelector.GetValue()], + [leftoverRules, phSelectorValue], (rules, oldString) => { // 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 + const ph = phSelectorValue.data if (ph) { str += ph + ";" } @@ -138,7 +139,7 @@ export default class OpeningHoursInput extends InputElement { const ohPicker = new OpeningHoursPicker(rulesFromOhPicker) - this._element = new Combine([leftoverWarning, ohPicker, phSelector]) + this._element = new Combine([leftoverWarning, ohPicker, new SvelteUIElement(PublicHolidaySelector, {value: phSelectorValue})]) } GetValue(): UIEventSource { diff --git a/src/UI/OpeningHours/PublicHolidayInput.ts b/src/UI/OpeningHours/PublicHolidayInput.ts deleted file mode 100644 index f52bc701a4..0000000000 --- a/src/UI/OpeningHours/PublicHolidayInput.ts +++ /dev/null @@ -1,111 +0,0 @@ -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" - -export default class PublicHolidayInput extends InputElement { - IsSelected: UIEventSource = new UIEventSource(false) - - private readonly _value: UIEventSource - - constructor(value: UIEventSource = new UIEventSource("")) { - super() - this._value = value - } - - GetValue(): UIEventSource { - return this._value - } - - IsValid(_: string): boolean { - return true - } - - 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" }, - { 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 w-full") - /* - * Either "" (unknown), " " (opened) or "off" (closed) - * */ - const mode = dropdown.GetValue() - - const start = new TextField({ - placeholder: "starthour", - htmlType: "time", - }).SetClass("inline-block") - const end = new TextField({ - placeholder: "starthour", - htmlType: "time", - }).SetClass("inline-block") - - 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()) - - return new Combine([dropdown, askHours]).SetClass("w-full flex").ConstructElement() - } - - private SetupDataSync( - mode: UIEventSource, - startTime: UIEventSource, - endTime: UIEventSource - ) { - const value = this._value - value - .map((ph) => OH.ParsePHRule(ph)) - .addCallbackAndRunD((parsed) => { - if (parsed === null) { - return - } - 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] - ) - } -} diff --git a/src/UI/OpeningHours/PublicHolidaySelector.svelte b/src/UI/OpeningHours/PublicHolidaySelector.svelte new file mode 100644 index 0000000000..fdba022269 --- /dev/null +++ b/src/UI/OpeningHours/PublicHolidaySelector.svelte @@ -0,0 +1,95 @@ + + + + +{#if $mode === " "} +
+ + + + +
+{/if}