JAVASCRIPT   63

event-touch-debug.js

Guest on 12th October 2021 03:10:44 PM

  1. /*
  2. YUI 3.17.2 (build 9c3c78e)
  3. Copyright  Yahoo! Inc. All rights reserved.
  4. Licensed under the BSD License.
  5. http://yuilibrary.com/license/
  6. */
  7.  
  8. YUI.add('event-touch', function (Y, NAME) {
  9.  
  10. /**
  11. Adds touch event facade normalization properties (touches, changedTouches, targetTouches etc.) to the DOM event facade. Adds
  12. touch events to the DOM events whitelist.
  13.  
  14. @example
  15.     YUI().use('event-touch', function (Y) {
  16.         Y.one('#myDiv').on('touchstart', function(e) {
  17.             ...
  18.         });
  19.     });
  20. @module event
  21. @submodule event-touch
  22.  */
  23. var SCALE = "scale",
  24.     ROTATION = "rotation",
  25.     IDENTIFIER = "identifier",
  26.     win = Y.config.win,
  27.     GESTURE_MAP = {};
  28.  
  29. /**
  30.  * Adds touch event facade normalization properties to the DOM event facade
  31.  *
  32.  * @method _touch
  33.  * @for DOMEventFacade
  34.  * @private
  35.  * @param ev {Event} the DOM event
  36.  * @param currentTarget {HTMLElement} the element the listener was attached to
  37.  * @param wrapper {CustomEvent} the custom event wrapper for this DOM event
  38.  */
  39. Y.DOMEventFacade.prototype._touch = function(e, currentTarget, wrapper) {
  40.  
  41.     var i,l, etCached, et,touchCache;
  42.  
  43.     Y.log("Calling facade._touch() with e = " + e, "debug", "event-touch");
  44.  
  45.     if (e.touches) {
  46.         Y.log("Found e.touches. Replicating on facade", "info", "event-touch");
  47.  
  48.         /**
  49.          * Array of individual touch events for touch points that are still in
  50.          * contact with the touch surface.
  51.          *
  52.          * @property touches
  53.          * @type {DOMEventFacade[]}
  54.          */
  55.         this.touches = [];
  56.         touchCache = {};
  57.  
  58.         for (i = 0, l = e.touches.length; i < l; ++i) {
  59.             et = e.touches[i];
  60.             touchCache[Y.stamp(et)] = this.touches[i] = new Y.DOMEventFacade(et, currentTarget, wrapper);
  61.         }
  62.     }
  63.  
  64.     if (e.targetTouches) {
  65.         Y.log("Found e.targetTouches. Replicating on facade", "info", "event-touch");
  66.  
  67.         /**
  68.          * Array of individual touch events still in contact with the touch
  69.          * surface and whose `touchstart` event occurred inside the same taregt
  70.          * element as the current target element.
  71.          *
  72.          * @property targetTouches
  73.          * @type {DOMEventFacade[]}
  74.          */
  75.         this.targetTouches = [];
  76.  
  77.         for (i = 0, l = e.targetTouches.length; i < l; ++i) {
  78.             et = e.targetTouches[i];
  79.             etCached = touchCache && touchCache[Y.stamp(et, true)];
  80.  
  81.             this.targetTouches[i] = etCached || new Y.DOMEventFacade(et, currentTarget, wrapper);
  82.  
  83.             if (etCached) { Y.log("Found native event in touches. Using same facade in targetTouches", "info", "event-touch"); }
  84.         }
  85.     }
  86.  
  87.     if (e.changedTouches) {
  88.         Y.log("Found e.changedTouches. Replicating on facade", "info", "event-touch");
  89.  
  90.         /**
  91.         An array of event-specific touch events.
  92.  
  93.         For `touchstart`, the touch points that became active with the current
  94.         event.
  95.  
  96.         For `touchmove`, the touch points that have changed since the last
  97.         event.
  98.  
  99.         For `touchend`, the touch points that have been removed from the touch
  100.         surface.
  101.  
  102.         @property changedTouches
  103.         @type {DOMEventFacade[]}
  104.         **/
  105.         this.changedTouches = [];
  106.  
  107.         for (i = 0, l = e.changedTouches.length; i < l; ++i) {
  108.             et = e.changedTouches[i];
  109.             etCached = touchCache && touchCache[Y.stamp(et, true)];
  110.  
  111.             this.changedTouches[i] = etCached || new Y.DOMEventFacade(et, currentTarget, wrapper);
  112.  
  113.             if (etCached) { Y.log("Found native event in touches. Using same facade in changedTouches", "info", "event-touch"); }
  114.         }
  115.     }
  116.  
  117.     if (SCALE in e) {
  118.         this[SCALE] = e[SCALE];
  119.     }
  120.  
  121.     if (ROTATION in e) {
  122.         this[ROTATION] = e[ROTATION];
  123.     }
  124.  
  125.     if (IDENTIFIER in e) {
  126.         this[IDENTIFIER] = e[IDENTIFIER];
  127.     }
  128. };
  129.  
  130. //Adding MSPointer events to whitelisted DOM Events. MSPointer event payloads
  131. //have the same properties as mouse events.
  132. if (Y.Node.DOM_EVENTS) {
  133.     Y.mix(Y.Node.DOM_EVENTS, {
  134.         touchstart:1,
  135.         touchmove:1,
  136.         touchend:1,
  137.         touchcancel:1,
  138.         gesturestart:1,
  139.         gesturechange:1,
  140.         gestureend:1,
  141.         MSPointerDown:1,
  142.         MSPointerUp:1,
  143.         MSPointerMove:1,
  144.         MSPointerCancel:1,
  145.         pointerdown:1,
  146.         pointerup:1,
  147.         pointermove:1,
  148.         pointercancel:1
  149.     });
  150. }
  151.  
  152. //Add properties to Y.EVENT.GESTURE_MAP based on feature detection.
  153. if ((win && ("ontouchstart" in win)) && !(Y.UA.chrome && Y.UA.chrome < 6)) {
  154.     GESTURE_MAP.start = ["touchstart", "mousedown"];
  155.     GESTURE_MAP.end = ["touchend", "mouseup"];
  156.     GESTURE_MAP.move = ["touchmove", "mousemove"];
  157.     GESTURE_MAP.cancel = ["touchcancel", "mousecancel"];
  158. }
  159.  
  160. else if (win && win.PointerEvent) {
  161.     GESTURE_MAP.start = "pointerdown";
  162.     GESTURE_MAP.end = "pointerup";
  163.     GESTURE_MAP.move = "pointermove";
  164.     GESTURE_MAP.cancel = "pointercancel";
  165. }
  166.  
  167. else if (win && ("msPointerEnabled" in win.navigator)) {
  168.     GESTURE_MAP.start = "MSPointerDown";
  169.     GESTURE_MAP.end = "MSPointerUp";
  170.     GESTURE_MAP.move = "MSPointerMove";
  171.     GESTURE_MAP.cancel = "MSPointerCancel";
  172. }
  173.  
  174. else {
  175.     GESTURE_MAP.start = "mousedown";
  176.     GESTURE_MAP.end = "mouseup";
  177.     GESTURE_MAP.move = "mousemove";
  178.     GESTURE_MAP.cancel = "mousecancel";
  179. }
  180.  
  181. /**
  182.  * A object literal with keys "start", "end", and "move". The value for each key is a
  183.  * string representing the event for that environment. For touch environments, the respective
  184.  * values are "touchstart", "touchend" and "touchmove". Mouse and MSPointer environments are also
  185.  * supported via feature detection.
  186.  *
  187.  * @property _GESTURE_MAP
  188.  * @type Object
  189.  * @static
  190.  */
  191. Y.Event._GESTURE_MAP = GESTURE_MAP;
  192.  
  193.  
  194. }, '3.17.2', {"requires": ["node-base"]});

Raw Paste


Login or Register to edit or fork this paste. It's free.