1
- import {
2
- generateSimpleGetters ,
3
- generateSimpleMutations ,
4
- } from '@repositoryname/vuex-generators'
1
+ import { generateSimpleMutations } from '@repositoryname/vuex-generators'
5
2
import { passesBoundaryCheck } from '@polar/lib-passes-boundary-check'
6
3
import VectorLayer from 'ol/layer/Vector'
7
4
import Point from 'ol/geom/Point'
@@ -13,56 +10,52 @@ import { toLonLat, transform } from 'ol/proj'
13
10
import { pointerMove } from 'ol/events/condition'
14
11
import { Geometry } from 'ol/geom'
15
12
import { Coordinate } from 'ol/coordinate'
16
- import { PinsState } from '../types'
13
+ import { PinsState , PinsGetters } from '../types'
17
14
import getPointCoordinate from '../util/getPointCoordinate'
18
15
import { getPinStyle } from '../util/getPinStyle'
19
-
20
- const getInitialState = ( ) : PinsState => ( {
21
- isActive : false ,
22
- transformedCoordinate : [ ] ,
23
- latLon : [ ] ,
24
- coordinatesAfterDrag : [ ] ,
25
- getsDragged : false ,
26
- toZoomLevel : 0 ,
27
- atZoomLevel : 0 ,
28
- } )
16
+ import { getInitialState } from './state'
17
+ import getters from './getters'
29
18
30
19
// OK for module creation
31
20
// eslint-disable-next-line max-lines-per-function
32
21
export const makeStoreModule = ( ) => {
33
22
let pinsLayer : VectorLayer < Vector < Geometry > >
34
-
35
23
const move = new Select ( {
36
24
layers : ( l ) => l === pinsLayer ,
37
25
style : null ,
38
26
condition : pointerMove ,
39
27
} )
40
28
41
- const storeModule : PolarModule < PinsState , PinsState > = {
29
+ const storeModule : PolarModule < PinsState , PinsGetters > = {
42
30
namespaced : true ,
43
31
state : getInitialState ( ) ,
44
32
actions : {
45
- /**
46
- * Responsible for setting up the module by adding a watcher. This watcher
47
- * calls removeMarker and showMarker if the store for addressSearch changes
48
- * its value for the chosenAddress.
49
- */
50
- setupModule ( { getters, rootGetters, dispatch, commit } ) : void {
51
- const { appearOnClick, coordinateSource, movable, toZoomLevel } =
52
- rootGetters . configuration . pins || { }
53
- const interactions = rootGetters . map . getInteractions ( )
54
- if ( toZoomLevel ) {
55
- commit ( 'setToZoomLevel' , toZoomLevel )
56
- }
57
- if ( appearOnClick ?. atZoomLevel ) {
58
- commit ( 'setAtZoomLevel' , appearOnClick . atZoomLevel )
59
- }
60
- const showPin = appearOnClick === undefined ? true : appearOnClick . show
33
+ setupModule ( { rootGetters, dispatch } ) : void {
34
+ dispatch ( 'setupClickInteraction' )
35
+ dispatch ( 'setupCoordinateSource' )
36
+ rootGetters . map . addInteraction ( move )
37
+ move . on ( 'select' , ( { selected } ) => {
38
+ const { movable } = rootGetters . configuration . pins || { }
39
+ if ( ! movable || movable === 'none' ) {
40
+ document . body . style . cursor = selected . length ? 'not-allowed' : ''
41
+ }
42
+ } )
43
+ dispatch ( 'setupInitial' )
44
+ // without update, map will pan during drag
45
+ this . watch (
46
+ ( ) => rootGetters . hasSmallWidth || rootGetters . hasSmallHeight ,
47
+ ( ) => dispatch ( 'updateMarkerDraggability' )
48
+ )
49
+ } ,
50
+ setupClickInteraction ( { rootGetters, getters, commit, dispatch } ) : void {
51
+ const { appearOnClick, movable } = rootGetters . configuration . pins || { }
61
52
if ( typeof movable === 'boolean' ) {
62
53
console . warn (
63
54
"@polar/plugin-pins: Using a boolean for the configuration parameter 'movable' has been deprecated and will be removed in the next major release."
64
55
)
65
56
}
57
+ const interactions = rootGetters . map . getInteractions ( )
58
+ const showPin = appearOnClick === undefined ? true : appearOnClick . show
66
59
rootGetters . map . on ( 'singleclick' , async ( { coordinate } ) => {
67
60
const isDrawing = interactions . getArray ( ) . some (
68
61
( interaction ) =>
@@ -72,7 +65,6 @@ export const makeStoreModule = () => {
72
65
// @ts -expect-error | internal hack to detect it from Draw plugin
73
66
interaction . _isDeleteSelect
74
67
)
75
-
76
68
if (
77
69
( ( typeof movable === 'boolean' && movable ) ||
78
70
movable === 'drag' ||
@@ -85,15 +77,17 @@ export const makeStoreModule = () => {
85
77
( await dispatch ( 'isCoordinateInBoundaryLayer' , coordinate ) )
86
78
) {
87
79
const payload = { coordinates : coordinate , clicked : true }
88
-
89
80
dispatch ( 'removeMarker' )
90
81
dispatch ( 'showMarker' , payload )
91
82
commit ( 'setCoordinatesAfterDrag' , coordinate )
92
83
dispatch ( 'updateCoordinates' , coordinate )
93
84
}
94
85
} )
95
-
86
+ } ,
87
+ setupCoordinateSource ( { rootGetters, dispatch } ) : void {
88
+ const { coordinateSource } = rootGetters . configuration . pins || { }
96
89
if ( coordinateSource ) {
90
+ // redo marker if source (e.g. from addressSearch) changes
97
91
this . watch (
98
92
( ) => rootGetters [ coordinateSource ] ,
99
93
( feature ) => {
@@ -113,16 +107,6 @@ export const makeStoreModule = () => {
113
107
{ deep : true }
114
108
)
115
109
}
116
-
117
- rootGetters . map . addInteraction ( move )
118
- move . on ( 'select' , ( { selected } ) => {
119
- const { movable } = rootGetters . configuration . pins || { }
120
- if ( ! movable || movable === 'none' ) {
121
- document . body . style . cursor = selected . length ? 'not-allowed' : ''
122
- }
123
- } )
124
-
125
- dispatch ( 'setupInitial' )
126
110
} ,
127
111
setupInitial ( { rootGetters, getters, dispatch, commit } ) : void {
128
112
const { initial } = rootGetters . configuration . pins as PinsConfiguration
@@ -132,7 +116,6 @@ export const makeStoreModule = () => {
132
116
typeof epsg === 'string'
133
117
? transform ( coordinates , epsg , rootGetters . configuration . epsg )
134
118
: coordinates
135
-
136
119
dispatch ( 'removeMarker' )
137
120
dispatch ( 'showMarker' , {
138
121
coordinates : transformedCoordinates ,
@@ -190,23 +173,20 @@ export const makeStoreModule = () => {
190
173
map . addLayer ( pinsLayer )
191
174
pinsLayer . setZIndex ( 100 )
192
175
commit ( 'setIsActive' , true )
193
- const movable = configuration . pins ?. movable
194
- if ( typeof movable === 'boolean' && movable ) {
195
- dispatch ( 'makeMarkerDraggable' )
196
- } else if ( movable === 'drag' ) {
197
- dispatch ( 'makeMarkerDraggable' )
198
- }
176
+ dispatch ( 'updateMarkerDraggability' )
199
177
}
200
178
} ,
201
- /**
202
- * Makes the mapMarker draggable
203
- */
204
- makeMarkerDraggable ( {
205
- rootGetters : { map } ,
179
+ // Decides whether to make the mapMarker draggable and, if so, does so.
180
+ updateMarkerDraggability ( {
181
+ rootGetters : { map, configuration } ,
206
182
getters,
207
183
commit,
208
184
dispatch,
209
185
} ) : void {
186
+ const movable = configuration . pins ?. movable
187
+ if ( movable !== 'drag' && movable !== true ) {
188
+ return
189
+ }
210
190
const { atZoomLevel } = getters
211
191
const previousTranslate = map
212
192
. getInteractions ( )
@@ -221,7 +201,6 @@ export const makeStoreModule = () => {
221
201
map . removeInteraction ( previousTranslate )
222
202
}
223
203
map . addInteraction ( translate )
224
-
225
204
translate . on ( 'translatestart' , ( ) => {
226
205
commit ( 'setGetsDragged' , true )
227
206
} )
@@ -231,7 +210,6 @@ export const makeStoreModule = () => {
231
210
const geometry = feat . getGeometry ( )
232
211
// @ts -expect-error | abstract method missing on type, exists in all implementations
233
212
let coordinates = geometry ?. getCoordinates ( )
234
-
235
213
if ( ! ( await dispatch ( 'isCoordinateInBoundaryLayer' , coordinates ) ) ) {
236
214
coordinates = getters . transformedCoordinate
237
215
dispatch ( 'removeMarker' )
@@ -245,15 +223,10 @@ export const makeStoreModule = () => {
245
223
} )
246
224
} )
247
225
} ,
248
- /**
249
- * Removes the mapMarker from the map by removing its vectorLayer
250
- */
226
+ // Removes the mapMarker from the map by removing its vectorLayer
251
227
removeMarker ( { rootGetters : { map } , commit } ) : void {
252
228
map . getLayers ( ) . forEach ( function ( layer ) {
253
- if (
254
- layer !== undefined &&
255
- layer . get ( 'polarInternalId' ) === 'mapMarkerVectorLayer'
256
- ) {
229
+ if ( layer ?. get ?.( 'polarInternalId' ) === 'mapMarkerVectorLayer' ) {
257
230
map . removeLayer ( layer )
258
231
}
259
232
} )
@@ -266,7 +239,6 @@ export const makeStoreModule = () => {
266
239
updateCoordinates ( { commit, rootGetters } , coordinates : Coordinate ) {
267
240
const lonLat = toLonLat ( coordinates , rootGetters . configuration . epsg )
268
241
const latLon = [ lonLat [ 1 ] , lonLat [ 0 ] ]
269
-
270
242
commit ( 'setTransformedCoordinate' , coordinates )
271
243
commit ( 'setLatLon' , latLon )
272
244
} ,
@@ -281,13 +253,11 @@ export const makeStoreModule = () => {
281
253
) : Promise < boolean > {
282
254
const { boundaryLayerId, toastAction, boundaryOnError } =
283
255
rootGetters . configuration ?. pins || { }
284
-
285
256
const boundaryCheckResult = await passesBoundaryCheck (
286
257
rootGetters . map ,
287
258
boundaryLayerId ,
288
259
coordinates
289
260
)
290
-
291
261
if (
292
262
! boundaryLayerId ||
293
263
// if a setup error occurred, client will act as if no boundaryLayerId specified
@@ -297,15 +267,10 @@ export const makeStoreModule = () => {
297
267
) {
298
268
return true
299
269
}
300
-
301
270
const errorOccurred = typeof boundaryCheckResult === 'symbol'
302
-
303
271
if ( toastAction ) {
304
272
const toast = errorOccurred
305
- ? {
306
- type : 'error' ,
307
- text : 'plugins.pins.toast.boundaryError' ,
308
- }
273
+ ? { type : 'error' , text : 'plugins.pins.toast.boundaryError' }
309
274
: {
310
275
type : 'info' ,
311
276
text : 'plugins.pins.toast.notInBoundary' ,
@@ -320,16 +285,11 @@ export const makeStoreModule = () => {
320
285
: [ 'Pin position outside of boundary layer:' , coordinates ]
321
286
)
322
287
}
323
-
324
288
return false
325
289
} ,
326
290
} ,
327
- mutations : {
328
- ...generateSimpleMutations ( getInitialState ( ) ) ,
329
- } ,
330
- getters : {
331
- ...generateSimpleGetters ( getInitialState ( ) ) ,
332
- } ,
291
+ mutations : { ...generateSimpleMutations ( getInitialState ( ) ) } ,
292
+ getters,
333
293
}
334
294
return storeModule
335
295
}
0 commit comments