UNPKG

@anglr/grid

Version:
135 lines 4.9 kB
import { Injectable, ElementRef, Injector, inject, signal, computed, ChangeDetectorRef } from '@angular/core'; import { isPresent } from '@jscrpt/common'; import { extend } from '@jscrpt/common/extend'; import { DEFAULT_OPTIONS, GRID_PLUGIN_INSTANCES, PAGING_OPTIONS } from '../../misc/tokens'; import * as i0 from "@angular/core"; /** * Abstract class that represents any paging component */ export class PagingAbstractComponent { /** * @inheritdoc */ get options() { return this.optionsValue(); } set options(options) { this.optionsValue.update(opts => extend(true, opts, options)); } /** * @inheritdoc */ get page() { return this.pageValue.asReadonly(); } /** * @inheritdoc */ get itemsPerPage() { return this.itemsPerPageValue.asReadonly(); } //######################### constructor ######################### constructor() { /** * Angular injector used for injecting dependencies */ this.injector = inject(Injector); /** * Instance of change detector */ this.changeDetector = inject(ChangeDetectorRef); //######################### protected properties - template bindings ######################### /** * Signal storing page value */ this.pageValue = signal(null); /** * Signal storing items per page value */ this.itemsPerPageValue = signal(null); /** * Total count of all available items in database */ this.totalCount = signal(0).asReadonly(); //######################### public properties - implementation of Paging ######################### /** * @inheritdoc */ this.pluginElement = inject((ElementRef)); /** * @inheritdoc */ this.gridPlugins = inject(GRID_PLUGIN_INSTANCES, { optional: true }); const options = inject(PAGING_OPTIONS, { optional: true }); const defaultOptions = inject(DEFAULT_OPTIONS); this.optionsValue = signal(extend(true, {}, defaultOptions, options)); } //######################### public methods - implementation of Paging ######################### /** * @inheritdoc */ async initialize(force) { if (!this.gridPlugins) { throw new Error('PagingAbstractComponent: missing gridPlugins!'); } const dataLoader = this.gridPlugins.dataLoader; //data loader obtained and its different instance if (force || (this.dataLoader && this.dataLoader != dataLoader)) { this.dataLoader = null; } //no data loader obtained if (!this.dataLoader && dataLoader) { this.dataLoader = dataLoader; this.totalCount = computed(() => dataLoader.result().totalCount); } const gridInitializer = this.gridPlugins.gridInitializer; //grid initializer obtained and its different instance if (force || (this.gridInitializer && this.gridInitializer != gridInitializer)) { this.gridInitializer = null; } //no grid initializer obtained if (!this.gridInitializer && gridInitializer) { this.gridInitializer = gridInitializer; const initPage = await gridInitializer.getPage(); const initItemsPerPage = await gridInitializer.getItemsPerPage(); if (isPresent(initPage)) { this.pageValue.set(initPage); } if (isPresent(initItemsPerPage)) { this.itemsPerPageValue.set(initItemsPerPage); } } } /** * @inheritdoc */ initOptions() { this.pageValue.set(this.optionsValue().initialPage); this.itemsPerPageValue.set(this.optionsValue().initialItemsPerPage); } /** * @inheritdoc */ invalidateVisuals() { } /** * @inheritdoc */ setPage(page) { this.gridInitializer?.setPage(page); this.pageValue.set(page); } /** * @inheritdoc */ setItemsPerPage(itemsPerPage) { this.gridInitializer?.setItemsPerPage(itemsPerPage); this.itemsPerPageValue.set(itemsPerPage); } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.1.0", ngImport: i0, type: PagingAbstractComponent, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); } static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "19.1.0", ngImport: i0, type: PagingAbstractComponent }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.1.0", ngImport: i0, type: PagingAbstractComponent, decorators: [{ type: Injectable }], ctorParameters: () => [] }); //# sourceMappingURL=pagingAbstract.component.js.map