mirror of
https://github.com/umap-project/umap.git
synced 2025-04-29 03:42:37 +02:00
feat: allow to search by code INSEE in communes importer (#2188)
Otherwise when searching for a very commun name (like "Saint-Paul"), one may not find the right one among the x returned results.   
This commit is contained in:
commit
a2d0d14857
2 changed files with 18 additions and 2 deletions
|
@ -249,6 +249,10 @@ export class BaseAjax extends BaseAutocomplete {
|
|||
}
|
||||
}
|
||||
|
||||
buildUrl(value) {
|
||||
return Util.template(this.url, { q: encodeURIComponent(value) })
|
||||
}
|
||||
|
||||
async search() {
|
||||
let val = this.input.value
|
||||
if (val.length < this.options.minChar) {
|
||||
|
@ -258,7 +262,7 @@ export class BaseAjax extends BaseAutocomplete {
|
|||
if (val === this.cache) return
|
||||
this.cache = val
|
||||
val = val.toLowerCase()
|
||||
const url = Util.template(this.url, { q: encodeURIComponent(val) })
|
||||
const url = this.buildUrl(val)
|
||||
this.handleResults(await this._search(url))
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import { DomUtil } from '../../../vendors/leaflet/leaflet-src.esm.js'
|
||||
import { BaseAjax, SingleMixin } from '../autocomplete.js'
|
||||
import * as Util from '../utils.js'
|
||||
|
||||
class Autocomplete extends SingleMixin(BaseAjax) {
|
||||
createResult(item) {
|
||||
|
@ -8,6 +9,17 @@ class Autocomplete extends SingleMixin(BaseAjax) {
|
|||
label: `${item.nom} (${item.code})`,
|
||||
})
|
||||
}
|
||||
|
||||
buildUrl(value) {
|
||||
let url = this.url
|
||||
let options = { q: encodeURIComponent(value) }
|
||||
const re = /^(0[1-9]|[1-9][ABab\d])\d{3}$/gm
|
||||
if (re.test(value)) {
|
||||
url = "https://geo.api.gouv.fr/communes?code={code}&limit=5"
|
||||
options = { code: encodeURIComponent(value) }
|
||||
}
|
||||
return Util.template(url, options)
|
||||
}
|
||||
}
|
||||
|
||||
export class Importer {
|
||||
|
@ -25,7 +37,7 @@ export class Importer {
|
|||
textContent: "Importer les contours d'une commune française.",
|
||||
})
|
||||
const options = {
|
||||
placeholder: 'Commune…',
|
||||
placeholder: 'Nom ou code INSEE…',
|
||||
url: 'https://geo.api.gouv.fr/communes?nom={q}&limit=5',
|
||||
on_select: (choice) => {
|
||||
importer.url = `https://geo.api.gouv.fr/communes?code=${choice.item.value}&format=geojson&geometry=contour`
|
||||
|
|
Loading…
Reference in a new issue