1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | 'use strict';
|
10 |
|
11 | var _prodInvariant = require('./reactProdInvariant');
|
12 |
|
13 | var EventPluginRegistry = require('./EventPluginRegistry');
|
14 | var EventPluginUtils = require('./EventPluginUtils');
|
15 | var ReactErrorUtils = require('./ReactErrorUtils');
|
16 |
|
17 | var accumulateInto = require('./accumulateInto');
|
18 | var forEachAccumulated = require('./forEachAccumulated');
|
19 | var invariant = require('fbjs/lib/invariant');
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | var listenerBank = {};
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | var eventQueue = null;
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | var executeDispatchesAndRelease = function (event, simulated) {
|
40 | if (event) {
|
41 | EventPluginUtils.executeDispatchesInOrder(event, simulated);
|
42 |
|
43 | if (!event.isPersistent()) {
|
44 | event.constructor.release(event);
|
45 | }
|
46 | }
|
47 | };
|
48 | var executeDispatchesAndReleaseSimulated = function (e) {
|
49 | return executeDispatchesAndRelease(e, true);
|
50 | };
|
51 | var executeDispatchesAndReleaseTopLevel = function (e) {
|
52 | return executeDispatchesAndRelease(e, false);
|
53 | };
|
54 |
|
55 | var getDictionaryKey = function (inst) {
|
56 |
|
57 |
|
58 | return '.' + inst._rootNodeID;
|
59 | };
|
60 |
|
61 | function isInteractive(tag) {
|
62 | return tag === 'button' || tag === 'input' || tag === 'select' || tag === 'textarea';
|
63 | }
|
64 |
|
65 | function shouldPreventMouseEvent(name, type, props) {
|
66 | switch (name) {
|
67 | case 'onClick':
|
68 | case 'onClickCapture':
|
69 | case 'onDoubleClick':
|
70 | case 'onDoubleClickCapture':
|
71 | case 'onMouseDown':
|
72 | case 'onMouseDownCapture':
|
73 | case 'onMouseMove':
|
74 | case 'onMouseMoveCapture':
|
75 | case 'onMouseUp':
|
76 | case 'onMouseUpCapture':
|
77 | return !!(props.disabled && isInteractive(type));
|
78 | default:
|
79 | return false;
|
80 | }
|
81 | }
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 | var EventPluginHub = {
|
106 | |
107 |
|
108 |
|
109 | injection: {
|
110 | |
111 |
|
112 |
|
113 |
|
114 | injectEventPluginOrder: EventPluginRegistry.injectEventPluginOrder,
|
115 |
|
116 | |
117 |
|
118 |
|
119 | injectEventPluginsByName: EventPluginRegistry.injectEventPluginsByName
|
120 | },
|
121 |
|
122 | |
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 | putListener: function (inst, registrationName, listener) {
|
130 | !(typeof listener === 'function') ? process.env.NODE_ENV !== 'production' ? invariant(false, 'Expected %s listener to be a function, instead got type %s', registrationName, typeof listener) : _prodInvariant('94', registrationName, typeof listener) : void 0;
|
131 |
|
132 | var key = getDictionaryKey(inst);
|
133 | var bankForRegistrationName = listenerBank[registrationName] || (listenerBank[registrationName] = {});
|
134 | bankForRegistrationName[key] = listener;
|
135 |
|
136 | var PluginModule = EventPluginRegistry.registrationNameModules[registrationName];
|
137 | if (PluginModule && PluginModule.didPutListener) {
|
138 | PluginModule.didPutListener(inst, registrationName, listener);
|
139 | }
|
140 | },
|
141 |
|
142 | |
143 |
|
144 |
|
145 |
|
146 |
|
147 | getListener: function (inst, registrationName) {
|
148 |
|
149 |
|
150 | var bankForRegistrationName = listenerBank[registrationName];
|
151 | if (shouldPreventMouseEvent(registrationName, inst._currentElement.type, inst._currentElement.props)) {
|
152 | return null;
|
153 | }
|
154 | var key = getDictionaryKey(inst);
|
155 | return bankForRegistrationName && bankForRegistrationName[key];
|
156 | },
|
157 |
|
158 | |
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 | deleteListener: function (inst, registrationName) {
|
165 | var PluginModule = EventPluginRegistry.registrationNameModules[registrationName];
|
166 | if (PluginModule && PluginModule.willDeleteListener) {
|
167 | PluginModule.willDeleteListener(inst, registrationName);
|
168 | }
|
169 |
|
170 | var bankForRegistrationName = listenerBank[registrationName];
|
171 |
|
172 | if (bankForRegistrationName) {
|
173 | var key = getDictionaryKey(inst);
|
174 | delete bankForRegistrationName[key];
|
175 | }
|
176 | },
|
177 |
|
178 | |
179 |
|
180 |
|
181 |
|
182 |
|
183 | deleteAllListeners: function (inst) {
|
184 | var key = getDictionaryKey(inst);
|
185 | for (var registrationName in listenerBank) {
|
186 | if (!listenerBank.hasOwnProperty(registrationName)) {
|
187 | continue;
|
188 | }
|
189 |
|
190 | if (!listenerBank[registrationName][key]) {
|
191 | continue;
|
192 | }
|
193 |
|
194 | var PluginModule = EventPluginRegistry.registrationNameModules[registrationName];
|
195 | if (PluginModule && PluginModule.willDeleteListener) {
|
196 | PluginModule.willDeleteListener(inst, registrationName);
|
197 | }
|
198 |
|
199 | delete listenerBank[registrationName][key];
|
200 | }
|
201 | },
|
202 |
|
203 | |
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 | extractEvents: function (topLevelType, targetInst, nativeEvent, nativeEventTarget) {
|
211 | var events;
|
212 | var plugins = EventPluginRegistry.plugins;
|
213 | for (var i = 0; i < plugins.length; i++) {
|
214 |
|
215 | var possiblePlugin = plugins[i];
|
216 | if (possiblePlugin) {
|
217 | var extractedEvents = possiblePlugin.extractEvents(topLevelType, targetInst, nativeEvent, nativeEventTarget);
|
218 | if (extractedEvents) {
|
219 | events = accumulateInto(events, extractedEvents);
|
220 | }
|
221 | }
|
222 | }
|
223 | return events;
|
224 | },
|
225 |
|
226 | |
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 | enqueueEvents: function (events) {
|
234 | if (events) {
|
235 | eventQueue = accumulateInto(eventQueue, events);
|
236 | }
|
237 | },
|
238 |
|
239 | |
240 |
|
241 |
|
242 |
|
243 |
|
244 | processEventQueue: function (simulated) {
|
245 |
|
246 |
|
247 | var processingEventQueue = eventQueue;
|
248 | eventQueue = null;
|
249 | if (simulated) {
|
250 | forEachAccumulated(processingEventQueue, executeDispatchesAndReleaseSimulated);
|
251 | } else {
|
252 | forEachAccumulated(processingEventQueue, executeDispatchesAndReleaseTopLevel);
|
253 | }
|
254 | !!eventQueue ? process.env.NODE_ENV !== 'production' ? invariant(false, 'processEventQueue(): Additional events were enqueued while processing an event queue. Support for this has not yet been implemented.') : _prodInvariant('95') : void 0;
|
255 |
|
256 | ReactErrorUtils.rethrowCaughtError();
|
257 | },
|
258 |
|
259 | |
260 |
|
261 |
|
262 | __purge: function () {
|
263 | listenerBank = {};
|
264 | },
|
265 |
|
266 | __getListenerBank: function () {
|
267 | return listenerBank;
|
268 | }
|
269 | };
|
270 |
|
271 | module.exports = EventPluginHub; |
\ | No newline at end of file |