forked from MapComplete/MapComplete
parent
5505a896bc
commit
4473560391
5 changed files with 234 additions and 162 deletions
140
UI/BigComponents/LevelSelector.ts
Normal file
140
UI/BigComponents/LevelSelector.ts
Normal file
|
@ -0,0 +1,140 @@
|
|||
import FloorLevelInputElement from "../Input/FloorLevelInputElement";
|
||||
import MapState, {GlobalFilter} from "../../Logic/State/MapState";
|
||||
import {TagsFilter} from "../../Logic/Tags/TagsFilter";
|
||||
import {RegexTag} from "../../Logic/Tags/RegexTag";
|
||||
import {Or} from "../../Logic/Tags/Or";
|
||||
import {Tag} from "../../Logic/Tags/Tag";
|
||||
import Translations from "../i18n/Translations";
|
||||
import Combine from "../Base/Combine";
|
||||
import {OsmFeature} from "../../Models/OsmFeature";
|
||||
import {BBox} from "../../Logic/BBox";
|
||||
import {TagUtils} from "../../Logic/Tags/TagUtils";
|
||||
import FeaturePipeline from "../../Logic/FeatureSource/FeaturePipeline";
|
||||
import {Store} from "../../Logic/UIEventSource";
|
||||
|
||||
/***
|
||||
* The element responsible for the level input element and picking the right level, showing and hiding at the right time, ...
|
||||
*/
|
||||
export default class LevelSelector extends Combine {
|
||||
|
||||
constructor(state: MapState & { featurePipeline: FeaturePipeline }) {
|
||||
|
||||
const levelsInView : Store< Record<string, number>> = state.currentBounds.map(bbox => {
|
||||
if (bbox === undefined) {
|
||||
return {}
|
||||
}
|
||||
const allElementsUnfiltered: OsmFeature[] = [].concat(...state.featurePipeline.GetAllFeaturesAndMetaWithin(bbox).map(ff => ff.features))
|
||||
const allElements = allElementsUnfiltered.filter(f => BBox.get(f).overlapsWith(bbox))
|
||||
const allLevelsRaw: string[] = allElements.map(f => f.properties["level"])
|
||||
|
||||
const levels : Record<string, number> = {"0": 0}
|
||||
for (const levelDescription of allLevelsRaw) {
|
||||
if(levelDescription === undefined){
|
||||
levels["0"] ++
|
||||
}
|
||||
for (const level of TagUtils.LevelsParser(levelDescription)) {
|
||||
levels[level] = (levels[level] ?? 0) + 1
|
||||
}
|
||||
}
|
||||
|
||||
return levels
|
||||
})
|
||||
|
||||
const levelSelect = new FloorLevelInputElement(levelsInView)
|
||||
|
||||
state.globalFilters.data.push({
|
||||
filter: {
|
||||
currentFilter: undefined,
|
||||
state: undefined,
|
||||
|
||||
},
|
||||
id: "level",
|
||||
onNewPoint: undefined
|
||||
})
|
||||
const isShown = levelsInView.map(levelsInView => {
|
||||
if (state.locationControl.data.zoom <= 16) {
|
||||
return false;
|
||||
}
|
||||
if (Object.keys(levelsInView).length == 1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
},
|
||||
[state.locationControl])
|
||||
|
||||
function setLevelFilter() {
|
||||
console.log("Updating levels filter to ", levelSelect.GetValue().data, " is shown:", isShown.data)
|
||||
const filter: GlobalFilter = state.globalFilters.data.find(gf => gf.id === "level")
|
||||
if (!isShown.data) {
|
||||
filter.filter = {
|
||||
state: "*",
|
||||
currentFilter: undefined,
|
||||
}
|
||||
filter.onNewPoint = undefined
|
||||
state.globalFilters.ping();
|
||||
return
|
||||
}
|
||||
|
||||
const l = levelSelect.GetValue().data
|
||||
if(l === undefined){
|
||||
return
|
||||
}
|
||||
|
||||
let neededLevel: TagsFilter = new RegexTag("level", new RegExp("(^|;)" + l + "(;|$)"));
|
||||
if (l === "0") {
|
||||
neededLevel = new Or([neededLevel, new Tag("level", "")])
|
||||
}
|
||||
filter.filter = {
|
||||
state: l,
|
||||
currentFilter: neededLevel
|
||||
}
|
||||
const t = Translations.t.general.levelSelection
|
||||
filter.onNewPoint = {
|
||||
confirmAddNew: t.confirmLevel.PartialSubs({level: l}),
|
||||
safetyCheck: t.addNewOnLevel.Subs({level: l}),
|
||||
tags: [new Tag("level", l)]
|
||||
}
|
||||
state.globalFilters.ping();
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
isShown.addCallbackAndRun(shown => {
|
||||
console.log("Is level selector shown?", shown)
|
||||
setLevelFilter()
|
||||
if (shown) {
|
||||
levelSelect.RemoveClass("invisible")
|
||||
} else {
|
||||
levelSelect.SetClass("invisible")
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
levelsInView.addCallbackAndRun(levels => {
|
||||
if(!isShown.data){
|
||||
return
|
||||
}
|
||||
const value = levelSelect.GetValue()
|
||||
if (!(levels[value.data] === undefined || levels[value.data] === 0)) {
|
||||
return;
|
||||
}
|
||||
// Nothing in view. Lets switch to a different level (the level with the most features)
|
||||
let mostElements = 0
|
||||
let mostElementsLevel = undefined
|
||||
for (const level in levels) {
|
||||
const count = levels[level]
|
||||
if(mostElementsLevel === undefined || mostElements < count){
|
||||
mostElementsLevel = level
|
||||
mostElements = count
|
||||
}
|
||||
}
|
||||
console.log("Force switching to a different level:", mostElementsLevel,"as it has",mostElements,"elements on that floor",levels,"(old level: "+value.data+")")
|
||||
value.setData(mostElementsLevel )
|
||||
|
||||
})
|
||||
levelSelect.GetValue().addCallback(_ => setLevelFilter())
|
||||
super([levelSelect])
|
||||
}
|
||||
|
||||
}
|
|
@ -3,18 +3,13 @@ import Toggle from "../Input/Toggle";
|
|||
import MapControlButton from "../MapControlButton";
|
||||
import GeoLocationHandler from "../../Logic/Actors/GeoLocationHandler";
|
||||
import Svg from "../../Svg";
|
||||
import MapState, {GlobalFilter} from "../../Logic/State/MapState";
|
||||
import LevelSelector from "../Input/LevelSelector";
|
||||
import MapState from "../../Logic/State/MapState";
|
||||
import FeaturePipeline from "../../Logic/FeatureSource/FeaturePipeline";
|
||||
import {Utils} from "../../Utils";
|
||||
import {TagUtils} from "../../Logic/Tags/TagUtils";
|
||||
import {RegexTag} from "../../Logic/Tags/RegexTag";
|
||||
import {Or} from "../../Logic/Tags/Or";
|
||||
import {Tag} from "../../Logic/Tags/Tag";
|
||||
import {TagsFilter} from "../../Logic/Tags/TagsFilter";
|
||||
import Translations from "../i18n/Translations";
|
||||
import {BBox} from "../../Logic/BBox";
|
||||
import {OsmFeature} from "../../Models/OsmFeature";
|
||||
import LevelSelector from "./LevelSelector";
|
||||
|
||||
export default class RightControls extends Combine {
|
||||
|
||||
|
@ -49,91 +44,8 @@ export default class RightControls extends Combine {
|
|||
state.locationControl.ping();
|
||||
});
|
||||
|
||||
const levelsInView = state.currentBounds.map(bbox => {
|
||||
if (bbox === undefined) {
|
||||
return []
|
||||
}
|
||||
const allElementsUnfiltered: OsmFeature[] = [].concat(... state.featurePipeline.GetAllFeaturesAndMetaWithin(bbox).map(ff => ff.features))
|
||||
const allElements = allElementsUnfiltered.filter(f => BBox.get(f).overlapsWith(bbox))
|
||||
const allLevelsRaw: string[] = allElements.map(f => f.properties["level"])
|
||||
const allLevels = [].concat(...allLevelsRaw.map(l => TagUtils.LevelsParser(l)))
|
||||
if (allLevels.indexOf("0") < 0) {
|
||||
allLevels.push("0")
|
||||
}
|
||||
allLevels.sort((a, b) => a < b ? -1 : 1)
|
||||
return Utils.Dedup(allLevels)
|
||||
})
|
||||
state.globalFilters.data.push({
|
||||
filter: {
|
||||
currentFilter: undefined,
|
||||
state: undefined,
|
||||
|
||||
},
|
||||
id: "level",
|
||||
onNewPoint: undefined
|
||||
})
|
||||
const levelSelect = new LevelSelector(levelsInView)
|
||||
|
||||
const isShown = levelsInView.map(levelsInView => {
|
||||
if (levelsInView.length == 0) {
|
||||
return false;
|
||||
}
|
||||
if (state.locationControl.data.zoom <= 16) {
|
||||
return false;
|
||||
}
|
||||
if (levelsInView.length == 1 && levelsInView[0] == "0") {
|
||||
return false
|
||||
}
|
||||
return true;
|
||||
},
|
||||
[state.locationControl])
|
||||
|
||||
function setLevelFilter() {
|
||||
console.log("Updating levels filter")
|
||||
const filter: GlobalFilter = state.globalFilters.data.find(gf => gf.id === "level")
|
||||
if (!isShown.data) {
|
||||
filter.filter = {
|
||||
state: "*",
|
||||
currentFilter: undefined,
|
||||
}
|
||||
filter.onNewPoint = undefined
|
||||
|
||||
} else {
|
||||
|
||||
const l = levelSelect.GetValue().data
|
||||
let neededLevel: TagsFilter = new RegexTag("level", new RegExp("(^|;)" + l + "(;|$)"));
|
||||
if (l === "0") {
|
||||
neededLevel = new Or([neededLevel, new Tag("level", "")])
|
||||
}
|
||||
filter.filter = {
|
||||
state: l,
|
||||
currentFilter: neededLevel
|
||||
}
|
||||
const t = Translations.t.general.levelSelection
|
||||
filter.onNewPoint = {
|
||||
confirmAddNew: t.confirmLevel.PartialSubs({level: l}),
|
||||
safetyCheck: t.addNewOnLevel.Subs({level: l}),
|
||||
tags: [new Tag("level", l)]
|
||||
}
|
||||
}
|
||||
state.globalFilters.ping();
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
isShown.addCallbackAndRun(shown => {
|
||||
console.log("Is level selector shown?", shown)
|
||||
setLevelFilter()
|
||||
if (shown) {
|
||||
levelSelect.RemoveClass("invisible")
|
||||
} else {
|
||||
levelSelect.SetClass("invisible")
|
||||
}
|
||||
})
|
||||
|
||||
levelSelect.GetValue().addCallback(_ => setLevelFilter())
|
||||
|
||||
super([new Combine([levelSelect]).SetClass(""), plus, min, geolocationButton].map(el => el.SetClass("m-0.5 md:m-1")))
|
||||
const levelSelector = new LevelSelector(state);
|
||||
super([levelSelector, plus, min, geolocationButton].map(el => el.SetClass("m-0.5 md:m-1")))
|
||||
this.SetClass("flex flex-col items-center")
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue