mapcomplete/UI/OpeningHours/OpeningHoursInput.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

153 lines
5.2 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 { Store, UIEventSource } from "../../Logic/UIEventSource"
import { VariableUiElement } from "../Base/VariableUIElement"
import Combine from "../Base/Combine"
import { FixedUiElement } from "../Base/FixedUiElement"
import { OH, OpeningHour } from "./OpeningHours"
import { InputElement } from "../Input/InputElement"
import PublicHolidayInput from "./PublicHolidayInput"
import Translations from "../i18n/Translations"
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
2022-09-08 21:40:48 +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 !== "") {
valueWithoutPrefix = value.sync(
(str) => {
if (str === undefined) {
return undefined
2022-09-08 21:40:48 +02:00
}
2021-11-07 16:34:51 +01:00
if (str === "") {
return ""
2022-09-08 21:40:48 +02:00
}
if (str.startsWith(prefix) && str.endsWith(postfix)) {
return str.substring(prefix.length, str.length - postfix.length)
2022-09-08 21:40:48 +02:00
}
return str
2022-09-08 21:40:48 +02:00
},
[],
2021-11-07 16:34:51 +01:00
(noPrefix) => {
if (noPrefix === undefined) {
return undefined
2022-09-08 21:40:48 +02:00
}
2021-11-07 16:34:51 +01:00
if (noPrefix === "") {
return ""
2022-09-08 21:40:48 +02:00
}
if (noPrefix.startsWith(prefix) && noPrefix.endsWith(postfix)) {
return noPrefix
2022-09-08 21:40:48 +02:00
}
return prefix + noPrefix + postfix
}
)
}
2021-06-10 01:36:20 +02:00
const leftoverRules: Store<string[]> = valueWithoutPrefix.map((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
})
2022-09-08 21:40:48 +02:00
let ph = ""
const rules = valueWithoutPrefix.data?.split(";") ?? []
for (const rule of rules) {
if (OH.ParsePHRule(rule) !== null) {
ph = rule
break
}
}
const phSelector = new PublicHolidayInput(new UIEventSource<string>(ph))
2022-09-08 21:40:48 +02:00
2021-06-16 16:39:48 +02:00
// Note: MUST be bound AFTER the leftover rules!
const rulesFromOhPicker: UIEventSource<OpeningHour[]> = valueWithoutPrefix.sync(
(str) => {
return OH.Parse(str)
},
[leftoverRules, phSelector.GetValue()],
(rules, oldString) => {
// We always add a ';', to easily add new rules. We remove the ';' again at the end of the function
// Important: spaces are _not_ allowed after a ';' as it'll destabilize the parsing!
let str = OH.ToString(rules) + ";"
const ph = phSelector.GetValue().data
2022-09-08 21:40:48 +02:00
if (ph) {
str += ph + ";"
}
2020-10-08 19:03:00 +02:00
str += leftoverRules.data.join(";") + ";"
2020-10-08 19:03:00 +02:00
2021-06-10 01:36:20 +02:00
str = str.trim()
if (str.endsWith(";")) {
str = str.substring(0, str.length - 1)
2022-09-08 21:40:48 +02:00
}
2021-06-10 01:36:20 +02:00
if (str.startsWith(";")) {
str = str.substring(1)
2022-09-08 21:40:48 +02:00
}
str.trim()
2020-10-08 19:03:00 +02:00
if (str === oldString) {
return oldString // We pass a reference to the old string to stabilize the EventSource
2022-09-08 21:40:48 +02:00
}
2020-10-08 19:03:00 +02:00
return str
}
2021-06-10 01:36:20 +02:00
)
2020-10-08 19:03:00 +02:00
2021-06-10 01:36:20 +02:00
const leftoverWarning = new VariableUiElement(
leftoverRules.map((leftovers: string[]) => {
if (leftovers.length == 0) {
return ""
2022-09-08 21:40:48 +02:00
}
2020-10-08 19:03:00 +02:00
return new Combine([
Translations.t.general.opening_hours.not_all_rules_parsed,
new FixedUiElement(leftovers.map((r) => `${r}<br/>`).join("")).SetClass(
2022-09-08 21:40:48 +02:00
"subtle"
),
])
2020-10-08 19:03:00 +02:00
})
2022-09-08 21:40:48 +02:00
)
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
}