@ag-grid-community/infinite-row-model
Version:
Advanced Data Grid / Data Table supporting Javascript / Typescript / React / Angular / Vue
2 lines (1 loc) • 10.7 kB
JavaScript
import{ModuleNames as c,RowModelHelperService as p,_RowNodeBlockModule as I,_SsrmInfiniteSharedApiModule as m,_defineModule as l}from"@ag-grid-community/core";import{BeanStub as k,NumberSequence as v,_getRowHeightAsNumber as u,_getRowIdCallback as B,_jsonEquals as S,_warnOnce as M}from"@ag-grid-community/core";import{BeanStub as y,NumberSequence as f,_exists as E,_getAllValuesInObject as N,_log as x}from"@ag-grid-community/core";import{RowNode as R,RowNodeBlock as b,_exists as L,_missing as H,_warnOnce as P}from"@ag-grid-community/core";var A=class extends b{wireBeans(e){this.beans=e}constructor(e,t,o){super(e),this.parentCache=t,this.params=o,this.startRow=e*o.blockSize,this.endRow=this.startRow+o.blockSize}postConstruct(){this.createRowNodes()}getBlockStateJson(){return{id:""+this.getId(),state:{blockNumber:this.getId(),startRow:this.getStartRow(),endRow:this.getEndRow(),pageStatus:this.getState()}}}setDataAndId(e,t,o){L(t)?e.setDataAndId(t,o.toString()):e.setDataAndId(void 0,void 0)}loadFromDatasource(){const e=this.createLoadParams();if(H(this.params.datasource.getRows)){P("datasource is missing getRows method");return}window.setTimeout(()=>{this.params.datasource.getRows(e)},0)}processServerFail(){}createLoadParams(){return{startRow:this.getStartRow(),endRow:this.getEndRow(),successCallback:this.pageLoaded.bind(this,this.getVersion()),failCallback:this.pageLoadFailed.bind(this,this.getVersion()),sortModel:this.params.sortModel,filterModel:this.params.filterModel,context:this.gos.getGridCommonParams().context}}forEachNode(e,t,o){this.rowNodes.forEach((s,i)=>{this.startRow+i<o&&e(s,t.next())})}getLastAccessed(){return this.lastAccessed}getRow(e,t=!1){t||(this.lastAccessed=this.params.lastAccessedSequence.next());const o=e-this.startRow;return this.rowNodes[o]}getStartRow(){return this.startRow}getEndRow(){return this.endRow}createRowNodes(){this.rowNodes=[];for(let e=0;e<this.params.blockSize;e++){const t=this.startRow+e,o=new R(this.beans);o.setRowHeight(this.params.rowHeight),o.uiLevel=0,o.setRowIndex(t),o.setRowTop(this.params.rowHeight*t),this.rowNodes.push(o)}}processServerResult(e){this.rowNodes.forEach((o,s)=>{const i=e.rowData?e.rowData[s]:void 0;!o.id&&o.alreadyRendered&&i&&(this.rowNodes[s]=new R(this.beans),this.rowNodes[s].setRowIndex(o.rowIndex),this.rowNodes[s].setRowTop(o.rowTop),this.rowNodes[s].setRowHeight(o.rowHeight),o.clearRowTopAndRowIndex()),this.setDataAndId(this.rowNodes[s],i,this.startRow+s)});const t=e.rowCount!=null&&e.rowCount>=0?e.rowCount:void 0;this.parentCache.pageLoaded(this,t)}destroy(){this.rowNodes.forEach(e=>{e.clearRowTopAndRowIndex()}),super.destroy()}},D=2,F=class extends y{constructor(e){super(),this.lastRowIndexKnown=!1,this.blocks={},this.blockCount=0,this.rowCount=e.initialRowCount,this.params=e}wireBeans(e){this.rowRenderer=e.rowRenderer,this.focusService=e.focusService}getRow(e,t=!1){const o=Math.floor(e/this.params.blockSize);let s=this.blocks[o];if(!s){if(t)return;s=this.createBlock(o)}return s.getRow(e)}createBlock(e){const t=this.createBean(new A(e,this,this.params));return this.blocks[t.getId()]=t,this.blockCount++,this.purgeBlocksIfNeeded(t),this.params.rowNodeBlockLoader.addBlock(t),t}refreshCache(){if(this.blockCount==0){this.purgeCache();return}this.getBlocksInOrder().forEach(t=>t.setStateWaitingToLoad()),this.params.rowNodeBlockLoader.checkBlockToLoad()}destroy(){this.getBlocksInOrder().forEach(e=>this.destroyBlock(e)),super.destroy()}getRowCount(){return this.rowCount}isLastRowIndexKnown(){return this.lastRowIndexKnown}pageLoaded(e,t){this.isAlive()&&(this.gos.get("debug")&&x(`InfiniteCache - onPageLoaded: page = ${e.getId()}, lastRow = ${t}`),this.checkRowCount(e,t),this.onCacheUpdated())}purgeBlocksIfNeeded(e){const t=this.getBlocksInOrder().filter(r=>r!=e),o=(r,n)=>n.getLastAccessed()-r.getLastAccessed();t.sort(o);const s=this.params.maxBlocksInCache>0,i=s?this.params.maxBlocksInCache-1:null,d=D-1;t.forEach((r,n)=>{const a=r.getState()==="needsLoading"&&n>=d,h=s?n>=i:!1;if(a||h){if(this.isBlockCurrentlyDisplayed(r)||this.isBlockFocused(r))return;this.removeBlockFromCache(r)}})}isBlockFocused(e){const t=this.focusService.getFocusCellToUseAfterRefresh();if(!t||t.rowPinned!=null)return!1;const o=e.getStartRow(),s=e.getEndRow();return t.rowIndex>=o&&t.rowIndex<s}isBlockCurrentlyDisplayed(e){const t=e.getStartRow(),o=e.getEndRow()-1;return this.rowRenderer.isRangeInRenderedViewport(t,o)}removeBlockFromCache(e){e&&this.destroyBlock(e)}checkRowCount(e,t){if(typeof t=="number"&&t>=0)this.rowCount=t,this.lastRowIndexKnown=!0;else if(!this.lastRowIndexKnown){const s=(e.getId()+1)*this.params.blockSize+this.params.overflowSize;this.rowCount<s&&(this.rowCount=s)}}setRowCount(e,t){this.rowCount=e,E(t)&&(this.lastRowIndexKnown=t),this.lastRowIndexKnown||this.rowCount%this.params.blockSize===0&&this.rowCount++,this.onCacheUpdated()}forEachNodeDeep(e){const t=new f;this.getBlocksInOrder().forEach(o=>o.forEachNode(e,t,this.rowCount))}getBlocksInOrder(){const e=(o,s)=>o.getId()-s.getId();return N(this.blocks).sort(e)}destroyBlock(e){delete this.blocks[e.getId()],this.destroyBean(e),this.blockCount--,this.params.rowNodeBlockLoader.removeBlock(e)}onCacheUpdated(){this.isAlive()&&(this.destroyAllBlocksPastVirtualRowCount(),this.eventService.dispatchEvent({type:"storeUpdated"}))}destroyAllBlocksPastVirtualRowCount(){const e=[];this.getBlocksInOrder().forEach(t=>{t.getId()*this.params.blockSize>=this.rowCount&&e.push(t)}),e.length>0&&e.forEach(t=>this.destroyBlock(t))}purgeCache(){this.getBlocksInOrder().forEach(e=>this.removeBlockFromCache(e)),this.lastRowIndexKnown=!1,this.rowCount===0&&(this.rowCount=this.params.initialRowCount),this.onCacheUpdated()}getRowNodesInRange(e,t){const o=[];let s=-1,i=!1;const d=new f;let r=!1;return this.getBlocksInOrder().forEach(a=>{if(!r){if(i&&s+1!==a.getId()){r=!0;return}s=a.getId(),a.forEachNode(h=>{const g=h===e||h===t;(i||g)&&o.push(h),g&&(i=!i)},d,this.rowCount)}}),r||i?[]:o}},T=class extends k{constructor(){super(...arguments),this.beanName="rowModel"}wireBeans(e){this.filterManager=e.filterManager,this.sortController=e.sortController,this.selectionService=e.selectionService,this.rowRenderer=e.rowRenderer,this.rowNodeBlockLoader=e.rowNodeBlockLoader}getRowBounds(e){return{rowHeight:this.rowHeight,rowTop:this.rowHeight*e}}ensureRowHeightsValid(){return!1}postConstruct(){this.gos.get("rowModelType")==="infinite"&&(this.rowHeight=u(this.gos),this.addEventListeners(),this.addDestroyFunc(()=>this.destroyCache()),this.verifyProps())}verifyProps(){this.gos.exists("initialGroupOrderComparator")&&M("initialGroupOrderComparator cannot be used with Infinite Row Model as sorting is done on the server side")}start(){this.setDatasource(this.gos.get("datasource"))}destroy(){this.destroyDatasource(),super.destroy()}destroyDatasource(){this.datasource&&(this.destroyBean(this.datasource),this.rowRenderer.datasourceChanged(),this.datasource=null)}addEventListeners(){this.addManagedEventListeners({filterChanged:this.onFilterChanged.bind(this),sortChanged:this.onSortChanged.bind(this),newColumnsLoaded:this.onColumnEverything.bind(this),storeUpdated:this.onCacheUpdated.bind(this)}),this.addManagedPropertyListener("datasource",()=>this.setDatasource(this.gos.get("datasource"))),this.addManagedPropertyListener("cacheBlockSize",()=>this.resetCache()),this.addManagedPropertyListener("rowHeight",()=>{this.rowHeight=u(this.gos),this.cacheParams.rowHeight=this.rowHeight,this.updateRowHeights()})}onFilterChanged(){this.reset()}onSortChanged(){this.reset()}onColumnEverything(){let e;this.cacheParams?e=this.isSortModelDifferent():e=!0,e&&this.reset()}isSortModelDifferent(){return!S(this.cacheParams.sortModel,this.sortController.getSortModel())}getType(){return"infinite"}setDatasource(e){this.destroyDatasource(),this.datasource=e,e&&this.reset()}isEmpty(){return!this.infiniteCache}isRowsToRender(){return!!this.infiniteCache}getNodesInRangeForSelection(e,t){return this.infiniteCache?this.infiniteCache.getRowNodesInRange(e,t):[]}reset(){if(!this.datasource)return;B(this.gos)!=null||this.selectionService.reset("rowDataChanged"),this.resetCache()}dispatchModelUpdatedEvent(){this.eventService.dispatchEvent({type:"modelUpdated",newPage:!1,newPageSize:!1,newData:!1,keepRenderedRows:!0,animate:!1})}resetCache(){this.destroyCache(),this.cacheParams={datasource:this.datasource,filterModel:this.filterManager?.getFilterModel()??{},sortModel:this.sortController.getSortModel(),rowNodeBlockLoader:this.rowNodeBlockLoader,initialRowCount:this.gos.get("infiniteInitialRowCount"),maxBlocksInCache:this.gos.get("maxBlocksInCache"),rowHeight:u(this.gos),overflowSize:this.gos.get("cacheOverflowSize"),blockSize:this.gos.get("cacheBlockSize"),lastAccessedSequence:new v},this.infiniteCache=this.createBean(new F(this.cacheParams)),this.eventService.dispatchEventOnce({type:"rowCountReady"}),this.dispatchModelUpdatedEvent()}updateRowHeights(){this.forEachNode(e=>{e.setRowHeight(this.rowHeight),e.setRowTop(this.rowHeight*e.rowIndex)}),this.dispatchModelUpdatedEvent()}destroyCache(){this.infiniteCache&&(this.infiniteCache=this.destroyBean(this.infiniteCache))}onCacheUpdated(){this.dispatchModelUpdatedEvent()}getRow(e){if(this.infiniteCache&&!(e>=this.infiniteCache.getRowCount()))return this.infiniteCache.getRow(e)}getRowNode(e){let t;return this.forEachNode(o=>{o.id===e&&(t=o)}),t}forEachNode(e){this.infiniteCache&&this.infiniteCache.forEachNodeDeep(e)}getTopLevelRowCount(){return this.getRowCount()}getTopLevelRowDisplayedIndex(e){return e}getRowIndexAtPixel(e){if(this.rowHeight!==0){const t=Math.floor(e/this.rowHeight),o=this.getRowCount()-1;return t>o?o:t}return 0}getRowCount(){return this.infiniteCache?this.infiniteCache.getRowCount():0}isRowPresent(e){return!!this.getRowNode(e.id)}refreshCache(){this.infiniteCache&&this.infiniteCache.refreshCache()}purgeCache(){this.infiniteCache&&this.infiniteCache.purgeCache()}isLastRowIndexKnown(){return this.infiniteCache?this.infiniteCache.isLastRowIndexKnown():!1}setRowCount(e,t){this.infiniteCache&&this.infiniteCache.setRowCount(e,t)}};function O(e){e.rowModelHelperService?.getInfiniteRowModel()?.refreshCache()}function _(e){e.rowModelHelperService?.getInfiniteRowModel()?.purgeCache()}function z(e){return e.rowModelHelperService?.getInfiniteRowModel()?.getRowCount()}var w="32.3.9",C=l({version:w,moduleName:`${c.InfiniteRowModelModule}-core`,rowModel:"infinite",beans:[T],dependantModules:[I]}),U=l({version:w,moduleName:`${c.InfiniteRowModelModule}-api`,beans:[p],apiFunctions:{refreshInfiniteCache:O,purgeInfiniteCache:_,getInfiniteRowCount:z},dependantModules:[C,m]}),K=l({version:w,moduleName:c.InfiniteRowModelModule,dependantModules:[C,U]});export{K as InfiniteRowModelModule};