1
- import { Modify } from 'ol/interaction' ;
2
- import { click } from 'ol/events/condition' ;
1
+ import { Modify , Interaction } from 'ol/interaction' ;
2
+ import { singleClick } from 'ol/events/condition' ;
3
3
import Control from './control' ;
4
4
import image from '../../img/modify_geometry2.svg' ;
5
5
import SelectMove from '../interaction/selectmove' ;
@@ -23,7 +23,7 @@ class ModifyControl extends Control {
23
23
* @param {Object } [options.moveInteractionOptions] Options for the move interaction.
24
24
* @param {Object } [options.modifyInteractionOptions] Options for the modify interaction.
25
25
* @param {Object } [options.deleteInteractionOptions] Options for the delete interaction.
26
- *
26
+ * @param { Object } [options.deselectInteractionOptions] Options for the deselect interaction. Default: features are deselected on click on map.
27
27
*/
28
28
constructor ( options ) {
29
29
super ( {
@@ -70,15 +70,13 @@ class ModifyControl extends Control {
70
70
this . cursorFilter = options . cursorFilter || ( ( ) => true ) ;
71
71
this . cursorHandler = this . cursorHandler . bind ( this ) ;
72
72
73
- /* onClickOutsideFeatures management */
74
- this . onClickOutsideFeatures = this . onClickOutsideFeatures . bind ( this ) ;
75
-
76
73
/* Interactions */
77
74
this . createSelectMoveInteraction ( options . selectMoveOptions ) ;
78
75
this . createSelectModifyInteraction ( options . selectModifyOptions ) ;
79
76
this . createModifyInteraction ( options . modifyInteractionOptions ) ;
80
77
this . createMoveInteraction ( options . moveInteractionOptions ) ;
81
78
this . createDeleteInteraction ( options . deleteInteractionOptions ) ;
79
+ this . createDeselectInteraction ( options . deselectInteractionOptions ) ;
82
80
}
83
81
84
82
/**
@@ -156,6 +154,7 @@ class ModifyControl extends Control {
156
154
/**
157
155
* Create the interaction used to move feature.
158
156
* @param {* } options
157
+ * @private
159
158
*/
160
159
createMoveInteraction ( options = { } ) {
161
160
/**
@@ -182,6 +181,7 @@ class ModifyControl extends Control {
182
181
/**
183
182
* Create the interaction used to modify vertexes of features.
184
183
* @param {* } options
184
+ * @private
185
185
*/
186
186
createModifyInteraction ( options = { } ) {
187
187
/**
@@ -190,7 +190,7 @@ class ModifyControl extends Control {
190
190
*/
191
191
this . modifyInteraction = new Modify ( {
192
192
features : this . selectModify . getFeatures ( ) ,
193
- deleteCondition : click ,
193
+ deleteCondition : singleClick ,
194
194
...options ,
195
195
} ) ;
196
196
@@ -209,6 +209,7 @@ class ModifyControl extends Control {
209
209
/**
210
210
* Create the interaction used to delete selected features.
211
211
* @param {* } options
212
+ * @private
212
213
*/
213
214
createDeleteInteraction ( options = { } ) {
214
215
/**
@@ -223,6 +224,42 @@ class ModifyControl extends Control {
223
224
this . deleteInteraction . setActive ( false ) ;
224
225
}
225
226
227
+ /**
228
+ * Create the interaction used to deselected features when we click on the map.
229
+ * @param {* } options
230
+ * @private
231
+ */
232
+ createDeselectInteraction ( options = { } ) {
233
+ // it's important that this condition was the same as the selectModify's
234
+ // deleteCondition to avoid the selection of the feature under the node to delete.
235
+ const condition = options . condition || singleClick ;
236
+
237
+ /**
238
+ * @type {ol.interaction.Interaction }
239
+ * @private
240
+ */
241
+ this . deselectInteraction = new Interaction ( {
242
+ handleEvent : ( mapBrowserEvent ) => {
243
+ if ( ! condition ( mapBrowserEvent ) ) {
244
+ return true ;
245
+ }
246
+ const onFeature = this . getFeatureAtPixel ( mapBrowserEvent . pixel ) ;
247
+ const onVertex = this . isHoverVertexFeatureAtPixel (
248
+ mapBrowserEvent . pixel ,
249
+ ) ;
250
+
251
+ if ( ! onVertex && ! onFeature ) {
252
+ // Default: Clear selection on click outside features.
253
+ this . selectMove . getFeatures ( ) . clear ( ) ;
254
+ this . selectModify . getFeatures ( ) . clear ( ) ;
255
+ return false ;
256
+ }
257
+ return true ;
258
+ } ,
259
+ } ) ;
260
+ this . deselectInteraction . setActive ( false ) ;
261
+ }
262
+
226
263
/**
227
264
* Get a selectable feature at a pixel.
228
265
* @param {* } pixel
@@ -324,36 +361,19 @@ class ModifyControl extends Control {
324
361
}
325
362
}
326
363
327
- /**
328
- * Clear selection on map's singleclick event.
329
- * @param {* } evt
330
- * @private
331
- */
332
- onClickOutsideFeatures ( evt ) {
333
- if ( ! this . getActive ( ) ) {
334
- return ;
335
- }
336
- const onFeature = this . getFeatureAtPixel ( evt . pixel ) ;
337
- const onVertex = this . isHoverVertexFeatureAtPixel ( evt . pixel ) ;
338
-
339
- if ( ! onVertex && ! onFeature ) {
340
- // Default: Clear selection on click outside features.
341
- this . selectMove . getFeatures ( ) . clear ( ) ;
342
- this . selectModify . getFeatures ( ) . clear ( ) ;
343
- }
344
- }
345
-
346
364
setMap ( map ) {
347
365
if ( this . map ) {
348
366
this . map . removeInteraction ( this . modifyInteraction ) ;
349
367
this . map . removeInteraction ( this . moveInteraction ) ;
350
368
this . map . removeInteraction ( this . selectMove ) ;
351
369
this . map . removeInteraction ( this . selectModify ) ;
352
370
this . map . removeInteraction ( this . deleteInteraction ) ;
371
+ this . map . removeInteraction ( this . deselectInteraction ) ;
353
372
this . removeListeners ( ) ;
354
373
}
355
374
super . setMap ( map ) ;
356
375
this . addListeners ( ) ;
376
+ this . map . addInteraction ( this . deselectInteraction ) ;
357
377
this . map . addInteraction ( this . deleteInteraction ) ;
358
378
this . map . addInteraction ( this . selectModify ) ;
359
379
// For the default behvior it's very important to add selectMove after selectModify.
@@ -363,16 +383,23 @@ class ModifyControl extends Control {
363
383
this . map . addInteraction ( this . modifyInteraction ) ;
364
384
}
365
385
386
+ /**
387
+ * Add others listeners on the map than interactions.
388
+ * @param {* } evt
389
+ * @private
390
+ */
366
391
addListeners ( ) {
367
392
this . removeListeners ( ) ;
368
- // To avoid bug, it's important that this event is the same as for modify's deleteCondition.
369
- this . map . on ( 'click' , this . onClickOutsideFeatures ) ;
370
393
this . map . on ( 'pointermove' , this . cursorHandler ) ;
371
394
}
372
395
396
+ /**
397
+ * Remove others listeners on the map than interactions.
398
+ * @param {* } evt
399
+ * @private
400
+ */
373
401
removeListeners ( ) {
374
402
clearTimeout ( this . cursorTimeout ) ;
375
- this . map . un ( 'click' , this . onClickOutsideFeatures ) ;
376
403
this . map . un ( 'pointermove' , this . cursorHandler ) ;
377
404
}
378
405
@@ -381,6 +408,7 @@ class ModifyControl extends Control {
381
408
*/
382
409
activate ( ) {
383
410
super . activate ( ) ;
411
+ this . deselectInteraction . setActive ( true ) ;
384
412
this . deleteInteraction . setActive ( true ) ;
385
413
this . selectModify . setActive ( true ) ;
386
414
// For the default behavior it's very important to add selectMove after selectModify.
@@ -395,6 +423,7 @@ class ModifyControl extends Control {
395
423
clearTimeout ( this . cursorTimeout ) ;
396
424
this . selectMove . getFeatures ( ) . clear ( ) ;
397
425
this . selectModify . getFeatures ( ) . clear ( ) ;
426
+ this . deselectInteraction . setActive ( false ) ;
398
427
this . deleteInteraction . setActive ( false ) ;
399
428
this . selectModify . setActive ( false ) ;
400
429
this . selectMove . setActive ( false ) ;
0 commit comments