react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / react-tools / src / event / EventPluginUtils.js
81152 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 EventPluginUtils9*/1011"use strict";1213var EventConstants = require('EventConstants');1415var invariant = require('invariant');1617/**18* Injected dependencies:19*/2021/**22* - `Mount`: [required] Module that can convert between React dom IDs and23* actual node references.24*/25var injection = {26Mount: null,27injectMount: function(InjectedMount) {28injection.Mount = InjectedMount;29if (__DEV__) {30invariant(31InjectedMount && InjectedMount.getNode,32'EventPluginUtils.injection.injectMount(...): Injected Mount module ' +33'is missing getNode.'34);35}36}37};3839var topLevelTypes = EventConstants.topLevelTypes;4041function isEndish(topLevelType) {42return topLevelType === topLevelTypes.topMouseUp ||43topLevelType === topLevelTypes.topTouchEnd ||44topLevelType === topLevelTypes.topTouchCancel;45}4647function isMoveish(topLevelType) {48return topLevelType === topLevelTypes.topMouseMove ||49topLevelType === topLevelTypes.topTouchMove;50}51function isStartish(topLevelType) {52return topLevelType === topLevelTypes.topMouseDown ||53topLevelType === topLevelTypes.topTouchStart;54}555657var validateEventDispatches;58if (__DEV__) {59validateEventDispatches = function(event) {60var dispatchListeners = event._dispatchListeners;61var dispatchIDs = event._dispatchIDs;6263var listenersIsArr = Array.isArray(dispatchListeners);64var idsIsArr = Array.isArray(dispatchIDs);65var IDsLen = idsIsArr ? dispatchIDs.length : dispatchIDs ? 1 : 0;66var listenersLen = listenersIsArr ?67dispatchListeners.length :68dispatchListeners ? 1 : 0;6970invariant(71idsIsArr === listenersIsArr && IDsLen === listenersLen,72'EventPluginUtils: Invalid `event`.'73);74};75}7677/**78* Invokes `cb(event, listener, id)`. Avoids using call if no scope is79* provided. The `(listener,id)` pair effectively forms the "dispatch" but are80* kept separate to conserve memory.81*/82function forEachEventDispatch(event, cb) {83var dispatchListeners = event._dispatchListeners;84var dispatchIDs = event._dispatchIDs;85if (__DEV__) {86validateEventDispatches(event);87}88if (Array.isArray(dispatchListeners)) {89for (var i = 0; i < dispatchListeners.length; i++) {90if (event.isPropagationStopped()) {91break;92}93// Listeners and IDs are two parallel arrays that are always in sync.94cb(event, dispatchListeners[i], dispatchIDs[i]);95}96} else if (dispatchListeners) {97cb(event, dispatchListeners, dispatchIDs);98}99}100101/**102* Default implementation of PluginModule.executeDispatch().103* @param {SyntheticEvent} SyntheticEvent to handle104* @param {function} Application-level callback105* @param {string} domID DOM id to pass to the callback.106*/107function executeDispatch(event, listener, domID) {108event.currentTarget = injection.Mount.getNode(domID);109var returnValue = listener(event, domID);110event.currentTarget = null;111return returnValue;112}113114/**115* Standard/simple iteration through an event's collected dispatches.116*/117function executeDispatchesInOrder(event, executeDispatch) {118forEachEventDispatch(event, executeDispatch);119event._dispatchListeners = null;120event._dispatchIDs = null;121}122123/**124* Standard/simple iteration through an event's collected dispatches, but stops125* at the first dispatch execution returning true, and returns that id.126*127* @return id of the first dispatch execution who's listener returns true, or128* null if no listener returned true.129*/130function executeDispatchesInOrderStopAtTrueImpl(event) {131var dispatchListeners = event._dispatchListeners;132var dispatchIDs = event._dispatchIDs;133if (__DEV__) {134validateEventDispatches(event);135}136if (Array.isArray(dispatchListeners)) {137for (var i = 0; i < dispatchListeners.length; i++) {138if (event.isPropagationStopped()) {139break;140}141// Listeners and IDs are two parallel arrays that are always in sync.142if (dispatchListeners[i](event, dispatchIDs[i])) {143return dispatchIDs[i];144}145}146} else if (dispatchListeners) {147if (dispatchListeners(event, dispatchIDs)) {148return dispatchIDs;149}150}151return null;152}153154/**155* @see executeDispatchesInOrderStopAtTrueImpl156*/157function executeDispatchesInOrderStopAtTrue(event) {158var ret = executeDispatchesInOrderStopAtTrueImpl(event);159event._dispatchIDs = null;160event._dispatchListeners = null;161return ret;162}163164/**165* Execution of a "direct" dispatch - there must be at most one dispatch166* accumulated on the event or it is considered an error. It doesn't really make167* sense for an event with multiple dispatches (bubbled) to keep track of the168* return values at each dispatch execution, but it does tend to make sense when169* dealing with "direct" dispatches.170*171* @return The return value of executing the single dispatch.172*/173function executeDirectDispatch(event) {174if (__DEV__) {175validateEventDispatches(event);176}177var dispatchListener = event._dispatchListeners;178var dispatchID = event._dispatchIDs;179invariant(180!Array.isArray(dispatchListener),181'executeDirectDispatch(...): Invalid `event`.'182);183var res = dispatchListener ?184dispatchListener(event, dispatchID) :185null;186event._dispatchListeners = null;187event._dispatchIDs = null;188return res;189}190191/**192* @param {SyntheticEvent} event193* @return {bool} True iff number of dispatches accumulated is greater than 0.194*/195function hasDispatches(event) {196return !!event._dispatchListeners;197}198199/**200* General utilities that are useful in creating custom Event Plugins.201*/202var EventPluginUtils = {203isEndish: isEndish,204isMoveish: isMoveish,205isStartish: isStartish,206207executeDirectDispatch: executeDirectDispatch,208executeDispatch: executeDispatch,209executeDispatchesInOrder: executeDispatchesInOrder,210executeDispatchesInOrderStopAtTrue: executeDispatchesInOrderStopAtTrue,211hasDispatches: hasDispatches,212injection: injection,213useTouchEvents: false214};215216module.exports = EventPluginUtils;217218219