(location: number[], sizing?: Sizing)
| 989 | } |
| 990 | |
| 991 | removeView(location: number[], sizing?: Sizing): IGridView { |
| 992 | if (this.hasMaximizedView()) { |
| 993 | this.exitMaximizedView(); |
| 994 | } |
| 995 | |
| 996 | const [rest, index] = tail(location); |
| 997 | const [pathToParent, parent] = this.getNode(rest); |
| 998 | |
| 999 | if (!(parent instanceof BranchNode)) { |
| 1000 | throw new Error('Invalid location'); |
| 1001 | } |
| 1002 | |
| 1003 | const nodeToRemove = parent.children[index]; |
| 1004 | |
| 1005 | if (!(nodeToRemove instanceof LeafNode)) { |
| 1006 | throw new Error('Invalid location'); |
| 1007 | } |
| 1008 | |
| 1009 | parent.removeChild(index, sizing); |
| 1010 | nodeToRemove.dispose(); |
| 1011 | |
| 1012 | if (parent.children.length !== 1) { |
| 1013 | return nodeToRemove.view; |
| 1014 | } |
| 1015 | |
| 1016 | // if the parent has only one child and we know the parent is a BranchNode we can make the tree |
| 1017 | // more efficiently spaced by replacing the parent BranchNode with the child. |
| 1018 | // if that child is a LeafNode then we simply replace the BranchNode with the child otherwise if the child |
| 1019 | // is a BranchNode too we should spread it's children into the grandparent. |
| 1020 | |
| 1021 | // refer to the remaining child as the sibling |
| 1022 | const sibling = parent.children[0]; |
| 1023 | |
| 1024 | if (pathToParent.length === 0) { |
| 1025 | // if the parent is root |
| 1026 | |
| 1027 | if (sibling instanceof LeafNode) { |
| 1028 | // if the sibling is a leaf node no action is required |
| 1029 | return nodeToRemove.view; |
| 1030 | } |
| 1031 | |
| 1032 | // otherwise the sibling is a branch node. since the parent is the root and the root has only one child |
| 1033 | // which is a branch node we can just set this branch node to be the new root node |
| 1034 | |
| 1035 | // for good housekeeping we'll removing the sibling from it's existing tree |
| 1036 | parent.removeChild(0, sizing); |
| 1037 | |
| 1038 | // and set that sibling node to be root |
| 1039 | this.root = sibling; |
| 1040 | |
| 1041 | return nodeToRemove.view; |
| 1042 | } |
| 1043 | |
| 1044 | // otherwise the parent is apart of a large sub-tree |
| 1045 | |
| 1046 | const [grandParent, ..._] = [...pathToParent].reverse(); |
| 1047 | const [parentIndex, ...__] = [...rest].reverse(); |
| 1048 |
no test coverage detected