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