@@ -266,42 +266,6 @@ _.mixin({
266
266
}
267
267
} ) ;
268
268
269
- // adapted from http://stackoverflow.com/a/22072374/649299
270
- Utils . unflatten = function ( array , idProperty , parentIdProperty , parent , tree ) {
271
-
272
- tree = typeof tree !== "undefined" ? tree : [ ] ;
273
-
274
- let children = [ ] ;
275
-
276
- if ( typeof parent === "undefined" ) {
277
- // if there is no parent, we're at the root level
278
- // so we return all root nodes (i.e. nodes with no parent)
279
- children = _ . filter ( array , node => ! node [ parentIdProperty ] ) ;
280
- } else {
281
- // if there *is* a parent, we return all its child nodes
282
- // (i.e. nodes whose parentId is equal to the parent's id.)
283
- children = _ . filter ( array , node => node [ parentIdProperty ] === parent [ idProperty ] ) ;
284
- }
285
-
286
- // if we found children, we keep on iterating
287
- if ( ! ! children . length ) {
288
-
289
- if ( typeof parent === "undefined" ) {
290
- // if we're at the root, then the tree consist of all root nodes
291
- tree = children ;
292
- } else {
293
- // else, we add the children to the parent as the "childrenResults" property
294
- parent . childrenResults = children ;
295
- }
296
-
297
- // we call the function on each child
298
- children . forEach ( child => {
299
- Utils . unflatten ( array , idProperty , parentIdProperty , child ) ;
300
- } ) ;
301
- }
302
-
303
- return tree ;
304
- }
305
269
306
270
Utils . getFieldLabel = ( fieldName , collection ) => {
307
271
const label = collection . simpleSchema ( ) . _schema [ fieldName ] . label ;
@@ -344,7 +308,9 @@ Utils.findIndex = (array, predicate) => {
344
308
}
345
309
346
310
// adapted from http://stackoverflow.com/a/22072374/649299
347
- Utils . unflatten = function ( array , idProperty , parentIdProperty , parent , tree ) {
311
+ Utils . unflatten = function ( array , idProperty , parentIdProperty , parent , level = 0 , tree ) {
312
+
313
+ level ++ ;
348
314
349
315
tree = typeof tree !== "undefined" ? tree : [ ] ;
350
316
@@ -353,11 +319,11 @@ Utils.unflatten = function( array, idProperty, parentIdProperty, parent, tree ){
353
319
if ( typeof parent === "undefined" ) {
354
320
// if there is no parent, we're at the root level
355
321
// so we return all root nodes (i.e. nodes with no parent)
356
- children = _ . filter ( array , node => ! node [ parentIdProperty ] ) ;
322
+ children = _ . filter ( array , node => ! node [ parentIdProperty ] ) ;
357
323
} else {
358
324
// if there *is* a parent, we return all its child nodes
359
325
// (i.e. nodes whose parentId is equal to the parent's id.)
360
- children = _ . filter ( array , node => node [ parentIdProperty ] === parent [ idProperty ] ) ;
326
+ children = _ . filter ( array , node => node [ parentIdProperty ] === parent [ idProperty ] ) ;
361
327
}
362
328
363
329
// if we found children, we keep on iterating
@@ -373,7 +339,8 @@ Utils.unflatten = function( array, idProperty, parentIdProperty, parent, tree ){
373
339
374
340
// we call the function on each child
375
341
children . forEach ( child => {
376
- Utils . unflatten ( array , idProperty , parentIdProperty , child ) ;
342
+ child . level = level ;
343
+ Utils . unflatten ( array , idProperty , parentIdProperty , child , level ) ;
377
344
} ) ;
378
345
}
379
346
0 commit comments