UNPKG

imng-kendo-grid-odata

Version:

This library was generated with [Nx](https://nx.dev).

280 lines (272 loc) 12.4 kB
import * as i2 from 'rxjs'; import { isObservable, first, map, merge, filter } from 'rxjs'; import * as i0 from '@angular/core'; import { InjectionToken, Inject, Component, inject, ChangeDetectorRef, Input, Directive, NgModule } from '@angular/core'; import { KendoGridBaseComponent, hasHiddenColumns, ImngKendoGridModule } from 'imng-kendo-grid'; import * as i1 from '@angular/router'; import { isCompositeFilterDescriptor } from '@progress/kendo-data-query'; import { map as map$1 } from 'rxjs/operators'; import { CommonModule } from '@angular/common'; import { GridComponent, GridModule, PDFModule, ExcelModule } from '@progress/kendo-angular-grid'; import { Subscriptions } from 'imng-ngrx-utils'; /* eslint-disable @angular-eslint/prefer-inject */ const FACADE = new InjectionToken('imng-grid-odata-facade'); const STATE = new InjectionToken('imng-grid-odata-odataState'); class KendoODataBasedComponent extends KendoGridBaseComponent { constructor(facade, state, router = null, //NOSONAR gridRefresh$ = null) { super(); this.facade = facade; this.state = state; this.router = router; this.gridRefresh$ = gridRefresh$; /** * This sets the amount of the maximum amount of sortable columns for this component. Default = 5. */ this.maxSortedColumnCount = 5; //NOSONAR this.gridStateQueryKey = 'odataState'; this.excelData = () => this.gridDataResult$; if (this.router?.routerState?.snapshot?.root.queryParams[this.gridStateQueryKey]) { try { this.gridDataState = this.deserializeODataState(this.router?.routerState?.snapshot?.root?.queryParams[this.gridStateQueryKey]); } catch { console.error( //NOSONAR `Exception thrown while deserializing query string parameter: ${this.gridStateQueryKey}.`); } } if (isObservable(state)) { this.allSubscriptions.push(state.subscribe((t) => { this.gridDataState = t; this.expanders = t.expanders; this.transformations = t.transformations; }), state.pipe(first()).subscribe((t) => (this.defaultFilter = t.filter))); } else { this.gridDataState = this.gridDataState ? { ...this.gridDataState, selectors: state.selectors, expanders: state.expanders, } : state; this.expanders = state.expanders; this.defaultFilter = state.filter; this.transformations = state.transformations; } if (gridRefresh$) { this.allSubscriptions.push(gridRefresh$.subscribe(() => this.loadEntities(this.gridDataState))); } } ngOnInit() { if (!this.gridRefresh$) { this.loadEntities(this.gridDataState); } this.loading$ = this.facade.loading$; this.gridDataResult$ = this.facade.gridData$?.pipe(map((gridData) => (gridData ? gridData : { total: 0, data: [] }))); this.gridPagerSettings$ = this.facade.gridPagerSettings$; } deserializeODataState(stateQueryParam) { const state = JSON.parse(atob(stateQueryParam)); state.filter?.filters?.forEach((filter) => this.normalizeFilters(filter)); return state; } /** * This method ensures the proper handling of date filters in an OData query * @param filter */ normalizeFilters(filter) { if (isCompositeFilterDescriptor(filter)) { filter.filters.forEach(this.normalizeFilters); } else if (filter?.field?.toUpperCase().endsWith('DATE') || filter?.field?.toUpperCase().endsWith('UTC')) { filter.value = new Date(filter.value); } } serializeODataState(odataState) { return btoa(JSON.stringify(odataState)); } /** * Will reset filters to initialGrid state passed into the constructor */ resetFilters() { this.gridDataState = { ...this.gridDataState, filter: this.defaultFilter, }; if (!isObservable(this.state)) { this.gridDataState = { ...this.gridDataState, inFilters: this.state.inFilters, childFilters: this.state.childFilters, }; } this.loadEntities(this.gridDataState); } dataStateChange(state) { this.gridDataState = { ...state, expanders: this.expanders, transformations: this.transformations, }; this.loadEntities(this.gridDataState); } loadEntities(odataState) { odataState = this.validateSortParameters(odataState); this.gridDataState = odataState; this.expanders = odataState.expanders; this.transformations = odataState.transformations; this.facade.loadEntities(this.gridDataState); this.updateRouterState(odataState); } validateSortParameters(state) { if (state.sort && (state.sort?.length || 0) > this.maxSortedColumnCount) { state = { ...state, sort: state.sort.slice(0, this.maxSortedColumnCount), }; console.warn(`You have exceeded the limit of ${this.maxSortedColumnCount} sorted columns for the current grid. MAX-Sorted-Column-Count`); //NOSONAR } return state; } updateRouterState(state) { if (this.router) { const tempState = { ...state }; delete tempState.selectors; delete tempState.expanders; this.router.navigate([], { relativeTo: this.router.routerState.root, queryParams: { [this.gridStateQueryKey]: this.serializeODataState(tempState), }, skipLocationChange: false, queryParamsHandling: 'merge', }); } } reloadEntities() { this.facade.reloadEntities(); } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.1.4", ngImport: i0, type: KendoODataBasedComponent, deps: [{ token: FACADE }, { token: STATE }, { token: i1.Router }, { token: i2.Observable }], target: i0.ɵɵFactoryTarget.Component }); } static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "20.1.4", type: KendoODataBasedComponent, isStandalone: false, selector: "ng-component", usesInheritance: true, ngImport: i0, template: '', isInline: true }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.1.4", ngImport: i0, type: KendoODataBasedComponent, decorators: [{ type: Component, args: [{ template: '', standalone: false, }] }], ctorParameters: () => [{ type: undefined, decorators: [{ type: Inject, args: [FACADE] }] }, { type: undefined, decorators: [{ type: Inject, args: [STATE] }] }, { type: i1.Router }, { type: i2.Observable }] }); const getODataPagerSettings = (m) => { if (!m.gridODataState || !m.gridODataState.take || !m.gridData || !m.gridData.total) { return false; } let pageCount = m.gridData.total / m.gridODataState.take; pageCount = Math.min(10, Math.ceil(pageCount)); return { buttonCount: pageCount, info: true, pageSizes: [10, 20, 50, 100], previousNext: true, type: 'numeric', }; }; const mapPagerSettings = () => (source) => source.pipe(map$1((m) => getODataPagerSettings(m))); function createKendoODataGridInitialState() { return { gridData: { data: [], total: 0 }, loading: false, gridODataState: {}, gridPagerSettings: false, error: undefined }; } class ImngODataGridDirective { constructor() { this.gridComponent = inject(GridComponent); this.changeDetectorRef = inject(ChangeDetectorRef); this.allSubscriptions = new Subscriptions(); } ngOnInit() { this.facade = this.odataComponent.facade || {}; this.gridComponent.reorderable = true; this.gridComponent.resizable = true; this.gridComponent.filterable = 'menu'; this.gridComponent.sortable = { allowUnsort: true, mode: 'multiple', }; this.gridComponent.navigable = true; this.allSubscriptions.push(this.facade.loading$.subscribe((t) => { this.gridComponent.loading = t; this.changeDetectorRef.markForCheck(); })); this.odataComponent.hasHiddenColumns$ = merge(this.odataComponent.facade.loading$.pipe(hasHiddenColumns(this.gridComponent)), this.gridComponent.columnVisibilityChange?.pipe(hasHiddenColumns(this.gridComponent))); } ngAfterViewInit() { this.allSubscriptions.push(this.gridComponent.dataStateChange.subscribe((t) => this.odataComponent.dataStateChange(t)), this.facade.gridData$.subscribe((t) => { this.gridComponent.data = t || []; this.changeDetectorRef.markForCheck(); }), this.facade.gridPagerSettings$.subscribe((t) => (this.gridComponent.pageable = t)), this.facade.gridODataState$.pipe(filter((t) => !!t)).subscribe((t) => { this.gridComponent.pageSize = t?.take || 20; //NOSONAR this.gridComponent.filter = t?.filter || { logic: 'and', filters: [] }; this.gridComponent.skip = t?.skip || 0; this.gridComponent.sort = t?.sort || []; })); } ngOnDestroy() { this.allSubscriptions.unsubscribeAll(); } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.1.4", ngImport: i0, type: ImngODataGridDirective, deps: [], target: i0.ɵɵFactoryTarget.Directive }); } static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "20.1.4", type: ImngODataGridDirective, isStandalone: false, selector: "[imngODataGrid]", inputs: { odataComponent: ["imngODataGrid", "odataComponent"] }, ngImport: i0 }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.1.4", ngImport: i0, type: ImngODataGridDirective, decorators: [{ type: Directive, args: [{ selector: '[imngODataGrid]', standalone: false, }] }], propDecorators: { odataComponent: [{ type: Input, args: ['imngODataGrid'] }] } }); class ImngKendoGridODataModule { static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.1.4", ngImport: i0, type: ImngKendoGridODataModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); } static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "20.1.4", ngImport: i0, type: ImngKendoGridODataModule, declarations: [ImngODataGridDirective], imports: [CommonModule, GridModule, PDFModule, ExcelModule, ImngKendoGridModule], exports: [ImngODataGridDirective] }); } static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "20.1.4", ngImport: i0, type: ImngKendoGridODataModule, imports: [CommonModule, GridModule, PDFModule, ExcelModule, ImngKendoGridModule] }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.1.4", ngImport: i0, type: ImngKendoGridODataModule, decorators: [{ type: NgModule, args: [{ declarations: [ImngODataGridDirective], imports: [ CommonModule, GridModule, PDFModule, ExcelModule, ImngKendoGridModule, ], exports: [ImngODataGridDirective], }] }] }); /** * Generated bundle index. Do not edit. */ export { ImngKendoGridODataModule, ImngODataGridDirective, KendoODataBasedComponent, createKendoODataGridInitialState, getODataPagerSettings, mapPagerSettings }; //# sourceMappingURL=imng-kendo-grid-odata.mjs.map