react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / react-tools / src / browser / eventPlugins / TapEventPlugin.js
81155 views/**1* Copyright 2013-2014, Facebook, Inc.2* All rights reserved.3*4* This source code is licensed under the BSD-style license found in the5* LICENSE file in the root directory of this source tree. An additional grant6* of patent rights can be found in the PATENTS file in the same directory.7*8* @providesModule TapEventPlugin9* @typechecks static-only10*/1112"use strict";1314var EventConstants = require('EventConstants');15var EventPluginUtils = require('EventPluginUtils');16var EventPropagators = require('EventPropagators');17var SyntheticUIEvent = require('SyntheticUIEvent');18var TouchEventUtils = require('TouchEventUtils');19var ViewportMetrics = require('ViewportMetrics');2021var keyOf = require('keyOf');22var topLevelTypes = EventConstants.topLevelTypes;2324var isStartish = EventPluginUtils.isStartish;25var isEndish = EventPluginUtils.isEndish;2627/**28* Number of pixels that are tolerated in between a `touchStart` and `touchEnd`29* in order to still be considered a 'tap' event.30*/31var tapMoveThreshold = 10;32var startCoords = {x: null, y: null};3334var Axis = {35x: {page: 'pageX', client: 'clientX', envScroll: 'currentPageScrollLeft'},36y: {page: 'pageY', client: 'clientY', envScroll: 'currentPageScrollTop'}37};3839function getAxisCoordOfEvent(axis, nativeEvent) {40var singleTouch = TouchEventUtils.extractSingleTouch(nativeEvent);41if (singleTouch) {42return singleTouch[axis.page];43}44return axis.page in nativeEvent ?45nativeEvent[axis.page] :46nativeEvent[axis.client] + ViewportMetrics[axis.envScroll];47}4849function getDistance(coords, nativeEvent) {50var pageX = getAxisCoordOfEvent(Axis.x, nativeEvent);51var pageY = getAxisCoordOfEvent(Axis.y, nativeEvent);52return Math.pow(53Math.pow(pageX - coords.x, 2) + Math.pow(pageY - coords.y, 2),540.555);56}5758var dependencies = [59topLevelTypes.topMouseDown,60topLevelTypes.topMouseMove,61topLevelTypes.topMouseUp62];6364if (EventPluginUtils.useTouchEvents) {65dependencies.push(66topLevelTypes.topTouchCancel,67topLevelTypes.topTouchEnd,68topLevelTypes.topTouchStart,69topLevelTypes.topTouchMove70);71}7273var eventTypes = {74touchTap: {75phasedRegistrationNames: {76bubbled: keyOf({onTouchTap: null}),77captured: keyOf({onTouchTapCapture: null})78},79dependencies: dependencies80}81};8283var TapEventPlugin = {8485tapMoveThreshold: tapMoveThreshold,8687eventTypes: eventTypes,8889/**90* @param {string} topLevelType Record from `EventConstants`.91* @param {DOMEventTarget} topLevelTarget The listening component root node.92* @param {string} topLevelTargetID ID of `topLevelTarget`.93* @param {object} nativeEvent Native browser event.94* @return {*} An accumulation of synthetic events.95* @see {EventPluginHub.extractEvents}96*/97extractEvents: function(98topLevelType,99topLevelTarget,100topLevelTargetID,101nativeEvent) {102if (!isStartish(topLevelType) && !isEndish(topLevelType)) {103return null;104}105var event = null;106var distance = getDistance(startCoords, nativeEvent);107if (isEndish(topLevelType) && distance < tapMoveThreshold) {108event = SyntheticUIEvent.getPooled(109eventTypes.touchTap,110topLevelTargetID,111nativeEvent112);113}114if (isStartish(topLevelType)) {115startCoords.x = getAxisCoordOfEvent(Axis.x, nativeEvent);116startCoords.y = getAxisCoordOfEvent(Axis.y, nativeEvent);117} else if (isEndish(topLevelType)) {118startCoords.x = 0;119startCoords.y = 0;120}121EventPropagators.accumulateTwoPhaseDispatches(event);122return event;123}124125};126127module.exports = TapEventPlugin;128129130