1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | 'use strict';
|
10 |
|
11 | var EventPluginHub = require('./EventPluginHub');
|
12 | var EventPropagators = require('./EventPropagators');
|
13 | var ExecutionEnvironment = require('fbjs/lib/ExecutionEnvironment');
|
14 | var ReactDOMComponentTree = require('./ReactDOMComponentTree');
|
15 | var ReactUpdates = require('./ReactUpdates');
|
16 | var SyntheticEvent = require('./SyntheticEvent');
|
17 |
|
18 | var inputValueTracking = require('./inputValueTracking');
|
19 | var getEventTarget = require('./getEventTarget');
|
20 | var isEventSupported = require('./isEventSupported');
|
21 | var isTextInputElement = require('./isTextInputElement');
|
22 |
|
23 | var eventTypes = {
|
24 | change: {
|
25 | phasedRegistrationNames: {
|
26 | bubbled: 'onChange',
|
27 | captured: 'onChangeCapture'
|
28 | },
|
29 | dependencies: ['topBlur', 'topChange', 'topClick', 'topFocus', 'topInput', 'topKeyDown', 'topKeyUp', 'topSelectionChange']
|
30 | }
|
31 | };
|
32 |
|
33 | function createAndAccumulateChangeEvent(inst, nativeEvent, target) {
|
34 | var event = SyntheticEvent.getPooled(eventTypes.change, inst, nativeEvent, target);
|
35 | event.type = 'change';
|
36 | EventPropagators.accumulateTwoPhaseDispatches(event);
|
37 | return event;
|
38 | }
|
39 |
|
40 |
|
41 |
|
42 | var activeElement = null;
|
43 | var activeElementInst = null;
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | function shouldUseChangeEvent(elem) {
|
49 | var nodeName = elem.nodeName && elem.nodeName.toLowerCase();
|
50 | return nodeName === 'select' || nodeName === 'input' && elem.type === 'file';
|
51 | }
|
52 |
|
53 | var doesChangeEventBubble = false;
|
54 | if (ExecutionEnvironment.canUseDOM) {
|
55 |
|
56 | doesChangeEventBubble = isEventSupported('change') && (!document.documentMode || document.documentMode > 8);
|
57 | }
|
58 |
|
59 | function manualDispatchChangeEvent(nativeEvent) {
|
60 | var event = createAndAccumulateChangeEvent(activeElementInst, nativeEvent, getEventTarget(nativeEvent));
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | ReactUpdates.batchedUpdates(runEventInBatch, event);
|
74 | }
|
75 |
|
76 | function runEventInBatch(event) {
|
77 | EventPluginHub.enqueueEvents(event);
|
78 | EventPluginHub.processEventQueue(false);
|
79 | }
|
80 |
|
81 | function startWatchingForChangeEventIE8(target, targetInst) {
|
82 | activeElement = target;
|
83 | activeElementInst = targetInst;
|
84 | activeElement.attachEvent('onchange', manualDispatchChangeEvent);
|
85 | }
|
86 |
|
87 | function stopWatchingForChangeEventIE8() {
|
88 | if (!activeElement) {
|
89 | return;
|
90 | }
|
91 | activeElement.detachEvent('onchange', manualDispatchChangeEvent);
|
92 | activeElement = null;
|
93 | activeElementInst = null;
|
94 | }
|
95 |
|
96 | function getInstIfValueChanged(targetInst, nativeEvent) {
|
97 | var updated = inputValueTracking.updateValueIfChanged(targetInst);
|
98 | var simulated = nativeEvent.simulated === true && ChangeEventPlugin._allowSimulatedPassThrough;
|
99 |
|
100 | if (updated || simulated) {
|
101 | return targetInst;
|
102 | }
|
103 | }
|
104 |
|
105 | function getTargetInstForChangeEvent(topLevelType, targetInst) {
|
106 | if (topLevelType === 'topChange') {
|
107 | return targetInst;
|
108 | }
|
109 | }
|
110 |
|
111 | function handleEventsForChangeEventIE8(topLevelType, target, targetInst) {
|
112 | if (topLevelType === 'topFocus') {
|
113 |
|
114 |
|
115 | stopWatchingForChangeEventIE8();
|
116 | startWatchingForChangeEventIE8(target, targetInst);
|
117 | } else if (topLevelType === 'topBlur') {
|
118 | stopWatchingForChangeEventIE8();
|
119 | }
|
120 | }
|
121 |
|
122 |
|
123 |
|
124 |
|
125 | var isInputEventSupported = false;
|
126 | if (ExecutionEnvironment.canUseDOM) {
|
127 |
|
128 |
|
129 |
|
130 | isInputEventSupported = isEventSupported('input') && (!document.documentMode || document.documentMode > 9);
|
131 | }
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 | function startWatchingForValueChange(target, targetInst) {
|
139 | activeElement = target;
|
140 | activeElementInst = targetInst;
|
141 | activeElement.attachEvent('onpropertychange', handlePropertyChange);
|
142 | }
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 | function stopWatchingForValueChange() {
|
149 | if (!activeElement) {
|
150 | return;
|
151 | }
|
152 | activeElement.detachEvent('onpropertychange', handlePropertyChange);
|
153 |
|
154 | activeElement = null;
|
155 | activeElementInst = null;
|
156 | }
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 | function handlePropertyChange(nativeEvent) {
|
163 | if (nativeEvent.propertyName !== 'value') {
|
164 | return;
|
165 | }
|
166 | if (getInstIfValueChanged(activeElementInst, nativeEvent)) {
|
167 | manualDispatchChangeEvent(nativeEvent);
|
168 | }
|
169 | }
|
170 |
|
171 | function handleEventsForInputEventPolyfill(topLevelType, target, targetInst) {
|
172 | if (topLevelType === 'topFocus') {
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 | stopWatchingForValueChange();
|
187 | startWatchingForValueChange(target, targetInst);
|
188 | } else if (topLevelType === 'topBlur') {
|
189 | stopWatchingForValueChange();
|
190 | }
|
191 | }
|
192 |
|
193 |
|
194 | function getTargetInstForInputEventPolyfill(topLevelType, targetInst, nativeEvent) {
|
195 | if (topLevelType === 'topSelectionChange' || topLevelType === 'topKeyUp' || topLevelType === 'topKeyDown') {
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 | return getInstIfValueChanged(activeElementInst, nativeEvent);
|
207 | }
|
208 | }
|
209 |
|
210 |
|
211 |
|
212 |
|
213 | function shouldUseClickEvent(elem) {
|
214 |
|
215 |
|
216 |
|
217 | var nodeName = elem.nodeName;
|
218 | return nodeName && nodeName.toLowerCase() === 'input' && (elem.type === 'checkbox' || elem.type === 'radio');
|
219 | }
|
220 |
|
221 | function getTargetInstForClickEvent(topLevelType, targetInst, nativeEvent) {
|
222 | if (topLevelType === 'topClick') {
|
223 | return getInstIfValueChanged(targetInst, nativeEvent);
|
224 | }
|
225 | }
|
226 |
|
227 | function getTargetInstForInputOrChangeEvent(topLevelType, targetInst, nativeEvent) {
|
228 | if (topLevelType === 'topInput' || topLevelType === 'topChange') {
|
229 | return getInstIfValueChanged(targetInst, nativeEvent);
|
230 | }
|
231 | }
|
232 |
|
233 | function handleControlledInputBlur(inst, node) {
|
234 |
|
235 | if (inst == null) {
|
236 | return;
|
237 | }
|
238 |
|
239 |
|
240 | var state = inst._wrapperState || node._wrapperState;
|
241 |
|
242 | if (!state || !state.controlled || node.type !== 'number') {
|
243 | return;
|
244 | }
|
245 |
|
246 |
|
247 | var value = '' + node.value;
|
248 | if (node.getAttribute('value') !== value) {
|
249 | node.setAttribute('value', value);
|
250 | }
|
251 | }
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 |
|
262 |
|
263 | var ChangeEventPlugin = {
|
264 | eventTypes: eventTypes,
|
265 |
|
266 | _allowSimulatedPassThrough: true,
|
267 | _isInputEventSupported: isInputEventSupported,
|
268 |
|
269 | extractEvents: function (topLevelType, targetInst, nativeEvent, nativeEventTarget) {
|
270 | var targetNode = targetInst ? ReactDOMComponentTree.getNodeFromInstance(targetInst) : window;
|
271 |
|
272 | var getTargetInstFunc, handleEventFunc;
|
273 | if (shouldUseChangeEvent(targetNode)) {
|
274 | if (doesChangeEventBubble) {
|
275 | getTargetInstFunc = getTargetInstForChangeEvent;
|
276 | } else {
|
277 | handleEventFunc = handleEventsForChangeEventIE8;
|
278 | }
|
279 | } else if (isTextInputElement(targetNode)) {
|
280 | if (isInputEventSupported) {
|
281 | getTargetInstFunc = getTargetInstForInputOrChangeEvent;
|
282 | } else {
|
283 | getTargetInstFunc = getTargetInstForInputEventPolyfill;
|
284 | handleEventFunc = handleEventsForInputEventPolyfill;
|
285 | }
|
286 | } else if (shouldUseClickEvent(targetNode)) {
|
287 | getTargetInstFunc = getTargetInstForClickEvent;
|
288 | }
|
289 |
|
290 | if (getTargetInstFunc) {
|
291 | var inst = getTargetInstFunc(topLevelType, targetInst, nativeEvent);
|
292 | if (inst) {
|
293 | var event = createAndAccumulateChangeEvent(inst, nativeEvent, nativeEventTarget);
|
294 | return event;
|
295 | }
|
296 | }
|
297 |
|
298 | if (handleEventFunc) {
|
299 | handleEventFunc(topLevelType, targetNode, targetInst);
|
300 | }
|
301 |
|
302 |
|
303 | if (topLevelType === 'topBlur') {
|
304 | handleControlledInputBlur(targetInst, targetNode);
|
305 | }
|
306 | }
|
307 | };
|
308 |
|
309 | module.exports = ChangeEventPlugin; |
\ | No newline at end of file |