-
Notifications
You must be signed in to change notification settings - Fork 6.8k
docs(tree-checklist-example): root tree node correct selected state #13523
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
josephperrott
merged 4 commits into
angular:master
from
fabioloreggian:tree-checklist-example-fix
Oct 17, 2018
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
eca35f9
docs(tree-checklist-example): root tree node correct selected state
fabioloreggian 3a6e594
docs(tree-checklist-example): Fix scenario where root node didn't upd…
fabioloreggian 663737b
docs(tree-checklist-example): Parent node updates correctly.
a5a94cb
docs(tree-checklist-example): Remove selection login out of getter
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -168,10 +168,13 @@ export class TreeChecklistExample { | |
return flatNode; | ||
} | ||
|
||
/** Whether all the descendants of the node are selected */ | ||
/** Whether all the descendants of the node are selected. */ | ||
descendantsAllSelected(node: TodoItemFlatNode): boolean { | ||
const descendants = this.treeControl.getDescendants(node); | ||
return descendants.every(child => this.checklistSelection.isSelected(child)); | ||
const descAllSelected = descendants.every(child => | ||
this.checklistSelection.isSelected(child) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like the |
||
); | ||
return descAllSelected; | ||
} | ||
|
||
/** Whether part of the descendants are selected */ | ||
|
@@ -188,6 +191,61 @@ export class TreeChecklistExample { | |
this.checklistSelection.isSelected(node) | ||
? this.checklistSelection.select(...descendants) | ||
: this.checklistSelection.deselect(...descendants); | ||
|
||
// Force update for the parent | ||
descendants.every(child => | ||
this.checklistSelection.isSelected(child) | ||
); | ||
this.checkAllParentsSelection(node); | ||
} | ||
|
||
/** Toggle a leaf to-do item selection. Check all the parents to see if they changed */ | ||
todoLeafItemSelectionToggle(node: TodoItemFlatNode): void { | ||
this.checklistSelection.toggle(node); | ||
this.checkAllParentsSelection(node); | ||
} | ||
|
||
/* Checks all the parents when a leaf node is selected/unselected */ | ||
checkAllParentsSelection(node: TodoItemFlatNode): void { | ||
let parent: TodoItemFlatNode | null = this.getParentNode(node); | ||
while (parent) { | ||
this.checkRootNodeSelection(parent); | ||
parent = this.getParentNode(parent); | ||
} | ||
} | ||
|
||
/** Check root node checked state and change it accordingly */ | ||
checkRootNodeSelection(node: TodoItemFlatNode): void { | ||
const nodeSelected = this.checklistSelection.isSelected(node); | ||
const descendants = this.treeControl.getDescendants(node); | ||
const descAllSelected = descendants.every(child => | ||
this.checklistSelection.isSelected(child) | ||
); | ||
if (nodeSelected && !descAllSelected) { | ||
this.checklistSelection.deselect(node); | ||
} else if (!nodeSelected && descAllSelected) { | ||
this.checklistSelection.select(node); | ||
} | ||
} | ||
|
||
/* Get the parent node of a node */ | ||
getParentNode(node: TodoItemFlatNode): TodoItemFlatNode | null { | ||
const currentLevel = this.getLevel(node); | ||
|
||
if (currentLevel < 1) { | ||
return null; | ||
} | ||
|
||
const startIndex = this.treeControl.dataNodes.indexOf(node) - 1; | ||
|
||
for (let i = startIndex; i >= 0; i--) { | ||
const currentNode = this.treeControl.dataNodes[i]; | ||
|
||
if (this.getLevel(currentNode) < currentLevel) { | ||
return currentNode; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
/** Select the category so we can insert the new item. */ | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: you can just use
return descendants.every(...)
here instead of assigning it to a constant.