@@ -242,7 +242,6 @@ fragment FamilyData on Family {
242
242
childFamilies {
243
243
name
244
244
}
245
- descendants
246
245
}
247
246
248
247
fragment AddedDelta on Added {
@@ -497,33 +496,33 @@ export default {
497
496
/**
498
497
* Object for looking up family ancestors
499
498
*
500
- * example return object
499
+ * example return mapping:
501
500
* {
502
- * FAMILY: ['PARENT_FAMILY', 'GRANDPARENT_FAMILY', 'GREAT_GRANDPARENT_FAMILY', 'root' ],
503
- * PARENT_FAMILY: [ 'GRANDPARENT_FAMILY', 'GREAT_GRANDPARENT_FAMILY', 'root' ],
504
- * GRANDPARENT_FAMILY: [ 'GREAT_GRANDPARENT_FAMILY', 'root' ],
505
- * GREAT_GRANDPARENT_FAMILY : ['root'],
506
501
* root: []
502
+ * GREAT_GRANDPARENT_FAMILY : ['root'],
503
+ * GRANDPARENT_FAMILY: [ 'GREAT_GRANDPARENT_FAMILY', 'root' ],
504
+ * PARENT_FAMILY: [ 'GRANDPARENT_FAMILY', 'GREAT_GRANDPARENT_FAMILY', 'root' ],
505
+ * FAMILY: ['PARENT_FAMILY', 'GRANDPARENT_FAMILY', 'GREAT_GRANDPARENT_FAMILY', 'root' ],
507
506
* }
508
507
*
509
508
* note: object value arrays contain family names as strings only - not nodes
510
509
*
511
- * @returns {Object } keys are family names, values are arrays containing all ancestors names
510
+ * @returns {Map<string, string[]> } keys are family names, values are arrays containing all ancestors names
512
511
*/
513
512
allParentLookUp () {
514
- const lookup = {}
513
+ const lookup = []
515
514
for (const namespace of this .namespaces ) {
516
- const array = []
515
+ const ancestors = []
517
516
let parent = namespace .node .firstParent
518
517
while (parent) {
519
- const childTokens = this .workflows [0 ].tokens .clone ({ cycle: ` $namespace|${ parent .name } ` })
520
- const childNode = this .cylcTree .$index [childTokens .id ]
521
- array .push (childNode .name )
522
- parent = childNode .node .firstParent
518
+ const parentNode = this .cylcTree .$index [parent .id ]
519
+ ancestors .push (parentNode .name )
520
+ parent = parentNode .node .firstParent
523
521
}
524
- lookup[namespace .name ] = array
522
+ lookup . push ( [namespace .name , ancestors])
525
523
}
526
- return lookup
524
+ // Sort by shortest ancestor list to longest:
525
+ return new Map (lookup .sort ((a , b ) => a[1 ].length - b[1 ].length ))
527
526
},
528
527
/**
529
528
* Object for looking up children
@@ -689,46 +688,23 @@ export default {
689
688
* @returns {Family[]} array containing nested structure of families
690
689
*/
691
690
getTree () {
692
- const root = {
693
- name: ' root' ,
694
- children: []
695
- }
696
- if (this .workflows ) {
697
- const tokens = this .workflows [0 ].tokens .clone ({ cycle: ' $namespace|root' })
698
- const node = this .cylcTree .$index [tokens .id ]
699
- if (node) {
700
- return this .getTreeHelper (root, node).children
701
- }
702
- }
703
- },
704
-
705
- /**
706
- * Get a nested object of families
707
- * @property {Family} store - nested object of families
708
- * @property {Node} node - node object
709
- * @property {number} counter - counter used for index
710
- * @returns {Family} nested structure of families
711
- */
712
- getTreeHelper (store , node ) {
713
- let tempItem
714
- const isParent = this .collapseFamily .includes (node .name )
715
- const isAncestor = this .allParentLookUp [node .name ].some (element => {
716
- return this .collapseFamily .includes (element)
717
- })
718
- const disabled = isParent || isAncestor
719
- for (const childFamily of node .node .descendants ) {
720
- const childNamespace = this .namespaces .find ((obj ) => obj .name === childFamily)
721
- if (childNamespace? .node .firstParent .id === node .id ) {
722
- tempItem = {
723
- name: childFamily,
724
- children: [],
725
- disabled
726
- }
727
- this .getTreeHelper (tempItem, childNamespace)
728
- store .children .push (tempItem)
691
+ const tree = []
692
+ for (const [name , ancestors ] of this .allParentLookUp ) {
693
+ if (name === ' root' ) continue
694
+ let pointer = tree
695
+ let ancestorName, disabled
696
+ for (let i = ancestors .length - 2 ; i >= 0 ; i-- ) {
697
+ ancestorName = ancestors[i]
698
+ pointer = pointer .find ((item ) => item .name === ancestorName).children
699
+ disabled || = this .collapseFamily .includes (ancestorName)
729
700
}
701
+ pointer .push ({
702
+ name,
703
+ children: [],
704
+ disabled,
705
+ })
730
706
}
731
- return store
707
+ return tree
732
708
},
733
709
734
710
/**
@@ -807,17 +783,18 @@ export default {
807
783
// the nodes first parent is collapsed
808
784
const firstParent = this .collapseFamily .includes (nodeFirstParent)
809
785
// a family member up the tree is collapsed
810
- const ancestor = this .allParentLookUp [ nodeFirstParent] .some (element => {
786
+ const ancestor = this .allParentLookUp . get ( nodeFirstParent) .some (element => {
811
787
return this .collapseFamily .includes (element)
812
788
})
813
789
if (firstParent && ! ancestor) {
814
790
// the node is collapsed by its first parent
815
791
return nodeFirstParent
816
792
} else if (ancestor) {
817
793
// the node is collapsed by an ancestor
818
- for (let i = this .allParentLookUp [nodeFirstParent].length - 1 ; i >= 0 ; i-- ) {
819
- if (this .collapseFamily .includes (this .allParentLookUp [nodeFirstParent][i])) {
820
- return this .allParentLookUp [nodeFirstParent][i]
794
+ const lookupParents = this .allParentLookUp .get (nodeFirstParent)
795
+ for (let i = lookupParents .length - 1 ; i >= 0 ; i-- ) {
796
+ if (this .collapseFamily .includes (lookupParents[i])) {
797
+ return lookupParents[i]
821
798
}
822
799
}
823
800
} else {
0 commit comments