summaryrefslogtreecommitdiff
path: root/loleaflet/src/layer/marker/Marker.Drag.js
blob: 179408e9bcbd848090bae8a73b14060b6cd824f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/* -*- js-indent-level: 8 -*- */
/*
 * L.Handler.MarkerDrag is used internally by L.Marker to make the markers draggable.
 */

L.Handler.MarkerDrag = L.Handler.extend({
	initialize: function (marker) {
		this._marker = marker;
	},

	addHooks: function () {
		var icon = this._marker._icon;

		if (!this._draggable) {
			this._draggable = new L.Draggable(icon, icon, true);
		}

		this._draggable.on({
			down: this._onDown,
			dragstart: this._onDragStart,
			drag: this._onDrag,
			dragend: this._onDragEnd,
			up: this._onUp
		}, this).enable();

		L.DomUtil.addClass(icon, 'leaflet-marker-draggable');
	},

	removeHooks: function () {
		this._draggable.off({
			down: this._onDown,
			dragstart: this._onDragStart,
			drag: this._onDrag,
			dragend: this._onDragEnd,
			up: this._onUp
		}, this).disable();

		if (this._marker._icon) {
			L.DomUtil.removeClass(this._marker._icon, 'leaflet-marker-draggable');
		}
	},

	moved: function () {
		return this._draggable && this._draggable._moved;
	},

	freezeX: function (boolChoice) {
		if (this._draggable)
			this._draggable.freezeX(boolChoice);
	},

	freezeY: function (boolChoice) {
		if (this._draggable)
			this._draggable.freezeY(boolChoice);
	},

	_onDown: function (e) {
		this._marker.fire('down', e);
	},

	_onDragStart: function (e) {
		this._marker
		    .closePopup()
		    .fire('movestart', e)
		    .fire('dragstart', e);
	},

	_onDrag: function (e) {
		var marker = this._marker,
		    shadow = marker._shadow,
		    iconPos = L.DomUtil.getPosition(marker._icon),
		    latlng = marker._map.layerPointToLatLng(iconPos);

		// update shadow position
		if (shadow) {
			L.DomUtil.setPosition(shadow, iconPos);
		}

		marker._latlng = latlng;
		e.latlng = latlng;

		marker
		    .fire('move', e)
		    .fire('drag', e);
	},

	_onDragEnd: function (e) {
		this._marker
		    .fire('moveend', e)
		    .fire('dragend', e);
	},

	_onUp: function (e) {
		this._marker.fire('up', e);
	}
});