handleAddItemsEvent method

  1. @visibleForTesting
void handleAddItemsEvent(
  1. NodeAddEvent<INode> event
)

Implementation

@visibleForTesting
void handleAddItemsEvent(NodeAddEvent<INode> event) {
  for (final node in event.items) {
    if (animatedListStateController.containsKey(node.path)) continue;

    if (node.isRoot || node.parent?.isRoot == true) {
      final root = node.root as ITreeNode<Data>;
      if (!root.isExpanded) {
        if (focusToNewNode) {
          expansionBehaviourController.expandNode(root);
        }
      } else {
        animatedListStateController.insertAll(
            animatedListStateController.list.length, List.from(event.items));
      }
      if (focusToNewNode) {
        expansionBehaviourController.scrollToLastVisibleChild(root);
      }
    } else {
      final parentIndex = animatedListStateController.list
          .indexWhere((element) => element.key == node.parent?.key);
      if (parentIndex < 0) continue;

      final parentNode = animatedListStateController.list[parentIndex];

      if (!parentNode.isExpanded) {
        expansionBehaviourController.expandNode(parentNode);
      } else {
        // if the node is expanded, add the items in the flatList and
        // the animatedList
        final insertAtIndex = animatedListStateController.list.lastIndexWhere(
            (element) =>
                element.path.startsWith(parentNode.path) &&
                element.level > parentNode.level);
        if (insertAtIndex < 0) {
          animatedListStateController.insertAll(
            animatedListStateController.length - 1,
            List.from(event.items),
          );
        } else {
          animatedListStateController.insertAll(
            insertAtIndex + 1,
            List.from(event.items),
          );
        }
      }
      if (focusToNewNode) {
        expansionBehaviourController.scrollToLastVisibleChild(parentNode);
      }
    }
  }
}