|
| 1 | +import { Injectable } from '@angular/core'; |
| 2 | +import { Observable, BehaviorSubject } from 'rxjs'; |
| 3 | +import { map } from 'rxjs/operators'; |
| 4 | +import { HttpClient } from '@angular/common/http'; |
| 5 | +import { IgxGridHierarchicalPipe } from 'projects/igniteui-angular/src/lib/grids/hierarchical-grid/hierarchical-grid.pipes'; |
| 6 | +import { IgxHierarchicalGridAPIService, IgxHierarchicalGridComponent } from 'igniteui-angular'; |
| 7 | + |
| 8 | +@Injectable() |
| 9 | +export class HierarchicalRemoteService { |
| 10 | + |
| 11 | + public remotePagingData: BehaviorSubject<any[]>; |
| 12 | + public cachedData = []; |
| 13 | + requestStartIndex = 0; |
| 14 | + totalCount: number; |
| 15 | + remoteData: Observable<any[]>; |
| 16 | + _remoteData: BehaviorSubject<any[]>; |
| 17 | + url = `https://services.odata.org/V4/Northwind/Northwind.svc/Products`; |
| 18 | + urlBuilder; |
| 19 | + |
| 20 | + constructor(private http: HttpClient, private hierarchyPipe: IgxGridHierarchicalPipe) { |
| 21 | + this._remoteData = new BehaviorSubject([]); |
| 22 | + this.remoteData = this._remoteData.asObservable(); |
| 23 | + } |
| 24 | + |
| 25 | + |
| 26 | + nullData() { |
| 27 | + this._remoteData.next(null); |
| 28 | + } |
| 29 | + |
| 30 | + undefinedData() { |
| 31 | + this._remoteData.next(undefined); |
| 32 | + } |
| 33 | + |
| 34 | + public getDataFromCache(data: any) { |
| 35 | + const startIndex = data.startIndex; |
| 36 | + const endIndex = (data.chunkSize || 11) + startIndex; |
| 37 | + const dataResult = this.cachedData.slice(startIndex, endIndex); |
| 38 | + while (dataResult.length < data.chunkSize) { |
| 39 | + dataResult.unshift({emptyRec: true}); |
| 40 | + } |
| 41 | + this._remoteData.next(dataResult); |
| 42 | + return dataResult; |
| 43 | + } |
| 44 | + |
| 45 | + private _updateCachedData (data: any, startIndex: number, lastChunk: boolean) { |
| 46 | + for (let i = 0; i < data.length; i++) { |
| 47 | + this.cachedData[i + startIndex] = data[i]; |
| 48 | + } |
| 49 | + if (lastChunk) { |
| 50 | + this.cachedData.splice(startIndex + data.length); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + getData(virtualizationState: any, grid: IgxHierarchicalGridComponent, cb?: (any) => void) { |
| 55 | + return this.http.get(this.buildUrl(virtualizationState, grid)).pipe( |
| 56 | + map(response => response), |
| 57 | + ) |
| 58 | + .subscribe(d => { |
| 59 | + const result = d['value']; |
| 60 | + this.totalCount = d['@odata.count']; |
| 61 | + if (this.cachedData.length === 0) { |
| 62 | + this.cachedData = new Array<any>(this.totalCount * 2).fill({emptyRec: true}); |
| 63 | + } |
| 64 | + const processedData = this.hierarchyPipe |
| 65 | + .addHierarchy(grid, result, grid.expansionStates, grid.primaryKey, grid.childLayoutKeys); |
| 66 | + |
| 67 | + const childRecsBeforeIndex = this.cachedData |
| 68 | + .filter((x, index) => grid.isChildGridRecord(x) && index < virtualizationState.startIndex); |
| 69 | + const size = virtualizationState.chunkSize || 11; |
| 70 | + const lastChunk = virtualizationState.startIndex + size === grid.verticalScrollContainer.totalItemCount; |
| 71 | + this._updateCachedData(processedData, this.requestStartIndex + childRecsBeforeIndex.length, lastChunk); |
| 72 | + const allChildRecords = this.cachedData.filter(x => grid.isChildGridRecord(x)); |
| 73 | + grid.verticalScrollContainer.totalItemCount = this.totalCount + allChildRecords.length; |
| 74 | + const dataResult = this.getDataFromCache(virtualizationState); |
| 75 | + this._remoteData.next(dataResult); |
| 76 | + if (cb) { |
| 77 | + cb(dataResult); |
| 78 | + } |
| 79 | + }); |
| 80 | + } |
| 81 | + |
| 82 | + public buildUrl(virtualizationArgs, grid) { |
| 83 | + let qS = ''; |
| 84 | + if (virtualizationArgs) { |
| 85 | + let requiredChunkSize: number; |
| 86 | + const childRecsBeforeIndex = this.cachedData |
| 87 | + .filter((x, index) => grid.isChildGridRecord(x) && index <= virtualizationArgs.startIndex); |
| 88 | + const skip = virtualizationArgs.startIndex - childRecsBeforeIndex.length; |
| 89 | + this.requestStartIndex = skip; |
| 90 | + requiredChunkSize = virtualizationArgs.chunkSize === 0 ? 11 : virtualizationArgs.chunkSize + childRecsBeforeIndex.length; |
| 91 | + const top = requiredChunkSize; |
| 92 | + qS = `&$skip=${skip}&$top=${top}&$count=true`; |
| 93 | + } |
| 94 | + return `${this.url}${qS}`; |
| 95 | + } |
| 96 | +} |
0 commit comments