mapcomplete/UI/OpeningHours/OpeningHoursInput.ts

138 lines
4.4 KiB
TypeScript
Raw Normal View History

2020-10-08 19:03:00 +02:00
/**
* The full opening hours element, including the table, opening hours picker.
* Keeps track of unparsed rules
* Exports everything conventiently as a string, for direct use
*/
import OpeningHoursPicker from "./OpeningHoursPicker";
import {UIEventSource} from "../../Logic/UIEventSource";
import {VariableUiElement} from "../Base/VariableUIElement";
import Combine from "../Base/Combine";
import {FixedUiElement} from "../Base/FixedUiElement";
import {OH} from "./OpeningHours";
import {InputElement} from "../Input/InputElement";
import PublicHolidayInput from "./PublicHolidayInput";
import Translations from "../i18n/Translations";
import {Utils} from "../../Utils";
2021-06-10 01:36:20 +02:00
import BaseUIElement from "../BaseUIElement";
2020-10-08 19:03:00 +02:00
export default class OpeningHoursInput extends InputElement<string> {
2021-06-10 01:36:20 +02:00
public readonly IsSelected: UIEventSource<boolean> = new UIEventSource<boolean>(false);
2020-10-08 19:03:00 +02:00
private readonly _value: UIEventSource<string>;
2021-06-10 01:36:20 +02:00
private readonly _element: BaseUIElement;
2020-10-08 19:03:00 +02:00
constructor(value: UIEventSource<string> = new UIEventSource<string>(""), prefix = "", postfix = "") {
2020-10-08 19:03:00 +02:00
super();
2021-06-24 02:33:43 +02:00
this._value = value;
let valueWithoutPrefix = value
if (prefix !== "" && postfix !== "") {
2021-11-07 16:34:51 +01:00
valueWithoutPrefix = value.map(str => {
if (str === undefined) {
return undefined;
}
2021-11-07 16:34:51 +01:00
if (str === "") {
return ""
}
if (str.startsWith(prefix) && str.endsWith(postfix)) {
return str.substring(prefix.length, str.length - postfix.length)
}
return str
}, [], noPrefix => {
if (noPrefix === undefined) {
return undefined;
}
2021-11-07 16:34:51 +01:00
if (noPrefix === "") {
return ""
}
if (noPrefix.startsWith(prefix) && noPrefix.endsWith(postfix)) {
return noPrefix
}
return prefix + noPrefix + postfix
})
}
2021-06-10 01:36:20 +02:00
const leftoverRules = valueWithoutPrefix.map<string[]>(str => {
2020-10-08 19:03:00 +02:00
if (str === undefined) {
return []
}
const leftOvers: string[] = [];
const rules = str.split(";");
for (const rule of rules) {
if (OH.ParseRule(rule) !== null) {
continue;
}
2021-06-16 16:39:48 +02:00
if (OH.ParsePHRule(rule) !== null) {
2020-10-08 19:03:00 +02:00
continue;
}
leftOvers.push(rule);
}
return leftOvers;
})
2021-06-16 16:39:48 +02:00
// Note: MUST be bound AFTER the leftover rules!
const rulesFromOhPicker = valueWithoutPrefix.map(OH.Parse);
2020-10-08 19:03:00 +02:00
const ph = valueWithoutPrefix.map<string>(str => {
2020-10-08 19:03:00 +02:00
if (str === undefined) {
return ""
}
const rules = str.split(";");
for (const rule of rules) {
2021-06-16 16:39:48 +02:00
if (OH.ParsePHRule(rule) !== null) {
2020-10-08 19:03:00 +02:00
return rule;
}
}
return "";
})
2021-06-10 01:36:20 +02:00
const phSelector = new PublicHolidayInput(ph);
2020-10-08 19:03:00 +02:00
function update() {
const regular = OH.ToString(rulesFromOhPicker.data);
2021-06-10 01:36:20 +02:00
const rules: string[] = [
regular,
...leftoverRules.data,
ph.data
]
valueWithoutPrefix.setData(Utils.NoEmpty(rules).join(";"));
2020-10-08 19:03:00 +02:00
}
rulesFromOhPicker.addCallback(update);
ph.addCallback(update);
2021-06-10 01:36:20 +02:00
const leftoverWarning = new VariableUiElement(leftoverRules.map((leftovers: string[]) => {
2020-10-08 19:03:00 +02:00
if (leftovers.length == 0) {
return "";
}
return new Combine([
Translations.t.general.opening_hours.not_all_rules_parsed,
2021-06-10 01:36:20 +02:00
new FixedUiElement(leftovers.map(r => `${r}<br/>`).join("")).SetClass("subtle")
]);
2020-10-08 19:03:00 +02:00
}))
2021-06-10 01:36:20 +02:00
const ohPicker = new OpeningHoursPicker(rulesFromOhPicker);
2020-10-08 19:03:00 +02:00
2021-06-10 01:36:20 +02:00
this._element = new Combine([
leftoverWarning,
ohPicker,
phSelector
])
2020-10-08 19:03:00 +02:00
}
GetValue(): UIEventSource<string> {
return this._value;
}
IsValid(t: string): boolean {
return true;
}
protected InnerConstructElement(): HTMLElement {
return this._element.ConstructElement()
}
2020-10-08 19:03:00 +02:00
}