Skip to content
Snippets Groups Projects
Commit 21d4e9a8 authored by Xiao Gui's avatar Xiao Gui
Browse files

chore: refactor virtual scroll

parent 94630445
No related branches found
No related tags found
No related merge requests found
import {EventEmitter, Component, Input, Output, ChangeDetectionStrategy, OnChanges, ViewChild} from "@angular/core"; import {EventEmitter, Component, Input, Output, ChangeDetectionStrategy, ViewChild, AfterViewChecked} from "@angular/core";
import { FlattenedTreeInterface } from "./flattener.pipe"; import { FlattenedTreeInterface } from "./flattener.pipe";
import {CdkVirtualScrollViewport} from "@angular/cdk/scrolling"; import {CdkVirtualScrollViewport} from "@angular/cdk/scrolling";
...@@ -15,7 +15,7 @@ import {CdkVirtualScrollViewport} from "@angular/cdk/scrolling"; ...@@ -15,7 +15,7 @@ import {CdkVirtualScrollViewport} from "@angular/cdk/scrolling";
changeDetection:ChangeDetectionStrategy.OnPush changeDetection:ChangeDetectionStrategy.OnPush
}) })
export class FlatTreeComponent implements OnChanges { export class FlatTreeComponent implements AfterViewChecked {
@Input() inputItem : any = { @Input() inputItem : any = {
name : 'Untitled', name : 'Untitled',
children : [] children : []
...@@ -34,8 +34,9 @@ export class FlatTreeComponent implements OnChanges { ...@@ -34,8 +34,9 @@ export class FlatTreeComponent implements OnChanges {
@Input() findChildren : (item:any)=>any[] = (item)=>item.children ? item.children : [] @Input() findChildren : (item:any)=>any[] = (item)=>item.children ? item.children : []
@Input() searchFilter : (item:any)=>boolean | null = ()=>true @Input() searchFilter : (item:any)=>boolean | null = ()=>true
@ViewChild('flatTreeVirtualScrollViewPort') _temp: CdkVirtualScrollViewport @ViewChild('flatTreeVirtualScrollViewPort') virtualScrollViewPort: CdkVirtualScrollViewport
@Output() uncollaspedItemsNumber = new EventEmitter<number>() @Output() totalRenderedListChanged = new EventEmitter<{ previous: number, current: number }>()
private totalDataLength: number = null
public flattenedItems : any[] = [] public flattenedItems : any[] = []
...@@ -46,10 +47,15 @@ export class FlatTreeComponent implements OnChanges { ...@@ -46,10 +47,15 @@ export class FlatTreeComponent implements OnChanges {
collapsedLevels: Set<string> = new Set() collapsedLevels: Set<string> = new Set()
uncollapsedLevels : Set<string> = new Set() uncollapsedLevels : Set<string> = new Set()
ngOnChanges(): void { ngAfterViewChecked(){
if (this._temp) { const currentTotalDataLength = this.virtualScrollViewPort.getDataLength()
setTimeout(() => {this.uncollaspedItemsNumber.emit(this._temp.getDataLength())}) const previousDataLength = this.totalDataLength
}
this.totalRenderedListChanged.emit({
current: currentTotalDataLength,
previous: previousDataLength
})
this.totalDataLength = currentTotalDataLength
} }
toggleCollapse(flattenedItem:FlattenedTreeInterface){ toggleCollapse(flattenedItem:FlattenedTreeInterface){
...@@ -62,7 +68,6 @@ export class FlatTreeComponent implements OnChanges { ...@@ -62,7 +68,6 @@ export class FlatTreeComponent implements OnChanges {
} }
this.collapsedLevels = new Set(this.collapsedLevels) this.collapsedLevels = new Set(this.collapsedLevels)
this.uncollapsedLevels = new Set(this.uncollapsedLevels) this.uncollapsedLevels = new Set(this.uncollapsedLevels)
setTimeout(() => {this.uncollaspedItemsNumber.emit(this._temp.getDataLength())})
} }
isCollapsed(flattenedItem:FlattenedTreeInterface):boolean{ isCollapsed(flattenedItem:FlattenedTreeInterface):boolean{
......
...@@ -62,15 +62,20 @@ export class RegionHierarchy implements OnInit, AfterViewInit{ ...@@ -62,15 +62,20 @@ export class RegionHierarchy implements OnInit, AfterViewInit{
@ViewChild('searchTermInput', {read: ElementRef}) @ViewChild('searchTermInput', {read: ElementRef})
private searchTermInput: ElementRef private searchTermInput: ElementRef
countItemsIntoTheTree: number /**
* set the height to max, bound by max-height
*/
numTotalRenderedRegions: number = 999
windowHeight: number windowHeight: number
@HostListener('document:click', ['$event']) @HostListener('document:click', ['$event'])
closeRegion(event: MouseEvent) { closeRegion(event: MouseEvent) {
const contains = this.el.nativeElement.contains(event.target) const contains = this.el.nativeElement.contains(event.target)
this.showRegionTree = contains this.showRegionTree = contains
if (!this.showRegionTree) if (!this.showRegionTree){
this.searchTerm = '' this.searchTerm = ''
this.numTotalRenderedRegions = 999
}
} }
@HostListener('window:resize', ['$event']) @HostListener('window:resize', ['$event'])
...@@ -149,15 +154,6 @@ export class RegionHierarchy implements OnInit, AfterViewInit{ ...@@ -149,15 +154,6 @@ export class RegionHierarchy implements OnInit, AfterViewInit{
this.changeSearchTerm(ev) this.changeSearchTerm(ev)
}) })
) )
setTimeout(() => {
this.countItemsIntoTheTree = 1
if (this.aggregatedRegionTree.children &&
this.aggregatedRegionTree.children.length > 0) {
this.countItems(this.aggregatedRegionTree)
}
})
} }
getInputPlaceholder(parcellation:any) { getInputPlaceholder(parcellation:any) {
...@@ -174,20 +170,14 @@ export class RegionHierarchy implements OnInit, AfterViewInit{ ...@@ -174,20 +170,14 @@ export class RegionHierarchy implements OnInit, AfterViewInit{
} }
countItems(objectToCount) { handleTotalRenderedListChanged(changeEvent: {previous: number, current: number}){
objectToCount.children.forEach(object => { const { current } = changeEvent
this.countItemsIntoTheTree += 1 this.numTotalRenderedRegions = current
if (object.children && object.children.length > 0) this.countItems(object)
})
}
uncollapsedFlatTreeItems(event) {
this.countItemsIntoTheTree = event
} }
regionHierarchyHeight(){ regionHierarchyHeight(){
return({ return({
'height' : (this.countItemsIntoTheTree * 15 + 60).toString() + 'px', 'height' : (this.numTotalRenderedRegions * 15 + 60).toString() + 'px',
'max-height': (this.windowHeight - 100) + 'px' 'max-height': (this.windowHeight - 100) + 'px'
}) })
} }
......
...@@ -38,7 +38,7 @@ ...@@ -38,7 +38,7 @@
class="tree-body"> class="tree-body">
<flat-tree-component <flat-tree-component
(treeNodeClick)="handleClickRegion($event)" (treeNodeClick)="handleClickRegion($event)"
(uncollaspedItemsNumber)="uncollapsedFlatTreeItems($event)" (totalRenderedListChanged)="handleTotalRenderedListChanged($event)"
[inputItem]="aggregatedRegionTree" [inputItem]="aggregatedRegionTree"
[renderNode]="displayTreeNode" [renderNode]="displayTreeNode"
[searchFilter]="filterTreeBySearch"> [searchFilter]="filterTreeBySearch">
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment