mapcomplete/UI/Input/OpeningHours/OpeningHoursPicker.ts

65 lines
2 KiB
TypeScript
Raw Normal View History

2020-10-04 01:04:46 +02:00
import {UIElement} from "../../UIElement";
import {InputElement} from "../InputElement";
2020-10-06 01:37:02 +02:00
import {OpeningHour, OH} from "../../../Logic/OpeningHours";
2020-10-04 01:04:46 +02:00
import {UIEventSource} from "../../../Logic/UIEventSource";
import OpeningHoursPickerTable from "./OpeningHoursPickerTable";
import OpeningHoursRange from "./OpeningHoursRange";
import Combine from "../../Base/Combine";
2020-10-04 01:04:46 +02:00
export default class OpeningHoursPicker extends InputElement<OpeningHour[]> {
private readonly _ohs: UIEventSource<OpeningHour[]>;
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
})
const r = new OpeningHoursRange(source);
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
}
}