import {UIElement} from "../../UIElement"; import {InputElement} from "../InputElement"; import {OpeningHour, OpeningHourUtils} from "../../../Logic/OpeningHours"; import {UIEventSource} from "../../../Logic/UIEventSource"; import OpeningHoursPickerTable from "./OpeningHoursPickerTable"; import OpeningHoursRange from "./OpeningHoursRange"; import Combine from "../../Base/Combine"; export default class OpeningHoursPicker extends InputElement { private readonly _ohs: UIEventSource; public readonly IsSelected: UIEventSource = new UIEventSource(false); private readonly _backgroundTable: OpeningHoursPickerTable; private readonly _weekdays: UIEventSource = new UIEventSource([]); constructor(ohs: UIEventSource = new UIEventSource([])) { super(); this._ohs = ohs; this._backgroundTable = new OpeningHoursPickerTable(this._weekdays); const self = this; this._backgroundTable.GetValue().addCallback(oh => { if (oh) { ohs.data.push(oh); ohs.ping(); } }); this._ohs.addCallback(ohs => { self._ohs.setData(OpeningHourUtils.MergeTimes(ohs)); }) ohs.addCallback(ohs => { const perWeekday: UIElement[][] = []; for (let i = 0; i < 7; i++) { perWeekday[i] = []; } for (const oh of ohs) { const source = new UIEventSource(oh) source.addCallback(_ => { self._ohs.setData(OpeningHourUtils.MergeTimes(self._ohs.data)) }) const r = new OpeningHoursRange(source); perWeekday[oh.weekday].push(r); } for (let i = 0; i < 7; i++) { self._weekdays.data[i] = new Combine(perWeekday[i]); } self._weekdays.ping(); }); } InnerRender(): string { return this._backgroundTable.Render(); } GetValue(): UIEventSource { return this._ohs } IsValid(t: OpeningHour[]): boolean { return true; } }