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