UNPKG

ng-cw-v12

Version:

Angular UI Component Library

158 lines (153 loc) 9.69 kB
import * as i0 from '@angular/core'; import { Component, ViewChild, Input, NgModule } from '@angular/core'; import * as i1 from '@angular/common'; import { CommonModule } from '@angular/common'; class VirtualScrollComponent { constructor(ngZone) { this.ngZone = ngZone; /** 列表数据 */ this.ncItems = []; /** 缓冲item数量,前后各2项 */ this.ncBufferCount = 2; this.initialized = false; // 标记 itemHeight 是否已初始化(模板中 *ngIf 使用,需为 public) this.viewInitialized = false; // 标记 ngAfterViewInit 是否已执行(templateContainer 才可用) this.visibleItems = []; //显示items this.totalPadding = 0; //总填充高度 this.topPadding = 0; this.scrollTop = 0; //已滚动高度 this.itemHeight = 1; //若设为0,下方代码获取不到this.templateContainer this.viewportHeight = 0; //视窗可视高度 } /** * 场景 触发行 原因 * 初始就有值(同步) 第 49 行measureAndUpdate ngOnChanges 先执行但 viewInitialized=false 跳过,由 ngAfterViewInit 补调 * 初始无值,后续赋值(异步) 第 75 行measureAndUpdate ngAfterViewInit 时无数据跳过,数据到达时 ngOnChanges 直接调用 */ ngAfterViewInit() { this.viewportHeight = this.viewport.nativeElement.clientHeight; this.viewInitialized = true; // 若 ncItems 在 ngAfterViewInit 之前就已同步赋值,则在此补充执行初始化 if (!this.initialized && this.ncItems.length > 0) { this.measureAndUpdate(); } // 监听 viewport 容器高度变化,在 Angular 区域外观察以避免频繁触发变更检测 this.ngZone.runOutsideAngular(() => { this.resizeObserver = new ResizeObserver(entries => { const newHeight = entries[0].contentRect.height; if (newHeight !== this.viewportHeight) { this.ngZone.run(() => { this.viewportHeight = newHeight; this.updateVisibleItems(); }); } }); this.resizeObserver.observe(this.viewport.nativeElement); }); } ngOnDestroy() { var _a; (_a = this.resizeObserver) === null || _a === void 0 ? void 0 : _a.disconnect(); } ngOnChanges(changes) { if (changes['ncItems'] && this.ncItems.length > 0) { if (!this.initialized) { // templateContainer 仅在 ngAfterViewInit 后可用,未初始化则等待 if (this.viewInitialized) { this.measureAndUpdate(); } // 否则由 ngAfterViewInit 补调,无需处理 } else { this.updateTotalPadding(); this.updateVisibleItems(); } } } /** 测量 itemHeight 并更新列表(需在 ngAfterViewInit 之后调用) */ measureAndUpdate() { // setTimeout(0) 确保 Angular 已完成本次 DOM 渲染 setTimeout(() => { var _a, _b; const element = (_b = (_a = this.templateContainer) === null || _a === void 0 ? void 0 : _a.nativeElement) === null || _b === void 0 ? void 0 : _b.firstChild; if (element) { const styles = getComputedStyle(element); this.itemHeight = element.offsetHeight + parseFloat(styles.marginTop) + parseFloat(styles.marginBottom); this.initialized = true; } this.updateTotalPadding(); this.updateVisibleItems(); }); } onScroll() { this.ngZone.run(() => { this.scrollTop = this.viewport.nativeElement.scrollTop; this.updateVisibleItems(); }); } updateVisibleItems() { const firstVisibleIndex = Math.floor(this.scrollTop / this.itemHeight); const startIndex = Math.max(0, firstVisibleIndex - this.ncBufferCount); const visibleItemsCount = Math.ceil(this.viewportHeight / this.itemHeight); const endIndex = Math.min(firstVisibleIndex + visibleItemsCount + this.ncBufferCount, this.ncItems.length - 1); this.visibleItems = []; for (let i = startIndex; i <= endIndex; i++) { this.visibleItems.push({ index: i, data: this.ncItems[i] }); } this.topPadding = startIndex * this.itemHeight; } updateTotalPadding() { this.totalPadding = this.ncItems.length * this.itemHeight; } //采用trackBy提升性能,只渲染改变的dom trackByIndex(index, item) { return item.index; } } VirtualScrollComponent.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.1.5", ngImport: i0, type: VirtualScrollComponent, deps: [{ token: i0.NgZone }], target: i0.ɵɵFactoryTarget.Component }); VirtualScrollComponent.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "12.0.0", version: "12.1.5", type: VirtualScrollComponent, selector: "nc-virtual-scroll", inputs: { ncItems: "ncItems", ncItemTemplate: "ncItemTemplate", ncBufferCount: "ncBufferCount" }, viewQueries: [{ propertyName: "viewport", first: true, predicate: ["viewport"], descendants: true, static: true }, { propertyName: "templateContainer", first: true, predicate: ["templateContainer"], descendants: true }], usesOnChanges: true, ngImport: i0, template: "<!-- \u7528\u4E8E\u6D4B\u91CF itemHeight \u7684\u9690\u85CF\u63A2\u9488\uFF0C\u4EC5\u5728\u521D\u59CB\u5316\u524D\u4E14\u6709\u6570\u636E\u65F6\u6E32\u67D3\uFF0C\u4E0E visibleItems \u5B8C\u5168\u89E3\u8026 -->\r\n<div #templateContainer *ngIf=\"!initialized && ncItems.length > 0\"\r\n style=\"visibility:hidden;position:absolute;pointer-events:none;\">\r\n <ng-container [ngTemplateOutlet]=\"ncItemTemplate\"\r\n [ngTemplateOutletContext]=\"{$implicit: ncItems[0], index: 0}\"></ng-container>\r\n</div>\r\n\r\n<div class=\"viewport\" #viewport (scroll)=\"onScroll()\">\r\n <div class=\"total-padding\" [style.height.px]=\"totalPadding\">\r\n <div class=\"item-container\" [style.transform]=\"'translateY(' + topPadding + 'px)'\">\r\n <div *ngFor=\"let item of visibleItems; trackBy: trackByIndex\">\r\n <ng-container [ngTemplateOutlet]=\"ncItemTemplate\"\r\n [ngTemplateOutletContext]=\"{$implicit: item.data, index: item.index}\"></ng-container>\r\n </div>\r\n </div>\r\n </div>\r\n</div>", styles: [".viewport{width:100%;height:100%;overflow-y:auto;position:relative}.viewport::-webkit-scrollbar{width:6px;height:6px}.viewport::-webkit-scrollbar-thumb{background-color:#cdc9cf;border-radius:10px;-webkit-transition:background-color .3s ease;transition:background-color .3s ease}.viewport::-webkit-scrollbar-thumb:hover{background-color:#766d7b}.viewport::-webkit-scrollbar-track{background:#0000;cursor:pointer}\n"], directives: [{ type: i1.NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { type: i1.NgTemplateOutlet, selector: "[ngTemplateOutlet]", inputs: ["ngTemplateOutletContext", "ngTemplateOutlet"] }, { type: i1.NgForOf, selector: "[ngFor][ngForOf]", inputs: ["ngForOf", "ngForTrackBy", "ngForTemplate"] }] }); i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.1.5", ngImport: i0, type: VirtualScrollComponent, decorators: [{ type: Component, args: [{ selector: 'nc-virtual-scroll', templateUrl: './virtual-scroll.component.html', styleUrls: ['./virtual-scroll.component.less'] }] }], ctorParameters: function () { return [{ type: i0.NgZone }]; }, propDecorators: { viewport: [{ type: ViewChild, args: ['viewport', { static: true }] }], templateContainer: [{ type: ViewChild, args: ['templateContainer'] }], ncItems: [{ type: Input }], ncItemTemplate: [{ type: Input }], ncBufferCount: [{ type: Input }] } }); class NcVirtualScrollModule { } NcVirtualScrollModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.1.5", ngImport: i0, type: NcVirtualScrollModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); NcVirtualScrollModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "12.0.0", version: "12.1.5", ngImport: i0, type: NcVirtualScrollModule, declarations: [VirtualScrollComponent], imports: [CommonModule], exports: [VirtualScrollComponent] }); NcVirtualScrollModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "12.1.5", ngImport: i0, type: NcVirtualScrollModule, imports: [[ CommonModule ]] }); i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.1.5", ngImport: i0, type: NcVirtualScrollModule, decorators: [{ type: NgModule, args: [{ declarations: [ VirtualScrollComponent ], imports: [ CommonModule ], exports: [ VirtualScrollComponent ] }] }] }); /** * Generated bundle index. Do not edit. */ export { NcVirtualScrollModule, VirtualScrollComponent }; //# sourceMappingURL=ng-cw-v12-virtual-scroll.js.map