Leaflet/src/dom/Draggable.js

138 lines
3.4 KiB
JavaScript
Raw Normal View History

/*
* L.Draggable allows you to add dragging capabilities to any element. Supports mobile devices too.
*/
2010-09-16 21:05:55 +08:00
L.Draggable = L.Class.extend({
includes: L.Mixin.Events,
2010-09-16 21:05:55 +08:00
statics: {
START: L.Browser.touch ? ['touchstart', 'mousedown'] : ['mousedown'],
2012-11-16 06:28:01 +08:00
END: {
mousedown: 'mouseup',
touchstart: 'touchend',
MSPointerDown: 'touchend'
},
MOVE: {
mousedown: 'mousemove',
touchstart: 'touchmove',
MSPointerDown: 'touchmove'
}
2010-09-16 21:05:55 +08:00
},
initialize: function (element, dragStartTarget) {
2010-09-16 21:05:55 +08:00
this._element = element;
this._dragStartTarget = dragStartTarget || element;
},
2011-12-09 22:51:31 +08:00
enable: function () {
2012-11-08 00:46:59 +08:00
if (this._enabled) { return; }
for (var i = L.Draggable.START.length - 1; i >= 0; i--) {
L.DomEvent.on(this._dragStartTarget, L.Draggable.START[i], this._onDown, this);
}
2013-04-19 20:36:18 +08:00
2010-09-16 21:05:55 +08:00
this._enabled = true;
},
2011-12-09 22:51:31 +08:00
disable: function () {
2012-11-08 00:46:59 +08:00
if (!this._enabled) { return; }
for (var i = L.Draggable.START.length - 1; i >= 0; i--) {
L.DomEvent.off(this._dragStartTarget, L.Draggable.START[i], this._onDown, this);
}
2013-04-19 20:36:18 +08:00
2010-09-16 21:05:55 +08:00
this._enabled = false;
this._moved = false;
2010-09-16 21:05:55 +08:00
},
2011-03-22 01:09:43 +08:00
2011-12-09 22:51:31 +08:00
_onDown: function (e) {
2013-04-19 19:45:01 +08:00
if (e.shiftKey || ((e.which !== 1) && (e.button !== 1) && !e.touches)) { return; }
L.DomEvent
.stopPropagation(e);
2013-06-25 04:31:25 +08:00
if (L.Draggable._disabled) { return; }
L.DomUtil.disableImageDrag();
var first = e.touches ? e.touches[0] : e,
2012-11-08 00:46:59 +08:00
el = first.target;
2013-04-19 19:45:01 +08:00
// if touching a link, highlight it
if (L.Browser.touch && el.tagName.toLowerCase() === 'a') {
L.DomUtil.addClass(el, 'leaflet-active');
}
this._moved = false;
2013-04-19 19:45:01 +08:00
2012-11-08 00:46:59 +08:00
if (this._moving) { return; }
this._startPoint = new L.Point(first.clientX, first.clientY);
2012-09-11 17:33:54 +08:00
this._startPos = this._newPos = L.DomUtil.getPosition(this._element);
2013-04-19 19:45:01 +08:00
L.DomEvent
.on(document, L.Draggable.MOVE[e.type], this._onMove, this)
.on(document, L.Draggable.END[e.type], this._onUp, this);
2010-09-16 21:05:55 +08:00
},
2011-12-09 22:51:31 +08:00
_onMove: function (e) {
2012-06-26 06:51:04 +08:00
if (e.touches && e.touches.length > 1) { return; }
2012-06-26 06:51:04 +08:00
var first = (e.touches && e.touches.length === 1 ? e.touches[0] : e),
2012-11-08 00:46:59 +08:00
newPoint = new L.Point(first.clientX, first.clientY),
2013-04-19 20:36:18 +08:00
offset = newPoint.subtract(this._startPoint);
2013-04-19 20:36:18 +08:00
if (!offset.x && !offset.y) { return; }
2012-06-26 06:51:04 +08:00
L.DomEvent.preventDefault(e);
2010-09-21 20:48:31 +08:00
if (!this._moved) {
2010-09-16 21:05:55 +08:00
this.fire('dragstart');
2013-04-19 20:36:18 +08:00
this._moved = true;
this._startPos = L.DomUtil.getPosition(this._element).subtract(offset);
if (!L.Browser.touch) {
L.DomUtil.disableTextSelection();
2013-04-19 19:45:01 +08:00
L.DomUtil.addClass(document.body, 'leaflet-dragging');
}
2010-09-16 21:05:55 +08:00
}
2013-04-19 20:36:18 +08:00
this._newPos = this._startPos.add(offset);
this._moving = true;
2012-02-22 15:00:54 +08:00
L.Util.cancelAnimFrame(this._animRequest);
this._animRequest = L.Util.requestAnimFrame(this._updatePosition, this, true, this._dragStartTarget);
2010-09-16 21:05:55 +08:00
},
2011-12-09 22:51:31 +08:00
_updatePosition: function () {
this.fire('predrag');
2010-09-21 23:36:00 +08:00
L.DomUtil.setPosition(this._element, this._newPos);
this.fire('drag');
2010-09-21 23:36:00 +08:00
},
_onUp: function () {
if (!L.Browser.touch) {
L.DomUtil.enableTextSelection();
2013-04-19 19:45:01 +08:00
L.DomUtil.removeClass(document.body, 'leaflet-dragging');
}
for (var i in L.Draggable.MOVE) {
L.DomEvent
.off(document, L.Draggable.MOVE[i], this._onMove)
.off(document, L.Draggable.END[i], this._onUp);
}
L.DomUtil.enableImageDrag();
if (this._moved) {
// ensure drag is not fired after dragend
L.Util.cancelAnimFrame(this._animRequest);
2010-09-21 21:10:31 +08:00
this.fire('dragend');
}
2013-04-19 19:45:01 +08:00
this._moving = false;
2010-09-16 21:05:55 +08:00
}
});