Commit | Line | Data |
---|---|---|
c5ba5b04 JW |
1 | /* |
2 | * L.Handler.MarkerDrag is used internally by L.Marker to make the markers draggable. | |
3 | */ | |
4 | ||
5 | L.Handler.MarkerDrag = L.Handler.extend({ | |
6 | initialize: function(marker) { | |
7 | this._marker = marker; | |
8 | }, | |
9 | ||
10 | enable: function() { | |
11 | if (this._enabled) { return; } | |
12 | if (!this._draggable) { | |
13 | this._draggable = new L.Draggable(this._marker._icon, this._marker._icon); | |
14 | this._draggable.on('dragstart', this._onDragStart, this); | |
15 | this._draggable.on('drag', this._onDrag, this); | |
16 | this._draggable.on('dragend', this._onDragEnd, this); | |
17 | } | |
18 | this._draggable.enable(); | |
19 | this._enabled = true; | |
20 | }, | |
21 | ||
22 | disable: function() { | |
23 | if (!this._enabled) { return; } | |
24 | this._draggable.disable(); | |
25 | this._enabled = false; | |
26 | }, | |
27 | ||
28 | moved: function() { | |
29 | return this._draggable && this._draggable._moved; | |
30 | }, | |
31 | ||
32 | _onDragStart: function(e) { | |
33 | this._marker.closePopup(); | |
34 | ||
35 | this._marker.fire('movestart'); | |
36 | this._marker.fire('dragstart'); | |
37 | }, | |
38 | ||
39 | _onDrag: function(e) { | |
40 | // update shadow position | |
41 | var iconPos = L.DomUtil.getPosition(this._marker._icon); | |
42 | L.DomUtil.setPosition(this._marker._shadow, iconPos); | |
43 | ||
44 | this._marker._latlng = this._marker._map.layerPointToLatLng(iconPos); | |
45 | ||
46 | this._marker.fire('move'); | |
47 | this._marker.fire('drag'); | |
48 | }, | |
49 | ||
50 | _onDragEnd: function() { | |
51 | this._marker.fire('moveend'); | |
52 | this._marker.fire('dragend'); | |
53 | } | |
54 | }); |