Skip to content

Commit

Permalink
Merge pull request #23 from tuj84257/master
Browse files Browse the repository at this point in the history
#5 Add `expandAll` and `collapseAll` methods
  • Loading branch information
daweilv committed Mar 27, 2022
2 parents 9c6c299 + 845bbd5 commit ef5c7d0
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 3 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,13 @@ const disabledNodes = myTree.disabledNodes;
| loaded | null | invoke after the tree load data |
| onChange | null | invoke when the node status change |

## Methods

| Method | Parameters | Description |
| ----------- | ------------ | ---------------------------------- |
| expandAll | null | expand all tree nodes |
| collapseAll | null | collapse all tree nodes |

## License

[MIT](./LICENSE)
Expand Down
55 changes: 55 additions & 0 deletions dist/tree.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/tree.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/tree.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/tree.min.js.map

Large diffs are not rendered by default.

33 changes: 33 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,27 @@ function animation(duration, callback) {
});
}

function collapseFromLeaf(tree, leafNode) {
try {
const nodeLiElement = tree.liElementsById[leafNode.parent.id];
if(!nodeLiElement.classList.contains('treejs-node__close'))
nodeLiElement.getElementsByClassName('treejs-switcher')[0].click();
} catch (error) {
return;
}
if(leafNode.hasOwnProperty('parent'))
collapseFromLeaf(tree, leafNode.parent);
}

function expandFromRoot(tree, root) {
const nodeLiElement = tree.liElementsById[root.id];
if(nodeLiElement.classList.contains('treejs-node__close'))
nodeLiElement.getElementsByClassName('treejs-switcher')[0].click();
if(root.hasOwnProperty('children'))
for(let child of root.children)
expandFromRoot(tree, child);
}

export default function Tree(container, options) {
const defaultOptions = {
selectMode: 'checkbox',
Expand Down Expand Up @@ -438,6 +459,18 @@ Tree.prototype.updateLiElement = function(node) {
}
};

Tree.prototype.collapseAll = function() {
const leafNodesById = this.leafNodesById;
for(let id in leafNodesById) {
const leafNode = leafNodesById[id];
collapseFromLeaf(this, leafNode);
}
}

Tree.prototype.expandAll = function() {
expandFromRoot(this, this.treeNodes[0]);
}

Tree.parseTreeData = function(data) {
const treeNodes = deepClone(data);
const nodesById = {};
Expand Down

0 comments on commit ef5c7d0

Please sign in to comment.