Leaflet/src/dom/Draggable.js

156 lines
3.9 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: {
2011-06-18 00:40:27 +08:00
START: L.Browser.touch ? 'touchstart' : 'mousedown',
END: L.Browser.touch ? 'touchend' : 'mouseup',
MOVE: L.Browser.touch ? 'touchmove' : 'mousemove',
TAP_TOLERANCE: 15
2010-09-16 21:05:55 +08:00
},
2011-12-09 22:51:31 +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 () {
2011-12-10 19:48:45 +08:00
if (this._enabled) {
return;
}
L.DomEvent.on(this._dragStartTarget, L.Draggable.START, this._onDown, this);
2010-09-16 21:05:55 +08:00
this._enabled = true;
},
2011-12-09 22:51:31 +08:00
disable: function () {
2011-12-10 19:48:45 +08:00
if (!this._enabled) {
return;
}
L.DomEvent.off(this._dragStartTarget, L.Draggable.START, this._onDown);
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) {
2011-12-10 19:48:45 +08:00
if ((!L.Browser.touch && e.shiftKey) || ((e.which !== 1) && (e.button !== 1) && !e.touches)) {
return;
}
this._simulateClick = true;
2011-12-10 19:48:45 +08:00
if (e.touches && e.touches.length > 1) {
this._simulateClick = false;
2011-12-10 19:48:45 +08:00
return;
}
var first = (e.touches && e.touches.length === 1 ? e.touches[0] : e),
el = first.target;
L.DomEvent.preventDefault(e);
if (L.Browser.touch && el.tagName.toLowerCase() === 'a') {
L.DomUtil.addClass(el, 'leaflet-active');
}
this._moved = false;
2011-12-10 19:48:45 +08:00
if (this._moving) {
return;
}
if (!L.Browser.touch) {
L.DomUtil.disableTextSelection();
this._setMovingCursor();
}
2011-06-18 00:40:27 +08:00
this._startPos = this._newPos = L.DomUtil.getPosition(this._element);
this._startPoint = new L.Point(first.clientX, first.clientY);
L.DomEvent.on(document, L.Draggable.MOVE, this._onMove, this);
L.DomEvent.on(document, L.Draggable.END, 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),
newPoint = new L.Point(first.clientX, first.clientY),
diffVec = newPoint.subtract(this._startPoint);
2012-06-26 06:51:04 +08:00
if (!diffVec.x && !diffVec.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');
2010-09-21 20:48:31 +08:00
this._moved = true;
2010-09-16 21:05:55 +08:00
}
this._newPos = this._startPos.add(diffVec);
this._moving = true;
2012-02-22 15:00:54 +08:00
L.Util.cancelAnimFrame(this._animRequest);
2012-07-21 02:15:07 +08:00
this._animRequest = L.Util.requestAnimFrame(this._updatePosition, this);
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
},
2011-12-09 22:51:31 +08:00
_onUp: function (e) {
if (this._simulateClick && e.changedTouches) {
var first = e.changedTouches[0],
el = first.target,
2011-06-18 00:40:27 +08:00
dist = (this._newPos && this._newPos.distanceTo(this._startPos)) || 0;
if (el.tagName.toLowerCase() === 'a') {
L.DomUtil.removeClass(el, 'leaflet-active');
}
if (dist < L.Draggable.TAP_TOLERANCE) {
this._simulateEvent('click', first);
}
}
if (!L.Browser.touch) {
L.DomUtil.enableTextSelection();
this._restoreCursor();
}
L.DomEvent.off(document, L.Draggable.MOVE, this._onMove);
L.DomEvent.off(document, L.Draggable.END, this._onUp);
2010-09-21 21:10:31 +08:00
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');
}
this._moving = false;
},
2011-12-09 22:51:31 +08:00
_setMovingCursor: function () {
L.DomUtil.addClass(document.body, 'leaflet-dragging');
2010-09-16 21:05:55 +08:00
},
2011-12-09 22:51:31 +08:00
_restoreCursor: function () {
L.DomUtil.removeClass(document.body, 'leaflet-dragging');
},
2011-12-09 22:51:31 +08:00
_simulateEvent: function (type, e) {
var simulatedEvent = document.createEvent('MouseEvents');
simulatedEvent.initMouseEvent(
type, true, true, window, 1,
e.screenX, e.screenY,
e.clientX, e.clientY,
false, false, false, false, 0, null);
e.target.dispatchEvent(simulatedEvent);
2010-09-16 21:05:55 +08:00
}
});