UNPKG

@tarojs/components

Version:
1 lines 7.17 kB
import{r as registerInstance,c as createEvent,h,H as Host,g as getElement}from"./index-ab3c86da.js";import Taro from"@tarojs/taro";import{c as classnames}from"./index-c3e4004b.js";var indexCss=".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}";function setTransform(e,t){e.transform=t;e.webkitTransform=t;e.MozTransform=t}var isWebView=typeof navigator!=="undefined"&&/(iPhone|iPod|iPad).*AppleWebKit(?!.*Safari)/i.test(navigator.userAgent);var INDICATOR={activate:"release",deactivate:"pull",release:"loading",finish:"finish"};var supportsPassive=false;try{var opts=Object.defineProperty({},"passive",{get:function(){supportsPassive=true}});window.addEventListener("cancel",(function(){return{}}),opts)}catch(e){}var willPreventDefault=supportsPassive?{passive:false}:false;var PullToRefresh=function(){function e(e){var t=this;registerInstance(this,e);this.onRefresh=createEvent(this,"refresh",7);this._ScreenY=0;this._startScreenY=0;this._lastScreenY=0;this._isMounted=false;this.triggerPullDownRefresh=function(e){if(!t.dragOnEdge&&t._isMounted){if(e){t._lastScreenY=t.distanceToRefresh+1;t.currSt="release";t.setContentStyle(t._lastScreenY)}else{t.currSt="finish";t.reset()}}};this.init=function(){var e=t.scrollContainer;var r=t.el.querySelector(".rmc-pull-to-refresh-content");t.el.appendChild=r===null||r===void 0?void 0:r.appendChild.bind(r);t.el.insertBefore=r===null||r===void 0?void 0:r.insertBefore.bind(r);t.el.replaceChild=r===null||r===void 0?void 0:r.replaceChild.bind(r);t.el.removeChild=r===null||r===void 0?void 0:r.removeChild.bind(r);t._to={touchstart:t.onTouchStart.bind(t,e),touchmove:t.onTouchMove.bind(t,e),touchend:t.onTouchEnd.bind(t,e),touchcancel:t.onTouchEnd.bind(t,e)};Object.keys(t._to).forEach((function(r){e.addEventListener(r,t._to[r],willPreventDefault)}))};this.destroy=function(){if(!t._to)return;var e=t.scrollContainer;Object.keys(t._to).forEach((function(r){e.removeEventListener(r,t._to[r])}))};this.onTouchStart=function(e,r){t._ScreenY=t._startScreenY=r.touches[0].screenY;t._lastScreenY=t._lastScreenY||0};this.isEdge=function(e){var r=t.scrollContainer;if(r&&r===document.body){var n=document.scrollingElement?document.scrollingElement:document.body;return n.scrollTop<=0}return e.scrollTop<=0};this.damp=function(e){if(Math.abs(t._lastScreenY)>t.damping){return 0}var r=Math.abs(t._ScreenY-t._startScreenY)/window.screen.height;e*=(1-r)*.6;return e};this.onTouchMove=function(e,r){var n=r.touches[0].screenY;if(t._startScreenY>n){return}if(t.isEdge(e)){if(!t.dragOnEdge){t._ScreenY=t._startScreenY=r.touches[0].screenY;t.dragOnEdge=true}if(r.cancelable){r.preventDefault()}var i=Math.round(n-t._ScreenY);t._ScreenY=n;t._lastScreenY+=t.damp(i);t.setContentStyle(t._lastScreenY);if(Math.abs(t._lastScreenY)<t.distanceToRefresh){if(t.currSt!=="deactivate"){t.currSt="deactivate"}}else{if(t.currSt==="deactivate"){t.currSt="activate"}}if(isWebView&&r.changedTouches[0].clientY<0){t.onTouchEnd()}}};this.onTouchEnd=function(){if(t.dragOnEdge){t.dragOnEdge=false}if(t.currSt==="activate"){t.currSt="release";t.onRefresh.emit(t);t._lastScreenY=t.distanceToRefresh+1;t.setContentStyle(t._lastScreenY)}else if(t.currSt==="release"){t._lastScreenY=t.distanceToRefresh+1;t.setContentStyle(t._lastScreenY)}else{t.reset()}};this.reset=function(){t._lastScreenY=0;t.setContentStyle(0)};this.setContentStyle=function(e){if(t.contentRef){if(e){setTransform(t.contentRef.style,"translate3d(0px,".concat(e,"px,0)"))}else{setTransform(t.contentRef.style,"")}}};this.prefixCls="rmc-pull-to-refresh";this.distanceToRefresh=50;this.damping=100;this.indicator=INDICATOR;this.currSt="deactivate";this.dragOnEdge=false}Object.defineProperty(e.prototype,"scrollContainer",{get:function(){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},enumerable:false,configurable:true});e.prototype.statusChange=function(){var e,t,r,n;var i=this.scrollContainer;switch(this.currSt){case"release":(t=(e=i===null||i===void 0?void 0:i.__page)===null||e===void 0?void 0:e.onPullDownRefresh)===null||t===void 0?void 0:t.call(e);break;case"deactivate":(n=(r=i===null||i===void 0?void 0:i.__page)===null||r===void 0?void 0:r.onPullIntercept)===null||n===void 0?void 0:n.call(r)}};e.prototype.disconnectedCallback=function(){this.destroy()};e.prototype.componentDidLoad=function(){var e=this;this.init();this._isMounted=true;Taro.eventCenter.on("__taroStartPullDownRefresh",(function(t){var r=t.successHandler,n=t.errorHandler;try{e.triggerPullDownRefresh(true);r({errMsg:"startPullDownRefresh: ok"})}catch(e){n({errMsg:"startPullDownRefresh: fail"})}}));Taro.eventCenter.on("__taroStopPullDownRefresh",(function(t){var r=t.successHandler,n=t.errorHandler;setTimeout((function(){try{e.triggerPullDownRefresh(false);r({errMsg:"stopPullDownRefresh: ok"})}catch(e){n({errMsg:"stopPullDownRefresh: fail"})}}),0)}))};e.prototype.render=function(){var e=this;var t=function(t){var r=e,n=r.currSt,i=r.dragOnEdge,o=r.prefixCls;var s=classnames(t,!i&&"".concat(o,"-transition"));var a=n==="activate"||n==="release";return h("div",{class:"".concat(o,"-content-wrapper")},h("div",{class:s,ref:function(t){e.contentRef=t}},a&&h("div",{class:"".concat(o,"-indicator")},h("div",null),h("div",null),h("div",null)),h("slot",null)))};if(this.scrollContainer){return t("".concat(this.prefixCls,"-content ").concat(this.prefixCls,"-down"))}return h(Host,{class:classnames(this.prefixCls,"".concat(this.prefixCls,"-down"))},t("".concat(this.prefixCls,"-content")))};Object.defineProperty(e.prototype,"el",{get:function(){return getElement(this)},enumerable:false,configurable:true});Object.defineProperty(e,"watchers",{get:function(){return{currSt:["statusChange"]}},enumerable:false,configurable:true});return e}();PullToRefresh.style=indexCss;export{PullToRefresh as taro_pull_to_refresh_core};