Skip to content

Instantly share code, notes, and snippets.

@jescalan
Created November 4, 2013 20:12
Show Gist options
  • Select an option

  • Save jescalan/7308511 to your computer and use it in GitHub Desktop.

Select an option

Save jescalan/7308511 to your computer and use it in GitHub Desktop.
component/swipe standalone build, does not break AMD but must be shimmed.
!function(){function t(n,e,i){var r=t.resolve(n);if(null==r){i=i||n,e=e||"root";var s=Error('Failed to require "'+i+'" from "'+e+'"');throw s.path=i,s.parent=e,s.require=!0,s}var o=t.modules[r];return o.exports||(o.exports={},o.client=o.component=!0,o.call(this,o.exports,t.relative(r),o)),o.exports}t.modules={},t.aliases={},t.resolve=function(n){"/"===n.charAt(0)&&(n=n.slice(1));for(var e=n+"/index.js",i=[n,n+".js",n+".json",n+"/index.js",n+"/index.json"],r=0;r<i.length;r++){var n=i[r];if(t.modules.hasOwnProperty(n))return n}return t.aliases.hasOwnProperty(e)?t.aliases[e]:void 0},t.normalize=function(t,n){var e=[];if("."!=n.charAt(0))return n;t=t.split("/"),n=n.split("/");for(var i=0;i<n.length;++i)".."==n[i]?t.pop():"."!=n[i]&&""!=n[i]&&e.push(n[i]);return t.concat(e).join("/")},t.register=function(n,e){t.modules[n]=e},t.alias=function(n,e){if(!t.modules.hasOwnProperty(n))throw Error('Failed to alias "'+n+'", it does not exist');t.aliases[e]=n},t.relative=function(n){function e(t,n){for(var e=t.length;e--;)if(t[e]===n)return e;return-1}function i(e){var r=i.resolve(e);return t(r,n,e)}var r=t.normalize(n,"..");return i.resolve=function(i){var s=i.charAt(0);if("/"==s)return i.slice(1);if("."==s)return t.normalize(r,i);var o=n.split("/"),a=e(o,"deps")+1;return a||(a=0),i=o.slice(0,a+1).join("/")+"/deps/"+i},i.exists=function(n){return t.modules.hasOwnProperty(i.resolve(n))},i},t.register("component-emitter/index.js",function(t,n,e){function i(t){return t?r(t):void 0}function r(t){for(var n in i.prototype)t[n]=i.prototype[n];return t}e.exports=i,i.prototype.on=function(t,n){return this._callbacks=this._callbacks||{},(this._callbacks[t]=this._callbacks[t]||[]).push(n),this},i.prototype.once=function(t,n){function e(){i.off(t,e),n.apply(this,arguments)}var i=this;return this._callbacks=this._callbacks||{},n._off=e,this.on(t,e),this},i.prototype.off=i.prototype.removeListener=i.prototype.removeAllListeners=function(t,n){if(this._callbacks=this._callbacks||{},0==arguments.length)return this._callbacks={},this;var e=this._callbacks[t];if(!e)return this;if(1==arguments.length)return delete this._callbacks[t],this;var i=e.indexOf(n._off||n);return~i&&e.splice(i,1),this},i.prototype.emit=function(t){this._callbacks=this._callbacks||{};var n=[].slice.call(arguments,1),e=this._callbacks[t];if(e){e=e.slice(0);for(var i=0,r=e.length;r>i;++i)e[i].apply(this,n)}return this},i.prototype.listeners=function(t){return this._callbacks=this._callbacks||{},this._callbacks[t]||[]},i.prototype.hasListeners=function(t){return!!this.listeners(t).length}}),t.register("component-event/index.js",function(t){t.bind=function(t,n,e,i){return t.addEventListener?t.addEventListener(n,e,i||!1):t.attachEvent("on"+n,e),e},t.unbind=function(t,n,e,i){return t.removeEventListener?t.removeEventListener(n,e,i||!1):t.detachEvent("on"+n,e),e}}),t.register("component-event-manager/index.js",function(t,n,e){function i(t,n){this.target=t,this.obj=n,this._bindings={}}e.exports=i,i.prototype.onbind=function(t){return this._bind=t,this},i.prototype.onunbind=function(t){return this._unbind=t,this},i.prototype.bind=function(t,n){var e=this.addBinding.apply(this,arguments);return this._onbind&&this._onbind(t,n,e),this._bind(t,e),e},i.prototype.addBinding=function(t,n){function e(){var t=[].slice.call(arguments).concat(r);i[n].apply(i,t)}var i=this.obj,n=n||"on"+t,r=[].slice.call(arguments,2);return this._bindings[t]=this._bindings[t]||{},this._bindings[t][n]=e,e},i.prototype.unbind=function(t,n){if(0==arguments.length)return this.unbindAll();if(1==arguments.length)return this.unbindAllOf(t);var e=this._bindings[t][n];return this._onunbind&&this._onunbind(t,n,e),this._unbind(t,e),e},i.prototype.unbindAll=function(){for(var t in this._bindings)this.unbindAllOf(t)},i.prototype.unbindAllOf=function(t){var n=this._bindings[t];if(n)for(var e in n)this.unbind(t,e)}}),t.register("component-events/index.js",function(t,n,e){var i=n("event-manager"),r=n("event");e.exports=function(t,n){var e=new i(t,n);return e.onbind(function(n,e){r.bind(t,n,e)}),e.onunbind(function(n,e){r.unbind(t,n,e)}),e}}),t.register("component-has-translate3d/index.js",function(t,n,e){var i=n("transform-property");if(!i)return e.exports=!1;var r={webkitTransform:"-webkit-transform",OTransform:"-o-transform",msTransform:"-ms-transform",MozTransform:"-moz-transform",transform:"transform"},s=document.createElement("div");s.style[i]="translate3d(1px,1px,1px)",document.body.insertBefore(s,null);var o=window.getComputedStyle(s).getPropertyValue(r[i]);document.body.removeChild(s),e.exports=null!=o&&o.length&&"none"!=o}),t.register("component-transform-property/index.js",function(t,n,e){for(var i,r=["webkitTransform","MozTransform","msTransform","OTransform","transform"],s=document.createElement("p"),o=0;o<r.length;o++)if(i=r[o],null!=s.style[i]){e.exports=i;break}}),t.register("component-transitionend-property/index.js",function(t,n,e){var i={WebkitTransition:"webkitTransitionEnd",MozTransition:"transitionend",OTransition:"oTransitionEnd",msTransition:"MSTransitionEnd",transition:"transitionend"},r=document.createElement("p");for(var s in i)if(null!=r.style[s]){e.exports=i[s];break}}),t.register("jkroso-computed-style/index.js",function(t,n,e){e.exports=window.getComputedStyle,e.exports||(e.exports=function(t){return t.currentStyle})}),t.register("swipe/index.js",function(t,n,e){function i(t){if(!(this instanceof i))return new i(t);if(!t)throw new TypeError("Swipe() requires an element");this.child=t.children[0],this.currentEl=this.children().visible[0],this.currentVisible=0,this.current=0,this.el=t,this.refresh(),this.interval(5e3),this.duration(300),this.fastThreshold(200),this.threshold(.5),this.show(0,0,{silent:!0}),this.bind()}function r(t,n){for(var e=0;e<t.length;e++)if(t[e]==n)return e;return-1}function s(t){return"none"!=u(t).display}var o=n("transitionend-property"),a=n("transform-property"),h=n("has-translate3d"),u=n("computed-style"),l=n("emitter"),c=n("event"),d=n("events"),p=Math.min,f=Math.max;e.exports=i,l(i.prototype),i.prototype.threshold=function(t){this._threshold=t},i.prototype.fastThreshold=function(t){this._fastThreshold=t},i.prototype.refresh=function(){var t=this.children(),n=t.visible.length,e=this.visible||n,i=r(t.visible,this.currentEl);n>=e||i>this.currentVisible||0>i?n>e&&i>this.currentVisible&&(this.currentVisible+=i-this.currentVisible):this.currentVisible-=this.currentVisible-i,this.visible=n,this.childWidth=this.el.getBoundingClientRect().width,this.width=Math.ceil(this.childWidth*n),this.child.style.width=this.width+"px",this.child.style.height=this.height+"px",this.show(this.currentVisible,0,{silent:!0})},i.prototype.bind=function(){this.events=d(this.child,this),this.events.bind("mousedown","ontouchstart"),this.events.bind("mousemove","ontouchmove"),this.events.bind("touchstart"),this.events.bind("touchmove"),this.docEvents=d(document,this),this.docEvents.bind("mouseup","ontouchend"),this.docEvents.bind("touchend")},i.prototype.unbind=function(){this.events.unbind(),this.docEvents.unbind()},i.prototype.ontouchstart=function(t){var n=t.changedTouches;n&&(n=n[0],this.transitionDuration(0),this.dx=0,this.updown=null,this.down={x:n.pageX,y:n.pageY,at:new Date})},i.prototype.ontouchmove=function(t){if(this.down&&!this.updown){var n=t.changedTouches;if(n&&t.touches.length<=1){n=n[0];var e=this.down,i=n.pageX,r=this.childWidth,s=this.currentVisible;if(this.dx=i-e.x,null==this.updown){var o=n.pageY,a=o-e.y,h=a/this.dx;if(h>1||-1>h)return this.updown=!0,void 0;this.updown=!1}t.preventDefault();var u=this.dx<0?1:0;this.isFirst()&&0==u&&(this.dx/=2),this.isLast()&&1==u&&(this.dx/=2),this.translate(s*r+-this.dx)}}},i.prototype.ontouchend=function(t){if(t.stopPropagation(),this.down){var n=t.changedTouches;if(n){n=n[0];var e=this.dx;n.pageX;var i=this.childWidth,r=new Date-this.down.at,s=r<this._fastThreshold?i/10:i*this._threshold,o=0>e?1:0,a=Math.abs(e)>=s;return this.down=null,this.isFirst()&&1==o&&a?this.next():this.isFirst()?this.prev():this.isLast()&&1==o?this.next():1==o&&a?this.next():0==o&&a?this.prev():(this.show(this.currentVisible),void 0)}}},i.prototype.duration=function(t){return this._duration=t,this},i.prototype.interval=function(t){return this._interval=t,this},i.prototype.play=function(){return this.timer?void 0:(this.timer=setInterval(this.cycle.bind(this),this._interval),this)},i.prototype.stop=function(){return clearInterval(this.timer),this.timer=null,this},i.prototype.cycle=function(){this.isLast()?(this.currentVisible=-1,this.next()):this.next()},i.prototype.isFirst=function(){return 0==this.currentVisible},i.prototype.isLast=function(){return this.currentVisible==this.visible-1},i.prototype.prev=function(){return this.show(this.currentVisible-1),this},i.prototype.next=function(){return this.show(this.currentVisible+1),this},i.prototype.show=function(t,n,e){e=e||{},null==n&&(n=this._duration);var i=this,s=this.children();if(t=f(0,p(t,s.visible.length-1)),this.currentVisible=t,this.currentEl=s.visible[t],this.current=r(s.all,this.currentEl),this.transitionDuration(n),this.translate(this.childWidth*t),!e.silent){if(this.emit("showing",this.current,this.currentEl),!n)return this;c.bind(this.child,o,function a(){i.current==t&&i.emit("show",i.current,i.currentEl),c.unbind(i.child,o,a)})}return this},i.prototype.children=function(){for(var t=this.child.children,n={all:t,visible:[],hidden:[]},e=0;e<t.length;e++){var i=t[e];s(i)?n.visible.push(i):n.hidden.push(i)}return n},i.prototype.transitionDuration=function(t){var n=this.child.style;n.webkitTransition=t+"ms -webkit-transform",n.MozTransition=t+"ms -moz-transform",n.msTransition=t+"ms -ms-transform",n.OTransition=t+"ms -o-transform",n.transition=t+"ms transform"},i.prototype.translate=function(t){var n=this.child.style;t=-t,n[a]=h?"translate3d("+t+"px, 0, 0)":"translateX("+t+"px)"}}),t.alias("component-emitter/index.js","swipe/deps/emitter/index.js"),t.alias("component-event/index.js","swipe/deps/event/index.js"),t.alias("component-events/index.js","swipe/deps/events/index.js"),t.alias("component-event/index.js","component-events/deps/event/index.js"),t.alias("component-event-manager/index.js","component-events/deps/event-manager/index.js"),t.alias("component-has-translate3d/index.js","swipe/deps/has-translate3d/index.js"),t.alias("component-transform-property/index.js","component-has-translate3d/deps/transform-property/index.js"),t.alias("component-transform-property/index.js","swipe/deps/transform-property/index.js"),t.alias("component-transitionend-property/index.js","swipe/deps/transitionend-property/index.js"),t.alias("component-transitionend-property/index.js","swipe/deps/transitionend-property/index.js"),t.alias("component-transitionend-property/index.js","component-transitionend-property/index.js"),t.alias("jkroso-computed-style/index.js","swipe/deps/computed-style/index.js"),"object"==typeof exports?module.exports=t("swipe"):"function"==typeof define&&define.amd?define(function(){return t("swipe")}):window.Swipe=t("swipe")}();
@jescalan
Copy link
Copy Markdown
Author

jescalan commented Nov 4, 2013

Docs can be found here: https://github.com/component/swipe

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment