@ag-grid-community/infinite-row-model
Version:
Advanced Data Grid / Data Table supporting Javascript / Typescript / React / Angular / Vue
2 lines (1 loc) • 12.2 kB
JavaScript
var __defProp=Object.defineProperty,__getOwnPropDesc=Object.getOwnPropertyDescriptor,__getOwnPropNames=Object.getOwnPropertyNames,__hasOwnProp=Object.prototype.hasOwnProperty,__export=(e,t)=>{for(var o in t)__defProp(e,o,{get:t[o],enumerable:!0})},__copyProps=(e,t,o,s)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of __getOwnPropNames(t))!__hasOwnProp.call(e,i)&&i!==o&&__defProp(e,i,{get:()=>t[i],enumerable:!(s=__getOwnPropDesc(t,i))||s.enumerable});return e},__toCommonJS=e=>__copyProps(__defProp({},"__esModule",{value:!0}),e),main_exports={};__export(main_exports,{InfiniteRowModelModule:()=>InfiniteRowModelModule}),module.exports=__toCommonJS(main_exports);var import_core4=require("@ag-grid-community/core"),import_core3=require("@ag-grid-community/core"),import_core2=require("@ag-grid-community/core"),import_core=require("@ag-grid-community/core"),InfiniteBlock=class extends import_core.RowNodeBlock{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){(0,import_core._exists)(t)?e.setDataAndId(t,o.toString()):e.setDataAndId(void 0,void 0)}loadFromDatasource(){const e=this.createLoadParams();if((0,import_core._missing)(this.params.datasource.getRows)){(0,import_core._warnOnce)("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 import_core.RowNode(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 import_core.RowNode(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()}},MAX_EMPTY_BLOCKS_TO_KEEP=2,InfiniteCache=class extends import_core2.BeanStub{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 InfiniteBlock(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")&&(0,import_core2._log)(`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=MAX_EMPTY_BLOCKS_TO_KEEP-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,(0,import_core2._exists)(t)&&(this.lastRowIndexKnown=t),this.lastRowIndexKnown||this.rowCount%this.params.blockSize===0&&this.rowCount++,this.onCacheUpdated()}forEachNodeDeep(e){const t=new import_core2.NumberSequence;this.getBlocksInOrder().forEach(o=>o.forEachNode(e,t,this.rowCount))}getBlocksInOrder(){const e=(o,s)=>o.getId()-s.getId();return(0,import_core2._getAllValuesInObject)(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 import_core2.NumberSequence;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 c=h===e||h===t;(i||c)&&o.push(h),c&&(i=!i)},d,this.rowCount)}}),r||i?[]:o}},InfiniteRowModel=class extends import_core3.BeanStub{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=(0,import_core3._getRowHeightAsNumber)(this.gos),this.addEventListeners(),this.addDestroyFunc(()=>this.destroyCache()),this.verifyProps())}verifyProps(){this.gos.exists("initialGroupOrderComparator")&&(0,import_core3._warnOnce)("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=(0,import_core3._getRowHeightAsNumber)(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!(0,import_core3._jsonEquals)(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;(0,import_core3._getRowIdCallback)(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:(0,import_core3._getRowHeightAsNumber)(this.gos),overflowSize:this.gos.get("cacheOverflowSize"),blockSize:this.gos.get("cacheBlockSize"),lastAccessedSequence:new import_core3.NumberSequence},this.infiniteCache=this.createBean(new InfiniteCache(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 refreshInfiniteCache(e){e.rowModelHelperService?.getInfiniteRowModel()?.refreshCache()}function purgeInfiniteCache(e){e.rowModelHelperService?.getInfiniteRowModel()?.purgeCache()}function getInfiniteRowCount(e){return e.rowModelHelperService?.getInfiniteRowModel()?.getRowCount()}var VERSION="32.3.9",InfiniteRowModelCoreModule=(0,import_core4._defineModule)({version:VERSION,moduleName:`${import_core4.ModuleNames.InfiniteRowModelModule}-core`,rowModel:"infinite",beans:[InfiniteRowModel],dependantModules:[import_core4._RowNodeBlockModule]}),InfiniteRowModelApiModule=(0,import_core4._defineModule)({version:VERSION,moduleName:`${import_core4.ModuleNames.InfiniteRowModelModule}-api`,beans:[import_core4.RowModelHelperService],apiFunctions:{refreshInfiniteCache,purgeInfiniteCache,getInfiniteRowCount},dependantModules:[InfiniteRowModelCoreModule,import_core4._SsrmInfiniteSharedApiModule]}),InfiniteRowModelModule=(0,import_core4._defineModule)({version:VERSION,moduleName:import_core4.ModuleNames.InfiniteRowModelModule,dependantModules:[InfiniteRowModelCoreModule,InfiniteRowModelApiModule]});