MapComplete/src/UI/OpeningHours/OpeningHoursState.ts

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

131 lines
4.6 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 conveniently as a string, for direct use
2020-10-08 19:03:00 +02:00
*/
import { Store, UIEventSource } from "../../Logic/UIEventSource"
import { OH, OpeningHour } from "./OpeningHours"
export default class OpeningHoursState {
public readonly normalOhs: UIEventSource<OpeningHour[]>
public readonly leftoverRules: Store<string[]>
public readonly phSelectorValue: UIEventSource<string>
2022-09-08 21:40:48 +02:00
constructor(
value: UIEventSource<string> = new UIEventSource<string>(""),
prefix = "",
2024-10-19 14:44:55 +02:00
postfix = ""
) {
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
2024-10-19 14:44:55 +02:00
}
)
}
2021-06-10 01:36:20 +02:00
this.leftoverRules = 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
}
if (leftOvers.indexOf(rule) >= 0) {
continue
}
2020-10-08 19:03:00 +02:00
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) {
2023-10-18 02:08:49 +02:00
// We found the rule containing the public holiday information
ph = rule
break
}
}
this.phSelectorValue = new UIEventSource<string>(ph ?? "")
2021-06-16 16:39:48 +02:00
// Note: MUST be bound AFTER the leftover rules!
this.normalOhs = valueWithoutPrefix.sync(
(str) => {
return OH.Parse(str)
},
[this.leftoverRules, this.phSelectorValue],
(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 = this.phSelectorValue.data
2022-09-08 21:40:48 +02:00
if (ph) {
str += " " + ph + ";" // There must be a space after every ";"
}
2020-10-08 19:03:00 +02:00
str += this.leftoverRules.data.join("; ") + ";"
2020-10-08 19:03:00 +02:00
2021-06-10 01:36:20 +02:00
str = str.trim()
while (str.endsWith(";")) {
2021-06-10 01:36:20 +02:00
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
2024-10-19 14:44:55 +02:00
}
2022-09-08 21:40:48 +02:00
)
/*
const leftoverWarning = new VariableUiElement(
leftoverRules.map((leftovers: string[]) => {
if (leftovers.length == 0) {
return ""
}
return new Combine([
Translations.t.general.opening_hours.not_all_rules_parsed,
new FixedUiElement(leftovers.map((r) => `${r}<br/>`).join("")).SetClass(
"subtle"
),
])
})
)*/
2020-10-08 19:03:00 +02:00
}
}