-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.touchdragv.min.js
6 lines (6 loc) · 10.6 KB
/
jquery.touchdragv.min.js
1
2
3
4
5
6
/*! jQuery.touchdragv (https://github.com/Takazudo/jQuery.touchdragv)
* lastupdate: 2013-04-19
* version: 1.1.2
* author: 'Takazudo' Takeshi Takatsudo <takazudo@gmail.com>
* License: MIT */
(function(){var t=[].slice,e={}.hasOwnProperty,r=function(t,r){function n(){this.constructor=t}for(var i in r)e.call(r,i)&&(t[i]=r[i]);return n.prototype=r.prototype,t.prototype=new n,t.__super__=r.prototype,t},n=function(t,e){return function(){return t.apply(e,arguments)}};(function(e,i,o){var s,h;return s=e(o),h=e.TouchdragvNs={},h.normalizeXY=function(t){var e,r,n;return r={},e=t.originalEvent,null!=e.changedTouches?(n=e.changedTouches[0],r.x=n.pageX,r.y=n.pageY):(r.x=t.pageX||e.pageX,r.y=t.pageY||e.pageY),r},h.support={},h.ua={},h.support.addEventListener="addEventListener"in o,h.support.touch="ontouchend"in o,h.support.mspointer=i.navigator.msPointerEnabled||!1,h.ua.win8orhigh=function(){var t,e,r;return e=navigator.userAgent,(t=e.match(/Windows NT ([\d\.]+)/))?(r=1*t[1],6.2>r?!1:!0):!1}(),h.getEventNameSet=function(t){var e;switch(e={},t){case"touchstart":e.move="touchmove",e.end="touchend";break;case"mousedown":e.move="mousemove",e.end="mouseup";break;case"MSPointerDown":e.move="MSPointerMove",e.end="MSPointerUp";break;case"pointerdown":e.move="pointermove",e.end="pointerup"}return e},h.getTopPx=function(t){var e;return e=t.css("top"),e="auto"===e?0:1*e.replace(/px/,"")},h.startWatchGestures=function(){var t,e;return e=!1,t=function(){return e=!0,s.bind("gesturestart",function(){return h.whileGesture=!0}),s.bind("gestureend",function(){return h.whileGesture=!1})},function(){return this.initDone?void 0:t()}}(),h.Event=function(){function e(){}return e.prototype.on=function(t,e){var r,n,i,o,s;for(null==this._callbacks&&(this._callbacks={}),r=t.split(" "),o=0,s=r.length;s>o;o++)n=r[o],(i=this._callbacks)[n]||(i[n]=[]),this._callbacks[n].push(e);return this},e.prototype.once=function(t,e){return this.on(t,function(){return this.off(t,arguments.callee),e.apply(this,arguments)}),this},e.prototype.trigger=function(){var e,r,n,i,o,s,h;if(e=arguments.length>=1?t.call(arguments,0):[],n=e.shift(),i=null!=(h=this._callbacks)?h[n]:void 0){for(o=0,s=i.length;s>o&&(r=i[o],r.apply(this,e)!==!1);o++);return this}},e.prototype.off=function(t,e){var r,n,i,o,s,h;if(!t)return this._callbacks={},this;if(i=null!=(h=this._callbacks)?h[t]:void 0,!i)return this;if(!e)return delete this._callbacks[t],this;for(n=o=0,s=i.length;s>o;n=++o)if(r=i[n],r===e){i=i.slice(),i.splice(n,1),this._callbacks[t]=i;break}return this},e}(),h.OneDrag=function(t){function e(){this._scrollDirectionDecided=!1}return r(e,t),e.prototype.applyTouchStart=function(t){var e;return e=h.normalizeXY(t),this.startPageX=e.x,this.startPageY=e.y,this},e.prototype.applyTouchMove=function(t){var e,r,n,i,o=this;return e=h.normalizeXY(t),i=function(){var t;return t=e.y-o.startPageY,o.trigger("dragmove",{y:t})},this._scrollDirectionDecided?i():(r=Math.abs(e.x-this.startPageX),n=Math.abs(e.y-this.startPageY),(r>5||n>5)&&(this._scrollDirectionDecided=!0,r>5?this.trigger("xscrolldetected"):n>5&&this.trigger("yscrolldetected"))),this},e.prototype.destroy=function(){return this.off(),this},e}(h.Event),h.TouchdragvEl=function(t){function i(t,r){this.$el=t,this._handleTouchEnd=n(this._handleTouchEnd,this),this._handleTouchMove=n(this._handleTouchMove,this),this._handleTouchStart=n(this._handleTouchStart,this),this._handleClickToIgnore=n(this._handleClickToIgnore,this),this.el=this.$el[0],this.options=e.extend({},this.defaults,r),this.disabled=!1,h.startWatchGestures(),this._handlePointerEvents(),this._prepareEls(),this._eventify(),this.options.triggerrefreshimmediately&&this.refresh()}return r(i,t),i.prototype.defaults={inner:"> *",backanim_duration:250,backanim_easing:"swing",beforefirstrefresh:null,triggerrefreshimmediately:!0,tweakinnerpositionstyle:!1},i.prototype.refresh=function(){return this._calcMinMaxTop(),this._handleTooNarrow(),this._handleInnerOver(),this._firstRefreshDone||(this.options.beforefirstrefresh&&this.options.beforefirstrefresh(this),this.trigger("firstrefresh",this),this._firstRefreshDone=!0),this.trigger("refresh",this),this},i.prototype._handlePointerEvents=function(){return h.support.mspointer?(this.el.style.msTouchAction="none",this):this},i.prototype._prepareEls=function(){return this.$inner=this.$el.find(this.options.inner),this.options.tweakinnerpositionstyle&&this.$inner.css({position:"relative"}),this},i.prototype._calcMinMaxTop=function(){return this._maxTop=0,this._minTop=-(this.$inner.outerHeight()-this.$el.innerHeight()),this},i.prototype._eventify=function(){var t;return t="pointerdown MSPointerDown touchstart mousedown",this.$el.bind(t,this._handleTouchStart),h.support.addEventListener&&this.el.addEventListener("click",e.noop,!0),this},i.prototype._handleClickToIgnore=function(t){return t.stopPropagation(),t.preventDefault(),this},i.prototype._handleTouchStart=function(t){var e,r=this;return this.disabled?this:this._whileDrag?this:h.whileGesture?this:("mousedown"===t.type&&t.preventDefault(),this._currentEventNameSet=h.getEventNameSet(t.type),this._whileDrag=!0,this._slidecanceled=!1,this._shouldSlideInner=!1,e=this._currentDrag=new h.OneDrag,e.on("xscrolldetected",function(){return r._whileDrag=!1,r._slidecanceled=!0,r.trigger("slidecancel")}),e.on("yscrolldetected",function(){return r._shouldSlideInner=!0,r.trigger("dragstart"),r.$el.delegate("a","click",r._handleClickToIgnore)}),e.on("dragmove",function(t){return r.trigger("drag"),r._moveInner(t.y)}),this._innerStartTop=h.getTopPx(this.$inner),e.applyTouchStart(t),s.bind(this._currentEventNameSet.move,this._handleTouchMove),s.bind(this._currentEventNameSet.end,this._handleTouchEnd),this)},i.prototype._handleTouchMove=function(t){return this._whileDrag?h.whileGesture?this:(this._currentDrag.applyTouchMove(t),this._shouldSlideInner&&(t.preventDefault(),t.stopPropagation()),this):this},i.prototype._handleTouchEnd=function(){var t=this;return this._whileDrag=!1,s.unbind(this._currentEventNameSet.move,this._handleTouchMove),s.unbind(this._currentEventNameSet.end,this._handleTouchEnd),this._currentDrag.destroy(),this._currentEventNameSet=null,this._slidecanceled||this.trigger("dragend"),setTimeout(function(){return t.$el.undelegate("a","click",t._handleClickToIgnore)},10),this._handleInnerOver(!0),this},i.prototype._moveInner=function(t){var e,r;return r=this._innerStartTop+t,r>this._maxTop?r=this._maxTop+(r-this._maxTop)/3:this._minTop>r&&(r=this._minTop+(r-this._minTop)/3),this.$inner.css("top",r),e={top:r},this.trigger("move",e),this},i.prototype._handleInnerOver=function(t){var e,r,n,i,o,s=this;return null==t&&(t=!1),this.isInnerTooNarrow()?this:(o=function(){return t?s.trigger("moveend"):void 0},n=null,i=this.currentSlideTop(),r=i>this._maxTop,e=this._minTop>i,r||e?(r&&(n=this._maxTop),e&&(n=this._minTop),this.slide(n,!0,function(){return o()}),this):(o(),this))},i.prototype._handleTooNarrow=function(){return this.isInnerTooNarrow()?(this.disable(),this.$inner.css("top",0)):this.enable(),this},i.prototype.isInnerTooNarrow=function(){var t,e;return t=this.$el.height(),e=this.$inner.height(),t>=e},i.prototype.disable=function(){return this.disabled=!0,this},i.prototype.enable=function(){return this.disabled=!1,this},i.prototype.slide=function(t,r,n){var i,o,s,h=this;return null==r&&(r=!1),t>this._maxTop&&(t=this._maxTop),this._minTop>t&&(t=this._minTop),i=this.options.backanim_duration,o=this.options.backanim_easing,s={top:t},e.Deferred(function(t){var e;return h.trigger("beforeslide"),e=function(){return h.trigger("afterslide"),"function"==typeof n&&n(),t.resolve()},r?h.$inner.stop().animate(s,i,o,function(){return e()}):(h.$inner.stop().css(s),e())}).promise()},i.prototype.currentSlideTop=function(){return h.getTopPx(this.$inner)},i.prototype.updateInnerHeight=function(t){return this.$inner.height(t),this},i}(h.Event),h.TouchdragvFitty=function(t){function n(t,r){this.$el=t,this.options=e.extend({},this.defaults,r),this.currentIndex=this.options.startindex,this._preparetouchdragv(),this.options.triggerrefreshimmediately&&this.refresh()}return r(n,t),n.prototype.defaults={item:null,beforefirstfresh:null,startindex:0,triggerrefreshimmediately:!0},n.prototype._preparetouchdragv=function(){var t,r=this;return t=e.extend({},this.options),t.triggerrefreshimmediately=!1,t.beforefirstrefresh=function(t){return t.once("firstrefresh",function(){var t;return"function"==typeof(t=r.options).beforefirstrefresh&&t.beforefirstrefresh(r),r.trigger("firstrefresh",r),r._firstRefreshDone=!0}),t.on("refresh",function(){return r.trigger("refresh")}),t.on("slidecancel",function(){return r.trigger("slidecancel")}),t.on("dragstart",function(){return r.trigger("dragstart")}),t.on("drag",function(){return r.trigger("drag")}),t.on("dragend",function(){return r.trigger("dragend")}),t.on("moveend",function(){var e,n,i,o;return o=-t.currentSlideTop(),n=r.$el.innerHeight(),i=null,e=o/n,r.currentIndex>e?i=r.currentIndex-1:e>r.currentIndex&&(i=r.currentIndex+1),null!==i?(r.updateIndex(i),r.adjustToFit(n,!0)):void 0})},this._touchdragv=new h.TouchdragvEl(this.$el,t),this},n.prototype.updateIndex=function(t){var e,r;return t>=0&&this.$items.length>t?(r=this.currentIndex,this.currentIndex=t,r!==t&&(e={index:this.currentIndex},this.trigger("indexchange",e)),!0):!1},n.prototype.refresh=function(){var t,e;return this.$items=this.$el.find(this.options.item),e=this._itemHeight=this.$el.innerHeight(),t=e*this.$items.length,this._touchdragv.updateInnerHeight(t),this.$items.height(e),this._touchdragv.refresh(),this.adjustToFit(e),this},n.prototype.adjustToFit=function(t,r,n){var i=this;return null==r&&(r=!1),null==t&&(t=this.$items.height()),e.Deferred(function(e){var o,s,h;return o=i.currentIndex,s=-t*o,h=i._touchdragv.currentSlideTop(),s===h?(e.resolve(),i):(i._sliding||i.trigger("slidestart"),i._sliding=!0,i._touchdragv.slide(s,r,function(){var t;return i._sliding=!1,t={index:i.currentIndex},i.trigger("slideend",t),"function"==typeof n&&n(),e.resolve()}))}).promise()},n.prototype.to=function(t,r){var n,i=this;return null==r&&(r=!1),n=this.updateIndex(t),e.Deferred(function(t){return n?i.adjustToFit(null,r,function(){return t.resolve()}):(i.trigger("invalidindexrequested"),t.resolve())}).promise()},n.prototype.next=function(t){return null==t&&(t=!1),this.to(this.currentIndex+1,t)},n.prototype.prev=function(t){return null==t&&(t=!1),this.to(this.currentIndex-1,t)},n}(h.Event),e.fn.touchdragv=function(t){return this.each(function(r,n){var i,o;i=e(n),o=new h.TouchdragvEl(i,t),i.data("touchdragv",o)})},e.fn.touchdragvfitty=function(t){return this.each(function(r,n){var i,o;i=e(n),o=new h.TouchdragvFitty(i,t),i.data("touchdragvfitty",o)})},e.Touchdragv=h.touchdragvEl,e.TouchdragvFitty=h.touchdragvFitty})(jQuery,window,document)}).call(this);