Attempt to handle IE >= 10 pointer events

Using the same technique as Leaflet, this relays MSPointer events to touch
events. Using the simulator, I can verify that even pinch-zoom works with
this change, but the visual experience is a bit jumpy. Would be great if
people could test this on physical touch devices and see how it looks/feels
there.
This commit is contained in:
ahocevar
2013-04-16 11:18:17 +02:00
parent 80a6185947
commit 9cdc991b8c
4 changed files with 164 additions and 2 deletions

View File

@@ -566,6 +566,7 @@ OpenLayers.Events = OpenLayers.Class({
this.listeners = {};
this.extensions = {};
this.extensionCount = {};
this._msTouches = [];
// if a dom element is specified, add a listeners list
// for browser events on the element and register them
@@ -632,11 +633,16 @@ OpenLayers.Events = OpenLayers.Class({
);
}
this.element = element;
var msTouch = !!window.navigator.msMaxTouchPoints;
var type;
for (var i = 0, len = this.BROWSER_EVENTS.length; i < len; i++) {
type = this.BROWSER_EVENTS[i];
// register the event cross-browser
OpenLayers.Event.observe(
element, this.BROWSER_EVENTS[i], this.eventHandler
OpenLayers.Event.observe(element, type, this.eventHandler
);
if (msTouch && type.indexOf('touch') === 0) {
this.addMsTouchListener(element, type, this.eventHandler);
}
}
// disable dragstart in IE so that mousedown/move/up works normally
OpenLayers.Event.observe(element, "dragstart", OpenLayers.Event.stop);
@@ -1014,5 +1020,154 @@ OpenLayers.Events = OpenLayers.Class({
);
},
/**
* Method: addMsTouchListener
*
* Parameters:
* element - {DOMElement} The DOM element to register the listener on
* type - {String} The event type
* handler - {Function} the handler
*/
addMsTouchListener: function (element, type, handler) {
var eventHandler = this.eventHandler;
var touches = this._msTouches;
function msHandler(evt) {
handler(OpenLayers.Util.applyDefaults({
stopPropagation: function() {
for (var i=touches.length-1; i>=0; --i) {
touches[i].stopPropagation();
}
},
preventDefault: function() {
for (var i=touches.length-1; i>=0; --i) {
touches[i].preventDefault();
}
},
type: type
}, evt));
}
switch (type) {
case 'touchstart':
return this.addMsTouchListenerStart(element, type, msHandler);
case 'touchend':
return this.addMsTouchListenerEnd(element, type, msHandler);
case 'touchmove':
return this.addMsTouchListenerMove(element, type, msHandler);
default:
throw 'Unknown touch event type';
}
},
/**
* Method: addMsTouchListenerStart
*
* Parameters:
* element - {DOMElement} The DOM element to register the listener on
* type - {String} The event type
* handler - {Function} the handler
*/
addMsTouchListenerStart: function(element, type, handler) {
var touches = this._msTouches;
var cb = function(e) {
var alreadyInArray = false;
for (var i = 0; i < touches.length; i++) {
if (touches[i].pointerId == e.pointerId) {
alreadyInArray = true;
break;
}
}
if (!alreadyInArray) {
touches.push(e);
}
e.touches = touches.slice();
e.changedTouches = [e];
handler(e);
};
OpenLayers.Event.observe(element, 'MSPointerDown', cb);
// Need to also listen for end events to keep the _msTouches list
// accurate
var internalCb = function(e) {
for (var i = 0; i < touches.length; i++) {
if (touches[i].pointerId == e.pointerId) {
touches.splice(i, 1);
break;
}
}
};
OpenLayers.Event.observe(element, 'MSPointerUp', internalCb);
},
/**
* Method: addMsTouchListenerMove
*
* Parameters:
* element - {DOMElement} The DOM element to register the listener on
* type - {String} The event type
* handler - {Function} the handler
*/
addMsTouchListenerMove: function (element, type, handler) {
var touches = this._msTouches;
var cb = function(e) {
//Don't fire touch moves when mouse isn't down
if (e.pointerType == e.MSPOINTER_TYPE_MOUSE && e.buttons == 0) {
return;
}
for (var i = 0; i < touches.length; i++) {
if (touches[i].pointerId == e.pointerId) {
touches[i] = e;
break;
}
}
e.touches = touches.slice();
e.changedTouches = [e];
e.type = type;
handler(e);
};
OpenLayers.Event.observe(element, 'MSPointerMove', cb);
},
/**
* Method: addMsTouchListenerEnd
*
* Parameters:
* element - {DOMElement} The DOM element to register the listener on
* type - {String} The event type
* handler - {Function} the handler
*/
addMsTouchListenerEnd: function (element, type, handler) {
var touches = this._msTouches;
var cb = function(e) {
for (var i = 0; i < touches.length; i++) {
if (touches[i].pointerId == e.pointerId) {
touches.splice(i, 1);
break;
}
}
e.touches = touches.slice();
e.changedTouches = [e];
e.type = type;
handler(e);
};
OpenLayers.Event.observe(element, 'MSPointerUp', cb);
},
CLASS_NAME: "OpenLayers.Events"
});