(id: number)
| 676 | } |
| 677 | |
| 678 | getIndexOfElementID(id: number): number | null { |
| 679 | const element = this.getElementByID(id); |
| 680 | |
| 681 | if (element === null || element.parentID === 0) { |
| 682 | return null; |
| 683 | } |
| 684 | |
| 685 | // Walk up the tree to the root. |
| 686 | // Increment the index by one for each node we encounter, |
| 687 | // and by the weight of all nodes to the left of the current one. |
| 688 | // This should be a relatively fast way of determining the index of a node within the tree. |
| 689 | let previousID = id; |
| 690 | let currentID = element.parentID; |
| 691 | let index = 0; |
| 692 | while (true) { |
| 693 | const current = this._idToElement.get(currentID); |
| 694 | if (current === undefined) { |
| 695 | return null; |
| 696 | } |
| 697 | |
| 698 | const {children} = current; |
| 699 | for (let i = 0; i < children.length; i++) { |
| 700 | const childID = children[i]; |
| 701 | if (childID === previousID) { |
| 702 | break; |
| 703 | } |
| 704 | |
| 705 | const child = this._idToElement.get(childID); |
| 706 | if (child === undefined) { |
| 707 | return null; |
| 708 | } |
| 709 | |
| 710 | index += child.isCollapsed ? 1 : child.weight; |
| 711 | } |
| 712 | |
| 713 | if (current.parentID === 0) { |
| 714 | // We found the root; stop crawling. |
| 715 | break; |
| 716 | } |
| 717 | |
| 718 | index++; |
| 719 | |
| 720 | previousID = current.id; |
| 721 | currentID = current.parentID; |
| 722 | } |
| 723 | |
| 724 | // At this point, the current ID is a root (from the previous loop). |
| 725 | // We also need to offset the index by previous root weights. |
| 726 | for (let i = 0; i < this._roots.length; i++) { |
| 727 | const rootID = this._roots[i]; |
| 728 | if (rootID === currentID) { |
| 729 | break; |
| 730 | } |
| 731 | |
| 732 | const root = this._idToElement.get(rootID); |
| 733 | if (root === undefined) { |
| 734 | return null; |
| 735 | } |
no test coverage detected