mapcomplete/UI/OpeningHours/OpeningHoursPicker.ts

65 lines
2.1 KiB
TypeScript
Raw Permalink Normal View History

import {UIEventSource} from "../../Logic/UIEventSource";
import {UIElement} from "../UIElement";
2020-10-04 01:04:46 +02:00
import OpeningHoursRange from "./OpeningHoursRange";
import Combine from "../Base/Combine";
import OpeningHoursPickerTable from "./OpeningHoursPickerTable";
import {OH, OpeningHour} from "./OpeningHours";
import {InputElement} from "../Input/InputElement";
2020-10-04 01:04:46 +02:00
export default class OpeningHoursPicker extends InputElement<OpeningHour[]> {
2020-10-08 19:03:00 +02:00
private readonly _ohs: UIEventSource<OpeningHour[]>;
2020-10-04 01:04:46 +02:00
public readonly IsSelected: UIEventSource<boolean> = new UIEventSource<boolean>(false);
2020-09-30 22:22:58 +02:00
2020-10-04 01:04:46 +02:00
private readonly _backgroundTable: OpeningHoursPickerTable;
2020-09-30 22:22:58 +02:00
2020-10-04 01:04:46 +02:00
private readonly _weekdays: UIEventSource<UIElement[]> = new UIEventSource<UIElement[]>([]);
2020-09-30 22:22:58 +02:00
2020-10-04 12:55:44 +02:00
constructor(ohs: UIEventSource<OpeningHour[]> = new UIEventSource<OpeningHour[]>([])) {
2020-09-30 22:22:58 +02:00
super();
2020-10-04 01:04:46 +02:00
this._ohs = ohs;
2020-10-06 01:37:02 +02:00
this._backgroundTable = new OpeningHoursPickerTable(this._weekdays, this._ohs);
2020-09-30 22:22:58 +02:00
const self = this;
2020-10-04 01:04:46 +02:00
this._ohs.addCallback(ohs => {
2020-10-06 01:37:02 +02:00
self._ohs.setData(OH.MergeTimes(ohs));
2020-10-04 01:04:46 +02:00
})
2020-09-30 22:22:58 +02:00
2020-10-06 01:37:02 +02:00
ohs.addCallbackAndRun(ohs => {
2020-10-04 01:04:46 +02:00
const perWeekday: UIElement[][] = [];
for (let i = 0; i < 7; i++) {
perWeekday[i] = [];
2020-09-30 22:22:58 +02:00
}
2020-10-04 01:04:46 +02:00
for (const oh of ohs) {
const source = new UIEventSource<OpeningHour>(oh)
source.addCallback(_ => {
2020-10-06 01:37:02 +02:00
self._ohs.setData(OH.MergeTimes(self._ohs.data))
2020-10-04 01:04:46 +02:00
})
2020-10-08 19:03:00 +02:00
const r = new OpeningHoursRange(source, `oh-table-${this._backgroundTable.id}`);
perWeekday[oh.weekday].push(r);
2020-09-30 22:22:58 +02:00
}
2020-10-04 01:04:46 +02:00
for (let i = 0; i < 7; i++) {
self._weekdays.data[i] = new Combine(perWeekday[i]);
}
self._weekdays.ping();
2020-09-30 22:22:58 +02:00
2020-10-04 01:04:46 +02:00
});
2020-09-30 22:22:58 +02:00
}
2020-10-04 01:04:46 +02:00
InnerRender(): string {
return this._backgroundTable.Render();
}
GetValue(): UIEventSource<OpeningHour[]> {
return this._ohs
2020-09-30 22:22:58 +02:00
}
2020-10-04 01:04:46 +02:00
IsValid(t: OpeningHour[]): boolean {
return true;
2020-09-30 22:22:58 +02:00
}
}