gp-grid-core 0.1.2 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +203 -0
- package/README.md +423 -362
- package/dist/index.d.ts +214 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +157 -1
- package/dist/index.js.map +1 -1
- package/package.json +19 -7
package/dist/index.js
CHANGED
|
@@ -1,3 +1,159 @@
|
|
|
1
1
|
var e=class{state={activeCell:null,range:null,anchor:null,selectionMode:!1};options;listeners=[];constructor(e){this.options=e}onInstruction(e){return this.listeners.push(e),()=>{this.listeners=this.listeners.filter(t=>t!==e)}}emit(e){for(let t of this.listeners)t(e)}getState(){return{...this.state}}getActiveCell(){return this.state.activeCell}getSelectionRange(){return this.state.range}isSelected(e,t){let{range:n}=this.state;if(!n)return!1;let r=Math.min(n.startRow,n.endRow),i=Math.max(n.startRow,n.endRow),a=Math.min(n.startCol,n.endCol),o=Math.max(n.startCol,n.endCol);return e>=r&&e<=i&&t>=a&&t<=o}isActiveCell(e,t){let{activeCell:n}=this.state;return n?.row===e&&n?.col===t}startSelection(e,t={}){let{shift:n=!1,ctrl:r=!1}=t,{row:i,col:a}=this.clampPosition(e);n&&this.state.anchor?(this.state.range={startRow:this.state.anchor.row,startCol:this.state.anchor.col,endRow:i,endCol:a},this.state.activeCell={row:i,col:a}):(this.state.activeCell={row:i,col:a},this.state.anchor={row:i,col:a},this.state.range=null),this.state.selectionMode=r,this.emit({type:`SET_ACTIVE_CELL`,position:this.state.activeCell}),this.emit({type:`SET_SELECTION_RANGE`,range:this.state.range})}moveFocus(e,t=!1){if(!this.state.activeCell){this.startSelection({row:0,col:0});return}let{row:n,col:r}=this.state.activeCell,i=n,a=r;switch(e){case`up`:i=Math.max(0,n-1);break;case`down`:i=Math.min(this.options.getRowCount()-1,n+1);break;case`left`:a=Math.max(0,r-1);break;case`right`:a=Math.min(this.options.getColumnCount()-1,r+1);break}t?(this.state.anchor||(this.state.anchor={row:n,col:r}),this.state.range={startRow:this.state.anchor.row,startCol:this.state.anchor.col,endRow:i,endCol:a},this.state.activeCell={row:i,col:a},this.emit({type:`SET_ACTIVE_CELL`,position:this.state.activeCell}),this.emit({type:`SET_SELECTION_RANGE`,range:this.state.range})):(this.state.activeCell={row:i,col:a},this.state.anchor={row:i,col:a},this.state.range=null,this.emit({type:`SET_ACTIVE_CELL`,position:this.state.activeCell}),this.emit({type:`SET_SELECTION_RANGE`,range:null}))}selectAll(){let e=this.options.getRowCount(),t=this.options.getColumnCount();e===0||t===0||(this.state.range={startRow:0,startCol:0,endRow:e-1,endCol:t-1},this.state.activeCell||(this.state.activeCell={row:0,col:0},this.emit({type:`SET_ACTIVE_CELL`,position:this.state.activeCell})),this.emit({type:`SET_SELECTION_RANGE`,range:this.state.range}))}clearSelection(){this.state.activeCell=null,this.state.range=null,this.state.anchor=null,this.state.selectionMode=!1,this.emit({type:`SET_ACTIVE_CELL`,position:null}),this.emit({type:`SET_SELECTION_RANGE`,range:null})}setActiveCell(e,t){let n=this.clampPosition({row:e,col:t});this.state.activeCell=n,this.state.anchor=n,this.state.range=null,this.emit({type:`SET_ACTIVE_CELL`,position:this.state.activeCell}),this.emit({type:`SET_SELECTION_RANGE`,range:null})}setSelectionRange(e){this.state.range=e,this.emit({type:`SET_SELECTION_RANGE`,range:this.state.range})}getSelectedData(){let{range:e,activeCell:t}=this.state;if(!e&&!t)return[];let n=e||{startRow:t.row,startCol:t.col,endRow:t.row,endCol:t.col},r=Math.min(n.startRow,n.endRow),i=Math.max(n.startRow,n.endRow),a=Math.min(n.startCol,n.endCol),o=Math.max(n.startCol,n.endCol),s=[];for(let e=r;e<=i;e++){let t=[];for(let n=a;n<=o;n++)t.push(this.options.getCellValue(e,n));s.push(t)}return s}async copySelectionToClipboard(){let e=this.getSelectedData();if(e.length===0)return;let t=e.map(e=>e.map(e=>e==null?``:String(e)).join(` `)).join(`
|
|
2
|
-
`);try{await navigator.clipboard.writeText(t)}catch{let e=document.createElement(`textarea`);e.value=t,e.style.position=`fixed`,e.style.left=`-9999px`,document.body.appendChild(e),e.select(),document.execCommand(`copy`),document.body.removeChild(e)}}clampPosition(e){let t=this.options.getRowCount(),n=this.options.getColumnCount();return{row:Math.max(0,Math.min(e.row,t-1)),col:Math.max(0,Math.min(e.col,n-1))}}},t=class{state=null;options;listeners=[];constructor(e){this.options=e}onInstruction(e){return this.listeners.push(e),()=>{this.listeners=this.listeners.filter(t=>t!==e)}}emit(e){for(let t of this.listeners)t(e)}getState(){return this.state?{...this.state}:null}isActive(){return this.state!==null}startFillDrag(e){this.state={sourceRange:e,targetRow:e.endRow,targetCol:e.endCol},this.emit({type:`START_FILL`,sourceRange:e})}updateFillDrag(e,t){if(!this.state)return;let n=this.options.getRowCount(),r=this.options.getColumnCount();e=Math.max(0,Math.min(e,n-1)),t=Math.max(0,Math.min(t,r-1)),this.state.targetRow=e,this.state.targetCol=t,this.emit({type:`UPDATE_FILL`,targetRow:e,targetCol:t})}commitFillDrag(){if(!this.state)return;let{sourceRange:e,targetRow:t,targetCol:n}=this.state,r=this.calculateFilledCells(e,t,n);for(let{row:e,col:t,value:n}of r)this.options.setCellValue(e,t,n);this.emit({type:`COMMIT_FILL`,filledCells:r}),this.state=null}cancelFillDrag(){this.state&&(this.state=null,this.emit({type:`CANCEL_FILL`}))}calculateFilledCells(e,t,n){let r=[],i=Math.min(e.startRow,e.endRow),a=Math.max(e.startRow,e.endRow),o=Math.min(e.startCol,e.endCol),s=Math.max(e.startCol,e.endCol),c=t>a,l=t<i,u=n>s,d=n<o;if(c||l)for(let e=o;e<=s;e++){let n=this.getSourceColumnValues(i,a,e),o=this.detectPattern(n);if(c)for(let i=a+1;i<=t;i++){let t=i-a-1,s=this.applyPattern(o,n,t);r.push({row:i,col:e,value:s})}else if(l)for(let a=i-1;a>=t;a--){let t=i-a-1,s=this.applyPattern(o,n,t,!0);r.push({row:a,col:e,value:s})}}if(u||d)for(let e=i;e<=a;e++){let t=this.getSourceRowValues(o,s,e),i=this.detectPattern(t);if(u)for(let a=s+1;a<=n;a++){let n=a-s-1,o=this.applyPattern(i,t,n);r.push({row:e,col:a,value:o})}else if(d)for(let a=o-1;a>=n;a--){let n=o-a-1,s=this.applyPattern(i,t,n,!0);r.push({row:e,col:a,value:s})}}return r}getSourceColumnValues(e,t,n){let r=[];for(let i=e;i<=t;i++)r.push(this.options.getCellValue(i,n));return r}getSourceRowValues(e,t,n){let r=[];for(let i=e;i<=t;i++)r.push(this.options.getCellValue(n,i));return r}detectPattern(e){if(e.length===0)return{type:`constant`,value:null};if(e.length===1)return{type:`constant`,value:e[0]??null};let t=e.map(e=>typeof e==`number`?e:Number(e));if(t.every(e=>!isNaN(e))){let e=[];for(let n=1;n<t.length;n++)e.push(t[n]-t[n-1]);if(e.every(t=>t===e[0])&&e[0]!==void 0)return{type:`arithmetic`,start:t[0],step:e[0]}}return{type:`repeat`,values:e}}applyPattern(e,t,n,r=!1){switch(e.type){case`constant`:return e.value;case`arithmetic`:{let i=r?-(n+1):n+1;return(r?e.start:e.start+e.step*(t.length-1))+e.step*i}case`repeat`:{let t=e.values.length;if(t===0)return null;if(r){let r=(t-1-n%t+t)%t;return e.values[r]??null}return e.values[n%t]??null}}}},n=class{columns;dataSource;rowHeight;headerHeight;overscan;scrollTop=0;scrollLeft=0;viewportWidth=800;viewportHeight=600;cachedRows=new Map;totalRows=0;currentPageIndex=0;pageSize=1e6;sortModel=[];filterModel={};slotPool={slots:new Map,rowToSlot:new Map,nextSlotId:0};selection;fill;editState=null;columnPositions=[];listeners=[];batchListeners=[];constructor(n){this.columns=n.columns,this.dataSource=n.dataSource,this.rowHeight=n.rowHeight,this.headerHeight=n.headerHeight??n.rowHeight,this.overscan=n.overscan??3,this.computeColumnPositions(),this.selection=new e({getRowCount:()=>this.totalRows,getColumnCount:()=>this.columns.length,getCellValue:(e,t)=>this.getCellValue(e,t),getRowData:e=>this.cachedRows.get(e),getColumn:e=>this.columns[e]}),this.selection.onInstruction(e=>this.emit(e)),this.fill=new t({getRowCount:()=>this.totalRows,getColumnCount:()=>this.columns.length,getCellValue:(e,t)=>this.getCellValue(e,t),getColumn:e=>this.columns[e],setCellValue:(e,t,n)=>this.setCellValue(e,t,n)}),this.fill.onInstruction(e=>this.emit(e))}onInstruction(e){return this.listeners.push(e),()=>{this.listeners=this.listeners.filter(t=>t!==e)}}onBatchInstruction(e){return this.batchListeners.push(e),()=>{this.batchListeners=this.batchListeners.filter(t=>t!==e)}}emit(e){for(let t of this.listeners)t(e);for(let t of this.batchListeners)t([e])}emitBatch(e){if(e.length!==0){for(let t of this.batchListeners)t(e);for(let t of e)for(let e of this.listeners)e(t)}}async initialize(){await this.fetchData(),this.syncSlots(),this.emitContentSize(),this.emitHeaders()}setViewport(e,t,n,r){(this.scrollTop!==e||this.scrollLeft!==t||this.viewportWidth!==n||this.viewportHeight!==r)&&(this.scrollTop=e,this.scrollLeft=t,this.viewportWidth=n,this.viewportHeight=r,this.syncSlots())}syncSlots(){let e=Math.max(0,Math.floor(this.scrollTop/this.rowHeight)-this.overscan),t=Math.min(this.totalRows-1,Math.ceil((this.scrollTop+this.viewportHeight)/this.rowHeight)+this.overscan);if(this.totalRows===0||t<e){this.destroyAllSlots();return}let n=new Set;for(let r=e;r<=t;r++)n.add(r);let r=[],i=[];for(let[e,t]of this.slotPool.slots)n.has(t.rowIndex)?n.delete(t.rowIndex):(i.push(e),this.slotPool.rowToSlot.delete(t.rowIndex));let a=Array.from(n);for(let e=0;e<a.length;e++){let t=a[e],n=this.cachedRows.get(t);if(e<i.length){let a=i[e],o=this.slotPool.slots.get(a),s=this.getRowTranslateY(t);o.rowIndex=t,o.rowData=n??{},o.translateY=s,this.slotPool.rowToSlot.set(t,a),r.push({type:`ASSIGN_SLOT`,slotId:a,rowIndex:t,rowData:n??{}}),r.push({type:`MOVE_SLOT`,slotId:a,translateY:s})}else{let e=`slot-${this.slotPool.nextSlotId++}`,i=this.getRowTranslateY(t),a={slotId:e,rowIndex:t,rowData:n??{},translateY:i};this.slotPool.slots.set(e,a),this.slotPool.rowToSlot.set(t,e),r.push({type:`CREATE_SLOT`,slotId:e}),r.push({type:`ASSIGN_SLOT`,slotId:e,rowIndex:t,rowData:n??{}}),r.push({type:`MOVE_SLOT`,slotId:e,translateY:i})}}for(let e=a.length;e<i.length;e++){let t=i[e];this.slotPool.slots.delete(t),r.push({type:`DESTROY_SLOT`,slotId:t})}for(let[e,t]of this.slotPool.slots){let n=this.getRowTranslateY(t.rowIndex);t.translateY!==n&&(t.translateY=n,r.push({type:`MOVE_SLOT`,slotId:e,translateY:n}))}this.emitBatch(r)}destroyAllSlots(){let e=[];for(let t of this.slotPool.slots.keys())e.push({type:`DESTROY_SLOT`,slotId:t});this.slotPool.slots.clear(),this.slotPool.rowToSlot.clear(),this.emitBatch(e)}getRowTranslateY(e){return e*this.rowHeight+this.headerHeight}async fetchData(){this.emit({type:`DATA_LOADING`});try{let e={pagination:{pageIndex:this.currentPageIndex,pageSize:this.pageSize},sort:this.sortModel.length>0?this.sortModel:void 0,filter:Object.keys(this.filterModel).length>0?this.filterModel:void 0},t=await this.dataSource.fetch(e);this.cachedRows.clear(),t.rows.forEach((e,t)=>{this.cachedRows.set(this.currentPageIndex*this.pageSize+t,e)}),this.totalRows=t.totalRows,t.totalRows>t.rows.length&&this.currentPageIndex===0&&await this.fetchAllData(),this.emit({type:`DATA_LOADED`,totalRows:this.totalRows})}catch(e){this.emit({type:`DATA_ERROR`,error:e instanceof Error?e.message:String(e)})}}async fetchAllData(){let e=Math.ceil(this.totalRows/this.pageSize);for(let t=1;t<e;t++){let e={pagination:{pageIndex:t,pageSize:this.pageSize},sort:this.sortModel.length>0?this.sortModel:void 0,filter:Object.keys(this.filterModel).length>0?this.filterModel:void 0};(await this.dataSource.fetch(e)).rows.forEach((e,n)=>{this.cachedRows.set(t*this.pageSize+n,e)})}}async setSort(e,t,n=!1){let r=this.sortModel.findIndex(t=>t.colId===e);n?t===null?r>=0&&this.sortModel.splice(r,1):r>=0?this.sortModel[r].direction=t:this.sortModel.push({colId:e,direction:t}):this.sortModel=t===null?[]:[{colId:e,direction:t}],await this.fetchData(),this.refreshAllSlots(),this.emitHeaders()}async setFilter(e,t){t===``?delete this.filterModel[e]:this.filterModel[e]=t,await this.fetchData(),this.refreshAllSlots(),this.emitContentSize()}refreshAllSlots(){let e=[];for(let[t,n]of this.slotPool.slots)if(n.rowIndex>=0&&n.rowIndex<this.totalRows){let r=this.cachedRows.get(n.rowIndex),i=this.getRowTranslateY(n.rowIndex);n.rowData=r??{},n.translateY=i,e.push({type:`ASSIGN_SLOT`,slotId:t,rowIndex:n.rowIndex,rowData:r??{}}),e.push({type:`MOVE_SLOT`,slotId:t,translateY:i})}this.emitBatch(e),this.syncSlots()}getSortModel(){return[...this.sortModel]}getFilterModel(){return{...this.filterModel}}startEdit(e,t){let n=this.columns[t];if(!n||n.editable!==!0)return;let r=this.getCellValue(e,t);this.editState={row:e,col:t,initialValue:r,currentValue:r},this.emit({type:`START_EDIT`,row:e,col:t,initialValue:r})}updateEditValue(e){this.editState&&(this.editState.currentValue=e)}commitEdit(){if(!this.editState)return;let{row:e,col:t,currentValue:n}=this.editState;this.setCellValue(e,t,n),this.emit({type:`COMMIT_EDIT`,row:e,col:t,value:n}),this.editState=null,this.emit({type:`STOP_EDIT`});let r=this.slotPool.rowToSlot.get(e);if(r){let t=this.cachedRows.get(e);t&&this.emit({type:`ASSIGN_SLOT`,slotId:r,rowIndex:e,rowData:t})}}cancelEdit(){this.editState=null,this.emit({type:`STOP_EDIT`})}getEditState(){return this.editState?{...this.editState}:null}getCellValue(e,t){let n=this.cachedRows.get(e);if(!n)return null;let r=this.columns[t];return r?this.getFieldValue(n,r.field):null}setCellValue(e,t,n){let r=this.cachedRows.get(e);if(!r||typeof r!=`object`)return;let i=this.columns[t];i&&this.setFieldValue(r,i.field,n)}getFieldValue(e,t){let n=t.split(`.`),r=e;for(let e of n){if(typeof r!=`object`||!r)return null;r=r[e]}return r??null}setFieldValue(e,t,n){let r=t.split(`.`),i=e;for(let e=0;e<r.length-1;e++){let t=r[e];t in i||(i[t]={}),i=i[t]}let a=r[r.length-1];i[a]=n}computeColumnPositions(){this.columnPositions=[0];let e=0;for(let t of this.columns)e+=t.width,this.columnPositions.push(e)}emitContentSize(){let e=this.columnPositions[this.columnPositions.length-1]??0,t=this.totalRows*this.rowHeight+this.headerHeight;this.emit({type:`SET_CONTENT_SIZE`,width:e,height:t})}emitHeaders(){let e=new Map;this.sortModel.forEach((t,n)=>{e.set(t.colId,{direction:t.direction,index:n+1})});for(let t=0;t<this.columns.length;t++){let n=this.columns[t],r=n.colId??n.field,i=e.get(r);this.emit({type:`UPDATE_HEADER`,colIndex:t,column:n,sortDirection:i?.direction,sortIndex:i?.index})}}getColumns(){return this.columns}getColumnPositions(){return[...this.columnPositions]}getRowCount(){return this.totalRows}getRowHeight(){return this.rowHeight}getHeaderHeight(){return this.headerHeight}getTotalWidth(){return this.columnPositions[this.columnPositions.length-1]??0}getTotalHeight(){return this.totalRows*this.rowHeight+this.headerHeight}getRowData(e){return this.cachedRows.get(e)}async refresh(){await this.fetchData(),this.syncSlots(),this.emitContentSize()}refreshSlotData(){this.refreshAllSlots()}async setDataSource(e){this.dataSource=e,await this.refresh()}setColumns(e){this.columns=e,this.computeColumnPositions(),this.emitContentSize(),this.emitHeaders(),this.syncSlots()}};function r(e,t={}){let{getFieldValue:n=a}=t;return{async fetch(t){let r=[...e];t.filter&&Object.keys(t.filter).length>0&&(r=o(r,t.filter,n)),t.sort&&t.sort.length>0&&(r=s(r,t.sort,n));let i=r.length,{pageIndex:a,pageSize:c}=t.pagination,l=a*c;return{rows:r.slice(l,l+c),totalRows:i}}}}function i(e){return{async fetch(t){return e(t)}}}function a(e,t){let n=t.split(`.`),r=e;for(let e of n){if(typeof r!=`object`||!r)return null;r=r[e]}return r??null}function o(e,t,n){let r=Object.entries(t).filter(([,e])=>e!==``);return r.length===0?e:e.filter(e=>{for(let[t,i]of r){let r=n(e,t),a=String(r??``).toLowerCase(),o=i.toLowerCase();if(!a.includes(o))return!1}return!0})}function s(e,t,n){return[...e].sort((e,r)=>{for(let{colId:i,direction:a}of t){let t=c(n(e,i),n(r,i));if(t!==0)return a===`asc`?t:-t}return 0})}function c(e,t){if(e==null&&t==null)return 0;if(e==null)return 1;if(t==null)return-1;let n=Number(e),r=Number(t);return!isNaN(n)&&!isNaN(r)?n-r:e instanceof Date&&t instanceof Date?e.getTime()-t.getTime():String(e).localeCompare(String(t))}function l(e){return r(e)}export{t as FillManager,n as GridCore,e as SelectionManager,r as createClientDataSource,l as createDataSourceFromArray,i as createServerDataSource};
|
|
2
|
+
`);try{await navigator.clipboard.writeText(t)}catch{let e=document.createElement(`textarea`);e.value=t,e.style.position=`fixed`,e.style.left=`-9999px`,document.body.appendChild(e),e.select(),document.execCommand(`copy`),document.body.removeChild(e)}}clampPosition(e){let t=this.options.getRowCount(),n=this.options.getColumnCount();return{row:Math.max(0,Math.min(e.row,t-1)),col:Math.max(0,Math.min(e.col,n-1))}}},t=class{state=null;options;listeners=[];constructor(e){this.options=e}onInstruction(e){return this.listeners.push(e),()=>{this.listeners=this.listeners.filter(t=>t!==e)}}emit(e){for(let t of this.listeners)t(e)}getState(){return this.state?{...this.state}:null}isActive(){return this.state!==null}startFillDrag(e){this.state={sourceRange:e,targetRow:e.endRow,targetCol:e.endCol},this.emit({type:`START_FILL`,sourceRange:e})}updateFillDrag(e,t){if(!this.state)return;let n=this.options.getRowCount(),r=this.options.getColumnCount();e=Math.max(0,Math.min(e,n-1)),t=Math.max(0,Math.min(t,r-1)),this.state.targetRow=e,this.state.targetCol=t,this.emit({type:`UPDATE_FILL`,targetRow:e,targetCol:t})}commitFillDrag(){if(!this.state)return;let{sourceRange:e,targetRow:t,targetCol:n}=this.state,r=this.calculateFilledCells(e,t,n);for(let{row:e,col:t,value:n}of r)this.options.setCellValue(e,t,n);this.emit({type:`COMMIT_FILL`,filledCells:r}),this.state=null}cancelFillDrag(){this.state&&(this.state=null,this.emit({type:`CANCEL_FILL`}))}calculateFilledCells(e,t,n){let r=[],i=Math.min(e.startRow,e.endRow),a=Math.max(e.startRow,e.endRow),o=Math.min(e.startCol,e.endCol),s=Math.max(e.startCol,e.endCol),c=t>a,l=t<i,u=n>s,d=n<o;if(c||l)for(let e=o;e<=s;e++){let n=this.getSourceColumnValues(i,a,e),o=this.detectPattern(n);if(c)for(let i=a+1;i<=t;i++){let t=i-a-1,s=this.applyPattern(o,n,t);r.push({row:i,col:e,value:s})}else if(l)for(let a=i-1;a>=t;a--){let t=i-a-1,s=this.applyPattern(o,n,t,!0);r.push({row:a,col:e,value:s})}}if(u||d)for(let e=i;e<=a;e++){let t=this.getSourceRowValues(o,s,e),i=this.detectPattern(t);if(u)for(let a=s+1;a<=n;a++){let n=a-s-1,o=this.applyPattern(i,t,n);r.push({row:e,col:a,value:o})}else if(d)for(let a=o-1;a>=n;a--){let n=o-a-1,s=this.applyPattern(i,t,n,!0);r.push({row:e,col:a,value:s})}}return r}getSourceColumnValues(e,t,n){let r=[];for(let i=e;i<=t;i++)r.push(this.options.getCellValue(i,n));return r}getSourceRowValues(e,t,n){let r=[];for(let i=e;i<=t;i++)r.push(this.options.getCellValue(n,i));return r}detectPattern(e){if(e.length===0)return{type:`constant`,value:null};if(e.length===1)return{type:`constant`,value:e[0]??null};let t=e.map(e=>typeof e==`number`?e:Number(e));if(t.every(e=>!isNaN(e))){let e=[];for(let n=1;n<t.length;n++)e.push(t[n]-t[n-1]);if(e.every(t=>t===e[0])&&e[0]!==void 0)return{type:`arithmetic`,start:t[0],step:e[0]}}return{type:`repeat`,values:e}}applyPattern(e,t,n,r=!1){switch(e.type){case`constant`:return e.value;case`arithmetic`:{let i=r?-(n+1):n+1;return(r?e.start:e.start+e.step*(t.length-1))+e.step*i}case`repeat`:{let t=e.values.length;if(t===0)return null;if(r){let r=(t-1-n%t+t)%t;return e.values[r]??null}return e.values[n%t]??null}}}},n=class n{static MAX_SAFE_HEIGHT=1e6;columns;dataSource;rowHeight;headerHeight;overscan;scrollTop=0;scrollLeft=0;viewportWidth=800;viewportHeight=600;cachedRows=new Map;totalRows=0;currentPageIndex=0;pageSize=1e6;sortModel=[];filterModel={};slotPool={slots:new Map,rowToSlot:new Map,nextSlotId:0};selection;fill;editState=null;columnPositions=[];listeners=[];batchListeners=[];constructor(n){this.columns=n.columns,this.dataSource=n.dataSource,this.rowHeight=n.rowHeight,this.headerHeight=n.headerHeight??n.rowHeight,this.overscan=n.overscan??3,this.computeColumnPositions(),this.selection=new e({getRowCount:()=>this.totalRows,getColumnCount:()=>this.columns.length,getCellValue:(e,t)=>this.getCellValue(e,t),getRowData:e=>this.cachedRows.get(e),getColumn:e=>this.columns[e]}),this.selection.onInstruction(e=>this.emit(e)),this.fill=new t({getRowCount:()=>this.totalRows,getColumnCount:()=>this.columns.length,getCellValue:(e,t)=>this.getCellValue(e,t),getColumn:e=>this.columns[e],setCellValue:(e,t,n)=>this.setCellValue(e,t,n)}),this.fill.onInstruction(e=>this.emit(e))}onInstruction(e){return this.listeners.push(e),()=>{this.listeners=this.listeners.filter(t=>t!==e)}}onBatchInstruction(e){return this.batchListeners.push(e),()=>{this.batchListeners=this.batchListeners.filter(t=>t!==e)}}emit(e){for(let t of this.listeners)t(e);for(let t of this.batchListeners)t([e])}emitBatch(e){if(e.length!==0){for(let t of this.batchListeners)t(e);for(let t of e)for(let e of this.listeners)e(t)}}async initialize(){await this.fetchData(),this.syncSlots(),this.emitContentSize(),this.emitHeaders()}setViewport(e,t,n,r){(this.scrollTop!==e||this.scrollLeft!==t||this.viewportWidth!==n||this.viewportHeight!==r)&&(this.scrollTop=e,this.scrollLeft=t,this.viewportWidth=n,this.viewportHeight=r,this.syncSlots())}syncSlots(){let e=this.getVirtualScrollTop(),t=Math.max(0,Math.floor(e/this.rowHeight)-this.overscan),n=Math.min(this.totalRows-1,Math.ceil((e+this.viewportHeight)/this.rowHeight)+this.overscan);if(this.totalRows===0||n<t){this.destroyAllSlots();return}let r=new Set;for(let e=t;e<=n;e++)r.add(e);let i=[],a=[];for(let[e,t]of this.slotPool.slots)r.has(t.rowIndex)?r.delete(t.rowIndex):(a.push(e),this.slotPool.rowToSlot.delete(t.rowIndex));let o=Array.from(r);for(let e=0;e<o.length;e++){let t=o[e],n=this.cachedRows.get(t);if(e<a.length){let r=a[e],o=this.slotPool.slots.get(r),s=this.getRowTranslateY(t);o.rowIndex=t,o.rowData=n??{},o.translateY=s,this.slotPool.rowToSlot.set(t,r),i.push({type:`ASSIGN_SLOT`,slotId:r,rowIndex:t,rowData:n??{}}),i.push({type:`MOVE_SLOT`,slotId:r,translateY:s})}else{let e=`slot-${this.slotPool.nextSlotId++}`,r=this.getRowTranslateY(t),a={slotId:e,rowIndex:t,rowData:n??{},translateY:r};this.slotPool.slots.set(e,a),this.slotPool.rowToSlot.set(t,e),i.push({type:`CREATE_SLOT`,slotId:e}),i.push({type:`ASSIGN_SLOT`,slotId:e,rowIndex:t,rowData:n??{}}),i.push({type:`MOVE_SLOT`,slotId:e,translateY:r})}}for(let e=o.length;e<a.length;e++){let t=a[e];this.slotPool.slots.delete(t),i.push({type:`DESTROY_SLOT`,slotId:t})}for(let[e,t]of this.slotPool.slots){let n=this.getRowTranslateY(t.rowIndex);t.translateY!==n&&(t.translateY=n,i.push({type:`MOVE_SLOT`,slotId:e,translateY:n}))}this.emitBatch(i)}destroyAllSlots(){let e=[];for(let t of this.slotPool.slots.keys())e.push({type:`DESTROY_SLOT`,slotId:t});this.slotPool.slots.clear(),this.slotPool.rowToSlot.clear(),this.emitBatch(e)}getVirtualHeight(){return this.totalRows*this.rowHeight+this.headerHeight}getDisplayHeight(){return Math.min(this.getVirtualHeight(),n.MAX_SAFE_HEIGHT)}needsScaling(){return this.getVirtualHeight()>n.MAX_SAFE_HEIGHT}getScrollProgress(){let e=this.getDisplayHeight()-this.viewportHeight;return e<=0?0:Math.min(1,Math.max(0,this.scrollTop/e))}getVirtualScrollTop(){if(!this.needsScaling())return this.scrollTop;let e=this.getVirtualHeight()-this.viewportHeight;return this.getScrollProgress()*e}getRowTranslateY(e){let t=e*this.rowHeight+this.headerHeight;if(!this.needsScaling())return t;let n=this.getDisplayHeight(),r=this.getVirtualHeight(),i=t+this.getScrollProgress()*(n-r);return Math.min(i,n)}async fetchData(){this.emit({type:`DATA_LOADING`});try{let e={pagination:{pageIndex:this.currentPageIndex,pageSize:this.pageSize},sort:this.sortModel.length>0?this.sortModel:void 0,filter:Object.keys(this.filterModel).length>0?this.filterModel:void 0},t=await this.dataSource.fetch(e);this.cachedRows.clear(),t.rows.forEach((e,t)=>{this.cachedRows.set(this.currentPageIndex*this.pageSize+t,e)}),this.totalRows=t.totalRows,t.totalRows>t.rows.length&&this.currentPageIndex===0&&await this.fetchAllData(),this.emit({type:`DATA_LOADED`,totalRows:this.totalRows})}catch(e){this.emit({type:`DATA_ERROR`,error:e instanceof Error?e.message:String(e)})}}async fetchAllData(){let e=Math.ceil(this.totalRows/this.pageSize);for(let t=1;t<e;t++){let e={pagination:{pageIndex:t,pageSize:this.pageSize},sort:this.sortModel.length>0?this.sortModel:void 0,filter:Object.keys(this.filterModel).length>0?this.filterModel:void 0};(await this.dataSource.fetch(e)).rows.forEach((e,n)=>{this.cachedRows.set(t*this.pageSize+n,e)})}}async setSort(e,t,n=!1){let r=this.sortModel.findIndex(t=>t.colId===e);n?t===null?r>=0&&this.sortModel.splice(r,1):r>=0?this.sortModel[r].direction=t:this.sortModel.push({colId:e,direction:t}):this.sortModel=t===null?[]:[{colId:e,direction:t}],await this.fetchData(),this.refreshAllSlots(),this.emitHeaders()}async setFilter(e,t){t===``?delete this.filterModel[e]:this.filterModel[e]=t,await this.fetchData(),this.refreshAllSlots(),this.emitContentSize()}refreshAllSlots(){let e=[];for(let[t,n]of this.slotPool.slots)if(n.rowIndex>=0&&n.rowIndex<this.totalRows){let r=this.cachedRows.get(n.rowIndex),i=this.getRowTranslateY(n.rowIndex);n.rowData=r??{},n.translateY=i,e.push({type:`ASSIGN_SLOT`,slotId:t,rowIndex:n.rowIndex,rowData:r??{}}),e.push({type:`MOVE_SLOT`,slotId:t,translateY:i})}this.emitBatch(e),this.syncSlots()}getSortModel(){return[...this.sortModel]}getFilterModel(){return{...this.filterModel}}startEdit(e,t){let n=this.columns[t];if(!n||n.editable!==!0)return;let r=this.getCellValue(e,t);this.editState={row:e,col:t,initialValue:r,currentValue:r},this.emit({type:`START_EDIT`,row:e,col:t,initialValue:r})}updateEditValue(e){this.editState&&(this.editState.currentValue=e)}commitEdit(){if(!this.editState)return;let{row:e,col:t,currentValue:n}=this.editState;this.setCellValue(e,t,n),this.emit({type:`COMMIT_EDIT`,row:e,col:t,value:n}),this.editState=null,this.emit({type:`STOP_EDIT`});let r=this.slotPool.rowToSlot.get(e);if(r){let t=this.cachedRows.get(e);t&&this.emit({type:`ASSIGN_SLOT`,slotId:r,rowIndex:e,rowData:t})}}cancelEdit(){this.editState=null,this.emit({type:`STOP_EDIT`})}getEditState(){return this.editState?{...this.editState}:null}getCellValue(e,t){let n=this.cachedRows.get(e);if(!n)return null;let r=this.columns[t];return r?this.getFieldValue(n,r.field):null}setCellValue(e,t,n){let r=this.cachedRows.get(e);if(!r||typeof r!=`object`)return;let i=this.columns[t];i&&this.setFieldValue(r,i.field,n)}getFieldValue(e,t){let n=t.split(`.`),r=e;for(let e of n){if(typeof r!=`object`||!r)return null;r=r[e]}return r??null}setFieldValue(e,t,n){let r=t.split(`.`),i=e;for(let e=0;e<r.length-1;e++){let t=r[e];t in i||(i[t]={}),i=i[t]}let a=r[r.length-1];i[a]=n}computeColumnPositions(){this.columnPositions=[0];let e=0;for(let t of this.columns)e+=t.width,this.columnPositions.push(e)}emitContentSize(){let e=this.columnPositions[this.columnPositions.length-1]??0,t=this.getDisplayHeight();this.emit({type:`SET_CONTENT_SIZE`,width:e,height:t})}emitHeaders(){let e=new Map;this.sortModel.forEach((t,n)=>{e.set(t.colId,{direction:t.direction,index:n+1})});for(let t=0;t<this.columns.length;t++){let n=this.columns[t],r=n.colId??n.field,i=e.get(r);this.emit({type:`UPDATE_HEADER`,colIndex:t,column:n,sortDirection:i?.direction,sortIndex:i?.index})}}getColumns(){return this.columns}getColumnPositions(){return[...this.columnPositions]}getRowCount(){return this.totalRows}getRowHeight(){return this.rowHeight}getHeaderHeight(){return this.headerHeight}getTotalWidth(){return this.columnPositions[this.columnPositions.length-1]??0}getTotalHeight(){return this.totalRows*this.rowHeight+this.headerHeight}getRowData(e){return this.cachedRows.get(e)}getDisplayYForRow(e){return this.getRowTranslateY(e)}isScalingActive(){return this.needsScaling()}getRowIndexAtDisplayY(e,t){if(!this.needsScaling())return Math.floor(e/this.rowHeight);let n=this.getDisplayHeight(),r=this.getVirtualHeight(),i=n-this.viewportHeight,a=e-(i>0?Math.min(1,Math.max(0,t/i)):0)*(n-r);return Math.floor(a/this.rowHeight)}getScrollTopForRow(e){if(!this.needsScaling())return Math.max(0,e*this.rowHeight);let t=this.getDisplayHeight(),n=Math.max(0,t-this.viewportHeight),r=this.totalRows>0?e/this.totalRows:0;return Math.min(n,Math.max(0,r*n))}getVisibleRowRange(){let e=this.getVirtualScrollTop(),t=Math.max(0,this.viewportHeight-this.headerHeight),n=Math.max(0,Math.ceil(e/this.rowHeight)),r=Math.min(this.totalRows-1,Math.floor((e+t)/this.rowHeight)-1);return{start:Math.min(n,r),end:r}}async refresh(){await this.fetchData(),this.syncSlots(),this.emitContentSize()}refreshSlotData(){this.refreshAllSlots()}async setDataSource(e){this.dataSource=e,await this.refresh()}setColumns(e){this.columns=e,this.computeColumnPositions(),this.emitContentSize(),this.emitHeaders(),this.syncSlots()}};function r(e,t){let n=t.split(`.`),r=e;for(let e of n){if(typeof r!=`object`||!r)return null;r=r[e]}return r??null}function i(e,t){if(e==null&&t==null)return 0;if(e==null)return 1;if(t==null)return-1;let n=Number(e),r=Number(t);return!isNaN(n)&&!isNaN(r)?n-r:e instanceof Date&&t instanceof Date?e.getTime()-t.getTime():String(e).localeCompare(String(t))}function a(e,t){return[...e].sort((e,n)=>{for(let{colId:a,direction:o}of t){let t=i(r(e,a),r(n,a));if(t!==0)return o===`asc`?t:-t}return 0})}typeof self<`u`&&self.onmessage!==void 0&&(self.onmessage=e=>{let{type:t,id:n,data:r,sortModel:i}=e.data;if(t===`sort`)try{let e=a(r,i);self.postMessage({type:`sorted`,id:n,data:e})}catch(e){self.postMessage({type:`error`,id:n,error:String(e)})}});var o=class{worker=null;workerUrl=null;pendingRequests=new Map;nextRequestId=0;isTerminated=!1;async sortInWorker(e,t){if(this.isTerminated)throw Error(`SortWorkerManager has been terminated`);this.worker||this.initializeWorker();let n=this.nextRequestId++;return new Promise((r,i)=>{this.pendingRequests.set(n,{resolve:r,reject:i});let a={type:`sort`,id:n,data:e,sortModel:t};this.worker.postMessage(a)})}async sortIndices(e,t){if(this.isTerminated)throw Error(`SortWorkerManager has been terminated`);this.worker||this.initializeWorker();let n=this.nextRequestId++,r=new Float64Array(e);return new Promise((e,i)=>{this.pendingRequests.set(n,{resolve:e,reject:i});let a={type:`sortIndices`,id:n,values:r,direction:t};this.worker.postMessage(a,[r.buffer])})}async sortMultiColumn(e,t){if(this.isTerminated)throw Error(`SortWorkerManager has been terminated`);this.worker||this.initializeWorker();let n=this.nextRequestId++,r=e.map(e=>new Float64Array(e)),i=new Int8Array(t.map(e=>e===`asc`?1:-1));return new Promise((e,t)=>{this.pendingRequests.set(n,{resolve:e,reject:t});let a={type:`sortMultiColumn`,id:n,columns:r,directions:i},o=r.map(e=>e.buffer);o.push(i.buffer),this.worker.postMessage(a,o)})}async sortStringHashes(e,t,n){if(this.isTerminated)throw Error(`SortWorkerManager has been terminated`);this.worker||this.initializeWorker();let r=this.nextRequestId++;return new Promise((i,a)=>{this.pendingRequests.set(r,{resolve:e=>{let{indices:r,collisionPairs:a}=e;a.length>0&&this.resolveCollisions(r,a,n,t),i(r)},reject:a});let o={type:`sortStringHashes`,id:r,hashChunks:e,direction:t},s=e.map(e=>e.buffer);this.worker.postMessage(o,s)})}resolveCollisions(e,t,n,r){let i=new Set;for(let e=0;e<t.length;e++)i.add(t[e]);if(i.size===0)return;let a=[],o=-1;for(let t=0;t<e.length;t++){let n=e[t];i.has(n)?o===-1&&(o=t):o!==-1&&(a.push({start:o,end:t}),o=-1)}o!==-1&&a.push({start:o,end:e.length});let s=r===`asc`?1:-1;for(let t of a){let r=Array.from(e.slice(t.start,t.end));r.sort((e,t)=>s*n[e].localeCompare(n[t]));for(let n=0;n<r.length;n++)e[t.start+n]=r[n]}}initializeWorker(){if(typeof Worker>`u`)throw Error(`Web Workers are not available in this environment`);let e=new Blob([`
|
|
3
|
+
// Inline sort worker code
|
|
4
|
+
function getFieldValue(row, field) {
|
|
5
|
+
const parts = field.split(".");
|
|
6
|
+
let value = row;
|
|
7
|
+
|
|
8
|
+
for (const part of parts) {
|
|
9
|
+
if (value == null || typeof value !== "object") {
|
|
10
|
+
return null;
|
|
11
|
+
}
|
|
12
|
+
value = value[part];
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return value ?? null;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function compareValues(a, b) {
|
|
19
|
+
if (a == null && b == null) return 0;
|
|
20
|
+
if (a == null) return 1;
|
|
21
|
+
if (b == null) return -1;
|
|
22
|
+
|
|
23
|
+
const aNum = Number(a);
|
|
24
|
+
const bNum = Number(b);
|
|
25
|
+
if (!isNaN(aNum) && !isNaN(bNum)) {
|
|
26
|
+
return aNum - bNum;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (a instanceof Date && b instanceof Date) {
|
|
30
|
+
return a.getTime() - b.getTime();
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return String(a).localeCompare(String(b));
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function sortData(data, sortModel) {
|
|
37
|
+
return [...data].sort((a, b) => {
|
|
38
|
+
for (const { colId, direction } of sortModel) {
|
|
39
|
+
const aVal = getFieldValue(a, colId);
|
|
40
|
+
const bVal = getFieldValue(b, colId);
|
|
41
|
+
const comparison = compareValues(aVal, bVal);
|
|
42
|
+
|
|
43
|
+
if (comparison !== 0) {
|
|
44
|
+
return direction === "asc" ? comparison : -comparison;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return 0;
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Index-based sorting - much faster for large datasets
|
|
52
|
+
// Uses Transferable typed arrays for zero-copy transfer
|
|
53
|
+
function sortIndices(values, direction) {
|
|
54
|
+
const len = values.length;
|
|
55
|
+
const indices = new Uint32Array(len);
|
|
56
|
+
for (let i = 0; i < len; i++) indices[i] = i;
|
|
57
|
+
|
|
58
|
+
// Sort indices by values
|
|
59
|
+
const mult = direction === "asc" ? 1 : -1;
|
|
60
|
+
indices.sort((a, b) => {
|
|
61
|
+
const va = values[a];
|
|
62
|
+
const vb = values[b];
|
|
63
|
+
if (va < vb) return -1 * mult;
|
|
64
|
+
if (va > vb) return 1 * mult;
|
|
65
|
+
return 0;
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
return indices;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// Multi-column sorting - compares columns in priority order
|
|
72
|
+
function sortMultiColumn(columns, directions) {
|
|
73
|
+
const len = columns[0].length;
|
|
74
|
+
const numCols = columns.length;
|
|
75
|
+
const indices = new Uint32Array(len);
|
|
76
|
+
for (let i = 0; i < len; i++) indices[i] = i;
|
|
77
|
+
|
|
78
|
+
indices.sort((a, b) => {
|
|
79
|
+
for (let c = 0; c < numCols; c++) {
|
|
80
|
+
const va = columns[c][a];
|
|
81
|
+
const vb = columns[c][b];
|
|
82
|
+
if (va < vb) return -1 * directions[c];
|
|
83
|
+
if (va > vb) return 1 * directions[c];
|
|
84
|
+
// Equal - continue to next column
|
|
85
|
+
}
|
|
86
|
+
return 0;
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
return indices;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// String hash sorting with collision detection
|
|
93
|
+
function sortStringHashes(hashChunks, direction) {
|
|
94
|
+
const len = hashChunks[0].length;
|
|
95
|
+
const numChunks = hashChunks.length;
|
|
96
|
+
const indices = new Uint32Array(len);
|
|
97
|
+
for (let i = 0; i < len; i++) indices[i] = i;
|
|
98
|
+
|
|
99
|
+
const mult = direction === "asc" ? 1 : -1;
|
|
100
|
+
const collisions = []; // Track collision pairs
|
|
101
|
+
|
|
102
|
+
indices.sort((a, b) => {
|
|
103
|
+
for (let c = 0; c < numChunks; c++) {
|
|
104
|
+
const va = hashChunks[c][a];
|
|
105
|
+
const vb = hashChunks[c][b];
|
|
106
|
+
if (va < vb) return -1 * mult;
|
|
107
|
+
if (va > vb) return 1 * mult;
|
|
108
|
+
}
|
|
109
|
+
// All chunks equal = collision, record for fallback
|
|
110
|
+
collisions.push(a, b);
|
|
111
|
+
return 0; // Stable sort preserves original order
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
return { indices, collisionPairs: new Uint32Array(collisions) };
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
self.onmessage = function(e) {
|
|
118
|
+
const { type, id } = e.data;
|
|
119
|
+
|
|
120
|
+
if (type === "sort") {
|
|
121
|
+
try {
|
|
122
|
+
const { data, sortModel } = e.data;
|
|
123
|
+
const sorted = sortData(data, sortModel);
|
|
124
|
+
self.postMessage({ type: "sorted", id, data: sorted });
|
|
125
|
+
} catch (error) {
|
|
126
|
+
self.postMessage({ type: "error", id, error: String(error) });
|
|
127
|
+
}
|
|
128
|
+
} else if (type === "sortIndices") {
|
|
129
|
+
try {
|
|
130
|
+
const { values, direction } = e.data;
|
|
131
|
+
const indices = sortIndices(values, direction);
|
|
132
|
+
// Transfer the indices buffer back (zero-copy)
|
|
133
|
+
self.postMessage({ type: "sortedIndices", id, indices }, [indices.buffer]);
|
|
134
|
+
} catch (error) {
|
|
135
|
+
self.postMessage({ type: "error", id, error: String(error) });
|
|
136
|
+
}
|
|
137
|
+
} else if (type === "sortMultiColumn") {
|
|
138
|
+
try {
|
|
139
|
+
const { columns, directions } = e.data;
|
|
140
|
+
const indices = sortMultiColumn(columns, directions);
|
|
141
|
+
self.postMessage({ type: "sortedMultiColumn", id, indices }, [indices.buffer]);
|
|
142
|
+
} catch (error) {
|
|
143
|
+
self.postMessage({ type: "error", id, error: String(error) });
|
|
144
|
+
}
|
|
145
|
+
} else if (type === "sortStringHashes") {
|
|
146
|
+
try {
|
|
147
|
+
const { hashChunks, direction } = e.data;
|
|
148
|
+
const result = sortStringHashes(hashChunks, direction);
|
|
149
|
+
self.postMessage(
|
|
150
|
+
{ type: "sortedStringHashes", id, indices: result.indices, collisionPairs: result.collisionPairs },
|
|
151
|
+
[result.indices.buffer, result.collisionPairs.buffer]
|
|
152
|
+
);
|
|
153
|
+
} catch (error) {
|
|
154
|
+
self.postMessage({ type: "error", id, error: String(error) });
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
`],{type:`application/javascript`});this.workerUrl=URL.createObjectURL(e),this.worker=new Worker(this.workerUrl),this.worker.onmessage=e=>{let{id:t}=e.data,n=this.pendingRequests.get(t);n&&(this.pendingRequests.delete(t),e.data.type===`sorted`?n.resolve(e.data.data):e.data.type===`sortedIndices`||e.data.type===`sortedMultiColumn`?n.resolve(e.data.indices):e.data.type===`sortedStringHashes`?n.resolve({indices:e.data.indices,collisionPairs:e.data.collisionPairs}):e.data.type===`error`&&n.reject(Error(e.data.error)))},this.worker.onerror=e=>{for(let[t,n]of this.pendingRequests)n.reject(Error(`Worker error: ${e.message}`)),this.pendingRequests.delete(t)}}terminate(){this.worker&&=(this.worker.terminate(),null),this.workerUrl&&=(URL.revokeObjectURL(this.workerUrl),null);for(let[,e]of this.pendingRequests)e.reject(Error(`Worker terminated`));this.pendingRequests.clear(),this.isTerminated=!0}isAvailable(){return!this.isTerminated&&typeof Worker<`u`}};let s=null;function c(){return s||=new o,s}function l(){s&&=(s.terminate(),null)}function u(e,t={}){let{getFieldValue:n=h,useWorker:r=!0}=t,i=r?new o:null;return{async fetch(t){let r=[...e];if(t.filter&&Object.keys(t.filter).length>0&&(r=g(r,t.filter,n)),t.sort&&t.sort.length>0)if(i&&i.isAvailable()&&r.length>=2e5){let e;if(t.sort.length===1){let{colId:a,direction:o}=t.sort[0],s=!1;for(let e of r){let t=n(e,a);if(t!=null){s=typeof t==`string`;break}}if(s){let t=[],s=Array.from({length:3},()=>[]);for(let e of r){let r=n(e,a),i=r==null?``:String(r);t.push(i);let o=m(i);for(let e=0;e<3;e++)s[e].push(o[e])}let c=s.map(e=>new Float64Array(e));e=await i.sortStringHashes(c,o,t)}else{let t=r.map(e=>f(n(e,a)));e=await i.sortIndices(t,o)}}else{let a=[],o=[];for(let{colId:e,direction:i}of t.sort){let t=r.map(t=>f(n(t,e)));a.push(t),o.push(i)}e=await i.sortMultiColumn(a,o)}let a=Array(r.length);for(let t=0;t<e.length;t++)a[t]=r[e[t]];r=a}else r=_(r,t.sort,n);let a=r.length,{pageIndex:o,pageSize:s}=t.pagination,c=o*s;return{rows:r.slice(c,c+s),totalRows:a}}}}function d(e){return{async fetch(t){return e(t)}}}function f(e){if(e==null)return Number.MAX_VALUE;if(typeof e==`number`)return e;if(e instanceof Date)return e.getTime();if(typeof e==`string`)return p(e);let t=Number(e);return isNaN(t)?0:t}function p(e){let t=e.toLowerCase(),n=Math.min(t.length,10),r=0;for(let e=0;e<n;e++){let n=t.charCodeAt(e),i;i=n>=97&&n<=122?n-97:n>=48&&n<=57?n-48+26:0,r=r*36+i}for(let e=n;e<10;e++)r*=36;return r}function m(e){let t=e.toLowerCase(),n=[];for(let e=0;e<3;e++){let r=e*10,i=0;for(let e=0;e<10;e++){let n=r+e,a=n<t.length?t.charCodeAt(n):0,o;o=a>=97&&a<=122?a-97:a>=48&&a<=57?a-48+26:0,i=i*36+o}n.push(i)}return n}function h(e,t){let n=t.split(`.`),r=e;for(let e of n){if(typeof r!=`object`||!r)return null;r=r[e]}return r??null}function g(e,t,n){let r=Object.entries(t).filter(([,e])=>e!==``);return r.length===0?e:e.filter(e=>{for(let[t,i]of r){let r=n(e,t),a=String(r??``).toLowerCase(),o=i.toLowerCase();if(!a.includes(o))return!1}return!0})}function _(e,t,n){return[...e].sort((e,r)=>{for(let{colId:i,direction:a}of t){let t=v(n(e,i),n(r,i));if(t!==0)return a===`asc`?t:-t}return 0})}function v(e,t){if(e==null&&t==null)return 0;if(e==null)return 1;if(t==null)return-1;let n=Number(e),r=Number(t);return!isNaN(n)&&!isNaN(r)?n-r:e instanceof Date&&t instanceof Date?e.getTime()-t.getTime():String(e).localeCompare(String(t))}function y(e){return u(e)}export{t as FillManager,n as GridCore,e as SelectionManager,o as SortWorkerManager,u as createClientDataSource,y as createDataSourceFromArray,d as createServerDataSource,c as getSharedSortWorker,l as terminateSharedSortWorker};
|
|
3
159
|
//# sourceMappingURL=index.js.map
|