fix: 🐛 Reimplement tree node toggle logic

Earlier, rows were hidden using style. Now, since we are using
hyperlist, it is better to refresh the whole list.
This commit is contained in:
Faris Ansari 2018-10-31 19:09:40 +05:30
parent 7f779a3dd8
commit 7eaabcbfe0
9 changed files with 44 additions and 31 deletions

View File

@ -35,7 +35,6 @@
</section> </section>
<script src="./node_modules/clusterize.js/clusterize.js"></script>
<script src="./node_modules/sortablejs/Sortable.js"></script> <script src="./node_modules/sortablejs/Sortable.js"></script>
<script src="./dist/frappe-datatable.js"></script> <script src="./dist/frappe-datatable.js"></script>
<script> <script>

View File

@ -9,7 +9,10 @@ export default class BodyRenderer {
this.rowmanager = instance.rowmanager; this.rowmanager = instance.rowmanager;
this.cellmanager = instance.cellmanager; this.cellmanager = instance.cellmanager;
this.bodyScrollable = instance.bodyScrollable; this.bodyScrollable = instance.bodyScrollable;
this.bodyDiv = $('.dt-body', this.bodyScrollable);
this.log = instance.log; this.log = instance.log;
this.bindEvents();
} }
renderRows(rows) { renderRows(rows) {
@ -23,7 +26,8 @@ export default class BodyRenderer {
return el.children[0]; return el.children[0];
} }
}; };
this.hyperlist.refresh($('.dt-body', this.bodyScrollable), config); this.hyperlist.refresh(this.bodyDiv, config);
this.visibleRows = rows.map(row => row.meta.rowIndex);
} }
render() { render() {
@ -41,8 +45,8 @@ export default class BodyRenderer {
}; };
if (!this.hyperlist) { if (!this.hyperlist) {
this.bodyScrollable.innerHTML = '<div class="dt-body"></div>'; this.hyperlist = new HyperList(this.bodyDiv, config);
this.hyperlist = new HyperList($('.dt-body', this.bodyScrollable), config); this.visibleRows = rows.map(row => row.meta.rowIndex);
} else { } else {
this.renderRows(rows); this.renderRows(rows);
} }

View File

@ -736,6 +736,8 @@ export default class CellManager {
isFilter isFilter
}); });
const row = this.datamanager.getRow(rowIndex);
const isBodyCell = !(isHeader || isFilter); const isBodyCell = !(isHeader || isFilter);
const className = [ const className = [
@ -745,7 +747,8 @@ export default class CellManager {
isBodyCell ? 'dt-cell--row-' + rowIndex : '', isBodyCell ? 'dt-cell--row-' + rowIndex : '',
isHeader ? 'dt-cell--header' : '', isHeader ? 'dt-cell--header' : '',
isHeader ? `dt-cell--header-${colIndex}` : '', isHeader ? `dt-cell--header-${colIndex}` : '',
isFilter ? 'dt-cell--filter' : '' isFilter ? 'dt-cell--filter' : '',
isBodyCell && row.meta.isTreeNodeClose ? 'dt-cell--tree-close' : ''
].join(' '); ].join(' ');
return ` return `

View File

@ -286,8 +286,7 @@ export default class ColumnManager {
.then(({ .then(({
rowsToShow rowsToShow
}) => { }) => {
const rows = rowsToShow.map(rowIndex => this.datamanager.getRow(rowIndex)); this.rowmanager.showRows(rowsToShow);
this.bodyRenderer.renderRows(rows);
}); });
} }

View File

@ -193,6 +193,7 @@ export default class DataManager {
row.meta.isLeaf = !nextRow || row.meta.isLeaf = !nextRow ||
notSet(nextRow.meta.indent) || notSet(nextRow.meta.indent) ||
nextRow.meta.indent <= row.meta.indent; nextRow.meta.indent <= row.meta.indent;
row.meta.isTreeNodeClose = false;
} }
}); });
} }

View File

