InfiniteScroll and Layer list filter
This commit is contained in:
43
Frontend/src/app/directives/scroll-end.directive.ts
Normal file
43
Frontend/src/app/directives/scroll-end.directive.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { Directive, ElementRef, EventEmitter, Input, OnDestroy, OnInit, Output } from '@angular/core';
|
||||
export enum SCROLLEND_DIRECTION {
|
||||
DOWN = 'down',
|
||||
UP = 'UP',
|
||||
}
|
||||
|
||||
@Directive({
|
||||
selector: '[diunabiScrollEnd]',
|
||||
standalone: true,
|
||||
})
|
||||
export class ScrollEndDirective implements OnInit, OnDestroy {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
@Output() diunabiScrollEnd: EventEmitter<any> = new EventEmitter();
|
||||
|
||||
@Input() rootMargin = '0px 0px 0px 0px';
|
||||
@Input() desiredDirection: SCROLLEND_DIRECTION = SCROLLEND_DIRECTION.DOWN;
|
||||
|
||||
observer?: IntersectionObserver;
|
||||
previousEntry?: IntersectionObserverEntry;
|
||||
scrollDirection?: SCROLLEND_DIRECTION;
|
||||
|
||||
constructor(
|
||||
private el: ElementRef,
|
||||
) { }
|
||||
|
||||
ngOnInit(): void {
|
||||
this.observer = new IntersectionObserver(entries => {
|
||||
entries.forEach(entry => {
|
||||
this.scrollDirection = this.previousEntry?.boundingClientRect.bottom ?? 0 > entry.boundingClientRect.bottom ? SCROLLEND_DIRECTION.DOWN : SCROLLEND_DIRECTION.UP;
|
||||
if (!this.previousEntry?.isIntersecting && entry.isIntersecting && this.scrollDirection === this.desiredDirection) {
|
||||
this.diunabiScrollEnd.emit();
|
||||
}
|
||||
this.previousEntry = entry;
|
||||
});
|
||||
}, {
|
||||
rootMargin: this.rootMargin,
|
||||
});
|
||||
this.observer.observe(this.el.nativeElement);
|
||||
}
|
||||
ngOnDestroy(): void {
|
||||
this.observer?.disconnect();
|
||||
}
|
||||
}
|
||||
@@ -76,13 +76,9 @@ export class Layer extends Base {
|
||||
});
|
||||
}
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
static getList(_http: HttpClient, start: number, limit: number, codes: string[]): any {
|
||||
static getList(_http: HttpClient, start: number, limit: number, name: string, type: LayerType | ''): any {
|
||||
return new Promise((resolve, reject) => {
|
||||
let codesQuery = "";
|
||||
if (codes.length) {
|
||||
codesQuery = `&${codes.map(x => `codes=${x}`).join('&')}`;
|
||||
}
|
||||
_http.get<Layer[]>(`${environment.api.url}/layers?start=${start}&limit=${limit}${codesQuery}`)
|
||||
_http.get<Layer[]>(`${environment.api.url}/layers?start=${start}&limit=${limit}&name=${name}&type=${type}`)
|
||||
.pipe(map(data => data.map(x => new Layer().deserialize(x))))
|
||||
.subscribe({
|
||||
next: (data) => resolve(data),
|
||||
|
||||
@@ -1,4 +1,25 @@
|
||||
<table mat-table [dataSource]="dataSource" (scroll)="onScroll($event)">
|
||||
<br>
|
||||
<div class="row">
|
||||
<div class="col">
|
||||
<mat-form-field appearance="outline" class="search-field">
|
||||
<mat-label>Name</mat-label>
|
||||
<input matInput [(ngModel)]="name" (ngModelChange)="nameUpdate.next($event)">
|
||||
</mat-form-field>
|
||||
</div>
|
||||
<div class="col">
|
||||
<mat-form-field appearance="outline" class="search-field">
|
||||
<mat-label>Type</mat-label>
|
||||
<mat-select (selectionChange)="loadList()" [(ngModel)]="type">
|
||||
<mat-option value="">All</mat-option>
|
||||
<mat-option [value]="LayerType.Import">Import</mat-option>
|
||||
<mat-option [value]="LayerType.Processed">Processed</mat-option>
|
||||
<mat-option [value]="LayerType.Administration">Administration</mat-option>
|
||||
<mat-option [value]="LayerType.Dictionary">Dictionary</mat-option>
|
||||
</mat-select>
|
||||
</mat-form-field>
|
||||
</div>
|
||||
</div>
|
||||
<table mat-table [dataSource]="dataSource">
|
||||
<ng-container matColumnDef="number">
|
||||
<th mat-header-cell *matHeaderCellDef>Number</th>
|
||||
<td mat-cell *matCellDef="let element">{{element.number}}</td>
|
||||
@@ -16,4 +37,5 @@
|
||||
|
||||
<tr mat-header-row *matHeaderRowDef="displayedColumns, sticky: false"></tr>
|
||||
<tr mat-row *matRowDef="let row; columns: displayedColumns;" [routerLink]="['Detail/', row.id]"></tr>
|
||||
</table>
|
||||
</table>
|
||||
<div (diunabiScrollEnd)="loadMore()"></div>
|
||||
@@ -0,0 +1,4 @@
|
||||
.search-field {
|
||||
width: 95%;
|
||||
margin: 5px;
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { Component, HostListener, OnInit } from '@angular/core';
|
||||
import { Component, OnInit } from '@angular/core';
|
||||
import { MatSortModule } from '@angular/material/sort';
|
||||
import { MatTableModule } from '@angular/material/table';
|
||||
import { Layer, LayerType } from 'src/app/models/layer.model';
|
||||
@@ -12,7 +12,9 @@ import { MatSelectModule } from '@angular/material/select';
|
||||
import { FormsModule } from '@angular/forms';
|
||||
import { MatChipsModule} from '@angular/material/chips';
|
||||
import { MatIconModule } from '@angular/material/icon';
|
||||
import { NgFor } from '@angular/common';
|
||||
import { KeyValuePipe, NgFor } from '@angular/common';
|
||||
import { ScrollEndDirective } from 'src/app/directives/scroll-end.directive';
|
||||
import { Subject, debounceTime, distinctUntilChanged } from 'rxjs';
|
||||
|
||||
@Component({
|
||||
selector: 'diunabi-layers-list',
|
||||
@@ -21,7 +23,7 @@ import { NgFor } from '@angular/common';
|
||||
standalone: true,
|
||||
imports: [MatGridListModule, MatButtonModule, RouterLink, MatFormFieldModule,
|
||||
MatInputModule, MatTableModule, MatSortModule, MatSelectModule, FormsModule,
|
||||
MatChipsModule, MatIconModule, NgFor]
|
||||
MatChipsModule, MatIconModule, NgFor, ScrollEndDirective, KeyValuePipe]
|
||||
})
|
||||
export class LayersListComponent implements OnInit {
|
||||
displayedColumns = ['number', 'name', 'type'];
|
||||
@@ -33,51 +35,36 @@ export class LayersListComponent implements OnInit {
|
||||
end: number = this.limit + this.start;
|
||||
loadingInProgress = false;
|
||||
|
||||
type = "";
|
||||
codes: string[] = [];
|
||||
type: LayerType | '' = '';
|
||||
name: string = '';
|
||||
nameUpdate = new Subject<string>();
|
||||
|
||||
constructor(
|
||||
private _http: HttpClient,
|
||||
) { }
|
||||
|
||||
//@HostListener('document:scroll', ['$event'])
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
public async onScroll(event: any) {
|
||||
console.log('on scrool', event);
|
||||
if (event.target.offsetHeight + event.target.scrollTop >= event.target.scrollHeight - 1 && !this.loadingInProgress) {
|
||||
this.loadingInProgress = true;
|
||||
this.dataSource = await Layer.getList(this._http, this.start, this.limit, this.codes);
|
||||
this.start = this.end;
|
||||
this.end = this.limit + this.start;
|
||||
setTimeout(() => {
|
||||
this.loadingInProgress = false;
|
||||
}, 500);
|
||||
}
|
||||
}
|
||||
async ngOnInit() {
|
||||
this.dataSource = await Layer.getList(this._http, this.start, this.limit, this.codes);
|
||||
this.nameUpdate.pipe(
|
||||
debounceTime(400),
|
||||
distinctUntilChanged())
|
||||
.subscribe(() => {
|
||||
this.loadList();
|
||||
});
|
||||
await this.loadList();
|
||||
}
|
||||
|
||||
async loadList() {
|
||||
this.start = 0;
|
||||
this.end = this.limit;
|
||||
this.dataSource = await Layer.getList(this._http, this.start, this.limit, this.name, this.type);
|
||||
}
|
||||
|
||||
async loadMore() {
|
||||
this.start = this.end;
|
||||
this.end = this.limit + this.start;
|
||||
}
|
||||
async removeCode(code: string) {
|
||||
const index = this.codes.indexOf(code);
|
||||
if (index >= 0) {
|
||||
this.start = 0;
|
||||
this.end = this.limit + this.start;
|
||||
this.codes.splice(index, 1);
|
||||
this.dataSource = await Layer.getList(this._http, this.start, this.limit, this.codes);
|
||||
}
|
||||
}
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
async addCode(event: any) {
|
||||
const value = (event.target.value || '').trim();
|
||||
if (value) {
|
||||
this.start = 0;
|
||||
this.end = this.limit + this.start;
|
||||
this.codes.push(value);
|
||||
this.dataSource = await Layer.getList(this._http, this.start, this.limit, this.codes);
|
||||
}
|
||||
event.target.value = '';
|
||||
this.end += this.limit;
|
||||
this.dataSource = this.dataSource.concat(
|
||||
await Layer.getList(this._http, this.start, this.limit, this.name, this.type)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user