@open-commune/web 0.2.7 → 0.2.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -36,6 +36,20 @@
|
|
|
36
36
|
</div>
|
|
37
37
|
</transition>
|
|
38
38
|
</div>
|
|
39
|
+
<p
|
|
40
|
+
v-if="isMissingCoordinates"
|
|
41
|
+
class="mt-2 flex flex-wrap items-center gap-x-2 rounded-md bg-amber-50 px-3 py-2 text-sm text-amber-800"
|
|
42
|
+
role="status"
|
|
43
|
+
>
|
|
44
|
+
<span>Cette adresse n'est pas géolocalisée : la carte du site ne peut pas la situer. Choisissez-la dans les suggestions.</span>
|
|
45
|
+
<button
|
|
46
|
+
type="button"
|
|
47
|
+
class="font-medium underline"
|
|
48
|
+
@click="_suggestFromAddress"
|
|
49
|
+
>
|
|
50
|
+
Voir les suggestions
|
|
51
|
+
</button>
|
|
52
|
+
</p>
|
|
39
53
|
<div class="w-full mt-2 border border-gray-200 rounded-md" style="min-height: 18rem;">
|
|
40
54
|
<div id="map" class="w-full h-full" style="min-height: 18rem;" />
|
|
41
55
|
</div>
|
|
@@ -47,6 +61,7 @@ import { onClickOutside, useDebounceFn } from '@vueuse/core'
|
|
|
47
61
|
import axios from 'axios'
|
|
48
62
|
import mapbox, { Map, Marker } from 'mapbox-gl'
|
|
49
63
|
import inputText from '~/components/forms/inputs/input-text'
|
|
64
|
+
import { FALLBACK_CENTER, FALLBACK_ZOOM, LOCATION_ZOOM, toLngLat } from '~/utils/location'
|
|
50
65
|
|
|
51
66
|
// Prepare composable
|
|
52
67
|
const $config = useRuntimeConfig()
|
|
@@ -89,6 +104,8 @@ const isLoading = ref(false)
|
|
|
89
104
|
|
|
90
105
|
// Prepare computed
|
|
91
106
|
const places = computed(() => list.value.map(({ id, place_name_fr: place, center }) => ({ id, place, center })))
|
|
107
|
+
const lngLat = computed(() => toLngLat(selection.value?.coordinates))
|
|
108
|
+
const isMissingCoordinates = computed(() => !!selection.value?.address && !lngLat.value)
|
|
92
109
|
|
|
93
110
|
// Prepare methods
|
|
94
111
|
const _createMap = () => {
|
|
@@ -96,27 +113,33 @@ const _createMap = () => {
|
|
|
96
113
|
map.value = new Map({
|
|
97
114
|
container: 'map',
|
|
98
115
|
style: 'mapbox://styles/mapbox/light-v11?optimized=true',
|
|
99
|
-
|
|
100
|
-
|
|
116
|
+
// Without a position, Mapbox would centre on [0, 0], the open ocean.
|
|
117
|
+
zoom: lngLat.value ? LOCATION_ZOOM : FALLBACK_ZOOM,
|
|
118
|
+
center: lngLat.value || FALLBACK_CENTER,
|
|
101
119
|
attributionControl: false,
|
|
102
120
|
interactive: true
|
|
103
121
|
})
|
|
104
122
|
map.value.on('load', _updateMarker)
|
|
105
123
|
}
|
|
106
124
|
const _updateMarker = () => {
|
|
107
|
-
if (
|
|
125
|
+
if (!map.value) {
|
|
126
|
+
return
|
|
127
|
+
}
|
|
128
|
+
marker.value?.remove()
|
|
129
|
+
marker.value = undefined
|
|
130
|
+
if (lngLat.value) {
|
|
108
131
|
marker.value = new Marker({ color: '#000' })
|
|
109
|
-
.setLngLat(
|
|
132
|
+
.setLngLat(lngLat.value)
|
|
110
133
|
.addTo(map.value)
|
|
111
|
-
map.value.flyTo({ center:
|
|
134
|
+
map.value.flyTo({ center: lngLat.value, zoom: LOCATION_ZOOM })
|
|
112
135
|
}
|
|
113
136
|
}
|
|
114
|
-
const _findLocation = useDebounceFn(async () => {
|
|
115
|
-
if (!isLoading.value &&
|
|
137
|
+
const _findLocation = useDebounceFn(async (query = search.value) => {
|
|
138
|
+
if (!isLoading.value && query) {
|
|
116
139
|
isLoading.value = true
|
|
117
140
|
try {
|
|
118
141
|
const { data: { features } } = await MAPBOX_CLIENT.get(
|
|
119
|
-
`/${
|
|
142
|
+
`/${encodeURIComponent(query)}.json?access_token=${$config.public.mapBoxToken}&types=postcode,district,place,locality,neighborhood,address,poi&autocomplete=true&country=fr&language=fr`
|
|
120
143
|
)
|
|
121
144
|
list.value = [...features]
|
|
122
145
|
} catch (err) {
|
|
@@ -127,7 +150,8 @@ const _findLocation = useDebounceFn(async () => {
|
|
|
127
150
|
}
|
|
128
151
|
}, 300)
|
|
129
152
|
const _select = (id) => {
|
|
130
|
-
const { place_name_fr: address, center
|
|
153
|
+
const { place_name_fr: address, center } = list.value.find(({ id: check }) => check === id) || {}
|
|
154
|
+
const coordinates = toLngLat(center)
|
|
131
155
|
if (address && coordinates) {
|
|
132
156
|
selection.value = {
|
|
133
157
|
address,
|
|
@@ -141,6 +165,11 @@ const _select = (id) => {
|
|
|
141
165
|
const _toggle = () => {
|
|
142
166
|
display.value = !display.value
|
|
143
167
|
}
|
|
168
|
+
// Geocodes the stored address so a legacy, never-geocoded value can be fixed in one click.
|
|
169
|
+
const _suggestFromAddress = () => {
|
|
170
|
+
display.value = true
|
|
171
|
+
_findLocation(selection.value.address)
|
|
172
|
+
}
|
|
144
173
|
const _close = () => {
|
|
145
174
|
search.value = ''
|
|
146
175
|
display.value = false
|
|
@@ -163,10 +192,14 @@ watch(
|
|
|
163
192
|
watch(
|
|
164
193
|
() => $props.value,
|
|
165
194
|
(newValue) => {
|
|
166
|
-
|
|
195
|
+
// Keep an address without coordinates too: the field shows it and says it is not on the map.
|
|
196
|
+
if (newValue?.address) {
|
|
167
197
|
selection.value = {
|
|
168
198
|
address: newValue.address,
|
|
169
|
-
coordinates: newValue.coordinates,
|
|
199
|
+
coordinates: newValue.coordinates || [],
|
|
200
|
+
}
|
|
201
|
+
if (map.value?.loaded()) {
|
|
202
|
+
_updateMarker()
|
|
170
203
|
}
|
|
171
204
|
}
|
|
172
205
|
},
|
package/package.json
CHANGED
|
@@ -17,8 +17,8 @@
|
|
|
17
17
|
"@iconify/vue": "^5.0.3",
|
|
18
18
|
"@nuxt/kit": "^4.6.1",
|
|
19
19
|
"@nuxt/ui": "^4.12.0",
|
|
20
|
-
"@open-commune/core": "^0.2.
|
|
21
|
-
"@open-commune/editor": "^0.
|
|
20
|
+
"@open-commune/core": "^0.2.13",
|
|
21
|
+
"@open-commune/editor": "^0.2.13",
|
|
22
22
|
"@pinia-orm/nuxt": "^2.0.0",
|
|
23
23
|
"@pinia/nuxt": "^1.0.3",
|
|
24
24
|
"@tanstack/vue-query": "^5.104.3",
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
},
|
|
41
41
|
"description": "The Open Commune application: the admin interface and the server, on a single origin.",
|
|
42
42
|
"license": "AGPL-3.0-or-later",
|
|
43
|
-
"version": "0.2.
|
|
43
|
+
"version": "0.2.13",
|
|
44
44
|
"type": "module",
|
|
45
45
|
"main": "./nuxt.config.ts",
|
|
46
46
|
"files": [
|
|
@@ -59,7 +59,8 @@
|
|
|
59
59
|
"plugins",
|
|
60
60
|
"public",
|
|
61
61
|
"server",
|
|
62
|
-
"store"
|
|
62
|
+
"store",
|
|
63
|
+
"utils"
|
|
63
64
|
],
|
|
64
65
|
"publishConfig": {
|
|
65
66
|
"access": "public"
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Location values of the `input-location` widget: `{ address, coordinates }`,
|
|
3
|
+
* where `coordinates` is a Mapbox `[longitude, latitude]` pair.
|
|
4
|
+
*
|
|
5
|
+
* Legacy records carry an address with `coordinates: []` (typed by hand, never
|
|
6
|
+
* geocoded). A map centred on such a value falls back to `[0, 0]` — the open
|
|
7
|
+
* ocean in the Gulf of Guinea — so every reader checks the pair first.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
// Metropolitan France, the view shown while a location has no coordinates.
|
|
11
|
+
export const FALLBACK_CENTER = [2.35, 46.6];
|
|
12
|
+
export const FALLBACK_ZOOM = 5;
|
|
13
|
+
export const LOCATION_ZOOM = 15;
|
|
14
|
+
|
|
15
|
+
/** The `[lng, lat]` pair as numbers, or `null` when it is not a usable position. */
|
|
16
|
+
export const toLngLat = (coordinates) => {
|
|
17
|
+
if (!Array.isArray(coordinates) || coordinates.length !== 2) {
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
const [lng, lat] = coordinates.map((value) =>
|
|
21
|
+
value === '' || value === null ? Number.NaN : Number(value),
|
|
22
|
+
);
|
|
23
|
+
if (!Number.isFinite(lng) || !Number.isFinite(lat)) {
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
if (lng < -180 || lng > 180 || lat < -90 || lat > 90) {
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
// [0, 0] is what a failed geocoding leaves behind, never a commune.
|
|
30
|
+
if (lng === 0 && lat === 0) {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
return [lng, lat];
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export const hasCoordinates = (location) => toLngLat(location?.coordinates) !== null;
|