refactoring

This commit is contained in:
Pieter Vander Vennet 2023-03-28 05:13:48 +02:00
parent b94a8f5745
commit 5d0fe31c41
114 changed files with 2412 additions and 2958 deletions

View file

@ -5,7 +5,6 @@ import Translations from "../../UI/i18n/Translations"
import { TagUtils } from "../../Logic/Tags/TagUtils"
import { TagConfigJson } from "./Json/TagConfigJson"
import { UIEventSource } from "../../Logic/UIEventSource"
import { FilterState } from "../FilteredLayer"
import { QueryParameters } from "../../Logic/Web/QueryParameters"
import { Utils } from "../../Utils"
import { RegexTag } from "../../Logic/Tags/RegexTag"
@ -144,14 +143,7 @@ export default class FilterConfig {
})
}
public initState(): UIEventSource<FilterState> {
function reset(state: FilterState): string {
if (state === undefined) {
return ""
}
return "" + state.state
}
public initState(): UIEventSource<undefined | number | string> {
let defaultValue = ""
if (this.options.length > 1) {
defaultValue = "" + (this.defaultSelection ?? 0)
@ -159,6 +151,8 @@ export default class FilterConfig {
// Only a single option
if (this.defaultSelection === 0) {
defaultValue = "true"
} else {
defaultValue = "false"
}
}
const qp = QueryParameters.GetQueryParameter(
@ -168,12 +162,6 @@ export default class FilterConfig {
)
if (this.options.length > 1) {
// This is a multi-option filter; state should be a number which selects the correct entry
const possibleStates: FilterState[] = this.options.map((opt, i) => ({
currentFilter: opt.osmTags,
state: i,
}))
// We map the query parameter for this case
return qp.sync(
(str) => {
@ -182,62 +170,29 @@ export default class FilterConfig {
// Nope, not a correct number!
return undefined
}
return possibleStates[parsed]
return parsed
},
[],
reset
(n) => "" + n
)
}
const option = this.options[0]
if (option.fields.length > 0) {
return qp.sync(
(str) => {
// There are variables in play!
// str should encode a json-hash
try {
const props = JSON.parse(str)
const origTags = option.originalTagsSpec
const rewrittenTags = Utils.WalkJson(origTags, (v) => {
if (typeof v !== "string") {
return v
}
for (const key in props) {
v = (<string>v).replace("{" + key + "}", props[key])
}
return v
})
const parsed = TagUtils.Tag(rewrittenTags)
return <FilterState>{
currentFilter: parsed,
state: str,
}
} catch (e) {
return undefined
}
},
[],
reset
)
return qp
}
// The last case is pretty boring: it is checked or it isn't
const filterState: FilterState = {
currentFilter: option.osmTags,
state: "true",
}
return qp.sync(
(str) => {
// Only a single option exists here
if (str === "true") {
return filterState
return 0
}
return undefined
},
[],
reset
(n) => (n === undefined ? "false" : "true")
)
}