index-15bfdd6f.js 5.1 KB

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