From 67f9717827f5d960cb01f523d2f2943106b55680 Mon Sep 17 00:00:00 2001 From: Saif Date: Thu, 25 Apr 2019 12:07:56 +0500 Subject: [PATCH] =?UTF-8?q?perf:=20=E2=9A=A1=EF=B8=8F=20Expand=20All=20and?= =?UTF-8?q?=20Collapse=20All=20render=20only=20once=20(#63)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * perf: ⚡️ Expand All and Collapse All render only once * Introduce setTreeDepth() function --- src/rowmanager.js | 68 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 55 insertions(+), 13 deletions(-) diff --git a/src/rowmanager.js b/src/rowmanager.js index 43a1260..cf9934f 100644 --- a/src/rowmanager.js +++ b/src/rowmanager.js @@ -187,27 +187,26 @@ export default class RowManager { this.showRows(rowIndices); } - openSingleNode(rowIndex) { + getChildrenToShowForNode(rowIndex) { const row = this.datamanager.getRow(rowIndex); row.meta.isTreeNodeClose = false; - const childrenToShow = this.datamanager.getImmediateChildren(rowIndex); + return this.datamanager.getImmediateChildren(rowIndex); + } + + openSingleNode(rowIndex) { + const childrenToShow = this.getChildrenToShowForNode(rowIndex); const visibleRowIndices = this.bodyRenderer.visibleRowIndices; const rowsToShow = uniq([...childrenToShow, ...visibleRowIndices]).sort(numberSortAsc); this.showRows(rowsToShow); } - closeSingleNode(rowIndex) { + getChildrenToHideForNode(rowIndex) { const row = this.datamanager.getRow(rowIndex); row.meta.isTreeNodeClose = true; const rowsToHide = this.datamanager.getChildren(rowIndex); - const visibleRows = this.bodyRenderer.visibleRowIndices; - const rowsToShow = visibleRows - .filter(rowIndex => !rowsToHide.includes(rowIndex)) - .sort(numberSortAsc); - rowsToHide.forEach(rowIndex => { const row = this.datamanager.getRow(rowIndex); if (!row.meta.isLeaf) { @@ -215,23 +214,66 @@ export default class RowManager { } }); + return rowsToHide; + } + + closeSingleNode(rowIndex) { + const rowsToHide = this.getChildrenToHideForNode(rowIndex); + const visibleRows = this.bodyRenderer.visibleRowIndices; + const rowsToShow = visibleRows + .filter(rowIndex => !rowsToHide.includes(rowIndex)) + .sort(numberSortAsc); + this.showRows(rowsToShow); } expandAllNodes() { let rows = this.datamanager.getRows(); let rootNodes = rows.filter(row => !row.meta.isLeaf); - rootNodes.map(row => { - this.openSingleNode(row.meta.rowIndex); - }); + + const childrenToShow = rootNodes.map(row => this.getChildrenToShowForNode(row.meta.rowIndex)).flat(); + const visibleRowIndices = this.bodyRenderer.visibleRowIndices; + const rowsToShow = uniq([...childrenToShow, ...visibleRowIndices]).sort(numberSortAsc); + + this.showRows(rowsToShow); } collapseAllNodes() { let rows = this.datamanager.getRows(); let rootNodes = rows.filter(row => row.meta.indent === 0); - rootNodes.map(row => { - this.closeSingleNode(row.meta.rowIndex); + + const rowsToHide = rootNodes.map(row => this.getChildrenToHideForNode(row.meta.rowIndex)).flat(); + const visibleRows = this.bodyRenderer.visibleRowIndices; + const rowsToShow = visibleRows + .filter(rowIndex => !rowsToHide.includes(rowIndex)) + .sort(numberSortAsc); + + this.showRows(rowsToShow); + } + + setTreeDepth(depth) { + let rows = this.datamanager.getRows(); + + const rowsToOpen = rows.filter(row => row.meta.indent < depth); + const rowsToClose = rows.filter(row => row.meta.indent >= depth); + const rowsToHide = rowsToClose.filter(row => row.meta.indent > depth); + + rowsToClose.forEach(row => { + if (!row.meta.isLeaf) { + row.meta.isTreeNodeClose = true; + } }); + rowsToOpen.forEach(row => { + if (!row.meta.isLeaf) { + row.meta.isTreeNodeClose = false; + } + }); + + const rowsToShow = rows + .filter(row => !rowsToHide.includes(row)) + .map(row => row.meta.rowIndex) + .sort(numberSortAsc); + this.showRows(rowsToShow); } getRow$(rowIndex) {