updateAllUnavailableNodes<T extends AbsNodeType> function

bool updateAllUnavailableNodes<T extends AbsNodeType>(
  1. TreeType<T> tree
)

This function is used to update all unavailable nodes of current tree.

Function returns a boolean, true if current tree is chosenable, else false.

When we first time parse data (json, map, list, etc) to tree data structure, there could be some nodes are unavailable but haven't been updated yet.

Example: Client wants to choose few employees (which represent leaves) in a department (which represent inner nodes). If department A doesn't have any available employee, it will be also marked as unavailable.

It is NECESSARY to call this function at the first time the tree initialized.

Implementation

bool updateAllUnavailableNodes<T extends AbsNodeType>(TreeType<T> tree) {
  if (tree.isLeaf) return !tree.data.isUnavailable;

  bool isThisTreeAvailable = false;
  for (var child in tree.children) {
    if (updateAllUnavailableNodes(child)) isThisTreeAvailable = true;
  }

  if (isThisTreeAvailable) {
    return true;
  } else {
    tree.data.isUnavailable = true;
    return false;
  }
}