人人商城

mui.pullToRefresh.js 7.5KB

1
  1. !function(a,b,c){var d="beforeChangeOffset",e="afterChangeOffset",f="pullstart",g="pulling",h=d,i=e,j="dragEndAfterChangeOffset",k=a.className("transitioning"),l=a.className("pull-top-tips"),m=a.className("pull-bottom-tips"),n=a.className("pull-loading"),o=a.className("scroll"),p=a.className("pull-loading")+" "+a.className("icon")+" "+a.className("icon-pulldown"),q=p+" "+a.className("reverse"),r=a.className("pull-loading")+" "+a.className("spinner"),s=a.className("hidden"),t="."+n;a.PullToRefresh=a.Class.extend({init:function(b,c){this.element=b,this.options=a.extend(!0,{down:{height:75,callback:!1},up:{auto:!1,offset:100,show:!0,contentdown:"上拉显示更多",contentrefresh:"正在加载...",contentnomore:"没有更多数据了",callback:!1},preventDefaultException:{tagName:/^(INPUT|TEXTAREA|BUTTON|SELECT)$/}},c),this.stopped=this.isNeedRefresh=this.isDragging=!1,this.state=d,this.isInScroll=this.element.classList.contains(o),this.initPullUpTips(),this.initEvent()},_preventDefaultException:function(a,b){for(var c in b)if(b[c].test(a[c]))return!0;return!1},initEvent:function(){a.isFunction(this.options.down.callback)&&(this.element.addEventListener("touchstart",this),this.element.addEventListener("drag",this),this.element.addEventListener("dragend",this)),this.pullUpTips&&(this.element.addEventListener("dragup",this),this.isInScroll?this.element.addEventListener("scrollbottom",this):b.addEventListener("scroll",this))},handleEvent:function(a){switch(a.type){case"touchstart":this.isInScroll&&this._canPullDown()&&a.target&&!this._preventDefaultException(a.target,this.options.preventDefaultException)&&a.preventDefault();break;case"drag":this._drag(a);break;case"dragend":this._dragend(a);break;case"webkitTransitionEnd":this._transitionEnd(a);break;case"dragup":case"scroll":this._dragup(a);break;case"scrollbottom":this.pullUpLoading(a)}},initPullDownTips:function(){var b=this;a.isFunction(b.options.down.callback)&&(b.pullDownTips=function(){var a=c.querySelector("."+l);return a&&a.parentNode.removeChild(a),a||(a=c.createElement("div"),a.classList.add(l),a.innerHTML='<div class="mui-pull-top-wrapper"><span class="mui-pull-loading mui-icon mui-icon-pulldown"></span></div>',a.addEventListener("webkitTransitionEnd",b)),b.pullDownTipsIcon=a.querySelector(t),c.body.appendChild(a),a}())},initPullUpTips:function(){var b=this;a.isFunction(b.options.up.callback)&&(b.pullUpTips=function(){var a=b.element.querySelector("."+m);return a||(a=c.createElement("div"),a.classList.add(m),b.options.up.show||a.classList.add(s),a.innerHTML='<div class="mui-pull-bottom-wrapper"><span class="mui-pull-loading">'+b.options.up.contentdown+"</span></div>",b.element.appendChild(a)),b.pullUpTipsIcon=a.querySelector(t),a}())},_transitionEnd:function(a){a.target===this.pullDownTips&&this.removing&&this.removePullDownTips()},_dragup:function(b){var c=this;if(!c.loading){if(b&&b.detail&&a.gestures.session.drag)c.isDraggingUp=!0;else if(!c.isDraggingUp)return;c.isDragging||c._canPullUp()&&c.pullUpLoading(b)}},_canPullUp:function(){if(this.removing)return!1;if(this.isInScroll){var d=this.element.parentNode.getAttribute("data-scroll");if(d){var e=a.data[d];return e.y===e.maxScrollY}}return b.pageYOffset+b.innerHeight+this.options.up.offset>=c.documentElement.scrollHeight},_canPullDown:function(){if(this.removing)return!1;if(this.isInScroll){var b=this.element.parentNode.getAttribute("data-scroll");if(b){var d=a.data[b];return 0===d.y}}return 0===c.body.scrollTop},_drag:function(f){if(this.loading||this.stopped)return f.stopPropagation(),void f.detail.gesture.preventDefault();var g=f.detail;if(!this.isDragging&&"down"===g.direction&&this._canPullDown()){if(c.querySelector("."+l))return f.stopPropagation(),void f.detail.gesture.preventDefault();this.isDragging=!0,this.removing=!1,this.startDeltaY=g.deltaY,a.gestures.session.lockDirection=!0,a.gestures.session.startDirection=g.direction,this._pullStart(this.startDeltaY)}if(this.isDragging){f.stopPropagation(),f.detail.gesture.preventDefault();var h=g.deltaY-this.startDeltaY;h=Math.min(h,1.5*this.options.down.height),this.deltaY=h,this._pulling(h);var i=h>this.options.down.height?e:d;if(this.state!==i&&(this.state=i,this.state===e?(this.removing=!1,this.isNeedRefresh=!0):(this.removing=!0,this.isNeedRefresh=!1),this["_"+i](h)),a.os.ios&&parseFloat(a.os.version)>=8){var j=g.gesture.touches[0].clientY;if(j+10>b.innerHeight||10>j)return void this._dragend(f)}}},_dragend:function(a){var b=this;b.isDragging&&(b.isDragging=!1,b._dragEndAfterChangeOffset(b.isNeedRefresh)),b.isPullingUp&&(b.pullingUpTimeout&&clearTimeout(b.pullingUpTimeout),b.pullingUpTimeout=setTimeout(function(){b.isPullingUp=!1},1e3))},_pullStart:function(b){this.pullStart(b),a.trigger(this.element,f,{api:this,startDeltaY:b})},_pulling:function(b){this.pulling(b),a.trigger(this.element,g,{api:this,deltaY:b})},_beforeChangeOffset:function(b){this.beforeChangeOffset(b),a.trigger(this.element,h,{api:this,deltaY:b})},_afterChangeOffset:function(b){this.afterChangeOffset(b),a.trigger(this.element,i,{api:this,deltaY:b})},_dragEndAfterChangeOffset:function(b){this.dragEndAfterChangeOffset(b),a.trigger(this.element,j,{api:this,isNeedRefresh:b})},removePullDownTips:function(){if(this.pullDownTips)try{this.pullDownTips.parentNode&&this.pullDownTips.parentNode.removeChild(this.pullDownTips),this.pullDownTips=null,this.removing=!1}catch(a){}},pullStart:function(a){this.initPullDownTips(a)},pulling:function(a){this.pullDownTips.style.webkitTransform="translate3d(0,"+a+"px,0)"},beforeChangeOffset:function(a){this.pullDownTipsIcon.className=p},afterChangeOffset:function(a){this.pullDownTipsIcon.className=q},dragEndAfterChangeOffset:function(a){a?(this.pullDownTipsIcon.className=r,this.pullDownLoading()):(this.pullDownTipsIcon.className=p,this.endPullDownToRefresh())},pullDownLoading:function(){if(!this.loading){if(!this.pullDownTips)return this.initPullDownTips(),void this.dragEndAfterChangeOffset(!0);this.loading=!0,this.pullDownTips.classList.add(k),this.pullDownTips.style.webkitTransform="translate3d(0,"+this.options.down.height+"px,0)",this.options.down.callback.apply(this)}},pullUpLoading:function(a){this.loading||this.finished||(this.loading=!0,this.isDraggingUp=!1,this.pullUpTips.classList.remove(s),a&&a.detail&&a.detail.gesture&&a.detail.gesture.preventDefault(),this.pullUpTipsIcon.innerHTML=this.options.up.contentrefresh,this.options.up.callback.apply(this))},endPullDownToRefresh:function(){this.loading=!1,this.pullUpTips&&this.pullUpTips.classList.remove(s),this.pullDownTips.classList.add(k),this.pullDownTips.style.webkitTransform="translate3d(0,0,0)",this.deltaY<=0?this.removePullDownTips():this.removing=!0},endPullUpToRefresh:function(a){a?(this.finished=!0,this.pullUpTipsIcon.innerHTML=this.options.up.contentnomore,this.element.removeEventListener("dragup",this),b.removeEventListener("scroll",this)):this.pullUpTipsIcon.innerHTML=this.options.up.contentdown,this.loading=!1},setStopped:function(a){a!=this.stopped&&(this.stopped=a,this.pullUpTips&&this.pullUpTips.classList[a?"add":"remove"](s))},refresh:function(a){a&&this.finished&&this.pullUpTipsIcon&&(this.pullUpTipsIcon.innerHTML=this.options.up.contentdown,this.element.addEventListener("dragup",this),b.addEventListener("scroll",this),this.finished=!1)}}),a.fn.pullToRefresh=function(b){var c=[];return b=b||{},this.each(function(){var d=this,e=null,f=d.getAttribute("data-pullToRefresh");f?e=a.data[f]:(f=++a.uuid,a.data[f]=e=new a.PullToRefresh(d,b),d.setAttribute("data-pullToRefresh",f)),b.up&&b.up.auto&&e.pullUpLoading(),c.push(e)}),1===c.length?c[0]:c}}(mui,window,document);