wix-incubator / angular-tree-control

Angular JS Tree
http://wix.github.io/angular-tree-control
MIT License
708 stars 277 forks source link

Collapse children of collapsed node possible? #276

Open mike-ward opened 7 years ago

mike-ward commented 7 years ago

Open a node several levels deep. Then collapse the first node opened. The nodes expanded under the first node are still expanded. You can see this by reopening a node. Is there a way to collapse the children of the collapsed node? If not maybe add a configuration option?

yoavaa commented 7 years ago

the list of expanded nodes is managed in the expanded nodes array. Just remove from it what you do not wanna have there.

I did not implement the logic to do so automatically.

Having such a configuration option sounds nice, if you create a PR (with tests) will merge it.

mike-ward commented 7 years ago

I looked at the array before posting this issue. I didn't see a way to determine if a node was a child of another (no parent reference). Maybe there's something I don't understand about the structure.

mike-ward commented 7 years ago

For what it's worth I was able to write a function to do this. Hooked it into the on-toggle-node event.

function collapseNode(node): void {
    scope.expandedNodes = scope.expandedNodes.filter(en => isNotChild(node, en));
}

function isNotChild(node, en): boolean {
    if (!node.children) return true;
    return node.children.every(n => n !== en && isNotChild(n, en));
}