-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathTree.js
More file actions
946 lines (854 loc) · 28.9 KB
/
Tree.js
File metadata and controls
946 lines (854 loc) · 28.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
import { isObject, cloneObject, isEmptyObject } from '../common'
/**
* Creates tree structure
* @class Tree
*
* @param {Tree} source A source tree structure to clone properties from
* @returns {Tree} Returns new tree structure
*/
export default function Tree(source) {
var _nodes = {}, // objects attached to nodes
_parents = {}, // parent node id for every node id. Both of them should exists in the tree.
_children = {}, // children node ids for every node id. All children and node itself should be in the tree.
_roots = {}, // id of non existing parent. If parent does not exists in the tree this hash contains its id.
_rootChildren = {}, // children of non existing parent. If parent id does not exists in the tree this collection contains it existing children.
/** @constant
@type {number}
@default
*/
BREAK = 1,
/** @constant
@type {number}
@default
*/
SKIP = 2;
_init(source);
function _init(source) {
if (isObject(source)) {
_nodes = cloneObject(source.nodes, true);
_parents = cloneObject(source.parents, true);
_children = cloneObject(source.children, false);
_roots = cloneObject(source.roots, false);
_rootChildren = cloneObject(source.rootChildren, true);
}
}
/**
* Callback for iterating tree nodes
*
* @callback onTreeItemCallback
* @param {string} itemid The node id
* @param {object} item The node
* @returns {boolean} Returns true to break the loop
*/
/**
* Loops through nodes of tree structure
*
* @param {Object} thisArg The callback function invocation context
* @param {onTreeItemCallback} onItem Callback function to call for every tree node
*/
function loop(thisArg, onItem) {
var item;
if (onItem != null) {
for (item in _nodes) {
if (_nodes.hasOwnProperty(item)) {
if (onItem.call(thisArg, item, _nodes[item])) {
break;
}
}
}
}
}
/**
* Callback for iterating the tree nodes level by level
*
* @callback onTreeItemWithLevelCallback
* @param {string} nodeid The node id
* @param {object} node The node context object
* @param {number} levelIndex The node level index
* @returns {number} Returns BREAK to break the loop and exit. Returns SKIP to skip node's branch traversing.
*/
/**
* Loops through child nodes of the tree structure level by level
*
* @param {Object} thisArg The callback function invocation context
* @param {string} arg0 The node id to start children traversing
* @param {onTreeItemWithLevelCallback} arg1 Callback function to call for every child node
*/
function loopLevels(thisArg, arg0, arg1) {
var levelIndex = 0,
items = [],
itemid,
onItem,
newItems,
key,
index, len;
switch (arguments.length) {
case 2:
onItem = arg0;
break;
case 3:
itemid = arg0;
onItem = arg1;
break;
}
if (onItem != null) {
if (itemid == null) {
for (key in _rootChildren) {
if (_rootChildren.hasOwnProperty(key)) {
items = items.concat(_rootChildren[key]);
}
}
} else {
if (_children[itemid] != null) {
items = items.concat(_children[itemid]);
}
}
while (items.length > 0) {
newItems = [];
for (index = 0, len = items.length; index < len; index += 1) {
itemid = items[index];
switch (onItem.call(thisArg, itemid, _nodes[itemid], levelIndex)) {
case BREAK:
newItems = [];
break;
case SKIP:
break;
default:
if (_children[itemid] != null) {
newItems = newItems.concat(_children[itemid]);
}
break;
}
}
items = newItems;
levelIndex += 1;
}
}
}
/**
* Callback for iterating nodes and providing parent in parameters
*
* @callback onTreeItemWithParentCallback
* @param {string} nodeid The node id
* @param {object} node The node context object
* @param {string} parentid The parent node id
* @param {object} parent The parent node context object
* @returns {number} Returns BREAK to break the loop and exit. Returns SKIP to skip node's branch traversing.
*/
/**
* Traverse tree structure in post order.
* Children first - parent last
* @param {Object} thisArg The callback function invocation context
* @param {onTreeItemWithParentCallback} onItem Callback function to call for every node
*/
function loopPostOrder(thisArg, onItem) {
var stack = [], nodeid,
key,
index,
prevParent,
children;
if (onItem != null) {
for (key in _rootChildren) {
if (_rootChildren.hasOwnProperty(key)) {
stack = stack.concat(_rootChildren[key]);
}
}
while (stack.length > 0) {
nodeid = stack[stack.length - 1];
if (nodeid != prevParent && (children = _children[nodeid]) != null) {
for (index = children.length - 1; index >= 0; index -= 1) {
stack.push(children[index]);
}
} else {
stack.pop();
prevParent = _parents[nodeid];
if (onItem.call(thisArg, nodeid, _nodes[nodeid], prevParent, _nodes[prevParent])) {
break;
}
}
}
}
}
/**
* Traverse tree structure in pre order.
* Parent first - children next
* @param {Object} thisArg The callback function invocation context
* @param {string} arg0 The node id to start traversing
* @param {onTreeItemWithParentCallback} arg1 A callback function to call for every node
*/
function loopPreOrder(thisArg, arg0, arg1) {
var stack = [], nodeid,
key,
index,
parentid,
prevParent,
children,
startNodeId,
onItem;
switch (arguments.length) {
case 2:
onItem = arg0;
break;
case 3:
startNodeId = arg0;
onItem = arg1;
break;
}
if (onItem != null) {
if(!node(startNodeId)) {
for (key in _rootChildren) {
if (_rootChildren.hasOwnProperty(key)) {
stack = stack.concat(_rootChildren[key]);
}
}
} else {
stack.push(startNodeId);
}
while (stack.length > 0) {
nodeid = stack[stack.length - 1];
if (nodeid != prevParent) {
parentid = _parents[nodeid];
if (onItem.call(thisArg, nodeid, _nodes[nodeid], parentid, _nodes[parentid])) {
break;
}
}
if (nodeid != prevParent && (children = _children[nodeid]) != null) {
for (index = children.length - 1; index >= 0; index -= 1) {
stack.push(children[index]);
}
} else {
stack.pop();
prevParent = _parents[nodeid];
}
}
}
}
/**
* Callback for iterating nodes in euler walk order
*
* @callback onItemEulerWalkCallback
* @param {string} nodeid The node id
* @param {object} node Context object of the node
* @param {number} level The node's level
* @returns {boolean} Returns true to break the iteration of nodes and exit.
*/
/**
* Loops tree nodes in "Euler Walk" order
*
* @param {Object} thisArg The callback function invocation context
* @param {onItemEulerWalkCallback} onItem Callback function to call for every node
*/
function loopEulerWalk(thisArg, onItem) {
var stack = [],
nodeid,
levels = [],
level = 0,
key,
index, len,
prevParent,
children;
if (onItem != null) {
for (key in _rootChildren) {
if (_rootChildren.hasOwnProperty(key)) {
children = _rootChildren[key];
for (index = 0, len = children.length; index < len; index += 1) {
stack.push(children[index]);
levels.push(0);
}
}
}
while (stack.length > 0) {
index = stack.length - 1;
nodeid = stack[index];
level = levels[index];
if (onItem.call(thisArg, nodeid, _nodes[nodeid], level)) {
break;
}
if (nodeid != prevParent && (children = _children[nodeid]) != null) {
for (index = children.length - 1; index >= 0; index -= 1) {
stack.push(children[index]);
levels.push(level + 1);
if (index > 0) {
stack.push(nodeid);
levels.push(level);
}
}
} else {
stack.pop();
levels.pop();
prevParent = _parents[nodeid];
}
}
}
}
/**
* Callback function to return pairs of nodes
*
* @callback onZipUpPairCallback
* @param {string} firstNodeId First node id
* @param {string} firstParentId Parent id of the first node
* @param {string} secondNodeid Second node id
* @param {string} secondParentId Parent id of the second node
* @returns {boolean} Returns true to break the iteration of nodes and exit.
*/
/**
* Iterates hierarchy nodes by pairs starting with given pair of start and second nodes and up to the root of the hierarchy.
* Breaks iteration when callback function returns true.
*
* @param {Object} thisArg The callback function invocation context
* @param {string} firstNodeId The first node to start iteration
* @param {string} secondNodeid The second node to start iteration
* @param {onZipUpPairCallback} onZip Callback function to call for every pair of nodes on the way up in the tree structure
*/
function zipUp(thisArg, firstNodeId, secondNodeid, onZip) {
var firstParentId,
secondParentId;
if (onZip != null) {
while (firstNodeId != null && secondNodeid != null && firstNodeId != secondNodeid) {
firstParentId = _parents[firstNodeId];
secondParentId = _parents[secondNodeid];
if (onZip.call(thisArg, firstNodeId, firstParentId, secondNodeid, secondParentId)) {
break;
}
firstNodeId = firstParentId;
secondNodeid = secondParentId;
}
}
}
/**
* Loops parents up to the root of the hierarchy starting with the given node.
* Breaks iteration if callback function returns true.
*
* @param {Object} thisArg The callback function invocation context
* @param {string} nodeid The node id to start iteration from
* @param {onTreeItemCallback} onItem Callback function to call for every parent node
* @param {boolean} includingStartItem If true the first call to callback function is made with start node id
*/
function loopParents(thisArg, nodeid, onItem, includingStartItem) { // onItem(nodeid, node)
var parentid = nodeid;
if (_nodes[parentid] != null) {
if (onItem != null) {
if (includingStartItem === true) {
if (onItem.call(thisArg, parentid, _nodes[parentid])) {
return;
}
}
while ((parentid = _parents[parentid]) != null) {
if (onItem.call(thisArg, parentid, _nodes[parentid])) {
break;
}
}
}
}
}
/**
* Callback function to loop through children of the given node
*
* @callback onTreeChildItemCallback
* @param {string} nodeid Child node id
* @param {object} node Context object of the child node
* @param {number} index Index of the child node
* @param {number} lastIndex Index of the last child
* @returns {boolean} Returns true to break the iteration of nodes and exit.
*/
/**
* Loops immediate children of the given node.
* Breaks iteration if callback function returns true.
*
* @param {Object} thisArg The callback function invocation context
* @param {string} nodeid The parent node id to loop children of
* @param {onTreeChildItemCallback} onItem Callback function to call for every child node
*/
function loopChildren(thisArg, nodeid, onItem) { // onItem(nodeid, node, index, lastIndex)
var items,
itemid,
index, len;
if (_nodes[nodeid] != null) {
items = _children[nodeid];
if (items != null) {
for (index = 0, len = items.length; index < len; index += 1) {
itemid = items[index];
if (onItem.call(thisArg, itemid, _nodes[itemid], index, len - 1)) {
break;
}
}
}
}
}
/**
* Callback function to loop through range of children for the given node
*
* @callback onTreeNodeWithIndexItemCallback
* @param {string} nodeid Child node id
* @param {object} node Context object of the child node
* @param {number} index Index of the child node
* @returns {boolean} Returns true to break the iteration of nodes and exit.
*/
/**
* Loops range of immediate children of the given node.
* Breaks iteration if callback function returns true.
*
* @param {Object} thisArg The callback function invocation context
* @param {string} nodeid The parent node id to loop children of
* @param {number} fromIndex Start index of iteration
* @param {number} toIndex End index of iteration
* @param {onTreeNodeWithIndexItemCallback} onItem Callback function to call for every child node
*/
function loopChildrenRange(thisArg, nodeid, fromIndex, toIndex, onItem) {
var items,
itemid,
index, len;
if (_nodes[nodeid] != null) {
items = _children[nodeid];
if (items != null) {
if (fromIndex < toIndex) {
fromIndex = Math.max(fromIndex, 0);
toIndex = Math.min(toIndex, items.length - 1);
for (index = fromIndex; index <= toIndex; index += 1) {
itemid = items[index];
if (onItem.call(thisArg, itemid, _nodes[itemid], index, len - 1)) {
break;
}
}
} else {
fromIndex = Math.min(fromIndex, items.length - 1);
toIndex = Math.max(0, toIndex);
for (index = fromIndex; index >= toIndex; index -= 1) {
itemid = items[index];
if (onItem.call(thisArg, itemid, _nodes[itemid], index, len - 1)) {
break;
}
}
}
}
}
}
/**
* Loops immediate children of the given node in reversed order.
* Breaks iteration if callback function returns true.
*
* @param {Object} thisArg The callback function invocation context
* @param {string} nodeid The parent node id to loop children of
* @param {onTreeChildItemCallback} onItem Callback function to call for every child node
*/
function loopChildrenReversed(thisArg, nodeid, onItem) {
var items,
itemid,
index, lastIndex;
if (_nodes[nodeid] != null) {
items = _children[nodeid];
lastIndex = items.length - 1;
if (items != null) {
for (index = lastIndex; index >= 0; index -= 1) {
itemid = items[index];
if (onItem.call(thisArg, itemid, _nodes[itemid], index, lastIndex)) {
break;
}
}
}
}
}
/**
* Orders children of the given node
*
* @param {string} nodeid The node id of the parent node which children should be ordered in the tree structure
* @param {string[]} children Collection of ordered children
*/
function arrangeChildren(nodeid, children) {
var childid,
index, len;
children = children.slice(0);
if (_nodes[nodeid] != null) {
if (_children[nodeid] != null) {
if (_children[nodeid].length == children.length) {
for (index = 0, len = children.length; index < len; index += 1) {
childid = children[index];
if (_parents[childid] != nodeid) {
throw "Child " + childid + " does not belong to given node!";
}
}
_children[nodeid] = children;
} else {
throw "Collections of children don't match each other!";
}
} else {
if (children.length > 0) {
throw "Collections of children don't match each other!";
}
}
}
}
/**
* Adds new tree item
* @param {string} parentid Parent id
* @param {string} nodeid New node id
* @param {object} node Context object of the new node
* @param {number} position Position of the new node in the collection of children
*/
function add(parentid, nodeid, node, position) {
var index, len, children, childid;
if (_nodes[nodeid] != null) {
throw "Node already exists";
}
if (nodeid != null && node != null && _nodes[nodeid] == null) {
if (_nodes[parentid] != null) {
_parents[nodeid] = parentid;
// existing parent
if (_children[parentid] != null) {
if (position == null) {
_children[parentid].push(nodeid);
} else {
_children[parentid].splice(position, 0, nodeid);
}
} else {
_children[parentid] = [nodeid];
}
} else {
_roots[nodeid] = parentid;
// missing parent
if (_rootChildren[parentid] != null) {
if (position == null) {
_rootChildren[parentid].push(nodeid);
} else {
_rootChildren[parentid].splice(position, 0, nodeid);
}
} else {
_rootChildren[parentid] = [nodeid];
}
}
_nodes[nodeid] = node;
if (_rootChildren[nodeid] != null) {
_children[nodeid] = _rootChildren[nodeid];
delete _rootChildren[nodeid];
children = _children[nodeid];
for (index = 0, len = children.length; index < len; index += 1) {
childid = children[index];
delete _roots[childid];
_parents[childid] = nodeid;
}
}
}
}
/**
* Inserts bundle node into the tree structure. The new bundle node becomes only child node of the parent node.
* All immediate children of the parent node become children of the inserted bundle node.
*
* @param {string} nodeid Parent node id
* @param {string} bundleid New bundle node id
* @param {object} bundle Context object of the bundle node
*/
function insert(nodeid, bundleid, bundle) {
if (_nodes[nodeid] != null && bundleid != null && _nodes[bundleid] == null && bundle != null) {
_nodes[bundleid] = bundle;
if (_children[nodeid] != null) {
_children[bundleid] = _children[nodeid];
}
_children[nodeid] = [bundleid];
loopChildren(this, bundleid, function (childid, node, index) {
_parents[childid] = bundleid;
});
_parents[bundleid] = nodeid;
}
}
/**
* Moves children form one node to another.
*
* @param {string} fromNodeid Source node node id
* @param {string} toNodeId Destination node id
*/
function moveChildren(fromNodeid, toNodeId) {
if (_nodes[fromNodeid] != null && _nodes[toNodeId] != null && fromNodeid != toNodeId) {
if (_children[fromNodeid] != null) {
loopChildren(this, fromNodeid, function (childid, node, index) {
_parents[childid] = toNodeId;
});
if (_children[toNodeId] != null) {
_children[toNodeId] = _children[toNodeId].concat(_children[fromNodeid]);
} else {
_children[toNodeId] = _children[fromNodeid];
}
delete _children[fromNodeid];
}
}
}
/**
* Return true if structure has nodes
*
* @returns {boolean} Returns true if structure has nodes
*/
function hasNodes() {
return !isEmptyObject(_rootChildren);
}
/**
* Returns parent node id
*
* @param {string} nodeid Node id
* @returns {string} Returns parent node id
*/
function parentid(nodeid) {
var result = null;
if (_parents[nodeid] != null) {
result = _parents[nodeid];
}
return result;
}
/**
* Returns context object of the parent node
*
* @param {string} nodeid Node id
* @returns {object} Returns context object of the parent node
*/
function parent(nodeid) {
var result = null;
if (_parents[nodeid] != null) {
result = _nodes[_parents[nodeid]];
}
return result;
}
/**
* Returns true if node has children
*
* @param {string} nodeid Node id
* @returns {boolean} Returns true if node has children
*/
function hasChildren(nodeid) {
return _children[nodeid] != null;
}
/**
* Returns number of children
*
* @param {string} nodeid Node id
* @returns {number} Returns number of child nodes
*/
function countChildren(nodeid) {
return _children[nodeid] != null ? _children[nodeid].length : 0;
}
/**
* Returns number of siblings
*
* @param {string} nodeid Node id
* @returns {number} Returns number of siblings
*/
function countSiblings(nodeid) {
var parent = parentid(nodeid);
return parent != null ? _children[parent].length : 0;
}
/**
* Returns index of the node in the children's collection
*
* @param {string} nodeid Node id
* @returns {number} Returns node index
*/
function indexOf(nodeid) {
var parent = parentid(nodeid);
return parent != null ? _children[parent].findIndex( itemid => itemid === nodeid) : null;
}
/**
* Returns child node by index in the children's collection
*
* @param {string} nodeid Node id
* @param {number} index Child index
* @returns {object} Returns child node
*/
function getChild(parentid, index) {
var result = null,
children;
if ((children = _children[parentid]) != null) {
result = _nodes[children[index]];
}
return result;
}
function _splice(collection, nodeid) {
var index, len = collection.length;
for (index = 0; index < len; index += 1) {
if (collection[index] == nodeid) {
collection.splice(index, 1);
return len - 1;
}
}
return len;
}
/**
* Adds existing node to the children of the parent node
*
* @param {string} parentid Parent Node id
* @param {string} nodeid Node id
*/
function adopt(parentid, nodeid) {
if (_nodes[parentid] != null && _nodes[nodeid] != null) {
if (parentid != nodeid) {
if (_roots.hasOwnProperty(nodeid)) {
if (!_splice(_rootChildren[_roots[nodeid]], nodeid)) {
delete _rootChildren[_roots[nodeid]];
}
delete _roots[nodeid];
}
if (_parents.hasOwnProperty(nodeid)) {
if (!_splice(_children[_parents[nodeid]], nodeid)) {
delete _children[_parents[nodeid]];
}
}
_parents[nodeid] = parentid;
if (_children[parentid] != null) {
_children[parentid].push(nodeid);
} else {
_children[parentid] = [nodeid];
}
}
else {
throw "Item cannot be parent of itself!";
}
} else {
throw "Both parent and child should be in hierarchy!";
}
}
/**
* Returns context object
* @param {string} nodeid Node id
* @returns {object} Context object of the node
*/
function node(nodeid) {
return _nodes[nodeid];
}
/**
* Validates internal data integrity of the structure
*
* @returns {boolean} Returns true if structure pass validation
*/
function validate() {
var result = true,
key;
for (key in _roots) {
if (_roots.hasOwnProperty(key)) {
if (_roots[key] != null) {
result = false;
break;
}
}
}
return result;
}
/**
* Clones tree structure
*
* @returns {tree} Returns clone of the tree
*/
function clone() {
return Tree({
nodes: _nodes,
parents: _parents,
children: _children,
roots: _roots,
rootChildren: _rootChildren
});
}
/**
* Callback for iterating tree node neighbours level by level
*
* @callback onTreeItemNeighbourCallback
* @param {string} itemid The node id
* @param {object} item The node
* @param {number} distance The neigbour node distance from the start node
* @returns {number} Returns true to skip further neighbous traversing.
*/
/**
* Loops through the node neighbours of the tree structure level by level
*
* @param {Object} thisArg The callback function invocation context
* @param {string} itemid The node id to start traversing neighbour nodes
* @param {number} distance Stop iteration of neighbours when distance exceeds the given value
* @param {onTreeItemNeighbourCallback} onItem A callback function to call for every neighbour node
*/
function loopNeighbours(thisArg, itemid, distance, onItem) {
var processed = {},
margin = [itemid],
newMargin,
currentDistance = 0;
if (onItem != null) {
if (_nodes.hasOwnProperty(itemid)) {
processed[itemid] = true;
while (margin.length > 0) {
newMargin = [];
for (var index = 0, len = margin.length; index < len; index += 1) {
var marginid = margin[index];
if (currentDistance > 0) {
if (onItem.call(thisArg, marginid, _nodes[marginid], currentDistance)) {
return;
}
}
if (currentDistance < distance) {
_loopNeighbours(this, marginid, function (neighbourid, neighbour) {
if (!processed.hasOwnProperty(neighbourid)) {
newMargin.push(neighbourid);
processed[neighbourid] = true;
}
});
}
}
margin = newMargin;
currentDistance += 1;
}
}
}
}
function _loopNeighbours(thisArg, itemid, onItem) {
if (onItem != null) {
if (_nodes.hasOwnProperty(itemid)) {
/* loop parent */
var parentItemId = parentid(itemid);
if (parentItemId != null) {
if (onItem.call(thisArg, parentItemId, _nodes[parentItemId])) {
return;
}
}
/* loop siblings */
loopChildren(thisArg, parentItemId, function (childItemId, childItem) {
if (childItemId != itemid) {
if (onItem.call(thisArg, childItemId, childItem)) {
return;
}
}
});
/* loop actual children */
loopChildren(thisArg, itemid, function (childItemId, childItem) {
if (onItem.call(thisArg, childItemId, childItem)) {
return;
}
});
}
}
}
return {
loop: loop,
loopLevels: loopLevels,
loopParents: loopParents,
loopChildren: loopChildren,
loopChildrenRange: loopChildrenRange,
loopChildrenReversed: loopChildrenReversed,
loopPostOrder: loopPostOrder, /* children first - parent last */
loopPreOrder: loopPreOrder, /* parent first - children next */
loopEulerWalk: loopEulerWalk, /* pre order loop with every parent revisited for every child */
loopNeighbours: loopNeighbours, /* loop items by distance. Siblings are as far as parent and children */
zipUp: zipUp,
parentid: parentid,
parent: parent,
adopt: adopt,
moveChildren: moveChildren,
node: node,
add: add,
insert: insert,
hasNodes: hasNodes,
hasChildren: hasChildren,
countChildren: countChildren,
countSiblings: countSiblings,
indexOf: indexOf,
getChild: getChild,
arrangeChildren: arrangeChildren,
/* force validation */
validate: validate,
clone: clone,
// callback return codes
BREAK: BREAK, // break loop immidiatly
SKIP: SKIP // skip loop of current node children
};
};