index-934f7716.js 5.1 KB

1
  1. import{t as j,T as I,x as G,v as _,d as U,n as K,j as q,y as re,a4 as ve,i as d,A as F,w as A,as as fe,at as de,aS as he,o as J,av as ge,aw as we,am as me,an as ye,D as Se,g as $,ar as L,p as Q,aG as z,G as be,H as R,K as Z,a7 as pe}from"./index-374e58b6.js";const[ee,M]=j("swipe"),xe={loop:I,width:G,height:G,vertical:Boolean,autoplay:_(0),duration:_(500),touchable:I,lazyRender:Boolean,initialSwipe:_(0),indicatorColor:String,showIndicators:I,stopPropagation:I},te=Symbol(ee);var Te=U({name:ee,props:xe,emits:["change","dragStart","dragEnd"],setup(a,{emit:S,slots:g}){const u=K(),h=K(),t=q({rect:null,width:0,height:0,offset:0,active:0,swiping:!1});let b=!1;const r=re(),{children:w,linkChildren:s}=ve(te),i=d(()=>w.length),o=d(()=>t[a.vertical?"height":"width"]),v=d(()=>a.vertical?r.deltaY.value:r.deltaX.value),y=d(()=>t.rect?(a.vertical?t.rect.height:t.rect.width)-o.value*i.value:0),k=d(()=>o.value?Math.ceil(Math.abs(y.value)/o.value):i.value),D=d(()=>i.value*o.value),p=d(()=>(t.active+i.value)%i.value),B=d(()=>{const e=a.vertical?"vertical":"horizontal";return r.direction.value===e}),ae=d(()=>{const e={transitionDuration:`${t.swiping?0:a.duration}ms`,transform:`translate${a.vertical?"Y":"X"}(${t.offset}px)`};if(o.value){const l=a.vertical?"height":"width",n=a.vertical?"width":"height";e[l]=`${D.value}px`,e[n]=a[n]?`${a[n]}px`:""}return e}),ie=e=>{const{active:l}=t;return e?a.loop?R(l+e,-1,i.value):R(l+e,0,k.value):l},Y=(e,l=0)=>{let n=e*o.value;a.loop||(n=Math.min(n,-y.value));let f=l-n;return a.loop||(f=R(f,y.value,0)),f},m=({pace:e=0,offset:l=0,emitChange:n})=>{if(i.value<=1)return;const{active:f}=t,c=ie(e),C=Y(c,l);if(a.loop){if(w[0]&&C!==y.value){const O=C<y.value;w[0].setOffset(O?D.value:0)}if(w[i.value-1]&&C!==0){const O=C>0;w[i.value-1].setOffset(O?-D.value:0)}}t.active=c,t.offset=C,n&&c!==f&&S("change",p.value)},E=()=>{t.swiping=!0,t.active<=-1?m({pace:i.value}):t.active>=i.value&&m({pace:-i.value})},ne=()=>{E(),r.reset(),z(()=>{t.swiping=!1,m({pace:-1,emitChange:!0})})},H=()=>{E(),r.reset(),z(()=>{t.swiping=!1,m({pace:1,emitChange:!0})})};let X;const T=()=>clearTimeout(X),P=()=>{T(),+a.autoplay>0&&i.value>1&&(X=setTimeout(()=>{H(),P()},+a.autoplay))},x=(e=+a.initialSwipe)=>{if(!u.value)return;const l=()=>{var n,f;if(!L(u)){const c={width:u.value.offsetWidth,height:u.value.offsetHeight};t.rect=c,t.width=+((n=a.width)!=null?n:c.width),t.height=+((f=a.height)!=null?f:c.height)}i.value&&(e=Math.min(i.value-1,e),e===-1&&(e=i.value-1)),t.active=e,t.swiping=!0,t.offset=Y(e),w.forEach(c=>{c.setOffset(0)}),P()};L(u)?Q().then(l):l()},N=()=>x(t.active);let V;const le=e=>{!a.touchable||e.touches.length>1||(r.start(e),b=!1,V=Date.now(),T(),E())},oe=e=>{a.touchable&&t.swiping&&(r.move(e),B.value&&(!a.loop&&(t.active===0&&v.value>0||t.active===i.value-1&&v.value<0)||(be(e,a.stopPropagation),m({offset:v.value}),b||(S("dragStart",{index:p.value}),b=!0))))},W=()=>{if(!a.touchable||!t.swiping)return;const e=Date.now()-V,l=v.value/e;if((Math.abs(l)>.25||Math.abs(v.value)>o.value/2)&&B.value){const f=a.vertical?r.offsetY.value:r.offsetX.value;let c=0;a.loop?c=f>0?v.value>0?-1:1:0:c=-Math[v.value>0?"ceil":"floor"](v.value/o.value),m({pace:c,emitChange:!0})}else v.value&&m({pace:0});b=!1,t.swiping=!1,S("dragEnd",{index:p.value}),P()},se=(e,l={})=>{E(),r.reset(),z(()=>{let n;a.loop&&e===i.value?n=t.active===0?0:e:n=e%i.value,l.immediate?z(()=>{t.swiping=!1}):t.swiping=!1,m({pace:n-t.active,emitChange:!0})})},ce=(e,l)=>{const n=l===p.value,f=n?{backgroundColor:a.indicatorColor}:void 0;return $("i",{style:f,class:M("indicator",{active:n})},null)},ue=()=>{if(g.indicator)return g.indicator({active:p.value,total:i.value});if(a.showIndicators&&i.value>1)return $("div",{class:M("indicators",{vertical:a.vertical})},[Array(i.value).fill("").map(ce)])};return F({prev:ne,next:H,state:t,resize:N,swipeTo:se}),s({size:o,props:a,count:i,activeIndicator:p}),A(()=>a.initialSwipe,e=>x(+e)),A(i,()=>x(t.active)),A(()=>a.autoplay,P),A([fe,de,()=>a.width,()=>a.height],N),A(he(),e=>{e==="visible"?P():T()}),J(x),ge(()=>x(t.active)),we(()=>x(t.active)),me(T),ye(T),Se("touchmove",oe,{target:h}),()=>{var e;return $("div",{ref:u,class:M()},[$("div",{ref:h,style:ae.value,class:M("track",{vertical:a.vertical}),onTouchstartPassive:le,onTouchend:W,onTouchcancel:W},[(e=g.default)==null?void 0:e.call(g)]),ue()])}}});const Ee=Z(Te),[Pe,Ce]=j("swipe-item");var Ae=U({name:Pe,setup(a,{slots:S}){let g;const u=q({offset:0,inited:!1,mounted:!1}),{parent:h,index:t}=pe(te);if(!h){console.error("[Vant] <SwipeItem> must be a child component of <Swipe>.");return}const b=d(()=>{const s={},{vertical:i}=h.props;return h.size.value&&(s[i?"height":"width"]=`${h.size.value}px`),u.offset&&(s.transform=`translate${i?"Y":"X"}(${u.offset}px)`),s}),r=d(()=>{const{loop:s,lazyRender:i}=h.props;if(!i||g)return!0;if(!u.mounted)return!1;const o=h.activeIndicator.value,v=h.count.value-1,y=o===0&&s?v:o-1,k=o===v&&s?0:o+1;return g=t.value===o||t.value===y||t.value===k,g}),w=s=>{u.offset=s};return J(()=>{Q(()=>{u.mounted=!0})}),F({setOffset:w}),()=>{var s;return $("div",{class:Ce(),style:b.value},[r.value?(s=S.default)==null?void 0:s.call(S):null])}}});const Ie=Z(Ae);export{Ee as S,Ie as a};