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