@ -72,6 +72,7 @@ class DataTable {
<div class="dt-header"> <div class="dt-header">
</div> </div>
<div class="dt-scrollable"> <div class="dt-scrollable">
<div class="dt-body"></div>
</div> </div>
<div class="dt-freeze"> <div class="dt-freeze">
<span class="dt-freeze__message"> <span class="dt-freeze__message">

View File

@ -3,7 +3,8 @@ import {
makeDataAttributeString, makeDataAttributeString,
nextTick, nextTick,
ensureArray, ensureArray,
linkProperties linkProperties,
uniq
} from './utils'; } from './utils';
export default class RowManager { export default class RowManager {
@ -174,18 +175,10 @@ export default class RowManager {
} }
} }
hideRows(rowIndices) {
rowIndices = ensureArray(rowIndices);
const selector = rowIndices.map(this.selector).join(',');
this.style.setStyle(selector, {
display: 'none'
});
}
showRows(rowIndices) { showRows(rowIndices) {
rowIndices = ensureArray(rowIndices); rowIndices = ensureArray(rowIndices);
const selector = rowIndices.map(this.selector).join(','); const rows = rowIndices.map(rowIndex => this.datamanager.getRow(rowIndex));
this.style.removeStyle(selector); this.bodyRenderer.renderRows(rows);
} }
showAllRows() { showAllRows() {
@ -194,25 +187,32 @@ export default class RowManager {
} }
openSingleNode(rowIndex) { openSingleNode(rowIndex) {
const rowsToShow = this.datamanager.getImmediateChildren(rowIndex); const row = this.datamanager.getRow(rowIndex);
row.meta.isTreeNodeClose = false;
const childrenToShow = this.datamanager.getImmediateChildren(rowIndex);
const visibleRows = this.bodyRenderer.visibleRows;
const rowsToShow = uniq([...childrenToShow, ...visibleRows]).sort();
this.showRows(rowsToShow); this.showRows(rowsToShow);
this.cellmanager.toggleTreeButton(rowIndex, true);
} }
closeSingleNode(rowIndex) { closeSingleNode(rowIndex) {
const children = this.datamanager.getImmediateChildren(rowIndex); const row = this.datamanager.getRow(rowIndex);
children.forEach(childIndex => { row.meta.isTreeNodeClose = true;
const row = this.datamanager.getRow(childIndex);
if (row.meta.isLeaf) { const rowsToHide = this.datamanager.getChildren(rowIndex);
// close const visibleRows = this.bodyRenderer.visibleRows;
this.hideRows(childIndex); const rowsToShow = visibleRows.filter(rowIndex => !rowsToHide.includes(rowIndex));
this.cellmanager.toggleTreeButton(childIndex, false);
} else { rowsToHide.forEach(rowIndex => {
this.closeSingleNode(childIndex); const row = this.datamanager.getRow(rowIndex);
this.hideRows(childIndex); if (!row.meta.isLeaf) {
row.meta.isTreeNodeClose = true;
} }
}); });
this.cellmanager.toggleTreeButton(rowIndex, false);
this.showRows(rowsToShow);
} }
getRow$(rowIndex) { getRow$(rowIndex) {

View File

@ -46,6 +46,7 @@
height: 40vw; height: 40vw;
overflow: auto; overflow: auto;
border-bottom: 1px solid var(--dt-border-color); border-bottom: 1px solid var(--dt-border-color);
border-right: 1px solid var(--dt-border-color);
&--highlight-all { &--highlight-all {
background-color: var(--dt-selection-highlight-color); background-color: var(--dt-selection-highlight-color);

View File

@ -1,5 +1,6 @@
import _throttle from 'lodash/throttle'; import _throttle from 'lodash/throttle';
import _debounce from 'lodash/debounce'; import _debounce from 'lodash/debounce';
import _uniq from 'lodash/uniq';
export function camelCaseToDash(str) { export function camelCaseToDash(str) {
return str.replace(/([A-Z])/g, (g) => `-${g[0].toLowerCase()}`); return str.replace(/([A-Z])/g, (g) => `-${g[0].toLowerCase()}`);
@ -125,3 +126,7 @@ export function ensureArray(val) {
} }
return val; return val;
} }
export function uniq(arr) {
return _uniq(arr);
}