siegel
Version:
Web application development ecosystem
1 lines • 2.08 kB
JavaScript
import{useLayoutEffect as t,useState as l,useRef as e}from"react";import i from"../../../../common/is/exists/index.js";let o=({scrollTop:t,itemHeight:l,preloadedItemsBySide:e,maxItemsCount:i,tableHeight:o})=>{let r=Math.min(Math.ceil((t+o)/l)+e,i);return{from:Math.max(Math.min(Math.ceil(t/l),r-Math.ceil(o/l))-e,0),to:r}},r=()=>({scrollTop:0,prevScrollTop:0,timeoutID:0,prevHeadAdjustment:{value:void 0,scrollTop:void 0,isScrollUp:void 0}}),a=t=>+t.replace("px","");function n(n){let{rootAttributes:p,state:{showPerPage:s},props:{withFooter:c,onScroll:u,entities:{sorted:m},virtualization:{itemHeight:d,preloadedItemsBySide:h=20,scrollUpdateInterval:v=350,tableHeight:f=innerHeight}}}=n;p.ref=e(null);let[S,x]=l(r()),{scrollTop:M}=S,T=c?Math.min(s,m.length):m.length;return{slideWindowRange:o({scrollTop:M,itemHeight:d,tableHeight:f,preloadedItemsBySide:h,maxItemsCount:T}),maxItemsCount:T,useVirtualizationScrolling:function(l){t(()=>{let{from:t,to:e}=o({itemHeight:d,preloadedItemsBySide:h,scrollTop:M,tableHeight:f,maxItemsCount:l}),i=p.ref.current;return i.firstChild.style.padding=`${t*d}px 0 ${(l-e)*d}px`,i.style.setProperty("--data_table_virtualization_scrolltop","0px"),()=>{clearTimeout(S.timeoutID)}},[M,s,l])},onScrollHandler:function(t){if(u?.(t),!t.defaultPrevented){let l=t.target;"sticky"==getComputedStyle(l.firstChild.tHead.rows[0].cells[0]).position&&function(t,l){let{firstChild:e,scrollTop:o}=t,{tBodies:r,style:{paddingTop:n}}=e,{prevHeadAdjustment:p,prevScrollTop:s}=l,c=o<s;l.prevScrollTop=o;let u=a(n),m=u+r[0].clientHeight,d=p.value-(p.scrollTop-o),h=o-u,v=o-m,f=o>m,S=o<u,x=p.isScrollUp==c,M=x&&f,T=x&&S,g=c?p.isScrollUp?M?v:Math.min(h,0):S?h:Math.max(d,0):p.isScrollUp?f?v:Math.min(d,0):T?h:Math.max(v,0);isNaN(g)&&(g=0),((c?g<=0||M:g>=0||T)||!x)&&(l.prevHeadAdjustment={scrollTop:o,isScrollUp:i(p.isScrollUp)?p.isScrollUp:c,value:g},t.style.setProperty("--data_table_virtualization_scrolltop",`${g}px`))}(l,S),S.timeoutID||(S.timeoutID=setTimeout(()=>{let t=r();t.scrollTop=t.prevScrollTop=l.scrollTop,x(t)},v))}}}}export{n as applyVirtualization};