1 | 'use strict';
|
2 |
|
3 | exports.__esModule = true;
|
4 |
|
5 | var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties');
|
6 |
|
7 | var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);
|
8 |
|
9 | var _typeof2 = require('babel-runtime/helpers/typeof');
|
10 |
|
11 | var _typeof3 = _interopRequireDefault(_typeof2);
|
12 |
|
13 | var _extends2 = require('babel-runtime/helpers/extends');
|
14 |
|
15 | var _extends3 = _interopRequireDefault(_extends2);
|
16 |
|
17 | exports.isSingle = isSingle;
|
18 | exports.isNull = isNull;
|
19 | exports.escapeForReg = escapeForReg;
|
20 | exports.filter = filter;
|
21 | exports.loopMap = loopMap;
|
22 | exports.parseDataSourceFromChildren = parseDataSourceFromChildren;
|
23 | exports.normalizeDataSource = normalizeDataSource;
|
24 | exports.flattingDataSource = flattingDataSource;
|
25 | exports.filterDataSource = filterDataSource;
|
26 | exports.getValueDataSource = getValueDataSource;
|
27 | exports.valueToSelectKey = valueToSelectKey;
|
28 |
|
29 | var _react = require('react');
|
30 |
|
31 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | function isSingle(mode) {
|
43 | return !mode || mode === 'single';
|
44 | }
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 | function isNull(n) {
|
52 | return n === null || n === undefined;
|
53 | }
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 | function escapeForReg(str) {
|
61 | return str.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&');
|
62 | }
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 | function filter(key, item) {
|
71 | var _key = escapeForReg('' + key);
|
72 | var regExp = new RegExp('(' + _key + ')', 'ig');
|
73 |
|
74 | return regExp.test('' + item.value) || regExp.test('' + item.label);
|
75 | }
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 | function loopMap(dataSource, callback) {
|
87 | var result = [];
|
88 | dataSource.forEach(function (option) {
|
89 | if (option.children) {
|
90 | var children = loopMap(option.children, callback);
|
91 | result.push((0, _extends3.default)({}, option, {
|
92 | children: children
|
93 | }));
|
94 | } else {
|
95 |
|
96 | var tmp = callback(option);
|
97 | tmp && result.push(tmp);
|
98 | }
|
99 | });
|
100 |
|
101 | return result;
|
102 | }
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 | function parseDataSourceFromChildren(children) {
|
111 | var deep = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
|
112 |
|
113 | var source = [];
|
114 |
|
115 | _react.Children.forEach(children, function (child, index) {
|
116 | if (!child) {
|
117 | return;
|
118 | }
|
119 | var type = child.type,
|
120 | childProps = child.props;
|
121 |
|
122 | var item2 = { deep: deep };
|
123 |
|
124 | var isOption = false;
|
125 | var isOptionGroup = false;
|
126 |
|
127 | if (typeof type === 'function' && type._typeMark === 'next_select_option' || type === 'option') {
|
128 | isOption = true;
|
129 | }
|
130 | if (typeof type === 'function' && type._typeMark === 'next_select_option_group' || type === 'optgroup') {
|
131 | isOptionGroup = true;
|
132 | }
|
133 |
|
134 | if (!isOption && !isOptionGroup) {
|
135 | return;
|
136 | }
|
137 |
|
138 | if (isOption) {
|
139 |
|
140 |
|
141 | var isStrChild = typeof childProps.children === 'string';
|
142 |
|
143 | item2.value = 'value' in childProps ? childProps.value : 'key' in childProps ? childProps.key : isStrChild ? childProps.children : '' + index;
|
144 |
|
145 | item2.label = childProps.label || childProps.children || '' + item2.value;
|
146 | if ('title' in childProps) {
|
147 | item2.title = childProps.title;
|
148 | }
|
149 | childProps.disabled === true && (item2.disabled = true);
|
150 |
|
151 | (0, _extends3.default)(item2, childProps['data-extra'] || {});
|
152 | } else if (isOptionGroup && deep < 1) {
|
153 |
|
154 | item2.label = childProps.label || 'Group';
|
155 |
|
156 | item2.children = parseDataSourceFromChildren(childProps.children, deep + 1);
|
157 | }
|
158 |
|
159 | source.push(item2);
|
160 | });
|
161 |
|
162 | return source;
|
163 | }
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 | function normalizeDataSource(dataSource) {
|
176 | var deep = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
|
177 | var showDataSourceChildren = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
|
178 |
|
179 | var source = [];
|
180 |
|
181 | dataSource.forEach(function (item, index) {
|
182 |
|
183 | if (/string|boolean|number/.test(typeof item === 'undefined' ? 'undefined' : (0, _typeof3.default)(item)) || item === null || item === undefined) {
|
184 | item = { label: '' + item, value: item };
|
185 | }
|
186 |
|
187 |
|
188 | if (item && item.__isAddon) {
|
189 | return;
|
190 | }
|
191 |
|
192 | var item2 = { deep: deep };
|
193 |
|
194 | if (Array.isArray(item.children) && deep < 1 && showDataSourceChildren) {
|
195 |
|
196 | item2.label = item.label || item.value || 'Group ' + index;
|
197 |
|
198 | item2.children = normalizeDataSource(item.children, deep + 1);
|
199 | } else {
|
200 | var _item = item,
|
201 | value = _item.value,
|
202 | label = _item.label,
|
203 | disabled = _item.disabled,
|
204 | title = _item.title,
|
205 | others = (0, _objectWithoutProperties3.default)(_item, ['value', 'label', 'disabled', 'title']);
|
206 |
|
207 |
|
208 | item2.value = typeof value !== 'undefined' ? value : '' + index;
|
209 | item2.label = label || '' + item2.value;
|
210 | if ('title' in item) {
|
211 | item2.title = title;
|
212 | }
|
213 | disabled === true && (item2.disabled = true);
|
214 |
|
215 | (0, _extends3.default)(item2, others);
|
216 | }
|
217 |
|
218 | source.push(item2);
|
219 | });
|
220 |
|
221 | return source;
|
222 | }
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 | function flattingDataSource(dataSource) {
|
231 | var source = [];
|
232 |
|
233 | dataSource.forEach(function (item) {
|
234 | if (Array.isArray(item.children)) {
|
235 | source.push.apply(source, flattingDataSource(item.children));
|
236 | } else {
|
237 | source.push(item);
|
238 | }
|
239 | });
|
240 |
|
241 | return source;
|
242 | }
|
243 |
|
244 | function filterDataSource(dataSource, key, filter, addonKey) {
|
245 | if (!Array.isArray(dataSource)) {
|
246 | return [];
|
247 | }
|
248 | if (typeof key === 'undefined' || key === null) {
|
249 | return [].concat(dataSource);
|
250 | }
|
251 |
|
252 | var addKey = true;
|
253 | var menuDataSource = loopMap(dataSource, function (option) {
|
254 | if (key === '' + option.value) {
|
255 | addKey = false;
|
256 | }
|
257 | return filter(key, option) && !option.__isAddon && option;
|
258 | });
|
259 |
|
260 |
|
261 | if (addonKey && key && addKey) {
|
262 | menuDataSource.unshift({
|
263 | value: key,
|
264 | label: key,
|
265 | __isAddon: true
|
266 | });
|
267 | }
|
268 |
|
269 | return menuDataSource;
|
270 | }
|
271 |
|
272 | function getKeyItemByValue(value, valueMap) {
|
273 | var item = void 0;
|
274 |
|
275 | if ((typeof value === 'undefined' ? 'undefined' : (0, _typeof3.default)(value)) === 'object') {
|
276 | if (value.hasOwnProperty('value')) {
|
277 | item = value;
|
278 | } else {
|
279 | item = (0, _extends3.default)({
|
280 | value: ''
|
281 | }, value);
|
282 | }
|
283 | } else {
|
284 | item = valueMap['' + value] || {
|
285 | value: value,
|
286 | label: value
|
287 | };
|
288 | }
|
289 |
|
290 | return item;
|
291 | }
|
292 |
|
293 |
|
294 |
|
295 |
|
296 |
|
297 |
|
298 |
|
299 |
|
300 | function getValueDataSource(value, mapValueDS, mapMenuDS) {
|
301 | if (isNull(value)) {
|
302 | return {};
|
303 | }
|
304 |
|
305 | var newValue = [];
|
306 | var newValueDS = [];
|
307 | var newMapValueDS = {};
|
308 | var _newMapDS = (0, _extends3.default)({}, mapValueDS, mapMenuDS);
|
309 |
|
310 | if (Array.isArray(value)) {
|
311 | value.forEach(function (v) {
|
312 | var item = getKeyItemByValue(v, _newMapDS);
|
313 |
|
314 | newValueDS.push(item);
|
315 | newMapValueDS['' + item.value] = item;
|
316 | newValue.push(item.value);
|
317 | });
|
318 |
|
319 | return {
|
320 | value: newValue,
|
321 | valueDS: newValueDS,
|
322 | mapValueDS: newMapValueDS
|
323 | };
|
324 | } else {
|
325 | var _mapValueDS;
|
326 |
|
327 | var item = getKeyItemByValue(value, _newMapDS);
|
328 |
|
329 | return {
|
330 | value: item.value,
|
331 | valueDS: item,
|
332 | mapValueDS: (_mapValueDS = {}, _mapValueDS['' + item.value] = item, _mapValueDS)
|
333 | };
|
334 | }
|
335 | }
|
336 |
|
337 |
|
338 |
|
339 |
|
340 |
|
341 |
|
342 |
|
343 | function valueToSelectKey(value) {
|
344 | var val = void 0;
|
345 | if ((typeof value === 'undefined' ? 'undefined' : (0, _typeof3.default)(value)) === 'object' && value.hasOwnProperty('value')) {
|
346 | val = value.value;
|
347 | } else {
|
348 | val = value;
|
349 | }
|
350 | return '' + val;
|
351 | }
|
352 |
|
353 |
|
354 |
|
355 |
|
356 |
|
357 |
|
358 |
|
359 |
|
360 |
|
361 |
|
362 |
|
363 |
|
364 |
|
365 |
|
366 |
|
367 |
|
368 |
|
369 |
|
370 |
|
371 |
|
372 |
|
373 |
|
374 |
|
375 |
|
376 |
|
377 |
|
378 |
|
379 |
|
380 |
|
381 |
|
382 |
|
383 |
|
384 |
|
385 |
|
386 |
|
387 |
|
388 |
|
389 |
|
390 |
|
391 |
|
392 |
|
393 |
|
394 |
|
395 |
|
\ | No newline at end of file |