UNPKG

@tarojs/components

Version:
1 lines 6.22 kB
import{r as t,c as i,h as s,H as r,g as e}from"./p-d0cabaaa.js";import h from"@tarojs/taro";import{c as a}from"./p-160d710a.js";const o="undefined"!=typeof navigator&&/(iPhone|iPod|iPad).*AppleWebKit(?!.*Safari)/i.test(navigator.userAgent),n={activate:"release",deactivate:"pull",release:"loading",finish:"finish"};let l=!1;try{const t=Object.defineProperty({},"passive",{get(){l=!0}});window.addEventListener("cancel",(()=>({})),t)}catch(t){}const c=!!l&&{passive:!1},d=class{constructor(s){t(this,s),this.onRefresh=i(this,"refresh",7),this._ScreenY=0,this._startScreenY=0,this._lastScreenY=0,this._isMounted=!1,this.triggerPullDownRefresh=t=>{!this.dragOnEdge&&this._isMounted&&(t?(this._lastScreenY=this.distanceToRefresh+1,this.currSt="release",this.setContentStyle(this._lastScreenY)):(this.currSt="finish",this.reset()))},this.init=()=>{const t=this.scrollContainer,i=this.el.querySelector(".rmc-pull-to-refresh-content");this.el.appendChild=null==i?void 0:i.appendChild.bind(i),this.el.insertBefore=null==i?void 0:i.insertBefore.bind(i),this.el.replaceChild=null==i?void 0:i.replaceChild.bind(i),this.el.removeChild=null==i?void 0:i.removeChild.bind(i),this._to={touchstart:this.onTouchStart.bind(this,t),touchmove:this.onTouchMove.bind(this,t),touchend:this.onTouchEnd.bind(this,t),touchcancel:this.onTouchEnd.bind(this,t)},Object.keys(this._to).forEach((i=>{t.addEventListener(i,this._to[i],c)}))},this.destroy=()=>{if(!this._to)return;const t=this.scrollContainer;Object.keys(this._to).forEach((i=>{t.removeEventListener(i,this._to[i])}))},this.onTouchStart=(t,i)=>{this._ScreenY=this._startScreenY=i.touches[0].screenY,this._lastScreenY=this._lastScreenY||0},this.isEdge=t=>{const i=this.scrollContainer;return i&&i===document.body?(document.scrollingElement?document.scrollingElement:document.body).scrollTop<=0:t.scrollTop<=0},this.damp=t=>Math.abs(this._lastScreenY)>this.damping?0:t*(.6*(1-Math.abs(this._ScreenY-this._startScreenY)/window.screen.height)),this.onTouchMove=(t,i)=>{const s=i.touches[0].screenY;if(!(this._startScreenY>s)&&this.isEdge(t)){this.dragOnEdge||(this._ScreenY=this._startScreenY=i.touches[0].screenY,this.dragOnEdge=!0),i.cancelable&&i.preventDefault();const t=Math.round(s-this._ScreenY);this._ScreenY=s,this._lastScreenY+=this.damp(t),this.setContentStyle(this._lastScreenY),Math.abs(this._lastScreenY)<this.distanceToRefresh?"deactivate"!==this.currSt&&(this.currSt="deactivate"):"deactivate"===this.currSt&&(this.currSt="activate"),o&&i.changedTouches[0].clientY<0&&this.onTouchEnd()}},this.onTouchEnd=()=>{this.dragOnEdge&&(this.dragOnEdge=!1),"activate"===this.currSt?(this.currSt="release",this.onRefresh.emit(this),this._lastScreenY=this.distanceToRefresh+1,this.setContentStyle(this._lastScreenY)):"release"===this.currSt?(this._lastScreenY=this.distanceToRefresh+1,this.setContentStyle(this._lastScreenY)):this.reset()},this.reset=()=>{this._lastScreenY=0,this.setContentStyle(0)},this.setContentStyle=t=>{var i,s;this.contentRef&&((i=this.contentRef.style).transform=s=t?`translate3d(0px,${t}px,0)`:"",i.webkitTransform=s,i.MozTransform=s)},this.prefixCls="rmc-pull-to-refresh",this.distanceToRefresh=50,this.damping=100,this.indicator=n,this.currSt="deactivate",this.dragOnEdge=!1}get scrollContainer(){return this.el.parentElement||this.el.closest(".taro_page_stationed")||document.querySelector(".taro_page_stationed")||document.querySelector(".taro_page")||document.querySelector(".taro_router")||document.querySelector(".taro-tabbar__panel")||document.body}statusChange(){var t,i,s,r;const e=this.scrollContainer;switch(this.currSt){case"release":null===(i=null===(t=null==e?void 0:e.__page)||void 0===t?void 0:t.onPullDownRefresh)||void 0===i||i.call(t);break;case"deactivate":null===(r=null===(s=null==e?void 0:e.__page)||void 0===s?void 0:s.onPullIntercept)||void 0===r||r.call(s)}}disconnectedCallback(){this.destroy()}componentDidLoad(){this.init(),this._isMounted=!0,h.eventCenter.on("__taroStartPullDownRefresh",(({successHandler:t,errorHandler:i})=>{try{this.triggerPullDownRefresh(!0),t({errMsg:"startPullDownRefresh: ok"})}catch(t){i({errMsg:"startPullDownRefresh: fail"})}})),h.eventCenter.on("__taroStopPullDownRefresh",(({successHandler:t,errorHandler:i})=>{setTimeout((()=>{try{this.triggerPullDownRefresh(!1),t({errMsg:"stopPullDownRefresh: ok"})}catch(t){i({errMsg:"stopPullDownRefresh: fail"})}}),0)}))}render(){const t=t=>{const{currSt:i,dragOnEdge:r,prefixCls:e}=this,h=a(t,!r&&`${e}-transition`);return s("div",{class:`${e}-content-wrapper`},s("div",{class:h,ref:t=>{this.contentRef=t}},("activate"===i||"release"===i)&&s("div",{class:`${e}-indicator`},s("div",null),s("div",null),s("div",null)),s("slot",null)))};return this.scrollContainer?t(`${this.prefixCls}-content ${this.prefixCls}-down`):s(r,{class:a(this.prefixCls,`${this.prefixCls}-down`)},t(`${this.prefixCls}-content`))}get el(){return e(this)}static get watchers(){return{currSt:["statusChange"]}}};d.style=".rmc-pull-to-refresh-content{-webkit-transform-origin:0 0;transform-origin:0 0}.rmc-pull-to-refresh-content-wrapper{min-height:100%}.rmc-pull-to-refresh-transition{-webkit-transition:-webkit-transform .3s;transition:-webkit-transform .3s;transition:transform .3s;transition:transform .3s, -webkit-transform .3s}@-webkit-keyframes rmc-pull-to-refresh-indicator{50%{opacity:.2}to{opacity:1}}@keyframes rmc-pull-to-refresh-indicator{50%{opacity:.2}to{opacity:1}}.rmc-pull-to-refresh-indicator{text-align:center;height:30px;line-height:10px}.rmc-pull-to-refresh-indicator>div{background-color:gray;border-radius:100%;width:6px;height:6px;margin:3px;-webkit-animation:.5s linear infinite both rmc-pull-to-refresh-indicator;animation:.5s linear infinite both rmc-pull-to-refresh-indicator;display:inline-block}.rmc-pull-to-refresh-indicator>div:nth-child(0){-webkit-animation-delay:-.1s!important;animation-delay:-.1s!important}.rmc-pull-to-refresh-indicator>div:first-child{-webkit-animation-delay:-.2s!important;animation-delay:-.2s!important}.rmc-pull-to-refresh-indicator>div:nth-child(2){-webkit-animation-delay:-.3s!important;animation-delay:-.3s!important}.rmc-pull-to-refresh-down .rmc-pull-to-refresh-indicator{margin-top:-25px}";export{d as taro_pull_to_refresh_core}