mower-ng/ui/dist/assets/VirtualList.js
2024-11-27 14:08:34 +08:00

1 line
7.9 KiB
JavaScript

import{r as p,j as k,A as H,D as se,B as J,c as oe,I as re,s as ue,b_ as ae,bU as ce,b as _,bj as $,H as T,bf as q,bA as W,aF as de}from"./_plugin-vue_export-helper.js";import{ai as fe,aj as O,a5 as he}from"./main.js";function K(t){return t&-t}class Q{constructor(l,i){this.l=l,this.min=i;const o=new Array(l+1);for(let s=0;s<l+1;++s)o[s]=0;this.ft=o}add(l,i){if(i===0)return;const{l:o,ft:s}=this;for(l+=1;l<=o;)s[l]+=i,l+=K(l)}get(l){return this.sum(l+1)-this.sum(l)}sum(l){if(l===void 0&&(l=this.l),l<=0)return 0;const{ft:i,min:o,l:s}=this;if(l>s)throw new Error("[FinweckTree.sum]: `i` is larger than length.");let a=l*o;for(;l>0;)a+=i[l],l-=K(l);return a}getBound(l){let i=0,o=this.l;for(;o>i;){const s=Math.floor((i+o)/2),a=this.sum(s);if(a>l){o=s;continue}else if(a<l){if(i===s)return this.sum(i+1)<=l?i+1:s;i=s}else return s}return i}}let E;function me(){return typeof document>"u"?!1:(E===void 0&&("matchMedia"in window?E=window.matchMedia("(pointer:coarse)").matches:E=!1),E)}let P;function U(){return typeof document>"u"?1:(P===void 0&&(P="chrome"in window?window.devicePixelRatio:1),P)}const Z="VVirtualListXScroll";function ve({columnsRef:t,renderColRef:l,renderItemWithColsRef:i}){const o=p(0),s=p(0),a=k(()=>{const r=t.value;if(r.length===0)return null;const g=new Q(r.length,0);return r.forEach((I,y)=>{g.add(y,I.width)}),g}),w=H(()=>{const r=a.value;return r!==null?Math.max(r.getBound(s.value)-1,0):0}),b=r=>{const g=a.value;return g!==null?g.sum(r):0},c=H(()=>{const r=a.value;return r!==null?Math.min(r.getBound(s.value+o.value)+1,t.value.length-1):0});return se(Z,{startIndexRef:w,endIndexRef:c,columnsRef:t,renderColRef:l,renderItemWithColsRef:i,getLeft:b}),{listWidthRef:o,scrollLeftRef:s}}const G=J({name:"VirtualListRow",props:{index:{type:Number,required:!0},item:{type:Object,required:!0}},setup(){const{startIndexRef:t,endIndexRef:l,columnsRef:i,getLeft:o,renderColRef:s,renderItemWithColsRef:a}=oe(Z);return{startIndex:t,endIndex:l,columns:i,renderCol:s,renderItemWithCols:a,getLeft:o}},render(){const{startIndex:t,endIndex:l,columns:i,renderCol:o,renderItemWithCols:s,getLeft:a,item:w}=this;if(s!=null)return s({itemIndex:this.index,startColIndex:t,endColIndex:l,allColumns:i,item:w,getLeft:a});if(o!=null){const b=[];for(let c=t;c<=l;++c){const r=i[c];b.push(o({column:r,left:a(c),item:w}))}return b}return null}}),ge=O(".v-vl",{maxHeight:"inherit",height:"100%",overflow:"auto",minWidth:"1px"},[O("&:not(.v-vl--show-scrollbar)",{scrollbarWidth:"none"},[O("&::-webkit-scrollbar, &::-webkit-scrollbar-track-piece, &::-webkit-scrollbar-thumb",{width:0,height:0,display:"none"})])]),Ie=J({name:"VirtualList",inheritAttrs:!1,props:{showScrollbar:{type:Boolean,default:!0},columns:{type:Array,default:()=>[]},renderCol:Function,renderItemWithCols:Function,items:{type:Array,default:()=>[]},itemSize:{type:Number,required:!0},itemResizable:Boolean,itemsStyle:[String,Object],visibleItemsTag:{type:[String,Object],default:"div"},visibleItemsProps:Object,ignoreItemResize:Boolean,onScroll:Function,onWheel:Function,onResize:Function,defaultScrollKey:[Number,String],defaultScrollIndex:Number,keyField:{type:String,default:"key"},paddingTop:{type:[Number,String],default:0},paddingBottom:{type:[Number,String],default:0}},setup(t){const l=re();ge.mount({id:"vueuc/virtual-list",head:!0,anchorMetaName:fe,ssr:l}),ue(()=>{const{defaultScrollIndex:e,defaultScrollKey:n}=t;e!=null?L({index:e}):n!=null&&L({key:n})});let i=!1,o=!1;ae(()=>{if(i=!1,!o){o=!0;return}L({top:x.value,left:w.value})}),ce(()=>{i=!0,o||(o=!0)});const s=H(()=>{if(t.renderCol==null&&t.renderItemWithCols==null||t.columns.length===0)return;let e=0;return t.columns.forEach(n=>{e+=n.width}),e}),a=k(()=>{const e=new Map,{keyField:n}=t;return t.items.forEach((u,f)=>{e.set(u[n],f)}),e}),{scrollLeftRef:w,listWidthRef:b}=ve({columnsRef:_(t,"columns"),renderColRef:_(t,"renderCol"),renderItemWithColsRef:_(t,"renderItemWithCols")}),c=p(null),r=p(void 0),g=new Map,I=k(()=>{const{items:e,itemSize:n,keyField:u}=t,f=new Q(e.length,n);return e.forEach((h,m)=>{const d=h[u],v=g.get(d);v!==void 0&&f.add(m,v)}),f}),y=p(0),x=p(0),F=H(()=>Math.max(I.value.getBound(x.value-$(t.paddingTop))-1,0)),ee=k(()=>{const{value:e}=r;if(e===void 0)return[];const{items:n,itemSize:u}=t,f=F.value,h=Math.min(f+Math.ceil(e/u+1),n.length-1),m=[];for(let d=f;d<=h;++d)m.push(n[d]);return m}),L=(e,n)=>{if(typeof e=="number"){B(e,n,"auto");return}const{left:u,top:f,index:h,key:m,position:d,behavior:v,debounce:R=!0}=e;if(u!==void 0||f!==void 0)B(u,f,v);else if(h!==void 0)X(h,v,R);else if(m!==void 0){const V=a.value.get(m);V!==void 0&&X(V,v,R)}else d==="bottom"?B(0,Number.MAX_SAFE_INTEGER,v):d==="top"&&B(0,0,v)};let C,M=null;function X(e,n,u){const{value:f}=I,h=f.sum(e)+$(t.paddingTop);if(!u)c.value.scrollTo({left:0,top:h,behavior:n});else{C=e,M!==null&&window.clearTimeout(M),M=window.setTimeout(()=>{C=void 0,M=null},16);const{scrollTop:m,offsetHeight:d}=c.value;if(h>m){const v=f.get(e);h+v<=m+d||c.value.scrollTo({left:0,top:h+v-d,behavior:n})}else c.value.scrollTo({left:0,top:h,behavior:n})}}function B(e,n,u){c.value.scrollTo({left:e,top:n,behavior:u})}function te(e,n){var u,f,h;if(i||t.ignoreItemResize||Y(n.target))return;const{value:m}=I,d=a.value.get(e),v=m.get(d),R=(h=(f=(u=n.borderBoxSize)===null||u===void 0?void 0:u[0])===null||f===void 0?void 0:f.blockSize)!==null&&h!==void 0?h:n.contentRect.height;if(R===v)return;R-t.itemSize===0?g.delete(e):g.set(e,R-t.itemSize);const z=R-v;if(z===0)return;m.add(d,z);const S=c.value;if(S!=null){if(C===void 0){const j=m.sum(d);S.scrollTop>j&&S.scrollBy(0,z)}else if(d<C)S.scrollBy(0,z);else if(d===C){const j=m.sum(d);R+j>S.scrollTop+S.offsetHeight&&S.scrollBy(0,z)}N()}y.value++}const D=!me();let A=!1;function ne(e){var n;(n=t.onScroll)===null||n===void 0||n.call(t,e),(!D||!A)&&N()}function le(e){var n;if((n=t.onWheel)===null||n===void 0||n.call(t,e),D){const u=c.value;if(u!=null){if(e.deltaX===0&&(u.scrollTop===0&&e.deltaY<=0||u.scrollTop+u.offsetHeight>=u.scrollHeight&&e.deltaY>=0))return;e.preventDefault(),u.scrollTop+=e.deltaY/U(),u.scrollLeft+=e.deltaX/U(),N(),A=!0,he(()=>{A=!1})}}}function ie(e){if(i||Y(e.target))return;if(t.renderCol==null&&t.renderItemWithCols==null){if(e.contentRect.height===r.value)return}else if(e.contentRect.height===r.value&&e.contentRect.width===b.value)return;r.value=e.contentRect.height,b.value=e.contentRect.width;const{onResize:n}=t;n!==void 0&&n(e)}function N(){const{value:e}=c;e!=null&&(x.value=e.scrollTop,w.value=e.scrollLeft)}function Y(e){let n=e;for(;n!==null;){if(n.style.display==="none")return!0;n=n.parentElement}return!1}return{listHeight:r,listStyle:{overflow:"auto"},keyToIndex:a,itemsStyle:k(()=>{const{itemResizable:e}=t,n=W(I.value.sum());return y.value,[t.itemsStyle,{boxSizing:"content-box",width:W(s.value),height:e?"":n,minHeight:e?n:"",paddingTop:W(t.paddingTop),paddingBottom:W(t.paddingBottom)}]}),visibleItemsStyle:k(()=>(y.value,{transform:`translateY(${W(I.value.sum(F.value))})`})),viewportItems:ee,listElRef:c,itemsElRef:p(null),scrollTo:L,handleListResize:ie,handleListScroll:ne,handleListWheel:le,handleItemResize:te}},render(){const{itemResizable:t,keyField:l,keyToIndex:i,visibleItemsTag:o}=this;return T(q,{onResize:this.handleListResize},{default:()=>{var s,a;return T("div",de(this.$attrs,{class:["v-vl",this.showScrollbar&&"v-vl--show-scrollbar"],onScroll:this.handleListScroll,onWheel:this.handleListWheel,ref:"listElRef"}),[this.items.length!==0?T("div",{ref:"itemsElRef",class:"v-vl-items",style:this.itemsStyle},[T(o,Object.assign({class:"v-vl-visible-items",style:this.visibleItemsStyle},this.visibleItemsProps),{default:()=>{const{renderCol:w,renderItemWithCols:b}=this;return this.viewportItems.map(c=>{const r=c[l],g=i.get(r),I=w!=null?T(G,{index:g,item:c}):void 0,y=b!=null?T(G,{index:g,item:c}):void 0,x=this.$slots.default({item:c,renderedCols:I,renderedItemWithCols:y,index:g})[0];return t?T(q,{key:r,onResize:F=>this.handleItemResize(r,F)},{default:()=>x}):(x.key=r,x)})}})]):(a=(s=this.$slots).empty)===null||a===void 0?void 0:a.call(s)])}})}});export{Ie as V};