2
2
3
3
import { extend , bindAll } from '../util/util' ;
4
4
import { Event , Evented } from '../util/evented' ;
5
+ import { MapMouseEvent } from '../ui/events' ;
5
6
import DOM from '../util/dom' ;
6
7
import LngLat from '../geo/lng_lat' ;
7
8
import Point from '@mapbox/point-geometry' ;
@@ -95,7 +96,7 @@ export default class Popup extends Evented {
95
96
constructor ( options : PopupOptions ) {
96
97
super ( ) ;
97
98
this . options = extend ( Object . create ( defaultOptions ) , options ) ;
98
- bindAll ( [ '_update' , '_onClose' , 'remove' ] , this ) ;
99
+ bindAll ( [ '_update' , '_onClose' , 'remove' , '_onMouseMove' , '_onMouseUp' , '_onDrag' ] , this ) ;
99
100
}
100
101
101
102
/**
@@ -105,6 +106,8 @@ export default class Popup extends Evented {
105
106
* @returns {Popup } `this`
106
107
*/
107
108
addTo ( map : Map ) {
109
+ if ( this . _map ) this . remove ( ) ;
110
+
108
111
this . _map = map ;
109
112
if ( this . options . closeOnClick ) {
110
113
this . _map . on ( 'click' , this . _onClose ) ;
@@ -118,8 +121,8 @@ export default class Popup extends Evented {
118
121
this . _update ( ) ;
119
122
120
123
if ( this . _trackPointer ) {
121
- this . _map . on ( 'mousemove' , ( e ) => { this . _update ( e . point ) ; } ) ;
122
- this . _map . on ( 'mouseup' , ( e ) => { this . _update ( e . point ) ; } ) ;
124
+ this . _map . on ( 'mousemove' , this . _onMouseMove ) ;
125
+ this . _map . on ( 'mouseup' , this . _onMouseUp ) ;
123
126
if ( this . _container ) {
124
127
this . _container . classList . add ( 'mapboxgl-popup-track-pointer' ) ;
125
128
}
@@ -172,7 +175,9 @@ export default class Popup extends Evented {
172
175
this . _map . off ( 'move' , this . _onClose ) ;
173
176
this . _map . off ( 'click' , this . _onClose ) ;
174
177
this . _map . off ( 'remove' , this . remove ) ;
175
- this . _map . off ( 'mousemove' ) ;
178
+ this . _map . off ( 'mousemove' , this . _onMouseMove ) ;
179
+ this . _map . off ( 'mouseup' , this . _onMouseUp ) ;
180
+ this . _map . off ( 'drag' , this . _onDrag ) ;
176
181
delete this . _map ;
177
182
}
178
183
@@ -219,7 +224,7 @@ export default class Popup extends Evented {
219
224
220
225
if ( this . _map ) {
221
226
this . _map . on ( 'move' , this . _update ) ;
222
- this . _map . off ( 'mousemove' ) ;
227
+ this . _map . off ( 'mousemove' , this . _onMouseMove ) ;
223
228
if ( this . _container ) {
224
229
this . _container . classList . remove ( 'mapboxgl-popup-track-pointer' ) ;
225
230
}
@@ -240,8 +245,8 @@ export default class Popup extends Evented {
240
245
this . _update ( ) ;
241
246
if ( this . _map ) {
242
247
this . _map . off ( 'move' , this . _update ) ;
243
- this . _map . on ( 'mousemove' , ( e ) => { this . _update ( e . point ) ; } ) ;
244
- this . _map . on ( 'drag' , ( e ) => { this . _update ( e . point ) ; } ) ;
248
+ this . _map . on ( 'mousemove' , this . _onMouseMove ) ;
249
+ this . _map . on ( 'drag' , this . _onDrag ) ;
245
250
if ( this . _container ) {
246
251
this . _container . classList . add ( 'mapboxgl-popup-track-pointer' ) ;
247
252
}
@@ -409,6 +414,18 @@ export default class Popup extends Evented {
409
414
410
415
}
411
416
417
+ _onMouseUp ( event : MapMouseEvent ) {
418
+ this . _update ( event . point ) ;
419
+ }
420
+
421
+ _onMouseMove ( event : MapMouseEvent ) {
422
+ this . _update ( event . point ) ;
423
+ }
424
+
425
+ _onDrag ( event : MapMouseEvent ) {
426
+ this . _update ( event . point ) ;
427
+ }
428
+
412
429
_update ( cursor : PointLike ) {
413
430
const hasPosition = this . _lngLat || this . _trackPointer ;
414
431
0 commit comments