MoveToParent removes the given node from its current parent and adds it as a child of the given new parent. The old and new parents can be in different trees (or not).
(child Node, parent Node)
| 75 | // and adds it as a child of the given new parent. |
| 76 | // The old and new parents can be in different trees (or not). |
| 77 | func MoveToParent(child Node, parent Node) { |
| 78 | oldParent := child.AsTree().Parent |
| 79 | if oldParent != nil { |
| 80 | idx := IndexOf(oldParent.AsTree().Children, child) |
| 81 | if idx >= 0 { |
| 82 | oldParent.AsTree().Children = slices.Delete(oldParent.AsTree().Children, idx, idx+1) |
| 83 | } |
| 84 | } |
| 85 | parent.AsTree().AddChild(child) |
| 86 | } |
| 87 | |
| 88 | // ParentByType returns the first parent of the given node that is |
| 89 | // of the given type, if any such node exists. |