Merge pull request #955 from danzel/longpress

Add LongPress support to mobile.
This commit is contained in:
Vladimir Agafonkin 2012-10-31 01:00:13 -07:00
commit 307c9d9953
2 changed files with 23 additions and 4 deletions

View File

@ -12,9 +12,10 @@ L.Draggable = L.Class.extend({
TAP_TOLERANCE: 15
},
initialize: function (element, dragStartTarget) {
initialize: function (element, dragStartTarget, longPress) {
this._element = element;
this._dragStartTarget = dragStartTarget || element;
this._longPress = longPress && !L.Browser.msTouch;
},
enable: function () {
@ -39,6 +40,7 @@ L.Draggable = L.Class.extend({
((e.which !== 1) && (e.button !== 1) && !e.touches)) { return; }
L.DomEvent.preventDefault(e);
L.DomEvent.stopPropagation(e);
if (L.Draggable._disabled) { return; }
@ -46,6 +48,7 @@ L.Draggable = L.Class.extend({
if (e.touches && e.touches.length > 1) {
this._simulateClick = false;
clearTimeout(this._longPressTimeout);
return;
}
@ -64,6 +67,19 @@ L.Draggable = L.Class.extend({
this._startPoint = new L.Point(first.clientX, first.clientY);
this._startPos = this._newPos = L.DomUtil.getPosition(this._element);
//Touch contextmenu event emulation
if (e.touches && e.touches.length === 1 && L.Browser.touch && this._longPress) {
this._longPressTimeout = setTimeout(L.Util.bind(function () {
var dist = (this._newPos && this._newPos.distanceTo(this._startPos)) || 0;
if (dist < L.Draggable.TAP_TOLERANCE) {
this._simulateClick = false;
this._onUp();
this._simulateEvent('contextmenu', first);
}
}, this), 1000);
}
L.DomEvent.on(document, L.Draggable.MOVE, this._onMove, this);
L.DomEvent.on(document, L.Draggable.END, this._onUp, this);
},
@ -106,6 +122,7 @@ L.Draggable = L.Class.extend({
_onUp: function (e) {
var simulateClickTouch;
clearTimeout(this._longPressTimeout);
if (this._simulateClick && e.changedTouches) {
var first = e.changedTouches[0],
el = first.target,

View File

@ -10,6 +10,8 @@ L.Map.mergeOptions({
inertiaMaxSpeed: 6000, // px/s
inertiaThreshold: L.Browser.touch ? 32 : 18, // ms
longPress: true,
// TODO refactor, move to CRS
worldCopyJump: true
});
@ -17,7 +19,9 @@ L.Map.mergeOptions({
L.Map.Drag = L.Handler.extend({
addHooks: function () {
if (!this._draggable) {
this._draggable = new L.Draggable(this._map._mapPane, this._map._container);
var options = this._map.options;
this._draggable = new L.Draggable(this._map._mapPane, this._map._container, options.longPress);
this._draggable.on({
'dragstart': this._onDragStart,
@ -25,8 +29,6 @@ L.Map.Drag = L.Handler.extend({
'dragend': this._onDragEnd
}, this);
var options = this._map.options;
if (options.worldCopyJump) {
this._draggable.on('predrag', this._onPreDrag, this);
this._map.on('viewreset', this._onViewReset, this);