forked from MapComplete/MapComplete
Add themes to search functionality, including quickswitch between recent themes
This commit is contained in:
parent
b4866cdbac
commit
329865a15e
22 changed files with 679 additions and 431 deletions
|
@ -1,12 +1,13 @@
|
|||
import GeocodingProvider, { GeoCodeResult, GeocodingOptions } from "./GeocodingProvider"
|
||||
import { Utils } from "../../Utils"
|
||||
|
||||
export default class CombinedSearcher implements GeocodingProvider {
|
||||
private _providers: ReadonlyArray<GeocodingProvider>
|
||||
private _providersWithSuggest: ReadonlyArray<GeocodingProvider>
|
||||
|
||||
constructor(...providers: ReadonlyArray<GeocodingProvider>) {
|
||||
this._providers = providers
|
||||
this._providersWithSuggest = providers.filter(pr => pr.suggest !== undefined)
|
||||
this._providers = Utils.NoNull(providers)
|
||||
this._providersWithSuggest = this._providers.filter(pr => pr.suggest !== undefined)
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -24,7 +24,7 @@ export type GeoCodeResult = {
|
|||
osm_type?: "node" | "way" | "relation"
|
||||
osm_id?: string,
|
||||
category?: GeocodingCategory,
|
||||
importance?: number
|
||||
payload?: object
|
||||
}
|
||||
|
||||
export interface GeocodingOptions {
|
||||
|
|
|
@ -7,21 +7,20 @@ import LayoutConfig from "../../Models/ThemeConfig/LayoutConfig"
|
|||
|
||||
export class RecentSearch {
|
||||
|
||||
private readonly _recentSearches: UIEventSource<string[]>
|
||||
public readonly recentSearches: Store<string[]>
|
||||
|
||||
private readonly _seenThisSession: UIEventSource<GeoCodeResult[]> = new UIEventSource<GeoCodeResult[]>([])
|
||||
public readonly seenThisSession: Store<GeoCodeResult[]> = this._seenThisSession
|
||||
private readonly _seenThisSession: UIEventSource<GeoCodeResult[]>
|
||||
public readonly seenThisSession: Store<GeoCodeResult[]>
|
||||
|
||||
constructor(state: { layout: LayoutConfig, osmConnection: OsmConnection, selectedElement: Store<Feature> }) {
|
||||
const longPref = state.osmConnection.preferencesHandler.GetLongPreference("recent-searches")
|
||||
this._recentSearches = longPref.sync(str => !str ? [] : <string[]>JSON.parse(str), [], strs => JSON.stringify(strs))
|
||||
this.recentSearches = this._recentSearches
|
||||
// const prefs = state.osmConnection.preferencesHandler.GetLongPreference("previous-searches")
|
||||
this._seenThisSession = new UIEventSource<GeoCodeResult[]>([])//UIEventSource.asObject<GeoCodeResult[]>(prefs, [])
|
||||
this.seenThisSession = this._seenThisSession
|
||||
|
||||
|
||||
state.selectedElement.addCallbackAndRunD(selected => {
|
||||
const [osm_type, osm_id] = selected.properties.id.split("/")
|
||||
const [lon, lat] = GeoOperations.centerpointCoordinates(selected)
|
||||
const entry = <GeoCodeResult> {
|
||||
const entry = <GeoCodeResult>{
|
||||
feature: selected,
|
||||
osm_id, osm_type,
|
||||
description: "Viewed recently",
|
||||
|
@ -33,7 +32,7 @@ export class RecentSearch {
|
|||
}
|
||||
|
||||
addSelected(entry: GeoCodeResult) {
|
||||
const arr = [...this.seenThisSession.data.slice(0, 20), entry]
|
||||
const arr = [...(this.seenThisSession.data ?? []).slice(0, 20), entry]
|
||||
|
||||
const seenIds = new Set<string>()
|
||||
for (let i = arr.length - 1; i >= 0; i--) {
|
||||
|
|
43
src/Logic/Geocoding/ThemeSearch.ts
Normal file
43
src/Logic/Geocoding/ThemeSearch.ts
Normal file
|
@ -0,0 +1,43 @@
|
|||
import GeocodingProvider, { GeoCodeResult, GeocodingOptions } from "./GeocodingProvider"
|
||||
import * as themeOverview from "../../assets/generated/theme_overview.json"
|
||||
import { MinimalLayoutInformation } from "../../Models/ThemeConfig/LayoutConfig"
|
||||
import { SpecialVisualizationState } from "../../UI/SpecialVisualization"
|
||||
import { Utils } from "../../Utils"
|
||||
import MoreScreen from "../../UI/BigComponents/MoreScreen"
|
||||
import { Store } from "../UIEventSource"
|
||||
|
||||
export default class ThemeSearch implements GeocodingProvider {
|
||||
|
||||
private static allThemes: MinimalLayoutInformation[] = (themeOverview["default"] ?? themeOverview)
|
||||
private readonly _state: SpecialVisualizationState
|
||||
private readonly _knownHiddenThemes: Store<Set<string>>
|
||||
|
||||
constructor(state: SpecialVisualizationState) {
|
||||
this._state = state
|
||||
this._knownHiddenThemes = MoreScreen.knownHiddenThemes(this._state.osmConnection)
|
||||
}
|
||||
|
||||
search(query: string, options?: GeocodingOptions): Promise<GeoCodeResult[]> {
|
||||
return this.suggest(query, options)
|
||||
}
|
||||
|
||||
async suggest?(query: string, options?: GeocodingOptions): Promise<GeoCodeResult[]> {
|
||||
if(query.length < 1){
|
||||
return []
|
||||
}
|
||||
const limit = options?.limit ?? 4
|
||||
query = Utils.simplifyStringForSearch(query)
|
||||
const withMatch = ThemeSearch.allThemes
|
||||
.filter(th => !th.hideFromOverview )
|
||||
.filter(th => th.id !== this._state.layout.id)
|
||||
.filter(th => MoreScreen.MatchesLayout(th, query))
|
||||
.slice(0, limit + 1)
|
||||
|
||||
return withMatch.map(match => (<GeoCodeResult> {
|
||||
payload: match,
|
||||
osm_id: match.id
|
||||
}))
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue