1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | 'use strict';
|
12 |
|
13 | var _prodInvariant = require('./reactProdInvariant');
|
14 |
|
15 | var ReactComponentEnvironment = require('./ReactComponentEnvironment');
|
16 | var ReactInstanceMap = require('./ReactInstanceMap');
|
17 | var ReactInstrumentation = require('./ReactInstrumentation');
|
18 |
|
19 | var ReactCurrentOwner = require('react/lib/ReactCurrentOwner');
|
20 | var ReactReconciler = require('./ReactReconciler');
|
21 | var ReactChildReconciler = require('./ReactChildReconciler');
|
22 |
|
23 | var emptyFunction = require('fbjs/lib/emptyFunction');
|
24 | var flattenChildren = require('./flattenChildren');
|
25 | var invariant = require('fbjs/lib/invariant');
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | function makeInsertMarkup(markup, afterNode, toIndex) {
|
35 |
|
36 | return {
|
37 | type: 'INSERT_MARKUP',
|
38 | content: markup,
|
39 | fromIndex: null,
|
40 | fromNode: null,
|
41 | toIndex: toIndex,
|
42 | afterNode: afterNode
|
43 | };
|
44 | }
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | function makeMove(child, afterNode, toIndex) {
|
54 |
|
55 | return {
|
56 | type: 'MOVE_EXISTING',
|
57 | content: null,
|
58 | fromIndex: child._mountIndex,
|
59 | fromNode: ReactReconciler.getHostNode(child),
|
60 | toIndex: toIndex,
|
61 | afterNode: afterNode
|
62 | };
|
63 | }
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 | function makeRemove(child, node) {
|
72 |
|
73 | return {
|
74 | type: 'REMOVE_NODE',
|
75 | content: null,
|
76 | fromIndex: child._mountIndex,
|
77 | fromNode: node,
|
78 | toIndex: null,
|
79 | afterNode: null
|
80 | };
|
81 | }
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 | function makeSetMarkup(markup) {
|
90 |
|
91 | return {
|
92 | type: 'SET_MARKUP',
|
93 | content: markup,
|
94 | fromIndex: null,
|
95 | fromNode: null,
|
96 | toIndex: null,
|
97 | afterNode: null
|
98 | };
|
99 | }
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 | function makeTextContent(textContent) {
|
108 |
|
109 | return {
|
110 | type: 'TEXT_CONTENT',
|
111 | content: textContent,
|
112 | fromIndex: null,
|
113 | fromNode: null,
|
114 | toIndex: null,
|
115 | afterNode: null
|
116 | };
|
117 | }
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 | function enqueue(queue, update) {
|
124 | if (update) {
|
125 | queue = queue || [];
|
126 | queue.push(update);
|
127 | }
|
128 | return queue;
|
129 | }
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 | function processQueue(inst, updateQueue) {
|
137 | ReactComponentEnvironment.processChildrenUpdates(inst, updateQueue);
|
138 | }
|
139 |
|
140 | var setChildrenForInstrumentation = emptyFunction;
|
141 | if (process.env.NODE_ENV !== 'production') {
|
142 | var getDebugID = function (inst) {
|
143 | if (!inst._debugID) {
|
144 |
|
145 | var internal;
|
146 | if (internal = ReactInstanceMap.get(inst)) {
|
147 | inst = internal;
|
148 | }
|
149 | }
|
150 | return inst._debugID;
|
151 | };
|
152 | setChildrenForInstrumentation = function (children) {
|
153 | var debugID = getDebugID(this);
|
154 |
|
155 |
|
156 | if (debugID !== 0) {
|
157 | ReactInstrumentation.debugTool.onSetChildren(debugID, children ? Object.keys(children).map(function (key) {
|
158 | return children[key]._debugID;
|
159 | }) : []);
|
160 | }
|
161 | };
|
162 | }
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 | var ReactMultiChild = {
|
171 | |
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 | Mixin: {
|
179 | _reconcilerInstantiateChildren: function (nestedChildren, transaction, context) {
|
180 | if (process.env.NODE_ENV !== 'production') {
|
181 | var selfDebugID = getDebugID(this);
|
182 | if (this._currentElement) {
|
183 | try {
|
184 | ReactCurrentOwner.current = this._currentElement._owner;
|
185 | return ReactChildReconciler.instantiateChildren(nestedChildren, transaction, context, selfDebugID);
|
186 | } finally {
|
187 | ReactCurrentOwner.current = null;
|
188 | }
|
189 | }
|
190 | }
|
191 | return ReactChildReconciler.instantiateChildren(nestedChildren, transaction, context);
|
192 | },
|
193 |
|
194 | _reconcilerUpdateChildren: function (prevChildren, nextNestedChildrenElements, mountImages, removedNodes, transaction, context) {
|
195 | var nextChildren;
|
196 | var selfDebugID = 0;
|
197 | if (process.env.NODE_ENV !== 'production') {
|
198 | selfDebugID = getDebugID(this);
|
199 | if (this._currentElement) {
|
200 | try {
|
201 | ReactCurrentOwner.current = this._currentElement._owner;
|
202 | nextChildren = flattenChildren(nextNestedChildrenElements, selfDebugID);
|
203 | } finally {
|
204 | ReactCurrentOwner.current = null;
|
205 | }
|
206 | ReactChildReconciler.updateChildren(prevChildren, nextChildren, mountImages, removedNodes, transaction, this, this._hostContainerInfo, context, selfDebugID);
|
207 | return nextChildren;
|
208 | }
|
209 | }
|
210 | nextChildren = flattenChildren(nextNestedChildrenElements, selfDebugID);
|
211 | ReactChildReconciler.updateChildren(prevChildren, nextChildren, mountImages, removedNodes, transaction, this, this._hostContainerInfo, context, selfDebugID);
|
212 | return nextChildren;
|
213 | },
|
214 |
|
215 | |
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 | mountChildren: function (nestedChildren, transaction, context) {
|
224 | var children = this._reconcilerInstantiateChildren(nestedChildren, transaction, context);
|
225 | this._renderedChildren = children;
|
226 |
|
227 | var mountImages = [];
|
228 | var index = 0;
|
229 | for (var name in children) {
|
230 | if (children.hasOwnProperty(name)) {
|
231 | var child = children[name];
|
232 | var selfDebugID = 0;
|
233 | if (process.env.NODE_ENV !== 'production') {
|
234 | selfDebugID = getDebugID(this);
|
235 | }
|
236 | var mountImage = ReactReconciler.mountComponent(child, transaction, this, this._hostContainerInfo, context, selfDebugID);
|
237 | child._mountIndex = index++;
|
238 | mountImages.push(mountImage);
|
239 | }
|
240 | }
|
241 |
|
242 | if (process.env.NODE_ENV !== 'production') {
|
243 | setChildrenForInstrumentation.call(this, children);
|
244 | }
|
245 |
|
246 | return mountImages;
|
247 | },
|
248 |
|
249 | |
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 | updateTextContent: function (nextContent) {
|
256 | var prevChildren = this._renderedChildren;
|
257 |
|
258 | ReactChildReconciler.unmountChildren(prevChildren, false);
|
259 | for (var name in prevChildren) {
|
260 | if (prevChildren.hasOwnProperty(name)) {
|
261 | !false ? process.env.NODE_ENV !== 'production' ? invariant(false, 'updateTextContent called on non-empty component.') : _prodInvariant('118') : void 0;
|
262 | }
|
263 | }
|
264 |
|
265 | var updates = [makeTextContent(nextContent)];
|
266 | processQueue(this, updates);
|
267 | },
|
268 |
|
269 | |
270 |
|
271 |
|
272 |
|
273 |
|
274 |
|
275 | updateMarkup: function (nextMarkup) {
|
276 | var prevChildren = this._renderedChildren;
|
277 |
|
278 | ReactChildReconciler.unmountChildren(prevChildren, false);
|
279 | for (var name in prevChildren) {
|
280 | if (prevChildren.hasOwnProperty(name)) {
|
281 | !false ? process.env.NODE_ENV !== 'production' ? invariant(false, 'updateTextContent called on non-empty component.') : _prodInvariant('118') : void 0;
|
282 | }
|
283 | }
|
284 | var updates = [makeSetMarkup(nextMarkup)];
|
285 | processQueue(this, updates);
|
286 | },
|
287 |
|
288 | |
289 |
|
290 |
|
291 |
|
292 |
|
293 |
|
294 |
|
295 | updateChildren: function (nextNestedChildrenElements, transaction, context) {
|
296 |
|
297 | this._updateChildren(nextNestedChildrenElements, transaction, context);
|
298 | },
|
299 |
|
300 | |
301 |
|
302 |
|
303 |
|
304 |
|
305 |
|
306 | _updateChildren: function (nextNestedChildrenElements, transaction, context) {
|
307 | var prevChildren = this._renderedChildren;
|
308 | var removedNodes = {};
|
309 | var mountImages = [];
|
310 | var nextChildren = this._reconcilerUpdateChildren(prevChildren, nextNestedChildrenElements, mountImages, removedNodes, transaction, context);
|
311 | if (!nextChildren && !prevChildren) {
|
312 | return;
|
313 | }
|
314 | var updates = null;
|
315 | var name;
|
316 |
|
317 |
|
318 | var nextIndex = 0;
|
319 | var lastIndex = 0;
|
320 |
|
321 | var nextMountIndex = 0;
|
322 | var lastPlacedNode = null;
|
323 | for (name in nextChildren) {
|
324 | if (!nextChildren.hasOwnProperty(name)) {
|
325 | continue;
|
326 | }
|
327 | var prevChild = prevChildren && prevChildren[name];
|
328 | var nextChild = nextChildren[name];
|
329 | if (prevChild === nextChild) {
|
330 | updates = enqueue(updates, this.moveChild(prevChild, lastPlacedNode, nextIndex, lastIndex));
|
331 | lastIndex = Math.max(prevChild._mountIndex, lastIndex);
|
332 | prevChild._mountIndex = nextIndex;
|
333 | } else {
|
334 | if (prevChild) {
|
335 |
|
336 | lastIndex = Math.max(prevChild._mountIndex, lastIndex);
|
337 |
|
338 | }
|
339 |
|
340 | updates = enqueue(updates, this._mountChildAtIndex(nextChild, mountImages[nextMountIndex], lastPlacedNode, nextIndex, transaction, context));
|
341 | nextMountIndex++;
|
342 | }
|
343 | nextIndex++;
|
344 | lastPlacedNode = ReactReconciler.getHostNode(nextChild);
|
345 | }
|
346 |
|
347 | for (name in removedNodes) {
|
348 | if (removedNodes.hasOwnProperty(name)) {
|
349 | updates = enqueue(updates, this._unmountChild(prevChildren[name], removedNodes[name]));
|
350 | }
|
351 | }
|
352 | if (updates) {
|
353 | processQueue(this, updates);
|
354 | }
|
355 | this._renderedChildren = nextChildren;
|
356 |
|
357 | if (process.env.NODE_ENV !== 'production') {
|
358 | setChildrenForInstrumentation.call(this, nextChildren);
|
359 | }
|
360 | },
|
361 |
|
362 | |
363 |
|
364 |
|
365 |
|
366 |
|
367 |
|
368 |
|
369 | unmountChildren: function (safely) {
|
370 | var renderedChildren = this._renderedChildren;
|
371 | ReactChildReconciler.unmountChildren(renderedChildren, safely);
|
372 | this._renderedChildren = null;
|
373 | },
|
374 |
|
375 | |
376 |
|
377 |
|
378 |
|
379 |
|
380 |
|
381 |
|
382 |
|
383 | moveChild: function (child, afterNode, toIndex, lastIndex) {
|
384 |
|
385 |
|
386 |
|
387 | if (child._mountIndex < lastIndex) {
|
388 | return makeMove(child, afterNode, toIndex);
|
389 | }
|
390 | },
|
391 |
|
392 | |
393 |
|
394 |
|
395 |
|
396 |
|
397 |
|
398 |
|
399 | createChild: function (child, afterNode, mountImage) {
|
400 | return makeInsertMarkup(mountImage, afterNode, child._mountIndex);
|
401 | },
|
402 |
|
403 | |
404 |
|
405 |
|
406 |
|
407 |
|
408 |
|
409 | removeChild: function (child, node) {
|
410 | return makeRemove(child, node);
|
411 | },
|
412 |
|
413 | |
414 |
|
415 |
|
416 |
|
417 |
|
418 |
|
419 |
|
420 |
|
421 |
|
422 |
|
423 |
|
424 | _mountChildAtIndex: function (child, mountImage, afterNode, index, transaction, context) {
|
425 | child._mountIndex = index;
|
426 | return this.createChild(child, afterNode, mountImage);
|
427 | },
|
428 |
|
429 | |
430 |
|
431 |
|
432 |
|
433 |
|
434 |
|
435 |
|
436 |
|
437 | _unmountChild: function (child, node) {
|
438 | var update = this.removeChild(child, node);
|
439 | child._mountIndex = null;
|
440 | return update;
|
441 | }
|
442 | }
|
443 | };
|
444 |
|
445 | module.exports = ReactMultiChild; |
\ | No newline at end of file |