Skip to content

fix(tree): improve nested tree node & fix nested tree control #10454

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 27, 2018

Conversation

tinayuangao
Copy link
Contributor

No description provided.

@googlebot googlebot added the cla: yes PR author has agreed to Google's Contributor License Agreement label Mar 16, 2018
@tinayuangao tinayuangao force-pushed the tree-improve-nested branch 2 times, most recently from 955dd08 to 10d85e9 Compare March 16, 2018 21:44
@tinayuangao tinayuangao added pr: needs review target: major This PR is targeted for the next major release labels Mar 17, 2018
@tinayuangao tinayuangao requested review from andrewseguin, jelbourn and crisbeto and removed request for andrewseguin March 20, 2018 18:04
@@ -26,15 +26,17 @@ export class NestedTreeControl<T> extends BaseTreeControl<T> {
expandAll(): void {
this.expansionModel.clear();
let toBeExpanded = <any>[];
this.dataNodes.forEach(dataNode => toBeExpanded.push(...this.getDescendants(dataNode)));
this.dataNodes.forEach(dataNode =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than using a forEach and a push, you should be able to use map or reduce instead.

this.expansionModel.select(...toBeExpanded);
}

/** Gets a list of descendant dataNodes of a subtree rooted at given data node recursively. */
getDescendants(dataNode: T): T[] {
const descendants = [];
this._getDescendants(descendants, dataNode);
return descendants;
// Remove the node itself
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only returns a slice with the second item in it, but it doesn't remove it. Is it intended?

});
this.nodeOutlet.changes.pipe(takeUntil(this._destroyed))
.subscribe((_) => this._addChildrenNodes());
.subscribe((_) => this._updateChildrenNodes());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not something that was added in this PR, but you can remove the _ parameter here.

} else {
throw getTreeNoValidDataSourceError();
}
}

/** Check for changes made in the data and render each change (node added/removed/moved). */
private _renderNodeChanges(dataNodes: T[]) {
const changes = this._dataDiffer.diff(dataNodes);
renderNodeChanges(data: T[], dataDiffer?: IterableDiffer<T>, viewContainer?: ViewContainerRef) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of doing the ternary for the dataDiffer and the viewContainer below, you can provide the default ones as parameters:

renderNodeChanges(data: T[], dataDiffer: IterableDiffer<T> = this._dataDiffer, viewContainer: ViewContainerRef = this._nodeOutlet.viewContainer) {

}

@tinayuangao tinayuangao force-pushed the tree-improve-nested branch from 10d85e9 to 8dcc6a0 Compare March 20, 2018 20:45
@tinayuangao
Copy link
Contributor Author

@crisbeto Comments addressed. Please take a look. Thanks for review!

Copy link
Member

@crisbeto crisbeto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, left a couple more nits.

const changes = this._dataDiffer.diff(dataNodes);
renderNodeChanges(data: T[], dataDiffer: IterableDiffer<T> = this._dataDiffer,
viewContainer: ViewContainerRef = this._nodeOutlet.viewContainer) {
const changes = (dataDiffer ? dataDiffer : this._dataDiffer).diff(data);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that you're guaranteed to have a dataDiffer, you don't need the ternary here anymore.

this.dataNodes.forEach(dataNode => toBeExpanded.push(...this.getDescendants(dataNode)));
this.expansionModel.select(...toBeExpanded);
const allNodes = this.dataNodes.map(dataNode => [...this.getDescendants(dataNode), dataNode])
.reduce((first, second) => [...first, ...second]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need both map and reduce here. Something like this should work the same:

this.dataNodes.reduce((accumulator, dataNode) => [...accumulator, ...this.getDescendants(dataNode), dataNode], []);

@tinayuangao
Copy link
Contributor Author

Updated and thanks for review!

@tinayuangao tinayuangao added action: merge The PR is ready for merge by the caretaker P1 Impacts a large percentage of users; if a workaround exists it is partial or overly painful labels Mar 21, 2018
@tinayuangao tinayuangao force-pushed the tree-improve-nested branch from aee0af9 to e20030e Compare March 27, 2018 17:29
@josephperrott josephperrott merged commit 2ddc257 into angular:master Mar 27, 2018
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 8, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker cla: yes PR author has agreed to Google's Contributor License Agreement P1 Impacts a large percentage of users; if a workaround exists it is partial or overly painful target: major This PR is targeted for the next major release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants