react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / react-tools / src / browser / syntheticEvents / SyntheticEvent.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 SyntheticEvent9* @typechecks static-only10*/1112"use strict";1314var PooledClass = require('PooledClass');1516var assign = require('Object.assign');17var emptyFunction = require('emptyFunction');18var getEventTarget = require('getEventTarget');1920/**21* @interface Event22* @see http://www.w3.org/TR/DOM-Level-3-Events/23*/24var EventInterface = {25type: null,26target: getEventTarget,27// currentTarget is set when dispatching; no use in copying it here28currentTarget: emptyFunction.thatReturnsNull,29eventPhase: null,30bubbles: null,31cancelable: null,32timeStamp: function(event) {33return event.timeStamp || Date.now();34},35defaultPrevented: null,36isTrusted: null37};3839/**40* Synthetic events are dispatched by event plugins, typically in response to a41* top-level event delegation handler.42*43* These systems should generally use pooling to reduce the frequency of garbage44* collection. The system should check `isPersistent` to determine whether the45* event should be released into the pool after being dispatched. Users that46* need a persisted event should invoke `persist`.47*48* Synthetic events (and subclasses) implement the DOM Level 3 Events API by49* normalizing browser quirks. Subclasses do not necessarily have to implement a50* DOM interface; custom application-specific events can also subclass this.51*52* @param {object} dispatchConfig Configuration used to dispatch this event.53* @param {string} dispatchMarker Marker identifying the event target.54* @param {object} nativeEvent Native browser event.55*/56function SyntheticEvent(dispatchConfig, dispatchMarker, nativeEvent) {57this.dispatchConfig = dispatchConfig;58this.dispatchMarker = dispatchMarker;59this.nativeEvent = nativeEvent;6061var Interface = this.constructor.Interface;62for (var propName in Interface) {63if (!Interface.hasOwnProperty(propName)) {64continue;65}66var normalize = Interface[propName];67if (normalize) {68this[propName] = normalize(nativeEvent);69} else {70this[propName] = nativeEvent[propName];71}72}7374var defaultPrevented = nativeEvent.defaultPrevented != null ?75nativeEvent.defaultPrevented :76nativeEvent.returnValue === false;77if (defaultPrevented) {78this.isDefaultPrevented = emptyFunction.thatReturnsTrue;79} else {80this.isDefaultPrevented = emptyFunction.thatReturnsFalse;81}82this.isPropagationStopped = emptyFunction.thatReturnsFalse;83}8485assign(SyntheticEvent.prototype, {8687preventDefault: function() {88this.defaultPrevented = true;89var event = this.nativeEvent;90event.preventDefault ? event.preventDefault() : event.returnValue = false;91this.isDefaultPrevented = emptyFunction.thatReturnsTrue;92},9394stopPropagation: function() {95var event = this.nativeEvent;96event.stopPropagation ? event.stopPropagation() : event.cancelBubble = true;97this.isPropagationStopped = emptyFunction.thatReturnsTrue;98},99100/**101* We release all dispatched `SyntheticEvent`s after each event loop, adding102* them back into the pool. This allows a way to hold onto a reference that103* won't be added back into the pool.104*/105persist: function() {106this.isPersistent = emptyFunction.thatReturnsTrue;107},108109/**110* Checks if this event should be released back into the pool.111*112* @return {boolean} True if this should not be released, false otherwise.113*/114isPersistent: emptyFunction.thatReturnsFalse,115116/**117* `PooledClass` looks for `destructor` on each instance it releases.118*/119destructor: function() {120var Interface = this.constructor.Interface;121for (var propName in Interface) {122this[propName] = null;123}124this.dispatchConfig = null;125this.dispatchMarker = null;126this.nativeEvent = null;127}128129});130131SyntheticEvent.Interface = EventInterface;132133/**134* Helper to reduce boilerplate when creating subclasses.135*136* @param {function} Class137* @param {?object} Interface138*/139SyntheticEvent.augmentClass = function(Class, Interface) {140var Super = this;141142var prototype = Object.create(Super.prototype);143assign(prototype, Class.prototype);144Class.prototype = prototype;145Class.prototype.constructor = Class;146147Class.Interface = assign({}, Super.Interface, Interface);148Class.augmentClass = Super.augmentClass;149150PooledClass.addPoolingTo(Class, PooledClass.threeArgumentPooler);151};152153PooledClass.addPoolingTo(SyntheticEvent, PooledClass.threeArgumentPooler);154155module.exports = SyntheticEvent;156157158