Rewrite longpress->contextmenu to live inside Draggable instead as it needs deep interaction with it. When an emulated contextmenu is fired on touch we stop dragging and stop a click event from occurring. New map option to disable it if required: touchContextMenuEmulation
This commit is contained in:
parent
d3d85198e2
commit
1aff46cb27
@ -170,7 +170,6 @@ var deps = {
|
||||
src: ['dom/DomEvent.js',
|
||||
'dom/DomEvent.DoubleTap.js',
|
||||
'dom/DomEvent.MsTouch.js',
|
||||
'dom/DomEvent.LongPress.js',
|
||||
'core/Handler.js',
|
||||
'map/handler/Map.TouchZoom.js'],
|
||||
deps: ['MapAnimationZoom'],
|
||||
|
@ -17,7 +17,6 @@
|
||||
'dom/DomEvent.js',
|
||||
'dom/DomEvent.DoubleTap.js',
|
||||
'dom/DomEvent.MsTouch.js',
|
||||
'dom/DomEvent.LongPress.js',
|
||||
'dom/DomUtil.js',
|
||||
'dom/Draggable.js',
|
||||
|
||||
|
@ -1,64 +0,0 @@
|
||||
L.Util.extend(L.DomEvent, {
|
||||
// inspired by Zepto touch code by Thomas Fuchs
|
||||
addLongPressListener: function (obj, handler, id) {
|
||||
var touch, touchStartX, touchStartY,
|
||||
start,
|
||||
timeoutId = null,
|
||||
delay = 1000,
|
||||
maxMovement = 10,
|
||||
diffX, diffY,
|
||||
pre = '_leaflet_',
|
||||
touchstart = 'touchstart',
|
||||
touchmove = 'touchmove',
|
||||
touchend = 'touchend';
|
||||
|
||||
function onTouchStart(e) {
|
||||
clearTimeout(timeoutId);
|
||||
|
||||
if (e.touches.length !== 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
touch = e.touches[0];
|
||||
touchStartX = touch.pageX;
|
||||
touchStartY = touch.pageY;
|
||||
start = Date.now();
|
||||
|
||||
timeoutId = setTimeout(function () {
|
||||
touch.pageX = touchStartX;
|
||||
touch.pageY = touchStartY;
|
||||
touch.type = 'contextmenu';
|
||||
handler(touch);
|
||||
}, delay);
|
||||
}
|
||||
|
||||
function onTouchMove(e) {
|
||||
diffX = e.touches[0].pageX - touchStartX;
|
||||
diffY = e.touches[0].pageY - touchStartY;
|
||||
|
||||
if (diffX * diffX + diffY * diffY > maxMovement * maxMovement) {
|
||||
clearTimeout(timeoutId);
|
||||
}
|
||||
}
|
||||
|
||||
function onTouchEnd() {
|
||||
clearTimeout(timeoutId);
|
||||
}
|
||||
obj[pre + touchstart + id] = onTouchStart;
|
||||
obj[pre + touchmove + id] = onTouchMove;
|
||||
obj[pre + touchend + id] = onTouchEnd;
|
||||
|
||||
obj.addEventListener(touchstart, onTouchStart, false);
|
||||
obj.addEventListener(touchmove, onTouchMove, false);
|
||||
obj.addEventListener(touchend, onTouchEnd, false);
|
||||
return this;
|
||||
},
|
||||
|
||||
removeLongPressListener: function (obj, id) {
|
||||
var pre = '_leaflet_';
|
||||
obj.removeEventListener(obj, obj[pre + 'touchstart' + id], false);
|
||||
obj.removeEventListener(obj, obj[pre + 'touchmove' + id], false);
|
||||
obj.removeEventListener(obj, obj[pre + 'touchend' + id], false);
|
||||
return this;
|
||||
}
|
||||
});
|
@ -21,9 +21,6 @@ L.DomEvent = {
|
||||
} else if (L.Browser.touch && (type === 'dblclick') && this.addDoubleTapListener) {
|
||||
return this.addDoubleTapListener(obj, handler, id);
|
||||
|
||||
} else if (L.Browser.touch && (type === 'contextmenu') && this.addLongPressListener) {
|
||||
return this.addLongPressListener(obj, handler, id);
|
||||
|
||||
} else if ('addEventListener' in obj) {
|
||||
|
||||
if (type === 'mousewheel') {
|
||||
|
@ -12,9 +12,10 @@ L.Draggable = L.Class.extend({
|
||||
TAP_TOLERANCE: 15
|
||||
},
|
||||
|
||||
initialize: function (element, dragStartTarget) {
|
||||
initialize: function (element, dragStartTarget, contextMenuEmulation) {
|
||||
this._element = element;
|
||||
this._dragStartTarget = dragStartTarget || element;
|
||||
this._contextMenuEmulation = contextMenuEmulation;
|
||||
},
|
||||
|
||||
enable: function () {
|
||||
@ -64,6 +65,20 @@ 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._contextMenuEmulation) {
|
||||
var self = this;
|
||||
this._contextMenuTimeout = setTimeout(function () {
|
||||
var dist = (self._newPos && self._newPos.distanceTo(self._startPos)) || 0;
|
||||
|
||||
if (dist < L.Draggable.TAP_TOLERANCE) {
|
||||
self._simulateClick = false;
|
||||
self._onUp();
|
||||
self._simulateEvent('contextmenu', first);
|
||||
}
|
||||
}, 1000);
|
||||
}
|
||||
|
||||
L.DomEvent.on(document, L.Draggable.MOVE, this._onMove, this);
|
||||
L.DomEvent.on(document, L.Draggable.END, this._onUp, this);
|
||||
},
|
||||
@ -106,6 +121,7 @@ L.Draggable = L.Class.extend({
|
||||
|
||||
_onUp: function (e) {
|
||||
var simulateClickTouch;
|
||||
clearTimeout(this._contextMenuTimeout);
|
||||
if (this._simulateClick && e.changedTouches) {
|
||||
var first = e.changedTouches[0],
|
||||
el = first.target,
|
||||
|
@ -10,6 +10,8 @@ L.Map.mergeOptions({
|
||||
inertiaMaxSpeed: 6000, // px/s
|
||||
inertiaThreshold: L.Browser.touch ? 32 : 18, // ms
|
||||
|
||||
touchContextMenuEmulation: 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.touchContextMenuEmulation);
|
||||
|
||||
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);
|
||||
|
Loading…
Reference in New Issue
Block a user