Skip to content

Commit 1ee6339

Browse files
committed
Remove need for descendants query & tidy family tree logic
1 parent d57e166 commit 1ee6339

File tree

1 file changed

+34
-57
lines changed

1 file changed

+34
-57
lines changed

src/views/Graph.vue

Lines changed: 34 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,6 @@ fragment FamilyData on Family {
242242
childFamilies {
243243
name
244244
}
245-
descendants
246245
}
247246
248247
fragment AddedDelta on Added {
@@ -497,33 +496,33 @@ export default {
497496
/**
498497
* Object for looking up family ancestors
499498
*
500-
* example return object
499+
* example return mapping:
501500
* {
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'],
506501
* 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' ],
507506
* }
508507
*
509508
* note: object value arrays contain family names as strings only - not nodes
510509
*
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
512511
*/
513512
allParentLookUp () {
514-
const lookup = {}
513+
const lookup = []
515514
for (const namespace of this.namespaces) {
516-
const array = []
515+
const ancestors = []
517516
let parent = namespace.node.firstParent
518517
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
523521
}
524-
lookup[namespace.name] = array
522+
lookup.push([namespace.name, ancestors])
525523
}
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))
527526
},
528527
/**
529528
* Object for looking up children
@@ -689,46 +688,23 @@ export default {
689688
* @returns {Family[]} array containing nested structure of families
690689
*/
691690
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)
729700
}
701+
pointer.push({
702+
name,
703+
children: [],
704+
disabled,
705+
})
730706
}
731-
return store
707+
return tree
732708
},
733709
734710
/**
@@ -807,17 +783,18 @@ export default {
807783
// the nodes first parent is collapsed
808784
const firstParent = this.collapseFamily.includes(nodeFirstParent)
809785
// 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 => {
811787
return this.collapseFamily.includes(element)
812788
})
813789
if (firstParent && !ancestor) {
814790
// the node is collapsed by its first parent
815791
return nodeFirstParent
816792
} else if (ancestor) {
817793
// 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]
821798
}
822799
}
823800
} else {

0 commit comments

Comments
 (0)