index-86832be9.js 5.0 KB

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