index-e888510b.js 5.0 KB

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