# base/node (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/base/node/

Hierarchy object.

## Node extends Object

Hierarchy object.
- Hierarchical structure that can have a parent and children.
- Active state setting.

Example: Build a hierarchy and find by name

```js
import { Node } from "./build/vanilla.js";

const root = new Node();
root.setName("root");
const child = new Node();
child.setName("child");
root.addChild(child);
const grandChild = new Node();
grandChild.setName("grandChild");
child.addChild(grandChild);
child.setActive(false);
const found = root.findChildRecursiveByName("grandChild");
console.log(found === grandChild, grandChild.isActive(), grandChild.isActiveInHierarchy());
console.log(root.getChildCount(), grandChild.getParent() === child, root.isRoot(), grandChild.isLeaf());
```

- `constructor()` (constructor, since 0.0.8): Constructor.
- `setParent(parent)` (method, since 0.0.8): Sets the parent.
  - `parent` Node | null: New parent. (if null, only detaches from the current parent)
  - returns boolean: True if attached or detached. (false without attaching under itself or its own descendant, since the hierarchy would become a cycle)
- `addChild(child)` (method, since 0.0.8): Adds a child.
  - `child` Node: Node to attach. (if it had another parent, it is detached from there and appended to the end of the child list)
  - returns boolean: True if attached. (false without attaching if it is this node itself or an ancestor of this node)
- `removeChild(child)` (method, since 0.0.8): Removes a child.
  - `child` Node: Node to detach. (detached only if it is a child of this node; otherwise nothing happens)
- `removeChildAt(childIndex)` (method, since 0.0.11): Removes a child.
  - `childIndex` number: Index of the child to detach. (from 0, does nothing if out of range)
- `removeChildren()` (method, since 0.0.8): Removes all children. (Only clears the direct child list, so hierarchies owned by the children are kept)
- `isRoot()` (method, since 0.0.8): Returns whether it has no parent.
  - returns boolean: True if it has no parent.
- `isLeaf()` (method, since 0.0.8): Returns whether it has no children.
  - returns boolean: True if it has no children.
- `hasParent()` (method, since 0.0.11): Returns whether it has a parent.
  - returns boolean: True if it has a parent.
- `getParent()` (method, since 0.0.8): Returns the parent.
  - returns Node: Parent node. (null if none)
- `getSiblings()` (method, since 0.0.11): Returns the sibling list.
  - returns Node[] | null: Child list of the parent. (the original array including this node, null if no parent)
- `getChildren()` (method, since 0.0.8): Returns the child list.
  - returns Node[]: List of direct children. (the original array, not a copy)
- `getChildCount()` (method, since 0.0.8): Returns the number of children.
  - returns number: Number of direct children.
- `getChild(childIndex)` (method, since 0.0.8): Returns a child.
  - `childIndex` number: Index of the child. (from 0)
  - returns Node | null: Direct child at that index. (null if out of range)
- `getChildIndex(child)` (method, since 0.0.11): Returns the position of a child.
  - `child` Node: Node to find.
  - returns number: Index in the direct child list. (-1 if not a child)
- `hasChild(child)` (method, since 0.0.9): Returns whether it contains the child.
  - `child` Node: Node to find.
  - returns boolean: True if it is a direct child.
- `setActive(active)` (method, since 0.0.8): Sets the active state.
  - `active` boolean: Active if true. (does not change the children's values)
- `isActive()` (method, since 0.0.8): Returns the active state.
  - returns boolean: True if this node itself is active. (does not look at the parents)
- `isActiveInHierarchy()` (method, since 0.0.8): Returns the active state of the whole hierarchy from here up to the root. (Returns false if any node up to the root is inactive)
  - returns boolean: True if this node and all parents up to the root are active.
- `setName(name)` (method, since 0.0.11): Sets the name.
  - `name` string: Node name. (duplicate names are not prevented)
- `getName()` (method, since 0.0.11): Returns the name.
  - returns string: Node name. (empty string by default)
- `findChild(predicate)` (method, since 0.0.11): Finds a direct child by condition.
  - `predicate` function(Node): boolean: Function that receives a direct child and returns true to pick it. (returns null if null)
  - returns Node | null: First matching direct child in child list order. (null if none)
- `findChildRecursive(predicate)` (method, since 0.0.11): Finds a direct child by condition. (depth-first)
  - `predicate` function(Node): boolean: Function that receives a descendant and returns true to pick it.
  - returns Node | null: First matching descendant in depth first order. (looks at grandchildren and below too, null if none)

  Example: Find by condition in the whole subtree

  ```js
  import { Node } from "./build/vanilla.js";
  
  const root = new Node();
  const child = new Node();
  const grandChild = new Node();
  root.addChild(child);
  child.addChild(grandChild);
  grandChild.setActive(false);
  const inactiveNode = root.findChildRecursive((node) => {
  	return !node.isActive();
  });
  console.log(inactiveNode === grandChild);
  ```

- `findChildByName(name)` (method, since 0.0.11): Finds a direct child by name.
  - `name` string: Name to find.
  - returns Node | null: First direct child with the same name. (null if none)
- `findChildRecursiveByName(name)` (method, since 0.0.11): Finds by name in the whole sub-hierarchy. (depth-first)
  - `name` string: Name to find.
  - returns Node | null: First descendant with the same name in depth first order. (null if none)
- `findDescendantByName(name)` (method, since 0.4.0): Finds by name in the whole sub-hierarchy. (depth-first, same as findChildRecursiveByName, same name as find_descendant_by_name in the Rust version)
  - `name` string: Name to find.
  - returns Node | null: First descendant with the same name in depth first order. (null if none)
- `destroyChildrenWhere(predicate)` (method, since 0.4.0): Detaches and destroys direct children matching a condition. (Returns the number detached, same name as destroy_children_where in the Rust version)
  - `predicate` function(Node): boolean: Function that receives a direct child and returns true to detach and destroy it.
  - returns number: Number of children detached and destroyed.

  Example: Detach and destroy children that match a condition

  ```js
  import { Node } from "./build/vanilla.js";
  
  const root = new Node();
  for (let index = 0; index < 4; ++index) {
  	const child = new Node();
  	child.setName("enemy" + index);
  	child.setActive(index % 2 === 0);
  	root.addChild(child);
  }
  const destroyedCount = root.destroyChildrenWhere((child) => {
  	return !child.isActive();
  });
  console.log(destroyedCount, root.getChildCount());
  ```

- `removeChildByName(name)` (method, since 0.0.11): Removes a direct child by name.
  - `name` string: Name of the direct child to detach. (only the first one if several share the name)
- `setSiblingIndex(childIndex)` (method, since 0.0.11): Sets the position among siblings.
  - `childIndex` number: New index among siblings. (clamped to 0 ~ sibling count - 1, does nothing if no parent)

  Example: Move to the front and back among siblings

  ```js
  import { Node } from "./build/vanilla.js";
  
  const root = new Node();
  const first = new Node();
  const second = new Node();
  const third = new Node();
  root.addChild(first);
  root.addChild(second);
  root.addChild(third);
  third.setSiblingIndex(0);
  first.setSiblingIndex(100);
  console.log(third.getSiblingIndex(), second.getSiblingIndex(), first.getSiblingIndex());
  ```

- `getSiblingIndex()` (method, since 0.0.11): Returns the position among siblings.
  - returns number: Index among siblings. (-1 if no parent)
