2010-09-16 23:32:55 +08:00
|
|
|
/*
|
|
|
|
* 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,
|
|
|
|
|
|
|
|
statics: {
|
|
|
|
START: L.Browser.mobileWebkit ? 'touchstart' : 'mousedown',
|
|
|
|
END: L.Browser.mobileWebkit ? 'touchend' : 'mouseup',
|
|
|
|
MOVE: L.Browser.mobileWebkit ? 'touchmove' : 'mousemove'
|
|
|
|
},
|
|
|
|
|
|
|
|
initialize: function(element, dragStartTarget) {
|
|
|
|
this._element = element;
|
|
|
|
this._dragStartTarget = dragStartTarget || element;
|
|
|
|
},
|
|
|
|
|
|
|
|
enable: function() {
|
|
|
|
if (this._enabled) { return; }
|
|
|
|
L.DomEvent.addListener(this._dragStartTarget, L.Draggable.START, this._onDown, this);
|
|
|
|
this._enabled = true;
|
|
|
|
},
|
|
|
|
|
|
|
|
disable: function() {
|
|
|
|
if (!this._enabled) { return; }
|
|
|
|
L.DomEvent.removeListener(this._dragStartTarget, L.Draggable.START, this._onDown);
|
|
|
|
this._enabled = false;
|
|
|
|
},
|
2011-03-22 01:09:43 +08:00
|
|
|
|
2010-09-16 21:05:55 +08:00
|
|
|
_onDown: function(e) {
|
2010-09-21 21:10:31 +08:00
|
|
|
if (e.shiftKey || ((e.which != 1) && (e.button != 1) && !e.touches)) { return; }
|
2010-09-16 21:05:55 +08:00
|
|
|
|
2010-09-21 21:10:31 +08:00
|
|
|
if (!e.touches) {
|
|
|
|
L.DomEvent.preventDefault(e);
|
|
|
|
}
|
2010-09-16 23:32:55 +08:00
|
|
|
if (e.touches && e.touches.length > 1) { return; }
|
|
|
|
if (e.touches && e.touches.length == 1) { e = e.touches[0]; }
|
2010-09-16 21:05:55 +08:00
|
|
|
|
|
|
|
this._dragStartPos = L.DomUtil.getPosition(this._element);
|
2011-03-22 01:09:43 +08:00
|
|
|
|
2010-09-16 21:05:55 +08:00
|
|
|
this._startX = e.clientX;
|
|
|
|
this._startY = e.clientY;
|
|
|
|
|
2010-12-16 01:55:57 +08:00
|
|
|
this._moved = false;
|
|
|
|
|
2011-03-22 01:09:43 +08:00
|
|
|
L.DomUtil.disableTextSelection();
|
2010-09-16 21:05:55 +08:00
|
|
|
|
|
|
|
this._setMovingCursor();
|
|
|
|
|
|
|
|
L.DomEvent.addListener(document, L.Draggable.MOVE, this._onMove, this);
|
|
|
|
L.DomEvent.addListener(document, L.Draggable.END, this._onUp, this);
|
|
|
|
},
|
|
|
|
|
|
|
|
_onMove: function(e) {
|
|
|
|
L.DomEvent.preventDefault(e);
|
2010-09-21 23:36:00 +08:00
|
|
|
//L.DomEvent.stopPropagation(e);
|
2010-09-16 21:05:55 +08:00
|
|
|
|
2010-09-16 23:32:55 +08:00
|
|
|
if (e.touches && e.touches.length > 1) { return; }
|
|
|
|
if (e.touches && e.touches.length == 1) { e = e.touches[0]; }
|
2010-09-16 21:05:55 +08:00
|
|
|
|
2011-03-22 01:09:43 +08:00
|
|
|
this._offset = new L.Point(e.clientX - this._startX, e.clientY - this._startY);
|
2010-09-21 23:36:00 +08:00
|
|
|
|
2011-03-22 01:09:43 +08:00
|
|
|
this._newPos = this._dragStartPos.add(this._offset);
|
2010-09-21 23:36:00 +08:00
|
|
|
|
|
|
|
this._updatePosition();
|
2010-09-16 21:05:55 +08:00
|
|
|
|
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.fire('drag');
|
|
|
|
},
|
|
|
|
|
2010-09-21 23:36:00 +08:00
|
|
|
_updatePosition: function() {
|
|
|
|
L.DomUtil.setPosition(this._element, this._newPos);
|
|
|
|
},
|
|
|
|
|
2010-09-16 21:05:55 +08:00
|
|
|
_onUp: function(e) {
|
2011-03-22 01:09:43 +08:00
|
|
|
L.DomUtil.enableTextSelection();
|
2010-09-16 21:05:55 +08:00
|
|
|
|
|
|
|
this._restoreCursor();
|
|
|
|
|
|
|
|
L.DomEvent.removeListener(document, L.Draggable.MOVE, this._onMove);
|
|
|
|
L.DomEvent.removeListener(document, L.Draggable.END, this._onUp);
|
|
|
|
|
2010-09-21 21:10:31 +08:00
|
|
|
if (this._moved) {
|
|
|
|
this.fire('dragend');
|
|
|
|
}
|
2010-09-16 21:05:55 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
_setMovingCursor: function() {
|
|
|
|
this._bodyCursor = document.body.style.cursor;
|
|
|
|
document.body.style.cursor = 'move';
|
|
|
|
},
|
|
|
|
|
|
|
|
_restoreCursor: function() {
|
|
|
|
document.body.style.cursor = this._bodyCursor;
|
|
|
|
}
|
|
|
|
});
|