Skip to content

Commit

Permalink
Search: close dialog when appropriate, move special layer logic to th…
Browse files Browse the repository at this point in the history
…emeViewState
  • Loading branch information
pietervdvn committed Sep 12, 2024
1 parent 902a479 commit 05e5a63
Show file tree
Hide file tree
Showing 11 changed files with 165 additions and 81 deletions.
42 changes: 42 additions & 0 deletions langs/layers/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -11657,6 +11657,48 @@
"question": "Show the raw OpenStreetMap-tags?",
"questionHint": "<b>Tags</b> are attributes that every element has. This is the technical data that is stored in the database. You don't need this information to edit with MapComplete, but advanced users might want to use this as reference."
},
"sync-visited-locations": {
"mappings": {
"0": {
"then": "Save the locations you search for and inspect and sync them via openstreetmap.org. OpenStreetMap and all apps you use can see this history"
},
"1": {
"then": "Save the locations you search for and inspect on my device"
},
"2": {
"then": "Don't save the locations you search for and inspect "
}
},
"question": "Should the locations you search for and inspect be remembered?",
"questionHint": "Those locations will be offered in the search menu"
},
"sync-visited-themes": {
"mappings": {
"0": {
"then": "Save the visited thematic maps and sync them via openstreetmap.org. OpenStreetMap and all apps you use can see this history"
},
"1": {
"then": "Save the visited thematic maps on my device"
},
"2": {
"then": "Don't save visited thematic maps"
}
},
"question": "Should the thematic maps you visit be saved?",
"questionHint": "If you visit a map about a certain topic, MapComplete can remember this and offer this as suggestion."
},
"title-editing": {
"render": "<h3>Editing settings</h3>"
},
"title-id": {
"render": "<h3>Mangrove ID management</h3>"
},
"title-map": {
"render": "<h3>Configure map</h3>"
},
"title-privacy-legal": {
"render": "<h3>Privacy and legal</h3>"
},
"translation-completeness": {
"mappings": {
"0": {
Expand Down
7 changes: 6 additions & 1 deletion src/Logic/Osm/OsmPreferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ export class OsmPreferences {
this.setPreferencesAll(key, initialValue)
}
pref.addCallback(v => {
length.set(Math.ceil(v.length / maxLength))
if(v === null || v === undefined || v === ""){
length.set(null)
return
}
length.set(Math.ceil((v?.length ?? 1) / maxLength))
let i = 0
while (v.length > 0) {
this.UploadPreference(key + "-" + i, v.substring(0, maxLength))
Expand All @@ -97,6 +101,7 @@ export class OsmPreferences {
}
}


/**
* OSM preferences can be at most 255 chars.
* This method chains multiple together.
Expand Down
6 changes: 4 additions & 2 deletions src/Logic/Search/FilterSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import Locale from "../../UI/i18n/Locale"
import Constants from "../../Models/Constants"
import FilterConfig, { FilterConfigOption } from "../../Models/ThemeConfig/FilterConfig"
import LayerConfig from "../../Models/ThemeConfig/LayerConfig"
import LayerState from "../State/LayerState"
import LayoutConfig from "../../Models/ThemeConfig/LayoutConfig"

export type FilterSearchResult = { option: FilterConfigOption, filter: FilterConfig, layer: LayerConfig, index: number }

Expand All @@ -12,9 +14,9 @@ export type FilterSearchResult = { option: FilterConfigOption, filter: FilterCon
* Searches matching filters
*/
export default class FilterSearch {
private readonly _state: SpecialVisualizationState
private readonly _state: {layerState: LayerState, layout: LayoutConfig}

constructor(state: SpecialVisualizationState) {
constructor(state: {layerState: LayerState, layout: LayoutConfig}) {
this._state = state
}

Expand Down
12 changes: 6 additions & 6 deletions src/Logic/Search/LayerSearch.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { SpecialVisualizationState } from "../../UI/SpecialVisualization"
import Constants from "../../Models/Constants"
import SearchUtils from "./SearchUtils"
import ThemeSearch from "./ThemeSearch"
import LayerConfig from "../../Models/ThemeConfig/LayerConfig"
import LayoutConfig from "../../Models/ThemeConfig/LayoutConfig"

export default class LayerSearch {

private readonly _state: SpecialVisualizationState
private readonly _layout: LayoutConfig
private readonly _layerWhitelist : Set<string>
constructor(state: SpecialVisualizationState) {
this._state = state
this._layerWhitelist = new Set(state.layout.layers.map(l => l.id).filter(id => Constants.added_by_default.indexOf(<any> id) < 0))
constructor(layout: LayoutConfig) {
this._layout = layout
this._layerWhitelist = new Set(layout.layers.map(l => l.id).filter(id => Constants.added_by_default.indexOf(<any> id) < 0))
}

static scoreLayers(query: string, layerWhitelist?: Set<string>): Record<string, number> {
Expand All @@ -35,7 +35,7 @@ export default class LayerSearch {
const asList:({layer: LayerConfig, score:number})[] = []
for (const layer in scores) {
asList.push({
layer: this._state.layout.getLayer(layer),
layer: this._layout.getLayer(layer),
score: scores[layer]
})
}
Expand Down
14 changes: 6 additions & 8 deletions src/Logic/Search/ThemeSearch.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { MinimalLayoutInformation } from "../../Models/ThemeConfig/LayoutConfig"
import { SpecialVisualizationState } from "../../UI/SpecialVisualization"
import LayoutConfig, { MinimalLayoutInformation } from "../../Models/ThemeConfig/LayoutConfig"
import { Store } from "../UIEventSource"
import UserRelatedState from "../State/UserRelatedState"
import { Utils } from "../../Utils"
import Locale from "../../UI/i18n/Locale"
import themeOverview from "../../assets/generated/theme_overview.json"
import LayerSearch from "./LayerSearch"
import SearchUtils from "./SearchUtils"
import { OsmConnection } from "../Osm/OsmConnection"


type ThemeSearchScore = {
Expand All @@ -22,7 +22,7 @@ export default class ThemeSearch {
public static readonly officialThemes: {
themes: MinimalLayoutInformation[],
layers: Record<string, Record<string, string[]>>
} = themeOverview
} = <any> themeOverview
public static readonly officialThemesById: Map<string, MinimalLayoutInformation> = new Map<string, MinimalLayoutInformation>()
static {
for (const th of ThemeSearch.officialThemes.themes ?? []) {
Expand All @@ -31,15 +31,13 @@ export default class ThemeSearch {
}


private readonly _state: SpecialVisualizationState
private readonly _knownHiddenThemes: Store<Set<string>>
private readonly _layersToIgnore: string[]
private readonly _otherThemes: MinimalLayoutInformation[]

constructor(state: SpecialVisualizationState) {
this._state = state
constructor(state: {osmConnection: OsmConnection, layout: LayoutConfig}) {
this._layersToIgnore = state.layout.layers.map(l => l.id)
this._knownHiddenThemes = UserRelatedState.initDiscoveredHiddenThemes(this._state.osmConnection).map(list => new Set(list))
this._knownHiddenThemes = UserRelatedState.initDiscoveredHiddenThemes(state.osmConnection).map(list => new Set(list))
this._otherThemes = ThemeSearch.officialThemes.themes
.filter(th => th.id !== state.layout.id)
}
Expand Down Expand Up @@ -144,7 +142,7 @@ export default class ThemeSearch {
return scored
}

public static sortedByLowest(search: string, themes: MinimalLayoutInformation[], ignoreLayers: string[] = [], maxDiff: number): MinimalLayoutInformation[] {
public static sortedByLowest(search: string, themes: MinimalLayoutInformation[], ignoreLayers: string[] = []): MinimalLayoutInformation[] {
return this.sortedByLowestScores(search, themes, ignoreLayers)
.map(th => th.theme)
}
Expand Down
35 changes: 21 additions & 14 deletions src/Logic/State/SearchState.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import GeocodingProvider, { GeocodingUtils, type SearchResult } from "../Search/GeocodingProvider"
import GeocodingProvider, { type SearchResult } from "../Search/GeocodingProvider"
import { ImmutableStore, Store, Stores, UIEventSource } from "../UIEventSource"
import CombinedSearcher from "../Search/CombinedSearcher"
import FilterSearch, { FilterSearchResult } from "../Search/FilterSearch"
Expand All @@ -11,9 +11,10 @@ import ThemeViewState from "../../Models/ThemeViewState"
import type { MinimalLayoutInformation } from "../../Models/ThemeConfig/LayoutConfig"
import { Translation } from "../../UI/i18n/Translation"
import GeocodingFeatureSource from "../Search/GeocodingFeatureSource"
import ShowDataLayer from "../../UI/Map/ShowDataLayer"
import LayerSearch from "../Search/LayerSearch"
import LayerConfig from "../../Models/ThemeConfig/LayerConfig"
import { FeatureSource } from "../FeatureSource/FeatureSource"
import { Feature } from "geojson"

export default class SearchState {

Expand All @@ -29,6 +30,7 @@ export default class SearchState {
private readonly state: ThemeViewState
public readonly showSearchDrawer: UIEventSource<boolean>
public readonly suggestionsSearchRunning: Store<boolean>
public readonly locationResults: FeatureSource

constructor(state: ThemeViewState) {
this.state = state
Expand Down Expand Up @@ -62,7 +64,7 @@ export default class SearchState {
const themeSearch = new ThemeSearch(state)
this.themeSuggestions = this.searchTerm.mapD(query => themeSearch.search(query, 3))

const layerSearch = new LayerSearch(state)
const layerSearch = new LayerSearch(state.layout)
this.layerSuggestions = this.searchTerm.mapD(query => layerSearch.search(query, 5))

const filterSearch = new FilterSearch(state)
Expand All @@ -77,17 +79,7 @@ export default class SearchState {
return !foundMatch
})
}, [state.layerState.activeFilters])
const geocodedFeatures = new GeocodingFeatureSource(this.suggestions.stabilized(250))
state.featureProperties.trackFeatureSource(geocodedFeatures)

new ShowDataLayer(
state.map,
{
layer: GeocodingUtils.searchLayer,
features: geocodedFeatures,
selectedElement: state.selectedElement,
},
)
this.locationResults =new GeocodingFeatureSource(this.suggestions.stabilized(250))

this.showSearchDrawer = new UIEventSource(false)

Expand Down Expand Up @@ -131,4 +123,19 @@ export default class SearchState {
}
}

closeIfFullscreen() {
if(window.innerWidth < 640){
this.showSearchDrawer.set(false)
}
}

clickedOnMap(feature: Feature) {
const osmid = feature.properties.osm_id
const localElement = this.state.indexedFeatures.featuresById.data.get(osmid)
if(localElement){
this.state.selectedElement.set(localElement)
return
}
console.log(">>>",feature)
}
}
2 changes: 1 addition & 1 deletion src/Models/Constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export default class Constants {
"last_click",
"favourite",
"summary",
"search"
] as const
/**
* Special layers which are not included in a theme by default
Expand All @@ -39,7 +40,6 @@ export default class Constants {
"usersettings",
"icons",
"filters",
"search"
] as const
/**
* Layer IDs of layers which have special properties through built-in hooks
Expand Down
Loading

0 comments on commit 05e5a63

Please sign in to comment.