react 15.0.0-rc.1 → 15.0.2-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (90) hide show
  1. package/dist/react-with-addons.js +1446 -1195
  2. package/dist/react-with-addons.min.js +6 -6
  3. package/dist/react.js +1330 -1109
  4. package/dist/react.min.js +6 -6
  5. package/lib/CSSProperty.js +6 -0
  6. package/lib/CSSPropertyOperations.js +31 -15
  7. package/lib/CallbackQueue.js +3 -2
  8. package/lib/DOMChildrenOperations.js +0 -3
  9. package/lib/DOMLazyTree.js +11 -2
  10. package/lib/DOMPropertyOperations.js +0 -46
  11. package/lib/EventPluginUtils.js +2 -2
  12. package/lib/FallbackCompositionState.js +3 -2
  13. package/lib/HTMLDOMPropertyConfig.js +111 -111
  14. package/lib/IOSDefaultEventPluginOrder.js +16 -0
  15. package/lib/IOSNativeBridgeEventPlugin.js +57 -0
  16. package/lib/NativeMethodsMixin.js +165 -0
  17. package/lib/OrderedMap.js +3 -2
  18. package/lib/PanResponder.js +362 -0
  19. package/lib/React.js +68 -9
  20. package/lib/ReactBrowserEventEmitter.js +3 -2
  21. package/lib/ReactCSSTransitionGroup.js +3 -3
  22. package/lib/ReactClass.js +7 -6
  23. package/lib/ReactComponent.js +2 -2
  24. package/lib/ReactCompositeComponent.js +5 -4
  25. package/lib/ReactDOMComponent.js +14 -15
  26. package/lib/ReactDOMDebugTool.js +0 -8
  27. package/lib/ReactDOMEmptyComponent.js +3 -3
  28. package/lib/ReactDOMInput.js +5 -4
  29. package/lib/ReactDOMOption.js +13 -2
  30. package/lib/ReactDOMSelect.js +3 -2
  31. package/lib/ReactDOMTextComponent.js +3 -2
  32. package/lib/ReactDOMTextarea.js +3 -2
  33. package/lib/ReactDebugInstanceMap.js +102 -0
  34. package/lib/ReactDefaultBatchingStrategy.js +3 -2
  35. package/lib/ReactDefaultPerf.js +46 -7
  36. package/lib/ReactDefaultPerfAnalysis.js +7 -7
  37. package/lib/ReactElement.js +5 -4
  38. package/lib/ReactEventListener.js +3 -2
  39. package/lib/ReactMarkupChecksum.js +7 -2
  40. package/lib/ReactMount.js +1 -0
  41. package/lib/ReactNative.js +71 -0
  42. package/lib/ReactNativeAttributePayload.js +397 -0
  43. package/lib/ReactNativeBaseComponent.js +196 -0
  44. package/lib/ReactNativeComponent.js +3 -2
  45. package/lib/ReactNativeComponentEnvironment.js +38 -0
  46. package/lib/ReactNativeComponentTree.js +66 -0
  47. package/lib/ReactNativeContainerInfo.js +21 -0
  48. package/lib/ReactNativeDOMIDOperations.js +83 -0
  49. package/lib/ReactNativeDefaultInjection.js +99 -0
  50. package/lib/ReactNativeEventEmitter.js +180 -0
  51. package/lib/ReactNativeGlobalInteractionHandler.js +33 -0
  52. package/lib/ReactNativeGlobalResponderHandler.js +25 -0
  53. package/lib/ReactNativeMount.js +190 -0
  54. package/lib/ReactNativePropRegistry.js +52 -0
  55. package/lib/ReactNativeReconcileTransaction.js +100 -0
  56. package/lib/ReactNativeTagHandles.js +54 -0
  57. package/lib/ReactNativeTextComponent.js +70 -0
  58. package/lib/ReactNativeTreeTraversal.js +127 -0
  59. package/lib/ReactOwner.js +4 -3
  60. package/lib/ReactPropTransferer.js +4 -3
  61. package/lib/ReactReconcileTransaction.js +3 -3
  62. package/lib/ReactServerRenderingTransaction.js +3 -3
  63. package/lib/ReactSimpleEmptyComponent.js +3 -3
  64. package/lib/ReactTestUtils.js +14 -4
  65. package/lib/ReactTransitionGroup.js +3 -2
  66. package/lib/ReactUMDEntry.js +26 -0
  67. package/lib/ReactUpdateQueue.js +20 -3
  68. package/lib/ReactUpdates.js +3 -2
  69. package/lib/ReactVersion.js +1 -1
  70. package/lib/ReactWithAddons.js +0 -7
  71. package/lib/ReactWithAddonsUMDEntry.js +26 -0
  72. package/lib/ResponderSyntheticEvent.js +1 -1
  73. package/lib/SVGDOMPropertyConfig.js +266 -52
  74. package/lib/SyntheticEvent.js +5 -4
  75. package/lib/TouchHistoryMath.js +99 -0
  76. package/lib/createReactNativeComponentClass.js +42 -0
  77. package/lib/deprecated.js +3 -2
  78. package/lib/findNodeHandle.js +89 -0
  79. package/lib/instantiateReactComponent.js +3 -2
  80. package/lib/reactComponentExpect.js +3 -2
  81. package/lib/setInnerHTML.js +1 -0
  82. package/lib/update.js +4 -3
  83. package/lib/validateDOMNesting.js +3 -2
  84. package/package.json +5 -4
  85. package/addons.js +0 -15
  86. package/lib/Object.assign.js +0 -47
  87. package/lib/React.native.js +0 -5
  88. package/lib/ReactDOM.native.js +0 -12
  89. package/lib/ReactDOMSVGDeprecatedAttributeDevtool.js +0 -61
  90. package/lib/ReactIsomorphic.js +0 -74
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Copyright (c) 2015-present, Facebook, Inc.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree. An additional grant
7
+ * of patent rights can be found in the PATENTS file in the same directory.
8
+ *
9
+ * @providesModule IOSDefaultEventPluginOrder
10
+ *
11
+ */
12
+ 'use strict';
13
+
14
+ var IOSDefaultEventPluginOrder = ['ResponderEventPlugin', 'IOSNativeBridgeEventPlugin'];
15
+
16
+ module.exports = IOSDefaultEventPluginOrder;
@@ -0,0 +1,57 @@
1
+ /**
2
+ * Copyright (c) 2015-present, Facebook, Inc.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree. An additional grant
7
+ * of patent rights can be found in the PATENTS file in the same directory.
8
+ *
9
+ * @providesModule IOSNativeBridgeEventPlugin
10
+ *
11
+ */
12
+ 'use strict';
13
+
14
+ var EventPropagators = require('./EventPropagators');
15
+ var SyntheticEvent = require('./SyntheticEvent');
16
+ var UIManager = require('UIManager');
17
+
18
+ var merge = require('merge');
19
+ var warning = require('fbjs/lib/warning');
20
+
21
+ var customBubblingEventTypes = UIManager.customBubblingEventTypes;
22
+ var customDirectEventTypes = UIManager.customDirectEventTypes;
23
+
24
+ var allTypesByEventName = {};
25
+
26
+ for (var bubblingTypeName in customBubblingEventTypes) {
27
+ allTypesByEventName[bubblingTypeName] = customBubblingEventTypes[bubblingTypeName];
28
+ }
29
+
30
+ for (var directTypeName in customDirectEventTypes) {
31
+ process.env.NODE_ENV !== 'production' ? warning(!customBubblingEventTypes[directTypeName], 'Event cannot be both direct and bubbling: %s', directTypeName) : void 0;
32
+ allTypesByEventName[directTypeName] = customDirectEventTypes[directTypeName];
33
+ }
34
+
35
+ var IOSNativeBridgeEventPlugin = {
36
+
37
+ eventTypes: merge(customBubblingEventTypes, customDirectEventTypes),
38
+
39
+ /**
40
+ * @see {EventPluginHub.extractEvents}
41
+ */
42
+ extractEvents: function (topLevelType, targetInst, nativeEvent, nativeEventTarget) {
43
+ var bubbleDispatchConfig = customBubblingEventTypes[topLevelType];
44
+ var directDispatchConfig = customDirectEventTypes[topLevelType];
45
+ var event = SyntheticEvent.getPooled(bubbleDispatchConfig || directDispatchConfig, targetInst, nativeEvent, nativeEventTarget);
46
+ if (bubbleDispatchConfig) {
47
+ EventPropagators.accumulateTwoPhaseDispatches(event);
48
+ } else if (directDispatchConfig) {
49
+ EventPropagators.accumulateDirectDispatches(event);
50
+ } else {
51
+ return null;
52
+ }
53
+ return event;
54
+ }
55
+ };
56
+
57
+ module.exports = IOSNativeBridgeEventPlugin;
@@ -0,0 +1,165 @@
1
+ /**
2
+ * Copyright (c) 2015-present, Facebook, Inc.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree. An additional grant
7
+ * of patent rights can be found in the PATENTS file in the same directory.
8
+ *
9
+ * @providesModule NativeMethodsMixin
10
+ *
11
+ */
12
+ 'use strict';
13
+
14
+ var ReactNativeAttributePayload = require('./ReactNativeAttributePayload');
15
+ var TextInputState = require('TextInputState');
16
+ var UIManager = require('UIManager');
17
+
18
+ var findNodeHandle = require('./findNodeHandle');
19
+ var invariant = require('fbjs/lib/invariant');
20
+
21
+ function warnForStyleProps(props, validAttributes) {
22
+ for (var key in validAttributes.style) {
23
+ if (!(validAttributes[key] || props[key] === undefined)) {
24
+ console.error('You are setting the style `{ ' + key + ': ... }` as a prop. You ' + 'should nest it in a style object. ' + 'E.g. `{ style: { ' + key + ': ... } }`');
25
+ }
26
+ }
27
+ }
28
+
29
+ /**
30
+ * `NativeMethodsMixin` provides methods to access the underlying native
31
+ * component directly. This can be useful in cases when you want to focus
32
+ * a view or measure its on-screen dimensions, for example.
33
+ *
34
+ * The methods described here are available on most of the default components
35
+ * provided by React Native. Note, however, that they are *not* available on
36
+ * composite components that aren't directly backed by a native view. This will
37
+ * generally include most components that you define in your own app. For more
38
+ * information, see [Direct
39
+ * Manipulation](docs/direct-manipulation.html).
40
+ */
41
+ var NativeMethodsMixin = {
42
+ /**
43
+ * Determines the location on screen, width, and height of the given view and
44
+ * returns the values via an async callback. If successful, the callback will
45
+ * be called with the following arguments:
46
+ *
47
+ * - x
48
+ * - y
49
+ * - width
50
+ * - height
51
+ * - pageX
52
+ * - pageY
53
+ *
54
+ * Note that these measurements are not available until after the rendering
55
+ * has been completed in native. If you need the measurements as soon as
56
+ * possible, consider using the [`onLayout`
57
+ * prop](docs/view.html#onlayout) instead.
58
+ */
59
+ measure: function (callback) {
60
+ UIManager.measure(findNodeHandle(this), mountSafeCallback(this, callback));
61
+ },
62
+
63
+ /**
64
+ * Determines the location of the given view in the window and returns the
65
+ * values via an async callback. If the React root view is embedded in
66
+ * another native view, this will give you the absolute coordinates. If
67
+ * successful, the callback will be called with the following
68
+ * arguments:
69
+ *
70
+ * - x
71
+ * - y
72
+ * - width
73
+ * - height
74
+ *
75
+ * Note that these measurements are not available until after the rendering
76
+ * has been completed in native.
77
+ */
78
+ measureInWindow: function (callback) {
79
+ UIManager.measureInWindow(findNodeHandle(this), mountSafeCallback(this, callback));
80
+ },
81
+
82
+ /**
83
+ * Like [`measure()`](#measure), but measures the view relative an ancestor,
84
+ * specified as `relativeToNativeNode`. This means that the returned x, y
85
+ * are relative to the origin x, y of the ancestor view.
86
+ *
87
+ * As always, to obtain a native node handle for a component, you can use
88
+ * `React.findNodeHandle(component)`.
89
+ */
90
+ measureLayout: function (relativeToNativeNode, onSuccess, onFail /* currently unused */
91
+ ) {
92
+ UIManager.measureLayout(findNodeHandle(this), relativeToNativeNode, mountSafeCallback(this, onFail), mountSafeCallback(this, onSuccess));
93
+ },
94
+
95
+ /**
96
+ * This function sends props straight to native. They will not participate in
97
+ * future diff process - this means that if you do not include them in the
98
+ * next render, they will remain active (see [Direct
99
+ * Manipulation](docs/direct-manipulation.html)).
100
+ */
101
+ setNativeProps: function (nativeProps) {
102
+ if (process.env.NODE_ENV !== 'production') {
103
+ warnForStyleProps(nativeProps, this.viewConfig.validAttributes);
104
+ }
105
+
106
+ var updatePayload = ReactNativeAttributePayload.create(nativeProps, this.viewConfig.validAttributes);
107
+
108
+ UIManager.updateView(findNodeHandle(this), this.viewConfig.uiViewClassName, updatePayload);
109
+ },
110
+
111
+ /**
112
+ * Requests focus for the given input or view. The exact behavior triggered
113
+ * will depend on the platform and type of view.
114
+ */
115
+ focus: function () {
116
+ TextInputState.focusTextInput(findNodeHandle(this));
117
+ },
118
+
119
+ /**
120
+ * Removes focus from an input or view. This is the opposite of `focus()`.
121
+ */
122
+ blur: function () {
123
+ TextInputState.blurTextInput(findNodeHandle(this));
124
+ }
125
+ };
126
+
127
+ function throwOnStylesProp(component, props) {
128
+ if (props.styles !== undefined) {
129
+ var owner = component._owner || null;
130
+ var name = component.constructor.displayName;
131
+ var msg = '`styles` is not a supported property of `' + name + '`, did ' + 'you mean `style` (singular)?';
132
+ if (owner && owner.constructor && owner.constructor.displayName) {
133
+ msg += '\n\nCheck the `' + owner.constructor.displayName + '` parent ' + ' component.';
134
+ }
135
+ throw new Error(msg);
136
+ }
137
+ }
138
+ if (process.env.NODE_ENV !== 'production') {
139
+ // hide this from Flow since we can't define these properties outside of
140
+ // __DEV__ without actually implementing them (setting them to undefined
141
+ // isn't allowed by ReactClass)
142
+ var NativeMethodsMixin_DEV = NativeMethodsMixin;
143
+ !(!NativeMethodsMixin_DEV.componentWillMount && !NativeMethodsMixin_DEV.componentWillReceiveProps) ? process.env.NODE_ENV !== 'production' ? invariant(false, 'Do not override existing functions.') : invariant(false) : void 0;
144
+ NativeMethodsMixin_DEV.componentWillMount = function () {
145
+ throwOnStylesProp(this, this.props);
146
+ };
147
+ NativeMethodsMixin_DEV.componentWillReceiveProps = function (newProps) {
148
+ throwOnStylesProp(this, newProps);
149
+ };
150
+ }
151
+
152
+ /**
153
+ * In the future, we should cleanup callbacks by cancelling them instead of
154
+ * using this.
155
+ */
156
+ var mountSafeCallback = function (context, callback) {
157
+ return function () {
158
+ if (!callback || context.isMounted && !context.isMounted()) {
159
+ return undefined;
160
+ }
161
+ return callback.apply(context, arguments);
162
+ };
163
+ };
164
+
165
+ module.exports = NativeMethodsMixin;
package/lib/OrderedMap.js CHANGED
@@ -11,7 +11,8 @@
11
11
 
