TouchEventUtils.js 1.1 KB

1234567891011121314151617181920212223242526272829303132
  1. "use strict";
  2. /**
  3. * Copyright (c) 2013-present, Facebook, Inc.
  4. *
  5. * This source code is licensed under the MIT license found in the
  6. * LICENSE file in the root directory of this source tree.
  7. *
  8. */
  9. var TouchEventUtils = {
  10. /**
  11. * Utility function for common case of extracting out the primary touch from a
  12. * touch event.
  13. * - `touchEnd` events usually do not have the `touches` property.
  14. * http://stackoverflow.com/questions/3666929/
  15. * mobile-sarai-touchend-event-not-firing-when-last-touch-is-removed
  16. *
  17. * @param {Event} nativeEvent Native event that may or may not be a touch.
  18. * @return {TouchesObject?} an object with pageX and pageY or null.
  19. */
  20. extractSingleTouch: function extractSingleTouch(nativeEvent) {
  21. var touches = nativeEvent.touches;
  22. var changedTouches = nativeEvent.changedTouches;
  23. var hasTouches = touches && touches.length > 0;
  24. var hasChangedTouches = changedTouches && changedTouches.length > 0;
  25. return !hasTouches && hasChangedTouches ? changedTouches[0] : hasTouches ? touches[0] : nativeEvent;
  26. }
  27. };
  28. module.exports = TouchEventUtils;