pullToRefreshHelper.js 2.21 KB
define(function(){var h={UP:"up",DOWN:"down"},v="refreshing",p="releasing",g="end",a={el:null,container:document.body,area:null,distance:5,type:h.UP,moveDuration:225,minMovment:70,maxMovement:120,loadingDuration:300,movementDistance:15,loading:{show:function(){},hide:function(){}},loadData:function(){}},M={shouldPullToRefresh:function(t,e,n){return h.UP===e?t.scrollHeight-t.scrollTop-t.offsetHeight<=n:!t.scrollTop},namespace:function(t){return t+".pullToRefresh"},getEvts:function(){return{start:"touchstart",move:"touchmove",end:"touchend"}},clientY:function(t){return t.touches[0].clientY},isEffectArea:function(t,e){if(t){var n=$(t).offset(),o=n.top,i=n.top+n.height;return o<=e&&e<=i}},moveY:function(t,e,n){$(t).css({transform:"translate3d(0, "+e+"px, 0)",transition:"transform "+n+"ms ease 50ms"})},animateToMoveY:function(t,e,n){var o=$(t).parent(),i=function(t){return setTimeout(t,n)};return o.addClass("overflow-hidden"),M.moveY(t,e,n),i(function(){M.moveY(t,0,n),i(function(){o.removeClass("overflow-hidden")})})}},T=0,t=function(t){var r=this,e=++T,n=function(t){return M.namespace(t)+"_"+e},o=M.getEvts(),s=$.extend({},a,t),u=s.type,f=s.container,i=[n(o.start),n(o.move),n(o.end)].join(" "),c=0,l=0,m=f.offsetHeight===f.scrollHeight;this.uid=e,this.state=p,this.updateState=function(t){(this.state=t)===g&&this.destory()},this.destory=function(){$(f).off(n("scroll")),$(window).off(i)},$(f).off(n("scroll")).on(n("scroll"),function(t){m=M.shouldPullToRefresh(f,u,s.distance),t.preventDefault(),t.stopPropagation()}),$(window).off(i).on(n(o.start),function(t){m&&(c=M.clientY(t))}).on(n(o.move),function(t){if(l=M.clientY(t),m=M.shouldPullToRefresh(f,u,s.distance),t.stopPropagation(),!c||!m)return;var e=l-c,n=Math.abs(e)>=s.movementDistance,o=s.area||s.el;if(!M.isEffectArea(o,c)||!M.isEffectArea(o,l)||!n)return;e=h.UP===u?(e=Math.min(e,-s.minMovment),Math.max(e,-s.maxMovement)):(e=Math.max(e,s.minMovment),Math.min(e,s.maxMovement));var i=s.loading,a=p===r.state;if(clearTimeout(d),d=M.animateToMoveY(s.el,e,s.moveDuration),!a)return;i.show(),r.updateState(v),s.loadData(function(t){var e=t?p:g;setTimeout(function(){r.updateState(e)},s.loadingDuration),i.hide.apply(i,arguments)})}).on(n(o.end),function(){c=0});var d=0};return t.types=h,t});