mapcomplete/UI/OpeningHours/PublicHolidayInput.ts

176 lines
5.2 KiB
TypeScript
Raw Normal View History

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";
2021-06-10 14:05:26 +02:00
import BaseUIElement from "../BaseUIElement";
import {VariableUiElement} from "../Base/VariableUIElement";
2020-10-08 19:03:00 +02:00
export default class PublicHolidayInput extends InputElement<string> {
IsSelected: UIEventSource<boolean> = new UIEventSource<boolean>(false);
private readonly _value: UIEventSource<string>;
2021-06-10 14:05:26 +02:00
private readonly _dropdown: BaseUIElement;
2020-10-08 19:03:00 +02:00
private readonly _mode: UIEventSource<string>;
2021-06-10 14:05:26 +02:00
private readonly _startHour: BaseUIElement;
private readonly _endHour: BaseUIElement;
private _element: VariableUiElement;
2020-10-08 19:03:00 +02:00
constructor(value: UIEventSource<string> = new UIEventSource<string>("")) {
super();
this._value = value;
const dropdown = new DropDown(
Translations.t.general.opening_hours.open_during_ph,
[
{shown: Translations.t.general.opening_hours.ph_not_known, value: ""},
{shown: Translations.t.general.opening_hours.ph_closed, value: "off"},
{shown:Translations.t.general.opening_hours.ph_open, value: " "}
2020-10-08 19:03:00 +02:00
]
);
this._dropdown = dropdown.SetStyle("display:inline-block;");
this._mode = dropdown.GetValue();
const start = new TextField({
placeholder: "starthour",
htmlType: "time"
});
const end = new TextField({
placeholder: "starthour",
htmlType: "time"
});
this._startHour = start.SetStyle("display:inline-block;");
this._endHour = end.SetStyle("display:inline-block;");
const self = this;
this._value.addCallbackAndRun(ph => {
if (ph === undefined) {
return;
}
const parsed = PublicHolidayInput.LoadValue(ph);
if (parsed === null) {
return;
}
dropdown.GetValue().setData(parsed.mode);
if (parsed.start) {
start.GetValue().setData(parsed.start);
}
if (parsed.end) {
end.GetValue().setData(parsed.end);
}
})
function updateValue() {
const phStart = dropdown.GetValue().data;
if (phStart === undefined || phStart === "") {
// Unknown
self._value.setData("");
return;
}
if (phStart === " ") {
// THey are open, we need to include the start- and enddate
const startV = start.GetValue().data;
const endV = end.GetValue().data;
if (startV === undefined || endV === undefined) {
self._value.setData(`PH open`);
return;
}
self._value.setData(`PH ${startV}-${endV}`);
return;
}
self._value.setData(`PH ${phStart}`);
}
dropdown.GetValue().addCallbackAndRun(() => {
updateValue();
});
start.GetValue().addCallbackAndRun(() => {
updateValue();
});
end.GetValue().addCallbackAndRun(() => {
updateValue();
});
2021-06-10 14:05:26 +02:00
this._element = new VariableUiElement(this._mode.map(
mode => {
if (mode === " ") {
return new Combine([this._dropdown,
" ",
Translations.t.general.opening_hours.opensAt,
" ",
this._startHour,
" ",
Translations.t.general.opening_hours.openTill,
" ",
this._endHour]);
}
return this._dropdown;
}))
}
protected InnerConstructElement(): HTMLElement {
return this._element.ConstructElement();
2020-10-08 19:03:00 +02:00
}
public static LoadValue(str: string): {
mode: string,
start?: string,
end?: string
} {
str = str.trim();
if (!str.startsWith("PH")) {
return null;
}
str = str.trim();
if (str === "PH off") {
return {
mode: "off"
}
}
2020-10-19 12:08:42 +02:00
if(str === "PH open"){
return {
2020-10-23 02:26:14 +02:00
mode: " "
2020-10-19 12:08:42 +02:00
}
}
2020-10-08 19:03:00 +02:00
if (!str.startsWith("PH ")) {
return null;
}
try {
const timerange = OH.parseHHMMRange(str.substring(2));
if (timerange === null) {
return null;
}
return {
mode: " ",
start: OH.hhmm(timerange.startHour, timerange.startMinutes),
end: OH.hhmm(timerange.endHour, timerange.endMinutes),
}
} catch (e) {
return null;
}
}
GetValue(): UIEventSource<string> {
return this._value;
}
IsValid(t: string): boolean {
return true;
}
}