API Docs for: 3.8.0
Show:

File: event/js/event-facade-dom-touch.js

  1. /**
  2. Adds touch event facade normalization properties (touches, changedTouches, targetTouches etc.) to the DOM event facade. Adds
  3. touch events to the DOM events whitelist.

  4. @example
  5.     YUI().use('event-touch', function (Y) {
  6.         Y.one('#myDiv').on('touchstart', function(e) {
  7.             ...
  8.         });
  9.     });
  10. @module event
  11. @submodule event-touch
  12.  */
  13. var SCALE = "scale",
  14.     ROTATION = "rotation",
  15.     IDENTIFIER = "identifier",
  16.     win = Y.config.win,
  17.     GESTURE_MAP = {};

  18. /**
  19.  * Adds touch event facade normalization properties to the DOM event facade
  20.  *
  21.  * @method _touch
  22.  * @for DOMEventFacade
  23.  * @private
  24.  * @param ev {Event} the DOM event
  25.  * @param currentTarget {HTMLElement} the element the listener was attached to
  26.  * @param wrapper {Event.Custom} the custom event wrapper for this DOM event
  27.  */
  28. Y.DOMEventFacade.prototype._touch = function(e, currentTarget, wrapper) {

  29.     var i,l, etCached, et,touchCache;

  30.     Y.log("Calling facade._touch() with e = " + e, "info", "event-touch");

  31.     if (e.touches) {
  32.         Y.log("Found e.touches. Replicating on facade");

  33.         /**
  34.          * Array of individual touch events for touch points that are still in
  35.          * contact with the touch surface.
  36.          *
  37.          * @property touches
  38.          * @type {DOMEventFacade[]}
  39.          */
  40.         this.touches = [];
  41.         touchCache = {};

  42.         for (i = 0, l = e.touches.length; i < l; ++i) {
  43.             et = e.touches[i];
  44.             touchCache[Y.stamp(et)] = this.touches[i] = new Y.DOMEventFacade(et, currentTarget, wrapper);
  45.         }
  46.     }

  47.     if (e.targetTouches) {
  48.         Y.log("Found e.targetTouches. Replicating on facade", "info", "event-touch");

  49.         /**
  50.          * Array of individual touch events still in contact with the touch
  51.          * surface and whose `touchstart` event occurred inside the same taregt
  52.          * element as the current target element.
  53.          *
  54.          * @property targetTouches
  55.          * @type {DOMEventFacade[]}
  56.          */
  57.         this.targetTouches = [];

  58.         for (i = 0, l = e.targetTouches.length; i < l; ++i) {
  59.             et = e.targetTouches[i];
  60.             etCached = touchCache && touchCache[Y.stamp(et, true)];

  61.             this.targetTouches[i] = etCached || new Y.DOMEventFacade(et, currentTarget, wrapper);
  62.            
  63.             if (etCached) { Y.log("Found native event in touches. Using same facade in targetTouches", "info", "event-touch"); }
  64.         }
  65.     }

  66.     if (e.changedTouches) {
  67.         Y.log("Found e.changedTouches. Replicating on facade", "info", "event-touch");

  68.         /**
  69.         An array of event-specific touch events.

  70.         For `touchstart`, the touch points that became active with the current
  71.         event.

  72.         For `touchmove`, the touch points that have changed since the last
  73.         event.
  74.        
  75.         For `touchend`, the touch points that have been removed from the touch
  76.         surface.

  77.         @property changedTouches
  78.         @type {DOMEventFacade[]}
  79.         **/
  80.         this.changedTouches = [];

  81.         for (i = 0, l = e.changedTouches.length; i < l; ++i) {
  82.             et = e.changedTouches[i];
  83.             etCached = touchCache && touchCache[Y.stamp(et, true)];

  84.             this.changedTouches[i] = etCached || new Y.DOMEventFacade(et, currentTarget, wrapper);
  85.            
  86.             if (etCached) { Y.log("Found native event in touches. Using same facade in changedTouches", "info", "event-touch"); }
  87.         }
  88.     }

  89.     if (SCALE in e) {
  90.         this[SCALE] = e[SCALE];
  91.     }

  92.     if (ROTATION in e) {
  93.         this[ROTATION] = e[ROTATION];
  94.     }

  95.     if (IDENTIFIER in e) {
  96.         this[IDENTIFIER] = e[IDENTIFIER];
  97.     }
  98. };

  99. //Adding MSPointer events to whitelisted DOM Events. MSPointer event payloads
  100. //have the same properties as mouse events.
  101. if (Y.Node.DOM_EVENTS) {
  102.     Y.mix(Y.Node.DOM_EVENTS, {
  103.         touchstart:1,
  104.         touchmove:1,
  105.         touchend:1,
  106.         touchcancel:1,
  107.         gesturestart:1,
  108.         gesturechange:1,
  109.         gestureend:1,
  110.         MSPointerDown:1,
  111.         MSPointerUp:1,
  112.         MSPointerMove:1
  113.     });
  114. }

  115. //Add properties to Y.EVENT.GESTURE_MAP based on feature detection.
  116. if ((win && ("ontouchstart" in win)) && !(Y.UA.chrome && Y.UA.chrome < 6)) {
  117.     GESTURE_MAP.start = "touchstart";
  118.     GESTURE_MAP.end = "touchend";
  119.     GESTURE_MAP.move = "touchmove";
  120.     GESTURE_MAP.cancel = "touchcancel";
  121. }



  122. else if (win && ("msPointerEnabled" in win.navigator)) {
  123.     GESTURE_MAP.start = "MSPointerDown";
  124.     GESTURE_MAP.end = "MSPointerUp";
  125.     GESTURE_MAP.move = "MSPointerMove";
  126.     GESTURE_MAP.cancel = "MSPointerCancel";
  127. }

  128. else {
  129.     GESTURE_MAP.start = "mousedown";
  130.     GESTURE_MAP.end = "mouseup";
  131.     GESTURE_MAP.move = "mousemove";
  132.     GESTURE_MAP.cancel = "mousecancel";
  133. }

  134. /**
  135.  * A object literal with keys "start", "end", and "move". The value for each key is a
  136.  * string representing the event for that environment. For touch environments, the respective
  137.  * values are "touchstart", "touchend" and "touchmove". Mouse and MSPointer environments are also
  138.  * supported via feature detection.
  139.  *
  140.  * @property _GESTURE_MAP
  141.  * @type Object
  142.  * @static
  143.  */
  144. Y.Event._GESTURE_MAP = GESTURE_MAP;

  145.