76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav GlassYUI.add('event-touch', function(Y) {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass/**
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * Adds touch event facade normalization properties (touches, changedTouches, targetTouches etc.) to the DOM event facade
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass *
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @module event-touch
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass */
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glassvar SCALE = "scale",
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass ROTATION = "rotation",
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass IDENTIFIER = "identifier";
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass/**
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * Adds touch event facade normalization properties to the DOM event facade
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass *
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @method _touch
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @for DOMEventFacade
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @private
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @param ev {Event} the DOM event
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @param currentTarget {HTMLElement} the element the listener was attached to
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * @param wrapper {Event.Custom} the custom event wrapper for this DOM event
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass */
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav GlassY.DOMEventFacade.prototype._touch = function(e, currentTarget, wrapper) {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass var i,l, etCached, et,touchCache;
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass Y.log("Calling facade._touch() with e = " + e);
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass if (e.touches) {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass Y.log("Found e.touches. Replicating on facade");
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
dcde9f9fdb7cdb40d391c49e82df809afef42c01Luke Smith /**
dcde9f9fdb7cdb40d391c49e82df809afef42c01Luke Smith * Array of individual touch events for touch points that are still in
dcde9f9fdb7cdb40d391c49e82df809afef42c01Luke Smith * contact with the touch surface.
dcde9f9fdb7cdb40d391c49e82df809afef42c01Luke Smith *
dcde9f9fdb7cdb40d391c49e82df809afef42c01Luke Smith * @property touches
dcde9f9fdb7cdb40d391c49e82df809afef42c01Luke Smith * @type {DOMEventFacade[]}
dcde9f9fdb7cdb40d391c49e82df809afef42c01Luke Smith */
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass this.touches = [];
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass touchCache = {};
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass for (i = 0, l = e.touches.length; i < l; ++i) {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass et = e.touches[i];
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass touchCache[Y.stamp(et)] = this.touches[i] = new Y.DOMEventFacade(et, currentTarget, wrapper);
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass }
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass }
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass if (e.targetTouches) {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass Y.log("Found e.targetTouches. Replicating on facade");
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
dcde9f9fdb7cdb40d391c49e82df809afef42c01Luke Smith /**
dcde9f9fdb7cdb40d391c49e82df809afef42c01Luke Smith * Array of individual touch events still in contact with the touch
dcde9f9fdb7cdb40d391c49e82df809afef42c01Luke Smith * surface and whose `touchstart` event occurred inside the same taregt
dcde9f9fdb7cdb40d391c49e82df809afef42c01Luke Smith * element as the current target element.
dcde9f9fdb7cdb40d391c49e82df809afef42c01Luke Smith *
dcde9f9fdb7cdb40d391c49e82df809afef42c01Luke Smith * @property targetTouches
dcde9f9fdb7cdb40d391c49e82df809afef42c01Luke Smith * @type {DOMEventFacade[]}
dcde9f9fdb7cdb40d391c49e82df809afef42c01Luke Smith */
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass this.targetTouches = [];
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass for (i = 0, l = e.targetTouches.length; i < l; ++i) {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass et = e.targetTouches[i];
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass etCached = touchCache && touchCache[Y.stamp(et, true)];
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass this.targetTouches[i] = etCached || new Y.DOMEventFacade(et, currentTarget, wrapper);
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass if (etCached) { Y.log("Found native event in touches. Using same facade in targetTouches"); }
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass }
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass }
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass if (e.changedTouches) {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass Y.log("Found e.changedTouches. Replicating on facade");
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
dcde9f9fdb7cdb40d391c49e82df809afef42c01Luke Smith /**
dcde9f9fdb7cdb40d391c49e82df809afef42c01Luke Smith An array of event-specific touch events.
dcde9f9fdb7cdb40d391c49e82df809afef42c01Luke Smith
dcde9f9fdb7cdb40d391c49e82df809afef42c01Luke Smith For `touchstart`, the touch points that became active with the current
dcde9f9fdb7cdb40d391c49e82df809afef42c01Luke Smith event.
dcde9f9fdb7cdb40d391c49e82df809afef42c01Luke Smith
dcde9f9fdb7cdb40d391c49e82df809afef42c01Luke Smith For `touchmove`, the touch points that have changed since the last
dcde9f9fdb7cdb40d391c49e82df809afef42c01Luke Smith event.
dcde9f9fdb7cdb40d391c49e82df809afef42c01Luke Smith
dcde9f9fdb7cdb40d391c49e82df809afef42c01Luke Smith For `touchend`, the touch points that have been removed from the touch
dcde9f9fdb7cdb40d391c49e82df809afef42c01Luke Smith surface.
dcde9f9fdb7cdb40d391c49e82df809afef42c01Luke Smith
dcde9f9fdb7cdb40d391c49e82df809afef42c01Luke Smith @property changedTouches
dcde9f9fdb7cdb40d391c49e82df809afef42c01Luke Smith @type {DOMEventFacade[]}
dcde9f9fdb7cdb40d391c49e82df809afef42c01Luke Smith **/
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass this.changedTouches = [];
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass for (i = 0, l = e.changedTouches.length; i < l; ++i) {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass et = e.changedTouches[i];
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass etCached = touchCache && touchCache[Y.stamp(et, true)];
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass this.changedTouches[i] = etCached || new Y.DOMEventFacade(et, currentTarget, wrapper);
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass if (etCached) { Y.log("Found native event in touches. Using same facade in changedTouches"); }
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass }
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass }
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass if (SCALE in e) {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass this[SCALE] = e[SCALE];
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass }
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass if (ROTATION in e) {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass this[ROTATION] = e[ROTATION];
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass }
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass if (IDENTIFIER in e) {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass this[IDENTIFIER] = e[IDENTIFIER];
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass }
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass};
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glassif (Y.Node.DOM_EVENTS) {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass Y.mix(Y.Node.DOM_EVENTS, {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass touchstart:1,
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass touchmove:1,
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass touchend:1,
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass touchcancel:1,
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass gesturestart:1,
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass gesturechange:1,
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass gestureend:1
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass });
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass}
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass}, '@VERSION@' ,{requires:['node-base']});