12
12
  'use strict';
13
13
 
14
- var assign = require('./Object.assign');
14
+ var _assign = require('object-assign');
15
+
15
16
  var invariant = require('fbjs/lib/invariant');
16
17
 
17
18
  var PREFIX = 'key:';
@@ -435,7 +436,7 @@ var OrderedMapMethods = {
435
436
  }
436
437
  };
437
438
 
438
- assign(OrderedMapImpl.prototype, OrderedMapMethods);
439
+ _assign(OrderedMapImpl.prototype, OrderedMapMethods);
439
440
 
440
441
  var OrderedMap = {
441
442
  from: function (orderedMap) {
@@ -0,0 +1,362 @@
1
+ /**
2
+ * @providesModule PanResponder
3
+ */
4
+
5
+ 'use strict';
6
+
7
+ var TouchHistoryMath = require('./TouchHistoryMath');
8
+
9
+ var currentCentroidXOfTouchesChangedAfter = TouchHistoryMath.currentCentroidXOfTouchesChangedAfter;
10
+ var currentCentroidYOfTouchesChangedAfter = TouchHistoryMath.currentCentroidYOfTouchesChangedAfter;
11
+ var previousCentroidXOfTouchesChangedAfter = TouchHistoryMath.previousCentroidXOfTouchesChangedAfter;
12
+ var previousCentroidYOfTouchesChangedAfter = TouchHistoryMath.previousCentroidYOfTouchesChangedAfter;
13
+ var currentCentroidX = TouchHistoryMath.currentCentroidX;
14
+ var currentCentroidY = TouchHistoryMath.currentCentroidY;
15
+
16
+ /**
17
+ * `PanResponder` reconciles several touches into a single gesture. It makes
18
+ * single-touch gestures resilient to extra touches, and can be used to
19
+ * recognize simple multi-touch gestures.
20
+ *
21
+ * It provides a predictable wrapper of the responder handlers provided by the
22
+ * [gesture responder system](docs/gesture-responder-system.html).
23
+ * For each handler, it provides a new `gestureState` object alongside the
24
+ * native event object:
25
+ *
26
+ * ```
27
+ * onPanResponderMove: (event, gestureState) => {}
28
+ * ```
29
+ *
30
+ * A native event is a synthetic touch event with the following form:
31
+ *
32
+ * - `nativeEvent`
33
+ * + `changedTouches` - Array of all touch events that have changed since the last event
34
+ * + `identifier` - The ID of the touch
35
+ * + `locationX` - The X position of the touch, relative to the element
36
+ * + `locationY` - The Y position of the touch, relative to the element
37
+ * + `pageX` - The X position of the touch, relative to the root element
38
+ * + `pageY` - The Y position of the touch, relative to the root element
39
+ * + `target` - The node id of the element receiving the touch event
40
+ * + `timestamp` - A time identifier for the touch, useful for velocity calculation
41
+ * + `touches` - Array of all current touches on the screen
42
+ *
43
+ * A `gestureState` object has the following:
44
+ *
45
+ * - `stateID` - ID of the gestureState- persisted as long as there at least
46
+ * one touch on screen
47
+ * - `moveX` - the latest screen coordinates of the recently-moved touch
48
+ * - `moveY` - the latest screen coordinates of the recently-moved touch
49
+ * - `x0` - the screen coordinates of the responder grant
50
+ * - `y0` - the screen coordinates of the responder grant
51
+ * - `dx` - accumulated distance of the gesture since the touch started
52
+ * - `dy` - accumulated distance of the gesture since the touch started
53
+ * - `vx` - current velocity of the gesture
54
+ * - `vy` - current velocity of the gesture
55
+ * - `numberActiveTouches` - Number of touches currently on screen
56
+ *
57
+ * ### Basic Usage
58
+ *
59
+ * ```
60
+ * componentWillMount: function() {
61
+ * this._panResponder = PanResponder.create({
62
+ * // Ask to be the responder:
63
+ * onStartShouldSetPanResponder: (evt, gestureState) => true,
64
+ * onStartShouldSetPanResponderCapture: (evt, gestureState) => true,
65
+ * onMoveShouldSetPanResponder: (evt, gestureState) => true,
66
+ * onMoveShouldSetPanResponderCapture: (evt, gestureState) => true,
67
+ *
68
+ * onPanResponderGrant: (evt, gestureState) => {
69
+ * // The guesture has started. Show visual feedback so the user knows
70
+ * // what is happening!
71
+ *
72
+ * // gestureState.{x,y}0 will be set to zero now
73
+ * },
74
+ * onPanResponderMove: (evt, gestureState) => {
75
+ * // The most recent move distance is gestureState.move{X,Y}
76
+ *
77
+ * // The accumulated gesture distance since becoming responder is
78
+ * // gestureState.d{x,y}
79
+ * },
80
+ * onPanResponderTerminationRequest: (evt, gestureState) => true,
81
+ * onPanResponderRelease: (evt, gestureState) => {
82
+ * // The user has released all touches while this view is the
83
+ * // responder. This typically means a gesture has succeeded
84
+ * },
85
+ * onPanResponderTerminate: (evt, gestureState) => {
86
+ * // Another component has become the responder, so this gesture
87
+ * // should be cancelled
88
+ * },
89
+ * onShouldBlockNativeResponder: (evt, gestureState) => {
90
+ * // Returns whether this component should block native components from becoming the JS
91
+ * // responder. Returns true by default. Is currently only supported on android.
92
+ * return true;
93
+ * },
94
+ * });
95
+ * },
96
+ *
97
+ * render: function() {
98
+ * return (
99
+ * <View {...this._panResponder.panHandlers} />
100
+ * );
101
+ * },
102
+ *
103
+ * ```
104
+ *
105
+ * ### Working Example
106
+ *
107
+ * To see it in action, try the
108
+ * [PanResponder example in UIExplorer](https://github.com/facebook/react-native/blob/master/Examples/UIExplorer/PanResponderExample.js)
109
+ */
110
+
111
+ var PanResponder = {
112
+
113
+ /**
114
+ *
115
+ * A graphical explanation of the touch data flow:
116
+ *
117
+ * +----------------------------+ +--------------------------------+
118
+ * | ResponderTouchHistoryStore | |TouchHistoryMath |
119
+ * +----------------------------+ +----------+---------------------+
120
+ * |Global store of touchHistory| |Allocation-less math util |
121
+ * |including activeness, start | |on touch history (centroids |
122
+ * |position, prev/cur position.| |and multitouch movement etc) |
123
+ * | | | |
124
+ * +----^-----------------------+ +----^---------------------------+
125
+ * | |
126
+ * | (records relevant history |
127
+ * | of touches relevant for |
128
+ * | implementing higher level |
129
+ * | gestures) |
130
+ * | |
131
+ * +----+-----------------------+ +----|---------------------------+
132
+ * | ResponderEventPlugin | | | Your App/Component |
133
+ * +----------------------------+ +----|---------------------------+
134
+ * |Negotiates which view gets | Low level | | High level |
135
+ * |onResponderMove events. | events w/ | +-+-------+ events w/ |
136
+ * |Also records history into | touchHistory| | Pan | multitouch + |
137
+ * |ResponderTouchHistoryStore. +---------------->Responder+-----> accumulative|
138
+ * +----------------------------+ attached to | | | distance and |
139
+ * each event | +---------+ velocity. |
140
+ * | |
141
+ * | |
142
+ * +--------------------------------+
143
+ *
144
+ *
145
+ *
146
+ * Gesture that calculates cumulative movement over time in a way that just
147
+ * "does the right thing" for multiple touches. The "right thing" is very
148
+ * nuanced. When moving two touches in opposite directions, the cumulative
149
+ * distance is zero in each dimension. When two touches move in parallel five
150
+ * pixels in the same direction, the cumulative distance is five, not ten. If
151
+ * two touches start, one moves five in a direction, then stops and the other
152
+ * touch moves fives in the same direction, the cumulative distance is ten.
153
+ *
154
+ * This logic requires a kind of processing of time "clusters" of touch events
155
+ * so that two touch moves that essentially occur in parallel but move every
156
+ * other frame respectively, are considered part of the same movement.
157
+ *
158
+ * Explanation of some of the non-obvious fields:
159
+ *
160
+ * - moveX/moveY: If no move event has been observed, then `(moveX, moveY)` is
161
+ * invalid. If a move event has been observed, `(moveX, moveY)` is the
162
+ * centroid of the most recently moved "cluster" of active touches.
163
+ * (Currently all move have the same timeStamp, but later we should add some
164
+ * threshold for what is considered to be "moving"). If a palm is
165
+ * accidentally counted as a touch, but a finger is moving greatly, the palm
166
+ * will move slightly, but we only want to count the single moving touch.
167
+ * - x0/y0: Centroid location (non-cumulative) at the time of becoming
168
+ * responder.
169
+ * - dx/dy: Cumulative touch distance - not the same thing as sum of each touch
170
+ * distance. Accounts for touch moves that are clustered together in time,
171
+ * moving the same direction. Only valid when currently responder (otherwise,
172
+ * it only represents the drag distance below the threshold).
173
+ * - vx/vy: Velocity.
174
+ */
175
+
176
+ _initializeGestureState: function (gestureState) {
177
+ gestureState.moveX = 0;
178
+ gestureState.moveY = 0;
179
+ gestureState.x0 = 0;
180
+ gestureState.y0 = 0;
181
+ gestureState.dx = 0;
182
+ gestureState.dy = 0;
183
+ gestureState.vx = 0;
184
+ gestureState.vy = 0;
185
+ gestureState.numberActiveTouches = 0;
186
+ // All `gestureState` accounts for timeStamps up until:
187
+ gestureState._accountsForMovesUpTo = 0;
188
+ },
189
+
190
+ /**
191
+ * This is nuanced and is necessary. It is incorrect to continuously take all
192
+ * active *and* recently moved touches, find the centroid, and track how that
193
+ * result changes over time. Instead, we must take all recently moved
194
+ * touches, and calculate how the centroid has changed just for those
195
+ * recently moved touches, and append that change to an accumulator. This is
196
+ * to (at least) handle the case where the user is moving three fingers, and
197
+ * then one of the fingers stops but the other two continue.
198
+ *
199
+ * This is very different than taking all of the recently moved touches and
200
+ * storing their centroid as `dx/dy`. For correctness, we must *accumulate
201
+ * changes* in the centroid of recently moved touches.
202
+ *
203
+ * There is also some nuance with how we handle multiple moved touches in a
204
+ * single event. With the way `ReactNativeEventEmitter` dispatches touches as
205
+ * individual events, multiple touches generate two 'move' events, each of
206
+ * them triggering `onResponderMove`. But with the way `PanResponder` works,
207
+ * all of the gesture inference is performed on the first dispatch, since it
208
+ * looks at all of the touches (even the ones for which there hasn't been a
209
+ * native dispatch yet). Therefore, `PanResponder` does not call
210
+ * `onResponderMove` passed the first dispatch. This diverges from the
211
+ * typical responder callback pattern (without using `PanResponder`), but
212
+ * avoids more dispatches than necessary.
213
+ */
214
+ _updateGestureStateOnMove: function (gestureState, touchHistory) {
215
+ gestureState.numberActiveTouches = touchHistory.numberActiveTouches;
216
+ gestureState.moveX = currentCentroidXOfTouchesChangedAfter(touchHistory, gestureState._accountsForMovesUpTo);
217
+ gestureState.moveY = currentCentroidYOfTouchesChangedAfter(touchHistory, gestureState._accountsForMovesUpTo);
218
+ var movedAfter = gestureState._accountsForMovesUpTo;
219
+ var prevX = previousCentroidXOfTouchesChangedAfter(touchHistory, movedAfter);
220
+ var x = currentCentroidXOfTouchesChangedAfter(touchHistory, movedAfter);
221
+ var prevY = previousCentroidYOfTouchesChangedAfter(touchHistory, movedAfter);
222
+ var y = currentCentroidYOfTouchesChangedAfter(touchHistory, movedAfter);
223
+ var nextDX = gestureState.dx + (x - prevX);
224
+ var nextDY = gestureState.dy + (y - prevY);
225
+
226
+ // TODO: This must be filtered intelligently.
227
+ var dt = touchHistory.mostRecentTimeStamp - gestureState._accountsForMovesUpTo;
228
+ gestureState.vx = (nextDX - gestureState.dx) / dt;
229
+ gestureState.vy = (nextDY - gestureState.dy) / dt;
230
+
231
+ gestureState.dx = nextDX;
232
+ gestureState.dy = nextDY;
233
+ gestureState._accountsForMovesUpTo = touchHistory.mostRecentTimeStamp;
234
+ },
235
+
236
+ /**
237
+ * @param {object} config Enhanced versions of all of the responder callbacks
238
+ * that provide not only the typical `ResponderSyntheticEvent`, but also the
239
+ * `PanResponder` gesture state. Simply replace the word `Responder` with
240
+ * `PanResponder` in each of the typical `onResponder*` callbacks. For
241
+ * example, the `config` object would look like:
242
+ *
243
+ * - `onMoveShouldSetPanResponder: (e, gestureState) => {...}`
244
+ * - `onMoveShouldSetPanResponderCapture: (e, gestureState) => {...}`
245
+ * - `onStartShouldSetPanResponder: (e, gestureState) => {...}`
246
+ * - `onStartShouldSetPanResponderCapture: (e, gestureState) => {...}`
247
+ * - `onPanResponderReject: (e, gestureState) => {...}`
248
+ * - `onPanResponderGrant: (e, gestureState) => {...}`
249
+ * - `onPanResponderStart: (e, gestureState) => {...}`
250
+ * - `onPanResponderEnd: (e, gestureState) => {...}`
251
+ * - `onPanResponderRelease: (e, gestureState) => {...}`
252
+ * - `onPanResponderMove: (e, gestureState) => {...}`
253
+ * - `onPanResponderTerminate: (e, gestureState) => {...}`
254
+ * - `onPanResponderTerminationRequest: (e, gestureState) => {...}`
255
+ * - `onShouldBlockNativeResponder: (e, gestureState) => {...}`
256
+ *
257
+ * In general, for events that have capture equivalents, we update the
258
+ * gestureState once in the capture phase and can use it in the bubble phase
259
+ * as well.
260
+ *
261
+ * Be careful with onStartShould* callbacks. They only reflect updated
262
+ * `gestureState` for start/end events that bubble/capture to the Node.
263
+ * Once the node is the responder, you can rely on every start/end event
264
+ * being processed by the gesture and `gestureState` being updated
265
+ * accordingly. (numberActiveTouches) may not be totally accurate unless you
266
+ * are the responder.
267
+ */
268
+ create: function (config) {
269
+ var gestureState = {
270
+ // Useful for debugging
271
+ stateID: Math.random()
272
+ };
273
+ PanResponder._initializeGestureState(gestureState);
274
+ var panHandlers = {
275
+ onStartShouldSetResponder: function (e) {
276
+ return config.onStartShouldSetPanResponder === undefined ? false : config.onStartShouldSetPanResponder(e, gestureState);
277
+ },
278
+ onMoveShouldSetResponder: function (e) {
279
+ return config.onMoveShouldSetPanResponder === undefined ? false : config.onMoveShouldSetPanResponder(e, gestureState);
280
+ },
281
+ onStartShouldSetResponderCapture: function (e) {
282
+ // TODO: Actually, we should reinitialize the state any time
283
+ // touches.length increases from 0 active to > 0 active.
284
+ if (e.nativeEvent.touches.length === 1) {
285
+ PanResponder._initializeGestureState(gestureState);
286
+ }
287
+ gestureState.numberActiveTouches = e.touchHistory.numberActiveTouches;
288
+ return config.onStartShouldSetPanResponderCapture !== undefined ? config.onStartShouldSetPanResponderCapture(e, gestureState) : false;
289
+ },
290
+
291
+ onMoveShouldSetResponderCapture: function (e) {
292
+ var touchHistory = e.touchHistory;
293
+ // Responder system incorrectly dispatches should* to current responder
294
+ // Filter out any touch moves past the first one - we would have
295
+ // already processed multi-touch geometry during the first event.
296
+ if (gestureState._accountsForMovesUpTo === touchHistory.mostRecentTimeStamp) {
297
+ return false;
298
+ }
299
+ PanResponder._updateGestureStateOnMove(gestureState, touchHistory);
300
+ return config.onMoveShouldSetPanResponderCapture ? config.onMoveShouldSetPanResponderCapture(e, gestureState) : false;
301
+ },
302
+
303
+ onResponderGrant: function (e) {
304
+ gestureState.x0 = currentCentroidX(e.touchHistory);
305
+ gestureState.y0 = currentCentroidY(e.touchHistory);
306
+ gestureState.dx = 0;
307
+ gestureState.dy = 0;
308
+ if (config.onPanResponderGrant) config.onPanResponderGrant(e, gestureState);
309
+ // TODO: t7467124 investigate if this can be removed
310
+ return config.onShouldBlockNativeResponder === undefined ? true : config.onShouldBlockNativeResponder();
311
+ },
312
+
313
+ onResponderReject: function (e) {
314
+ if (config.onPanResponderReject) config.onPanResponderReject(e, gestureState);
315
+ },
316
+
317
+ onResponderRelease: function (e) {
318
+ if (config.onPanResponderRelease) config.onPanResponderRelease(e, gestureState);
319
+ PanResponder._initializeGestureState(gestureState);
320
+ },
321
+
322
+ onResponderStart: function (e) {
323
+ var touchHistory = e.touchHistory;
324
+ gestureState.numberActiveTouches = touchHistory.numberActiveTouches;
325
+ if (config.onPanResponderStart) config.onPanResponderStart(e, gestureState);
326
+ },
327
+
328
+ onResponderMove: function (e) {
329
+ var touchHistory = e.touchHistory;
330
+ // Guard against the dispatch of two touch moves when there are two
331
+ // simultaneously changed touches.
332
+ if (gestureState._accountsForMovesUpTo === touchHistory.mostRecentTimeStamp) {
333
+ return;
334
+ }
335
+ // Filter out any touch moves past the first one - we would have
336
+ // already processed multi-touch geometry during the first event.
337
+ PanResponder._updateGestureStateOnMove(gestureState, touchHistory);
338
+ if (config.onPanResponderMove) config.onPanResponderMove(e, gestureState);
339
+ },
340
+
341
+ onResponderEnd: function (e) {
342
+ var touchHistory = e.touchHistory;
343
+ gestureState.numberActiveTouches = touchHistory.numberActiveTouches;
344
+ if (config.onPanResponderEnd) config.onPanResponderEnd(e, gestureState);
345
+ },
346
+
347
+ onResponderTerminate: function (e) {
348
+ if (config.onPanResponderTerminate) {
349
+ config.onPanResponderTerminate(e, gestureState);
350
+ }
351
+ PanResponder._initializeGestureState(gestureState);
352
+ },
353
+
354
+ onResponderTerminationRequest: function (e) {
355
+ return config.onPanResponderTerminationRequest === undefined ? true : config.onPanResponderTerminationRequest(e, gestureState);
356
+ }
357
+ };
358
+ return { panHandlers: panHandlers };
359
+ }
360
+ };
361
+
362
+ module.exports = PanResponder;