index-1fc69005.js 5.0 KB

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