Browse Source

add: solution template for loading-indicator

pull/20804/head
masum-ulu 2 years ago
parent
commit
a4fc181575
  1. 16
      npm/ng-packs/packages/components/extensible/src/lib/components/extensible-table/extensible-table.component.ts
  2. 51
      npm/ng-packs/packages/theme-shared/src/lib/directives/ngx-datatable-list.directive.ts

16
npm/ng-packs/packages/components/extensible/src/lib/components/extensible-table/extensible-table.component.ts

@ -74,7 +74,14 @@ const DEFAULT_ACTIONS_COLUMN_WIDTH = 150;
templateUrl: './extensible-table.component.html', templateUrl: './extensible-table.component.html',
changeDetection: ChangeDetectionStrategy.OnPush, changeDetection: ChangeDetectionStrategy.OnPush,
}) })
export class ExtensibleTableComponent<R = any> implements OnChanges, OnDestroy { export class ExtensibleTableComponent<R = any> implements OnChanges {
readonly #injector = inject(Injector);
readonly getInjected = this.#injector.get.bind(this.#injector);
protected readonly locale = inject(LOCALE_ID);
protected readonly config = inject(ConfigStateService);
protected readonly entityPropTypeClasses = inject(ENTITY_PROP_TYPE_CLASSES);
protected readonly permissionService = inject(PermissionService);
protected _actionsText!: string; protected _actionsText!: string;
@Input() @Input()
set actionsText(value: string) { set actionsText(value: string) {
@ -106,13 +113,6 @@ export class ExtensibleTableComponent<R = any> implements OnChanges, OnDestroy {
readonly actionList: EntityActionList<R>; readonly actionList: EntityActionList<R>;
readonly trackByFn: TrackByFunction<EntityProp<R>> = (_, item) => item.name; readonly trackByFn: TrackByFunction<EntityProp<R>> = (_, item) => item.name;
locale = inject(LOCALE_ID);
private config = inject(ConfigStateService);
entityPropTypeClasses = inject(ENTITY_PROP_TYPE_CLASSES);
#injector = inject(Injector);
getInjected = this.#injector.get.bind(this.#injector);
permissionService = this.#injector.get(PermissionService);
subscriptionService = this.#injector.get(SubscriptionService); subscriptionService = this.#injector.get(SubscriptionService);
listService = this.#injector.get(ListService); listService = this.#injector.get(ListService);
routerWaitService = this.#injector.get(RouterWaitService); routerWaitService = this.#injector.get(RouterWaitService);

51
npm/ng-packs/packages/theme-shared/src/lib/directives/ngx-datatable-list.directive.ts

@ -7,7 +7,7 @@ import {
DoCheck, DoCheck,
SimpleChanges, SimpleChanges,
inject, inject,
DestroyRef DestroyRef,
} from '@angular/core'; } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { DatatableComponent } from '@swimlane/ngx-datatable'; import { DatatableComponent } from '@swimlane/ngx-datatable';
@ -24,14 +24,14 @@ import {
exportAs: 'ngxDatatableList', exportAs: 'ngxDatatableList',
}) })
export class NgxDatatableListDirective implements OnChanges, OnInit, DoCheck { export class NgxDatatableListDirective implements OnChanges, OnInit, DoCheck {
@Input() list!: ListService;
protected readonly table = inject(DatatableComponent); protected readonly table = inject(DatatableComponent);
protected readonly cdRef = inject(ChangeDetectorRef); protected readonly cdRef = inject(ChangeDetectorRef);
protected readonly destroyRef = inject(DestroyRef); protected readonly destroyRef = inject(DestroyRef);
protected readonly localizationService = inject(LocalizationService); protected readonly localizationService = inject(LocalizationService);
protected readonly ngxDatatableMessages = inject(NGX_DATATABLE_MESSAGES, { optional: true }); protected readonly ngxDatatableMessages = inject(NGX_DATATABLE_MESSAGES, { optional: true });
@Input() list!: ListService;
constructor() { constructor() {
this.setInitialValues(); this.setInitialValues();
} }
@ -43,6 +43,25 @@ export class NgxDatatableListDirective implements OnChanges, OnInit, DoCheck {
ngOnInit() { ngOnInit() {
this.subscribeToPage(); this.subscribeToPage();
this.subscribeToSort(); this.subscribeToSort();
const indicator = document.createElement(`div`);
indicator.innerHTML = `
<div class="d-flex justify-content-center align-items-center m-1">
<div class="spinner-border" role="status" id="loading">
<span class="visually-hidden">Loading...</span>
</div>
</div>
`;
let ref: HTMLDivElement;
this.list.isLoading$.subscribe(isLoading => {
if (isLoading) {
ref = this.table.element.querySelector('.datatable-body').appendChild(indicator);
} else {
const { emptyMessage } = this.ngxDatatableMessages || defaultNgxDatatableMessages;
this.table.messages.emptyMessage = this.localizationService.instant(emptyMessage);
ref?.remove();
}
});
} }
ngOnChanges({ list }: SimpleChanges) { ngOnChanges({ list }: SimpleChanges) {
@ -63,24 +82,26 @@ export class NgxDatatableListDirective implements OnChanges, OnInit, DoCheck {
this.ngxDatatableMessages || defaultNgxDatatableMessages; this.ngxDatatableMessages || defaultNgxDatatableMessages;
this.table.messages = { this.table.messages = {
emptyMessage: this.localizationService.instant(emptyMessage), emptyMessage: '',
totalMessage: this.localizationService.instant(totalMessage), totalMessage: this.localizationService.instant(totalMessage),
selectedMessage: this.localizationService.instant(selectedMessage), selectedMessage: this.localizationService.instant(selectedMessage),
}; };
} }
protected subscribeToSort() { protected subscribeToSort() {
this.table.sort.pipe(takeUntilDestroyed(this.destroyRef)).subscribe(({ sorts: [{ prop, dir }] }) => { this.table.sort
if (prop === this.list.sortKey && this.list.sortOrder === 'desc') { .pipe(takeUntilDestroyed(this.destroyRef))
this.list.sortKey = ''; .subscribe(({ sorts: [{ prop, dir }] }) => {
this.list.sortOrder = ''; if (prop === this.list.sortKey && this.list.sortOrder === 'desc') {
this.table.sorts = []; this.list.sortKey = '';
this.cdRef.detectChanges(); this.list.sortOrder = '';
} else { this.table.sorts = [];
this.list.sortKey = prop; this.cdRef.detectChanges();
this.list.sortOrder = dir; } else {
} this.list.sortKey = prop;
}); this.list.sortOrder = dir;
}
});
} }
protected subscribeToPage() { protected subscribeToPage() {

Loading…
Cancel
Save