diff --git a/has.js b/has.js index dd765d32f8..0ab8c08034 100644 --- a/has.js +++ b/has.js @@ -99,6 +99,7 @@ define(["require", "module"], function(require, module){ // Common application level tests has.add("dom-addeventlistener", !!document.addEventListener); has.add("touch", "ontouchstart" in document || window.navigator.msMaxTouchPoints > 0); + has.add("pointer", !!window.navigator.msPointerEnabled); // I don't know if any of these tests are really correct, just a rough guess has.add("device-width", screen.availWidth || innerWidth); diff --git a/pointer.js b/pointer.js new file mode 100644 index 0000000000..72eaa25a01 --- /dev/null +++ b/pointer.js @@ -0,0 +1,178 @@ +define(["./_base/kernel", "./aspect", "./dom", "./dom-class", "./_base/lang", "./on", "./has", "./mouse", "./domReady", "./_base/window"], +function(dojo, aspect, dom, domClass, lang, on, has, mouse, domReady, win){ + + // module: + // dojo/pointer + + var hasTouch = has("touch"), + hasPointer = has("pointer"), + lastTouch, + mouseEventFireThreshold = 1000, + + POINTER_TYPE_TOUCH = "touch", + POINTER_TYPE_PEN = "pen", + POINTER_TYPE_MOUSE = "mouse", + + bindEvent = function(preferredEventName, mouseType, touchType, pointerType) { + + pointerType = browserSpecificEventName(pointerType); + + // Returns synthetic event that listens for pointer or both the specified mouse event and specified touch event. + // But ignore fake mouse events that were generated due to the user touching the screen. + if (hasPointer) { + // Pointer events are designed to handle both mouse and touch in a uniform way, + // so just use that regardless of hasTouch. + return function(node, listener) { + // TODO We may want to normalize Pointer events too since there could be a difference in comparison to the spec; + // for example in IE10 MSPointer.pointerType is int instead of string as per specification. + return on(node, pointerType, listener); + } + } + + if (hasTouch) { + return function(node, listener) { + var touchHandle = on(node, touchType, function(evt) { + var self = this; + lastTouch = (new Date()).getTime(); + + normalizeEvent(evt, POINTER_TYPE_TOUCH, preferredEventName).forEach(function(e){ + listener.call(self, e); + }) + + }), + mouseHandle = on(node, mouseType, function(evt) { + if (!lastTouch || (new Date()).getTime() > lastTouch + mouseEventFireThreshold) { + listener.call(this, normalizeEvent(evt, POINTER_TYPE_MOUSE, preferredEventName)[0]); + } + }); + return { + remove: function() { + touchHandle.remove(); + mouseHandle.remove(); + } + }; + }; + } + + // no Pointer or Touch support + // Avoid creating listeners for touch events on performance sensitive older browsers like IE6 + return function(node, listener) { + return on(node, mouseType, function(evt) { + listener.call(this, normalizeEvent(evt, POINTER_TYPE_MOUSE, preferredEventName)[0]); + }); + } + + }, + + /** + * Converts given Mouse or Touch event to a Pointer event. + * + * In case of Touch event returns separate Pointer event for each touch (event.changedTouches container). + * + * @param {Event} originalEvent TODO + * @param {Enum} [eventType] TODO + * @return {Array} A list of Pointer events. + */ + normalizeEvent = function (originalEvent, eventType, preferredEventName) { + // defines extra properties for normalized events (use default values) + var pointerProperties = {"width" : 0, "height" : 0, "pressure" : 0, "tiltX" : 0, "tiltY" : 0, + "type" : preferredEventName, "pointerType" : eventType, "isPrimary" : true, "POINTER_TYPE_TOUCH" : POINTER_TYPE_TOUCH, + "POINTER_TYPE_PEN" : POINTER_TYPE_PEN, "POINTER_TYPE_MOUSE" : POINTER_TYPE_MOUSE}, + normalizedEvents = []; + + if (eventType === POINTER_TYPE_MOUSE) { + + // Mouse is required to have a pointerId of 1 + pointerProperties.pointerId = 1; + + normalizedEvents.push(PointerEvent(originalEvent, pointerProperties)); + + } else if (eventType === POINTER_TYPE_TOUCH) { + + for(var i=0; i + + + + + Dojo Pointer events Testing + + + + + +
+
Touch here!
+
+
+ + diff --git a/tests/test_pointer_paint.html b/tests/test_pointer_paint.html new file mode 100644 index 0000000000..eb82de3da8 --- /dev/null +++ b/tests/test_pointer_paint.html @@ -0,0 +1,171 @@ + + + + + + Dojo Pointer Testing + + + + + + + + +
+ +
+ +