index-4ac95303.js 5.0 KB

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