File size: 1,329 Bytes
71174bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import type { TreeNodeList } from "../TreeNodeList/TreeNodeList";
import { newTreeNodeList } from "../TreeNodeMakers";
import type { TreeNode } from "./TreeNode";

/**
 * TreeNodeAncestry class.
 */
export class TreeNodeAncestry {
    private parentTreeNode: TreeNode;

    /**
     * Constructor.
     * 
     * @param  {TreeNode} parentTreeNode The parent TreeNode.
     */
    constructor(parentTreeNode: TreeNode) {
        this.parentTreeNode = parentTreeNode;
    }

    /**
     * Get a nodes ancestory. First element is most distant ancestor (greatest
     * grandparent), and last is this node itself.
     *
     * @param  {TreeNodeList}  mols  The list of molecules to search.
     * @returns {TreeNodeList}  The list of nodes in the ancestory.
     */
    public getAncestry(mols: TreeNodeList): TreeNodeList {
        // If you get here, node is of type TreeNode.

        let curNode = this.parentTreeNode;
        const ancestors = newTreeNodeList([this.parentTreeNode]);
        while (curNode.parentId) {
            const parentNode = mols.filters.onlyId(curNode.parentId);

            if (parentNode === null) {
                break;
            }

            // Add at first position
            ancestors.unshift(parentNode);
            curNode = parentNode;
        }

        return ancestors;
    }
}