1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | 'use strict';
|
12 |
|
13 | var PooledClass = require('./PooledClass');
|
14 | var ReactElement = require('./ReactElement');
|
15 |
|
16 | var emptyFunction = require('fbjs/lib/emptyFunction');
|
17 | var traverseAllChildren = require('./traverseAllChildren');
|
18 |
|
19 | var twoArgumentPooler = PooledClass.twoArgumentPooler;
|
20 | var fourArgumentPooler = PooledClass.fourArgumentPooler;
|
21 |
|
22 | var userProvidedKeyEscapeRegex = /\/+/g;
|
23 | function escapeUserProvidedKey(text) {
|
24 | return ('' + text).replace(userProvidedKeyEscapeRegex, '$&/');
|
25 | }
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | function ForEachBookKeeping(forEachFunction, forEachContext) {
|
36 | this.func = forEachFunction;
|
37 | this.context = forEachContext;
|
38 | this.count = 0;
|
39 | }
|
40 | ForEachBookKeeping.prototype.destructor = function () {
|
41 | this.func = null;
|
42 | this.context = null;
|
43 | this.count = 0;
|
44 | };
|
45 | PooledClass.addPoolingTo(ForEachBookKeeping, twoArgumentPooler);
|
46 |
|
47 | function forEachSingleChild(bookKeeping, child, name) {
|
48 | var func = bookKeeping.func,
|
49 | context = bookKeeping.context;
|
50 |
|
51 | func.call(context, child, bookKeeping.count++);
|
52 | }
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 | function forEachChildren(children, forEachFunc, forEachContext) {
|
67 | if (children == null) {
|
68 | return children;
|
69 | }
|
70 | var traverseContext = ForEachBookKeeping.getPooled(forEachFunc, forEachContext);
|
71 | traverseAllChildren(children, forEachSingleChild, traverseContext);
|
72 | ForEachBookKeeping.release(traverseContext);
|
73 | }
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 | function MapBookKeeping(mapResult, keyPrefix, mapFunction, mapContext) {
|
85 | this.result = mapResult;
|
86 | this.keyPrefix = keyPrefix;
|
87 | this.func = mapFunction;
|
88 | this.context = mapContext;
|
89 | this.count = 0;
|
90 | }
|
91 | MapBookKeeping.prototype.destructor = function () {
|
92 | this.result = null;
|
93 | this.keyPrefix = null;
|
94 | this.func = null;
|
95 | this.context = null;
|
96 | this.count = 0;
|
97 | };
|
98 | PooledClass.addPoolingTo(MapBookKeeping, fourArgumentPooler);
|
99 |
|
100 | function mapSingleChildIntoContext(bookKeeping, child, childKey) {
|
101 | var result = bookKeeping.result,
|
102 | keyPrefix = bookKeeping.keyPrefix,
|
103 | func = bookKeeping.func,
|
104 | context = bookKeeping.context;
|
105 |
|
106 |
|
107 | var mappedChild = func.call(context, child, bookKeeping.count++);
|
108 | if (Array.isArray(mappedChild)) {
|
109 | mapIntoWithKeyPrefixInternal(mappedChild, result, childKey, emptyFunction.thatReturnsArgument);
|
110 | } else if (mappedChild != null) {
|
111 | if (ReactElement.isValidElement(mappedChild)) {
|
112 | mappedChild = ReactElement.cloneAndReplaceKey(mappedChild,
|
113 |
|
114 |
|
115 | keyPrefix + (mappedChild.key && (!child || child.key !== mappedChild.key) ? escapeUserProvidedKey(mappedChild.key) + '/' : '') + childKey);
|
116 | }
|
117 | result.push(mappedChild);
|
118 | }
|
119 | }
|
120 |
|
121 | function mapIntoWithKeyPrefixInternal(children, array, prefix, func, context) {
|
122 | var escapedPrefix = '';
|
123 | if (prefix != null) {
|
124 | escapedPrefix = escapeUserProvidedKey(prefix) + '/';
|
125 | }
|
126 | var traverseContext = MapBookKeeping.getPooled(array, escapedPrefix, func, context);
|
127 | traverseAllChildren(children, mapSingleChildIntoContext, traverseContext);
|
128 | MapBookKeeping.release(traverseContext);
|
129 | }
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 | function mapChildren(children, func, context) {
|
145 | if (children == null) {
|
146 | return children;
|
147 | }
|
148 | var result = [];
|
149 | mapIntoWithKeyPrefixInternal(children, result, null, func, context);
|
150 | return result;
|
151 | }
|
152 |
|
153 | function forEachSingleChildDummy(traverseContext, child, name) {
|
154 | return null;
|
155 | }
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 | function countChildren(children, context) {
|
167 | return traverseAllChildren(children, forEachSingleChildDummy, null);
|
168 | }
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 | function toArray(children) {
|
177 | var result = [];
|
178 | mapIntoWithKeyPrefixInternal(children, result, null, emptyFunction.thatReturnsArgument);
|
179 | return result;
|
180 | }
|
181 |
|
182 | var ReactChildren = {
|
183 | forEach: forEachChildren,
|
184 | map: mapChildren,
|
185 | mapIntoWithKeyPrefixInternal: mapIntoWithKeyPrefixInternal,
|
186 | count: countChildren,
|
187 | toArray: toArray
|
188 | };
|
189 |
|
190 | module.exports = ReactChildren; |
\ | No newline at end of file |