1 | /**
|
2 | * Copyright 2013-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 | */
|
10 |
|
11 | ;
|
12 |
|
13 | var flattenChildren = require('./flattenChildren');
|
14 |
|
15 | var ReactTransitionChildMapping = {
|
16 | /**
|
17 | * Given `this.props.children`, return an object mapping key to child. Just
|
18 | * simple syntactic sugar around flattenChildren().
|
19 | *
|
20 | * @param {*} children `this.props.children`
|
21 | * @param {number=} selfDebugID Optional debugID of the current internal instance.
|
22 | * @return {object} Mapping of key to child
|
23 | */
|
24 | getChildMapping: function (children, selfDebugID) {
|
25 | if (!children) {
|
26 | return children;
|
27 | }
|
28 |
|
29 | if (process.env.NODE_ENV !== 'production') {
|
30 | return flattenChildren(children, selfDebugID);
|
31 | }
|
32 |
|
33 | return flattenChildren(children);
|
34 | },
|
35 |
|
36 | /**
|
37 | * When you're adding or removing children some may be added or removed in the
|
38 | * same render pass. We want to show *both* since we want to simultaneously
|
39 | * animate elements in and out. This function takes a previous set of keys
|
40 | * and a new set of keys and merges them with its best guess of the correct
|
41 | * ordering. In the future we may expose some of the utilities in
|
42 | * ReactMultiChild to make this easy, but for now React itself does not
|
43 | * directly have this concept of the union of prevChildren and nextChildren
|
44 | * so we implement it here.
|
45 | *
|
46 | * @param {object} prev prev children as returned from
|
47 | * `ReactTransitionChildMapping.getChildMapping()`.
|
48 | * @param {object} next next children as returned from
|
49 | * `ReactTransitionChildMapping.getChildMapping()`.
|
50 | * @return {object} a key set that contains all keys in `prev` and all keys
|
51 | * in `next` in a reasonable order.
|
52 | */
|
53 | mergeChildMappings: function (prev, next) {
|
54 | prev = prev || {};
|
55 | next = next || {};
|
56 |
|
57 | function getValueForKey(key) {
|
58 | if (next.hasOwnProperty(key)) {
|
59 | return next[key];
|
60 | } else {
|
61 | return prev[key];
|
62 | }
|
63 | }
|
64 |
|
65 | // For each key of `next`, the list of keys to insert before that key in
|
66 | // the combined list
|
67 | var nextKeysPending = {};
|
68 |
|
69 | var pendingKeys = [];
|
70 | for (var prevKey in prev) {
|
71 | if (next.hasOwnProperty(prevKey)) {
|
72 | if (pendingKeys.length) {
|
73 | nextKeysPending[prevKey] = pendingKeys;
|
74 | pendingKeys = [];
|
75 | }
|
76 | } else {
|
77 | pendingKeys.push(prevKey);
|
78 | }
|
79 | }
|
80 |
|
81 | var i;
|
82 | var childMapping = {};
|
83 | for (var nextKey in next) {
|
84 | if (nextKeysPending.hasOwnProperty(nextKey)) {
|
85 | for (i = 0; i < nextKeysPending[nextKey].length; i++) {
|
86 | var pendingNextKey = nextKeysPending[nextKey][i];
|
87 | childMapping[nextKeysPending[nextKey][i]] = getValueForKey(pendingNextKey);
|
88 | }
|
89 | }
|
90 | childMapping[nextKey] = getValueForKey(nextKey);
|
91 | }
|
92 |
|
93 | // Finally, add the keys which didn't appear before any key in `next`
|
94 | for (i = 0; i < pendingKeys.length; i++) {
|
95 | childMapping[pendingKeys[i]] = getValueForKey(pendingKeys[i]);
|
96 | }
|
97 |
|
98 | return childMapping;
|
99 | }
|
100 | };
|
101 |
|
102 | module.exports = ReactTransitionChildMapping; |
\ | No newline at end of file |