-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathFamily.js
More file actions
1916 lines (1704 loc) · 60.4 KB
/
Copy pathFamily.js
File metadata and controls
1916 lines (1704 loc) · 60.4 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
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { isObject, cloneObject, isEmptyObject } from '../common';
import Graph from './Graph';
import LinkedHashItems from './LinkedHashItems';
/**
* Family node
* @class FamilyNode
* @property {string} id Id
* @property {object} node Node
*/
function FamilyNode(id, node) {
this.id = id;
this.node = node;
}
/**
* Creates a family object
*
* @class Family
*
* @param {Family} [source=undefined] Reference to optional family object to clone properties from
*
* @returns {Family} Returns new instance of family structure
*/
export default function Family(source) {
var _roots = {}, // children hash of orphant parent id
_rootsCount = {},
_children = {}, // children hash by node id
_childrenCount = {},
_parents = {}, // parents hash by node id
_parentsCount = {},
_nodes = {}, // nodes by node id
/** @constant
@type {number}
@default
*/
BREAK = 1,
/** @constant
@type {number}
@default
*/
SKIP = 2;
_init(source);
function _init(source) {
if (isObject(source)) {
_roots = cloneObject(source.roots, false);
_rootsCount = cloneObject(source.rootsCount, true);
_children = cloneObject(source.children, false);
_childrenCount = cloneObject(source.childrenCount, true);
_parents = cloneObject(source.parents, false);
_parentsCount = cloneObject(source.parentsCount, true);
_nodes = cloneObject(source.nodes, true);
}
}
function _loop(thisArg, collection, itemid, onItem) {
var item, items;
if (onItem != null) {
items = collection[itemid];
if (items != null) {
for (item in items) {
if (items.hasOwnProperty(item)) {
if (onItem.call(thisArg, item, items[item])) {
break;
}
}
}
}
}
}
/**
* Adds new family member
* @param {string[]} parents A collection of parents ids
* @param {string} nodeid An id of the new node
* @param {object} node A reference to the new node
*/
function add(parents, nodeid, node) {
var index, len,
parentid,
processed = {};
if (!parents || parents.length === 0) {
parents = [null];
}
if (_nodes[nodeid] == null && node != null) {
_nodes[nodeid] = node;
for (index = 0, len = parents.length; index < len; index += 1) {
parentid = parents[index];
if (processed[parentid] == null && parentid != nodeid) {
processed[parentid] = true;
if (_nodes[parentid] != null) {
if (_parents[nodeid] == null) {
_parents[nodeid] = {};
_parentsCount[nodeid] = 0;
}
if (!_parents[nodeid][parentid]) {
_parents[nodeid][parentid] = true;
_parentsCount[nodeid] += 1;
}
if (_children[parentid] == null) {
_children[parentid] = {};
_childrenCount[parentid] = 0;
}
if (!_children[parentid][nodeid]) {
_children[parentid][nodeid] = true;
_childrenCount[parentid] += 1;
}
} else {
if (_roots[parentid] == null) {
_roots[parentid] = {};
_rootsCount[parentid] = 0;
}
if (!_roots[parentid][nodeid]) {
_roots[parentid][nodeid] = true;
_rootsCount[parentid] += 1;
}
}
}
}
if (_roots[nodeid] != null) {
_children[nodeid] = _roots[nodeid];
_childrenCount[nodeid] = _rootsCount[nodeid];
delete _roots[nodeid];
delete _rootsCount[nodeid];
_loop(this, _children, nodeid, function (itemid) {
if (_parents[itemid] == null) {
_parents[itemid] = {};
_parentsCount[itemid] = 0;
}
if (!_parents[itemid][nodeid]) {
_parents[itemid][nodeid] = true;
_parentsCount[itemid] += 1;
}
});
}
}
}
/**
* Returns family node by id
* @param {string} nodeid The id of the node
* @returns {object|undefined} A reference to the node or undefined if id does not exists
*/
function node(nodeid) {
return _nodes[nodeid];
}
/**
* Makes node to be a child of every parent in the collection of parents
* @param {string[]} parents A collection of parents ids
* @param {string} nodeid An id of the new node
*/
function adopt(parents, nodeid) {
var index, len,
parentid;
if (_nodes[nodeid] != null) {
for (index = 0, len = parents.length; index < len; index += 1) {
parentid = parents[index];
if (_parents[nodeid] == null) {
_parents[nodeid] = {};
_parentsCount[nodeid] = 0;
}
if (parentid != nodeid && _nodes[parentid] != null) {
if (!_parents[nodeid][parentid]) {
_parents[nodeid][parentid] = true;
_parentsCount[nodeid] += 1;
}
if (_children[parentid] == null) {
_children[parentid] = {};
_childrenCount[parentid] = 0;
}
if (!_children[parentid][nodeid]) {
_children[parentid][nodeid] = true;
_childrenCount[parentid] += 1;
}
} else {
throw "Item cannot be parent of itself and parent should exist in the structure!";
}
}
} else {
throw "Child should be in hierarchy!";
}
}
/**
* Removes node
* @param {string} nodeid The id of the node
*/
function removeNode(nodeid) {
if (_nodes[nodeid] != null) {
_loop(this, _children, nodeid, function (itemid) {
delete _parents[itemid][nodeid];
_parentsCount[itemid] -= 1;
if (!_parentsCount[itemid]) {
delete _parents[itemid];
delete _parentsCount[itemid];
if (_roots[null] == null) {
_roots[null] = {};
_rootsCount[null] = 0;
}
if (!_roots[null][itemid]) {
_roots[null][itemid] = true;
_rootsCount[null] += 1;
}
}
});
_loop(this, _parents, nodeid, function (itemid) {
delete _children[itemid][nodeid];
_childrenCount[itemid] -= 1;
if (!_childrenCount[itemid]) {
delete _children[itemid];
delete _childrenCount[itemid];
}
});
if (_roots[null] != null && _roots[null][nodeid] != null) {
delete _roots[null][nodeid];
_rootsCount[null] -= 1;
if (!_rootsCount[null]) {
delete _roots[null];
delete _rootsCount[null];
}
}
delete _children[nodeid];
delete _childrenCount[nodeid];
delete _parents[nodeid];
delete _parentsCount[nodeid];
delete _nodes[nodeid];
}
}
function _removeChildReference(parentid, childid) {
var result = false;
if (_children[parentid] != null && _children[parentid][childid] != null) {
delete _children[parentid][childid];
_childrenCount[parentid] -= 1;
delete _parents[childid][parentid];
_parentsCount[childid] -= 1;
if (!_childrenCount[parentid]) {
delete _children[parentid];
delete _childrenCount[parentid];
}
if (!_parents[childid]) {
delete _parents[childid];
delete _parentsCount[childid];
if (_roots[null] == null) {
_roots[null] = {};
_rootsCount[null] = 0;
}
_roots[null][childid] = true;
_rootsCount[null] += 1;
}
result = true;
}
return result;
}
/**
* Removes first available parent child or child parent relation
*
* @param {string} fromid From node id
* @param {string} toid To node id
* @returns {true} If relation was broken
*/
function removeRelation(fromid, toid) {
var result = false;
if (_nodes[fromid] != null && _nodes[toid] != null) {
result = _removeChildReference(fromid, toid) || _removeChildReference(toid, fromid);
}
return result;
}
/**
* Removes child relation
*
* @param {string} parentid The parent node id
* @param {string} childid The child node id
* @returns {true} If relation was broken
*/
function removeChildRelation(parentid, childid) {
var result = false;
if (_nodes[parentid] != null && _nodes[childid] != null) {
result = _removeChildReference(parentid, childid);
}
return result;
}
/**
* Returns true if structure has nodes.
*
* @returns {boolean} Returns true if family structure has nodes
*/
function hasNodes() {
return !isEmptyObject(_nodes);
}
/**
* Callback for iterating family nodes
*
* @callback onFamilyItemCallback
* @param {string} itemid The node id
* @param {object} item The node
* @returns {boolean} Returns true to break the loop
*/
/**
* Loops through nodes of family structure
*
* @param {Object} thisArg The callback function invocation context
* @param {onFamilyItemCallback} onItem A callback function to call for every family 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;
}
}
}
}
}
function _loopItems(thisArg, collection, items, onItem) { // onItem(itemid, item, levelIndex)
var newItems, itemid,
processed = {},
levelIndex = 0,
hasItems = true;
while (hasItems) {
newItems = {};
hasItems = false;
for (itemid in items) {
if (items.hasOwnProperty(itemid)) {
if (!processed[itemid]) {
processed[itemid] = true;
switch (onItem.call(thisArg, itemid, _nodes[itemid], levelIndex)) {
case 1/*BREAK*/:
newItems = {};
hasItems = false;
break;
case 2/*SKIP*/:
break;
default:
_loop(this, collection, itemid, function (newItemId) {
if (!processed[newItemId]) {
newItems[newItemId] = true;
hasItems = true;
}
}); //ignore jslint
break;
}
}
}
}
items = newItems;
levelIndex += 1;
}
}
/**
* Callback for iterating family nodes level by level
*
* @callback onFamilyItemWithLevelCallback
* @param {string} itemid The node id
* @param {object} item The node
* @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 family structure level by level
*
* @param {Object} thisArg The callback function invocation context
* @param {string} nodeid The node id to start children traversing
* @param {onFamilyItemWithLevelCallback} onItem A callback function to call for every child node
*/
function loopChildren(thisArg, nodeid, onItem) {
if (onItem != null) {
if (nodeid != null && _nodes[nodeid] != null && _children[nodeid] != null) {
_loopItems(thisArg, _children, _children[nodeid], onItem);
}
}
}
/**
* Loops through parent nodes of family structure level by level
*
* @param {Object} thisArg The callback function invocation context
* @param {string} nodeid The node id to start parents traversing
* @param {onFamilyItemWithLevelCallback} onItem A callback function to call for every parent node
*/
function loopParents(thisArg, nodeid, onItem) {
if (onItem != null) {
if (nodeid != null && _nodes[nodeid] != null && _parents[nodeid] != null) {
_loopItems(thisArg, _parents, _parents[nodeid], onItem);
}
}
}
function _loopTopo(thisArg, backwardCol, backwardCount, forwardCol, forwardCount, onItem) { // onItem(itemid, item, position)
var index, len, nodeid, references,
queue, newQueue, position;
if (onItem != null) {
/* count parents for every node */
queue = [];
references = {};
for (nodeid in _nodes) {
if (_nodes.hasOwnProperty(nodeid)) {
references[nodeid] = (backwardCount[nodeid] || 0);
if (!references[nodeid]) {
queue.push(nodeid);
}
}
}
/* iterate queue and reduce reference counts via children */
position = 0;
while (queue.length > 0) {
newQueue = [];
for (index = 0, len = queue.length; index < len; index += 1) {
nodeid = queue[index];
if (onItem.call(thisArg, nodeid, _nodes[nodeid], position)) {
newQueue = [];
break;
}
position += 1;
_loop(this, forwardCol, nodeid, function (itemid) {
references[itemid] -= 1;
if (references[itemid] === 0) {
newQueue.push(itemid);
}
}); //ignore jslint
}
queue = newQueue;
}
}
}
/**
* Callback for iterating family nodes in topological sort order
*
* @callback onFamilyTopoCallback
* @param {string} itemid The node id
* @param {object} item The node
* @param {number} position The node position in the sequence
* @returns {boolean} Returns true to break the loop and exit.
*/
/**
* Loops through topologically sorted nodes of family structure
*
* @param {Object} thisArg The callback function invocation context
* @param {onFamilyTopoCallback} onItem A callback function to call for every node
*/
function loopTopo(thisArg, onItem) {
_loopTopo(thisArg, _parents, _parentsCount, _children, _childrenCount, onItem);
}
/**
* Loops through reversed order topologically sorted nodes of family structure
*
* @param {Object} thisArg The callback function invocation context
* @param {onFamilyTopoCallback} onItem A callback function to call for every node
*/
function loopTopoReversed(thisArg, onItem) {
_loopTopo(thisArg, _children, _childrenCount, _parents, _parentsCount, onItem);
}
/**
* Loops through nodes of family structure level by level. This function aligns nodes top or bottom.
*
* @param {Object} thisArg The callback function invocation context
* @param {boolean} parentAligned True if nodes should be placed at the next level after their parents level,
* otherwise nodes placed at levels close to their children.
* @param {onFamilyItemWithLevelCallback} onItem A callback function to call for every node
*/
function loopLevels(thisArg, parentAligned, onItem) {
var topoSorted = [],
topoSortedPositions = {},
processed = {},
margin = [],
/* result items distribution by levels */
levels = {}, levelIndex,
groups = {}, hasGroups, newGroups, groupIndex, group,
itemsAtLevel, itemid,
minimumLevel = null,
loopFunc = parentAligned ? loopTopo : loopTopoReversed,
index, len,
mIndex, mLen, mItem, mLevel,
topoSortedItem,
bestPosition, bestItem, bestLevel, bestIsParent,
newMargin, hasNeighbours;
function Group() {
this.items = {};
this.minimumLevel = null;
}
Group.prototype.addItemToLevel = function (itemid, level) {
var items = this.items[level];
if (!items) {
items = [itemid];
this.items[level] = items;
} else {
items.push(itemid);
}
this.minimumLevel = this.minimumLevel == null ? level : Math.min(this.minimumLevel, level);
};
function addItemToLevel(itemid, index, level) {
var group = groups[index];
if (!group) {
group = new Group();
groups[index] = group;
}
group.addItemToLevel(itemid, level);
minimumLevel = minimumLevel == null ? level : Math.min(minimumLevel, level);
levels[itemid] = level;
processed[itemid] = true;
}
if (onItem != null) {
/* sort items topologically */
loopFunc(this, function (itemid, item, position) {
topoSorted.push(itemid);
topoSortedPositions[itemid] = position;
});
/* search for the first available non processed item in topological order */
for (index = 0, len = topoSorted.length; index < len; index += 1) {
topoSortedItem = topoSorted[index];
if (processed[topoSortedItem] == null) {
margin.push(topoSortedItem);
addItemToLevel(topoSortedItem, index, 0);
/* use regular graph breadth first search */
while (margin.length > 0) {
bestPosition = null;
bestItem = null;
bestLevel = null;
bestIsParent = !parentAligned;
newMargin = [];
for (mIndex = 0, mLen = margin.length; mIndex < mLen; mIndex += 1) {
mItem = margin[mIndex];
mLevel = levels[mItem];
hasNeighbours = false;
if (parentAligned) {
_loop(this, _parents, mItem, function (parentid) {
var topoSortedPosition;
if (!processed[parentid]) {
hasNeighbours = true;
topoSortedPosition = topoSortedPositions[parentid];
if (bestPosition == null || !bestIsParent || bestPosition < topoSortedPosition || (bestPosition == topoSortedPosition && bestLevel > mLevel - 1)) {
bestPosition = topoSortedPosition;
bestItem = parentid;
bestLevel = mLevel - 1;
bestIsParent = true;
}
}
}); //ignore jslint
_loop(this, _children, mItem, function (childid) {
var topoSortedPosition;
if (!processed[childid]) {
hasNeighbours = true;
topoSortedPosition = topoSortedPositions[childid];
if (bestPosition == null || (!bestIsParent && (bestPosition > topoSortedPosition || (bestPosition == topoSortedPosition && bestLevel < mLevel + 1)))) {
bestPosition = topoSortedPosition;
bestItem = childid;
bestLevel = mLevel + 1;
bestIsParent = false;
}
}
}); //ignore jslint
} else {
_loop(this, _children, mItem, function (childid) {
var topoSortedPosition;
if (!processed[childid]) {
hasNeighbours = true;
topoSortedPosition = topoSortedPositions[childid];
if (bestPosition == null || bestIsParent || bestPosition < topoSortedPosition || (bestPosition == topoSortedPosition && bestLevel < mLevel + 1)) {
bestPosition = topoSortedPosition;
bestItem = childid;
bestLevel = mLevel + 1;
bestIsParent = false;
}
}
}); //ignore jslint
_loop(this, _parents, mItem, function (parentid) {
var topoSortedPosition;
if (!processed[parentid]) {
hasNeighbours = true;
topoSortedPosition = topoSortedPositions[parentid];
if (bestPosition == null || (bestIsParent && (bestPosition > topoSortedPosition || (bestPosition == topoSortedPosition && bestLevel > mLevel - 1)))) {
bestPosition = topoSortedPosition;
bestItem = parentid;
bestLevel = mLevel - 1;
bestIsParent = true;
}
}
}); //ignore jslint
}
if (hasNeighbours) {
newMargin.push(mItem);
}
}
if (bestItem != null) {
newMargin.push(bestItem);
addItemToLevel(bestItem, index, bestLevel);
}
margin = newMargin;
}
}
}
hasGroups = true;
levelIndex = minimumLevel;
while (hasGroups) {
newGroups = {};
hasGroups = false;
for (groupIndex in groups) {
if (groups.hasOwnProperty(groupIndex)) {
group = groups[groupIndex];
itemsAtLevel = group.items[(group.minimumLevel - minimumLevel) + levelIndex];
if (itemsAtLevel != null) {
newGroups[groupIndex] = group;
hasGroups = true;
for (index = 0, len = itemsAtLevel.length; index < len; index += 1) {
itemid = itemsAtLevel[index];
if (onItem.call(thisArg, itemid, _nodes[itemid], levelIndex - minimumLevel)) {
hasGroups = false;
return true;
}
}
}
}
}
groups = newGroups;
levelIndex += 1;
}
}
}
/**
* Loops root nodes of family structure.
* @param {Object} thisArg The callback function invocation context
* @param {onFamilyItemCallback} onItem A callback function to call for every family root node
*/
function loopRoots(thisArg, onItem) {
var result = null,
minimum, counter = 0,
famMembers = {},
famCount = {},
isRoot,
roots = {},
processed = {},
famItemId, member, members, rootid,
membersRoots, memberRoots, memberRoot,
index, len;
loopTopoReversed(this, function (famItemId, famItem, position) {
/* every node has at least itself in members */
if (!famMembers.hasOwnProperty(famItemId)) {
famMembers[famItemId] = {};
famCount[famItemId] = 0;
}
famMembers[famItemId][famItemId] = true;
famCount[famItemId] += 1;
isRoot = true;
loopParents(this, famItem.id, function (parentid, parent, levelIndex) {
var items, itemid;
isRoot = false;
if (!famMembers.hasOwnProperty(parentid)) {
famMembers[parentid] = {};
famCount[parentid] = 0;
}
/* push famItem members to parent members collection */
if (!famCount[parentid] && _parentsCount[famItemId] == 1) {
famMembers[parentid] = famMembers[famItemId];
famCount[parentid] = famCount[famItemId];
} else {
items = famMembers[famItemId];
for (itemid in items) {
if (items.hasOwnProperty(itemid)) {
if (!famMembers[parentid][itemid]) {
famMembers[parentid][itemid] = true;
famCount[parentid] += 1;
}
}
}
}
return SKIP;
});
if (isRoot) {
roots[famItemId] = true;
counter += 1;
}
});
/* create collection of roots per member */
membersRoots = {};
for (rootid in roots) {
if (roots.hasOwnProperty(rootid)) {
members = famMembers[rootid];
for (member in members) {
if (members.hasOwnProperty(member)) {
if (!membersRoots[member]) {
membersRoots[member] = [];
}
membersRoots[member].push(rootid.toString());
}
}
}
}
/* loop minimal sub tree roots */
while (counter > 0) {
minimum = null;
for (famItemId in roots) {
if (roots.hasOwnProperty(famItemId)) {
if (!minimum || famCount[famItemId] < minimum) {
minimum = famCount[famItemId];
result = famItemId;
}
}
}
if (result != null) {
if (onItem != null) {
onItem.call(thisArg, result, _nodes[result]);
}
members = famMembers[result];
for (member in members) {
if (members.hasOwnProperty(member)) {
if (!processed[member]) {
memberRoots = membersRoots[member];
for (index = 0, len = memberRoots.length; index < len; index += 1) {
memberRoot = memberRoots[index];
famCount[memberRoot] -= 1;
}
processed[member] = true;
}
}
}
delete roots[result];
counter -= 1;
}
}
}
/**
* Finds root node having largest number of nodes in its hierarchy
*
* @returns {string} Returns largest sub-hierarchy root node id.
*/
function findLargestRoot() {
var result = null,
maximum,
famMembers = {},
famCount = {},
isRoot;
maximum = null;
loopTopoReversed(this, function (famItemId, famItem, position) {
/* every node has at least itself in members */
if (!famMembers.hasOwnProperty(famItemId)) {
famMembers[famItemId] = {};
famCount[famItemId] = 0;
}
famMembers[famItemId][famItemId] = true;
famCount[famItemId] += 1;
isRoot = true;
loopParents(this, famItem.id, function (parentid, parent, levelIndex) {
var items, itemid;
isRoot = false;
if (!famMembers.hasOwnProperty(parentid)) {
famMembers[parentid] = {};
famCount[parentid] = 0;
}
/* push famItem members to parent members collection */
if (!famCount[parentid] && _parentsCount[famItemId] == 1) {
famMembers[parentid] = famMembers[famItemId];
famCount[parentid] = famCount[famItemId];
} else {
items = famMembers[famItemId];
for (itemid in items) {
if (items.hasOwnProperty(itemid)) {
famMembers[parentid][itemid] = true;
famCount[parentid] += 1;
}
}
}
return SKIP;
});
if (isRoot && (!maximum || famCount[famItemId] > maximum)) {
maximum = famCount[famItemId];
result = famItemId;
}
});
return result;
}
/**
* Checks whether parents share a child node. Common child should belong only to the given collection
* of parents, if child's parents don't match given collection of parents,
* it is not considered as common child.
* @param {string[]} parents Collection of parents
* @returns {boolean} Returns true if common child exist.
*/
function hasCommonChild(parents) {
var result = false,
parentsHash, childrenHash,
parentsCount,
pIndex, pLen,
parent, child;
/* convert parents collection to hash, remove duplicates and ignore non-existing items */
parentsHash = {};
parentsCount = 0;
for (pIndex = 0, pLen = parents.length; pIndex < pLen; pIndex += 1) {
parent = parents[pIndex];
if (_nodes[parent] != null && !parentsHash[parent]) {
parentsHash[parent] = true;
parentsCount += 1;
}
}
/* collect number of parents referencing each child */
childrenHash = {};
for (parent in parentsHash) {
if (parentsHash.hasOwnProperty(parent)) {
_loop(this, _children, parent, function (child) {
if (!childrenHash[child]) {
childrenHash[child] = 1;
} else {
childrenHash[child] += 1;
}
}); //ignore jslint
}
}
/* find common child having number of references equal to number of existing parents */
for (child in childrenHash) {
if (childrenHash.hasOwnProperty(child)) {
if (_parents[child] != null && (_parentsCount[child] || 0) == childrenHash[child] && childrenHash[child] == parentsCount) {
result = true;
break;
}
}
}
return result;
}
function _bundleNodes(fromItem, items, bundleItemId, bundleItem, backwardCol, backwardCount, forwardCol, forwardCount, checkChildren) {
var isValid = false,
index, len,
child;
if (_nodes[fromItem] != null && forwardCol[fromItem] != null) {
/* validate target items */
isValid = true;
if (checkChildren) {
/* if we add new bundle all items should present */
for (index = 0, len = items.length; index < len; index += 1) {
child = items[index];
if (_nodes[child] == null || forwardCol[fromItem][child] == null) {
isValid = false;
}
}
}
if (isValid) {
if (bundleItem != null) {
/* add bundle node */
_nodes[bundleItemId] = bundleItem;
}
if (_nodes[bundleItemId] != null) {
/* update references */
if (!backwardCol[bundleItemId]) {
backwardCol[bundleItemId] = {};
backwardCount[bundleItemId] = 0;
}
if (!forwardCol[bundleItemId]) {
forwardCol[bundleItemId] = {};
forwardCount[bundleItemId] = 0;
}
if (!backwardCol[bundleItemId][fromItem]) {
backwardCol[bundleItemId][fromItem] = true;
backwardCount[bundleItemId] += 1;
}
if (!forwardCol[fromItem][bundleItemId]) {
forwardCol[fromItem][bundleItemId] = true;
forwardCount[fromItem] += 1;
}
for (index = 0, len = items.length; index < len; index += 1) {
child = items[index];
if (bundleItemId != child) {
if (forwardCol[fromItem][child] != null) {
delete forwardCol[fromItem][child];
forwardCount[fromItem] -= 1;
}
if (backwardCol[child][fromItem] != null) {
delete backwardCol[child][fromItem];
backwardCount[child] -= 1;
}
if (!backwardCol[child][bundleItemId]) {
backwardCol[child][bundleItemId] = true;
backwardCount[child] += 1;
}
if (!forwardCol[bundleItemId][child]) {
forwardCol[bundleItemId][child] = true;
forwardCount[bundleItemId] += 1;
}
}
}
}
}
}
return isValid;
}
/**
* Adds extra bundle item in between parent and its children. The parent node becomes parent of the bundle node,
* and bundle becomes parent of the children. Existing parent child relations are removed.
* @param {string} parent The parent node id
* @param {string[]} children The collection of child nodes ids
* @param {string} bundleItemId The bundle node id
* @param {object} bundleItem The bundle item context object
* @returns {boolean} Returns true if nodes bundle is valid
*/
function bundleChildren(parent, children, bundleItemId, bundleItem) {
return _bundleNodes(parent, children, bundleItemId, bundleItem, _parents, _parentsCount, _children, _childrenCount, true);
}
/**
* Adds extra bundle item in between child node and its parents. The child node becomes child of the bundle node,
* and bundle becomes child of the parents. Existing parent child relations are removed.
* @param {string} child The parent node id
* @param {string[]} parents The collection of child nodes ids
* @param {string} bundleItemId The bundle node id
* @param {object} bundleItem The bundle item context object
* @returns {boolean} Returns true if the bundle is valid
*/
function bundleParents(child, parents, bundleItemId, bundleItem) {
return _bundleNodes(child, parents, bundleItemId, bundleItem, _children, _childrenCount, _parents, _parentsCount, true);
}