react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / react-tools / src / browser / eventPlugins / SimpleEventPlugin.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 SimpleEventPlugin9*/1011"use strict";1213var EventConstants = require('EventConstants');14var EventPluginUtils = require('EventPluginUtils');15var EventPropagators = require('EventPropagators');16var SyntheticClipboardEvent = require('SyntheticClipboardEvent');17var SyntheticEvent = require('SyntheticEvent');18var SyntheticFocusEvent = require('SyntheticFocusEvent');19var SyntheticKeyboardEvent = require('SyntheticKeyboardEvent');20var SyntheticMouseEvent = require('SyntheticMouseEvent');21var SyntheticDragEvent = require('SyntheticDragEvent');22var SyntheticTouchEvent = require('SyntheticTouchEvent');23var SyntheticUIEvent = require('SyntheticUIEvent');24var SyntheticWheelEvent = require('SyntheticWheelEvent');2526var getEventCharCode = require('getEventCharCode');2728var invariant = require('invariant');29var keyOf = require('keyOf');30var warning = require('warning');3132var topLevelTypes = EventConstants.topLevelTypes;3334var eventTypes = {35blur: {36phasedRegistrationNames: {37bubbled: keyOf({onBlur: true}),38captured: keyOf({onBlurCapture: true})39}40},41click: {42phasedRegistrationNames: {43bubbled: keyOf({onClick: true}),44captured: keyOf({onClickCapture: true})45}46},47contextMenu: {48phasedRegistrationNames: {49bubbled: keyOf({onContextMenu: true}),50captured: keyOf({onContextMenuCapture: true})51}52},53copy: {54phasedRegistrationNames: {55bubbled: keyOf({onCopy: true}),56captured: keyOf({onCopyCapture: true})57}58},59cut: {60phasedRegistrationNames: {61bubbled: keyOf({onCut: true}),62captured: keyOf({onCutCapture: true})63}64},65doubleClick: {66phasedRegistrationNames: {67bubbled: keyOf({onDoubleClick: true}),68captured: keyOf({onDoubleClickCapture: true})69}70},71drag: {72phasedRegistrationNames: {73bubbled: keyOf({onDrag: true}),74captured: keyOf({onDragCapture: true})75}76},77dragEnd: {78phasedRegistrationNames: {79bubbled: keyOf({onDragEnd: true}),80captured: keyOf({onDragEndCapture: true})81}82},83dragEnter: {84phasedRegistrationNames: {85bubbled: keyOf({onDragEnter: true}),86captured: keyOf({onDragEnterCapture: true})87}88},89dragExit: {90phasedRegistrationNames: {91bubbled: keyOf({onDragExit: true}),92captured: keyOf({onDragExitCapture: true})93}94},95dragLeave: {96phasedRegistrationNames: {97bubbled: keyOf({onDragLeave: true}),98captured: keyOf({onDragLeaveCapture: true})99}100},101dragOver: {102phasedRegistrationNames: {103bubbled: keyOf({onDragOver: true}),104captured: keyOf({onDragOverCapture: true})105}106},107dragStart: {108phasedRegistrationNames: {109bubbled: keyOf({onDragStart: true}),110captured: keyOf({onDragStartCapture: true})111}112},113drop: {114phasedRegistrationNames: {115bubbled: keyOf({onDrop: true}),116captured: keyOf({onDropCapture: true})117}118},119focus: {120phasedRegistrationNames: {121bubbled: keyOf({onFocus: true}),122captured: keyOf({onFocusCapture: true})123}124},125input: {126phasedRegistrationNames: {127bubbled: keyOf({onInput: true}),128captured: keyOf({onInputCapture: true})129}130},131keyDown: {132phasedRegistrationNames: {133bubbled: keyOf({onKeyDown: true}),134captured: keyOf({onKeyDownCapture: true})135}136},137keyPress: {138phasedRegistrationNames: {139bubbled: keyOf({onKeyPress: true}),140captured: keyOf({onKeyPressCapture: true})141}142},143keyUp: {144phasedRegistrationNames: {145bubbled: keyOf({onKeyUp: true}),146captured: keyOf({onKeyUpCapture: true})147}148},149load: {150phasedRegistrationNames: {151bubbled: keyOf({onLoad: true}),152captured: keyOf({onLoadCapture: true})153}154},155error: {156phasedRegistrationNames: {157bubbled: keyOf({onError: true}),158captured: keyOf({onErrorCapture: true})159}160},161// Note: We do not allow listening to mouseOver events. Instead, use the162// onMouseEnter/onMouseLeave created by `EnterLeaveEventPlugin`.163mouseDown: {164phasedRegistrationNames: {165bubbled: keyOf({onMouseDown: true}),166captured: keyOf({onMouseDownCapture: true})167}168},169mouseMove: {170phasedRegistrationNames: {171bubbled: keyOf({onMouseMove: true}),172captured: keyOf({onMouseMoveCapture: true})173}174},175mouseOut: {176phasedRegistrationNames: {177bubbled: keyOf({onMouseOut: true}),178captured: keyOf({onMouseOutCapture: true})179}180},181mouseOver: {182phasedRegistrationNames: {183bubbled: keyOf({onMouseOver: true}),184captured: keyOf({onMouseOverCapture: true})185}186},187mouseUp: {188phasedRegistrationNames: {189bubbled: keyOf({onMouseUp: true}),190captured: keyOf({onMouseUpCapture: true})191}192},193paste: {194phasedRegistrationNames: {195bubbled: keyOf({onPaste: true}),196captured: keyOf({onPasteCapture: true})197}198},199reset: {200phasedRegistrationNames: {201bubbled: keyOf({onReset: true}),202captured: keyOf({onResetCapture: true})203}204},205scroll: {206phasedRegistrationNames: {207bubbled: keyOf({onScroll: true}),208captured: keyOf({onScrollCapture: true})209}210},211submit: {212phasedRegistrationNames: {213bubbled: keyOf({onSubmit: true}),214captured: keyOf({onSubmitCapture: true})215}216},217touchCancel: {218phasedRegistrationNames: {219bubbled: keyOf({onTouchCancel: true}),220captured: keyOf({onTouchCancelCapture: true})221}222},223touchEnd: {224phasedRegistrationNames: {225bubbled: keyOf({onTouchEnd: true}),226captured: keyOf({onTouchEndCapture: true})227}228},229touchMove: {230phasedRegistrationNames: {231bubbled: keyOf({onTouchMove: true}),232captured: keyOf({onTouchMoveCapture: true})233}234},235touchStart: {236phasedRegistrationNames: {237bubbled: keyOf({onTouchStart: true}),238captured: keyOf({onTouchStartCapture: true})239}240},241wheel: {242phasedRegistrationNames: {243bubbled: keyOf({onWheel: true}),244captured: keyOf({onWheelCapture: true})245}246}247};248249var topLevelEventsToDispatchConfig = {250topBlur: eventTypes.blur,251topClick: eventTypes.click,252topContextMenu: eventTypes.contextMenu,253topCopy: eventTypes.copy,254topCut: eventTypes.cut,255topDoubleClick: eventTypes.doubleClick,256topDrag: eventTypes.drag,257topDragEnd: eventTypes.dragEnd,258topDragEnter: eventTypes.dragEnter,259topDragExit: eventTypes.dragExit,260topDragLeave: eventTypes.dragLeave,261topDragOver: eventTypes.dragOver,262topDragStart: eventTypes.dragStart,263topDrop: eventTypes.drop,264topError: eventTypes.error,265topFocus: eventTypes.focus,266topInput: eventTypes.input,267topKeyDown: eventTypes.keyDown,268topKeyPress: eventTypes.keyPress,269topKeyUp: eventTypes.keyUp,270topLoad: eventTypes.load,271topMouseDown: eventTypes.mouseDown,272topMouseMove: eventTypes.mouseMove,273topMouseOut: eventTypes.mouseOut,274topMouseOver: eventTypes.mouseOver,275topMouseUp: eventTypes.mouseUp,276topPaste: eventTypes.paste,277topReset: eventTypes.reset,278topScroll: eventTypes.scroll,279topSubmit: eventTypes.submit,280topTouchCancel: eventTypes.touchCancel,281topTouchEnd: eventTypes.touchEnd,282topTouchMove: eventTypes.touchMove,283topTouchStart: eventTypes.touchStart,284topWheel: eventTypes.wheel285};286287for (var topLevelType in topLevelEventsToDispatchConfig) {288topLevelEventsToDispatchConfig[topLevelType].dependencies = [topLevelType];289}290291var SimpleEventPlugin = {292293eventTypes: eventTypes,294295/**296* Same as the default implementation, except cancels the event when return297* value is false. This behavior will be disabled in a future release.298*299* @param {object} Event to be dispatched.300* @param {function} Application-level callback.301* @param {string} domID DOM ID to pass to the callback.302*/303executeDispatch: function(event, listener, domID) {304var returnValue = EventPluginUtils.executeDispatch(event, listener, domID);305306warning(307typeof returnValue !== 'boolean',308'Returning `false` from an event handler is deprecated and will be ' +309'ignored in a future release. Instead, manually call ' +310'e.stopPropagation() or e.preventDefault(), as appropriate.'311);312313if (returnValue === false) {314event.stopPropagation();315event.preventDefault();316}317},318319/**320* @param {string} topLevelType Record from `EventConstants`.321* @param {DOMEventTarget} topLevelTarget The listening component root node.322* @param {string} topLevelTargetID ID of `topLevelTarget`.323* @param {object} nativeEvent Native browser event.324* @return {*} An accumulation of synthetic events.325* @see {EventPluginHub.extractEvents}326*/327extractEvents: function(328topLevelType,329topLevelTarget,330topLevelTargetID,331nativeEvent) {332var dispatchConfig = topLevelEventsToDispatchConfig[topLevelType];333if (!dispatchConfig) {334return null;335}336var EventConstructor;337switch (topLevelType) {338case topLevelTypes.topInput:339case topLevelTypes.topLoad:340case topLevelTypes.topError:341case topLevelTypes.topReset:342case topLevelTypes.topSubmit:343// HTML Events344// @see http://www.w3.org/TR/html5/index.html#events-0345EventConstructor = SyntheticEvent;346break;347case topLevelTypes.topKeyPress:348// FireFox creates a keypress event for function keys too. This removes349// the unwanted keypress events. Enter is however both printable and350// non-printable. One would expect Tab to be as well (but it isn't).351if (getEventCharCode(nativeEvent) === 0) {352return null;353}354/* falls through */355case topLevelTypes.topKeyDown:356case topLevelTypes.topKeyUp:357EventConstructor = SyntheticKeyboardEvent;358break;359case topLevelTypes.topBlur:360case topLevelTypes.topFocus:361EventConstructor = SyntheticFocusEvent;362break;363case topLevelTypes.topClick:364// Firefox creates a click event on right mouse clicks. This removes the365// unwanted click events.366if (nativeEvent.button === 2) {367return null;368}369/* falls through */370case topLevelTypes.topContextMenu:371case topLevelTypes.topDoubleClick:372case topLevelTypes.topMouseDown:373case topLevelTypes.topMouseMove:374case topLevelTypes.topMouseOut:375case topLevelTypes.topMouseOver:376case topLevelTypes.topMouseUp:377EventConstructor = SyntheticMouseEvent;378break;379case topLevelTypes.topDrag:380case topLevelTypes.topDragEnd:381case topLevelTypes.topDragEnter:382case topLevelTypes.topDragExit:383case topLevelTypes.topDragLeave:384case topLevelTypes.topDragOver:385case topLevelTypes.topDragStart:386case topLevelTypes.topDrop:387EventConstructor = SyntheticDragEvent;388break;389case topLevelTypes.topTouchCancel:390case topLevelTypes.topTouchEnd:391case topLevelTypes.topTouchMove:392case topLevelTypes.topTouchStart:393EventConstructor = SyntheticTouchEvent;394break;395case topLevelTypes.topScroll:396EventConstructor = SyntheticUIEvent;397break;398case topLevelTypes.topWheel:399EventConstructor = SyntheticWheelEvent;400break;401case topLevelTypes.topCopy:402case topLevelTypes.topCut:403case topLevelTypes.topPaste:404EventConstructor = SyntheticClipboardEvent;405break;406}407invariant(408EventConstructor,409'SimpleEventPlugin: Unhandled event type, `%s`.',410topLevelType411);412var event = EventConstructor.getPooled(413dispatchConfig,414topLevelTargetID,415nativeEvent416);417EventPropagators.accumulateTwoPhaseDispatches(event);418return event;419}420421};422423module.exports = SimpleEventPlugin;424425426