Fix up my stupidness with add/removeEventListener. Fixes the zoom drag off screen bug without needing the work around

This commit is contained in:
danzel 2012-10-16 10:32:28 +13:00
parent af2318afaa
commit 84d5f614da

View File

@ -24,6 +24,7 @@ L.Util.extend(L.DomEvent, {
touches = this._msTouches; touches = this._msTouches;
var cb = function (e) { var cb = function (e) {
//console.log(e.type + ' ' + touches.length + ' id: ' + e.pointerId);
var alreadyInArray = false; var alreadyInArray = false;
for (var i = 0; i < touches.length; i++) { for (var i = 0; i < touches.length; i++) {
@ -44,11 +45,12 @@ L.Util.extend(L.DomEvent, {
}; };
obj[pre + 'touchstart' + id] = cb; obj[pre + 'touchstart' + id] = cb;
obj.addEventListener('MSPointerDown', cb, this); obj.addEventListener('MSPointerDown', cb, false);
//Need to also listen for end events to keep the _msTouches list accurate //Need to also listen for end events to keep the _msTouches list accurate
var internalCb = function (e) { var internalCb = function (e) {
//console.log('internal up ' + e.pointerId);
for (var i = 0; i < touches.length; i++) { for (var i = 0; i < touches.length; i++) {
if (touches[i].pointerId === e.pointerId) { if (touches[i].pointerId === e.pointerId) {
touches.splice(i, 1); touches.splice(i, 1);
@ -56,9 +58,10 @@ L.Util.extend(L.DomEvent, {
} }
} }
}; };
obj.addEventListener('MSPointerUp', internalCb, this); //We listen on the documentElement as any drags that end by moving the touch off the screen get fired there
obj.addEventListener('MSPointerCancel', internalCb, this); obj.addEventListener('MSPointerUp', internalCb, false);
obj[pre + 'touchstartend' + id] = cb; obj.addEventListener('MSPointerCancel', internalCb, false);
obj[pre + 'touchstartend' + id] = internalCb;
return this; return this;
}, },
@ -88,7 +91,7 @@ L.Util.extend(L.DomEvent, {
}; };
obj[pre + 'touchmove' + id] = cb; obj[pre + 'touchmove' + id] = cb;
obj.addEventListener('MSPointerMove', cb, this); obj.addEventListener('MSPointerMove', cb, false);
return this; return this;
}, },
@ -99,19 +102,13 @@ L.Util.extend(L.DomEvent, {
var cb = function (e) { var cb = function (e) {
//console.log(e.type + ' ' + touches.length + ' id: ' + e.pointerId); //console.log(e.type + ' ' + touches.length + ' id: ' + e.pointerId);
var found = false;
for (var i = 0; i < touches.length; i++) { for (var i = 0; i < touches.length; i++) {
if (touches[i].pointerId === e.pointerId) { if (touches[i].pointerId === e.pointerId) {
touches.splice(i, 1); touches.splice(i, 1);
found = true;
break; break;
} }
} }
if (!found) { //Crappy work around for pointerIDs breaking when leaving screen edge https://github.com/CloudMade/Leaflet/issues/871#issuecomment-9125445
touches.splice(0, touches.length);
}
e.touches = touches.slice(); e.touches = touches.slice();
e.changedTouches = [e]; e.changedTouches = [e];
@ -119,8 +116,8 @@ L.Util.extend(L.DomEvent, {
}; };
obj[pre + 'touchend' + id] = cb; obj[pre + 'touchend' + id] = cb;
obj.addEventListener('MSPointerUp', cb, this); obj.addEventListener('MSPointerUp', cb, false);
obj.addEventListener('MSPointerCancel', cb, this); obj.addEventListener('MSPointerCancel', cb, false);
return this; return this;
}, },
@ -131,16 +128,17 @@ L.Util.extend(L.DomEvent, {
switch (type) { switch (type) {
case 'touchstart': case 'touchstart':
obj.removeEventListener('MSPointerDown', cb, this); obj.removeEventListener('MSPointerDown', cb, false);
obj.removeEventListener('MSPointerUp', obj[pre + 'touchstartend' + id], this);
obj.removeEventListener('MSPointerCancel', obj[pre + 'touchstartend' + id], this); obj.removeEventListener('MSPointerUp', obj[pre + 'touchstartend' + id], false);
obj.removeEventListener('MSPointerCancel', obj[pre + 'touchstartend' + id], false);
break; break;
case 'touchmove': case 'touchmove':
obj.removeEventListener('MSPointerMove', cb, this); obj.removeEventListener('MSPointerMove', cb, false);
break; break;
case 'touchend': case 'touchend':
obj.removeEventListener('MSPointerUp', cb, this); obj.removeEventListener('MSPointerUp', cb, false);
obj.removeEventListener('MSPointerCancel', cb, this); obj.removeEventListener('MSPointerCancel', cb, false);
break; break;
} }