-
Notifications
You must be signed in to change notification settings - Fork 22
/
microfiche.min.js
1 lines (1 loc) · 10.6 KB
/
microfiche.min.js
1
!function(){window.Microfiche=function(a){return this.initialize(a),this},Microfiche.VERSION="1.8.4",CALIBRATE_FIRST_GUESS=1e5,$.extend(Microfiche.prototype,{options:{autoplay:!1,autopause:!1,buttons:!0,bullets:!0,cyclic:!1,keyboard:!1,swipe:!0,clickToAdvance:!1,minDuration:250,duration:500,maxDuration:500,dragThreshold:25,elasticity:.5,swipeThreshold:.125,refreshOnResize:!1,prevButtonLabel:"←",nextButtonLabel:"→",noScrollAlign:"left"},x:0,initialize:function(a){return this.options=$.extend({},this.options,a),this.el=$(a.el),this.initialContents=this.el.contents(),this.el.data("microfiche",this),this.createFilm(),this.createScreen(),this.calibrate(CALIBRATE_FIRST_GUESS),this.film.width()<=this.screen.width()?(this.noScrollAlign(this.options.noScrollAlign),this.refreshOnResize(this.options.refreshOnResize),void 0):(this.createControls(),this.enableTouch(),this.enableKeyboard(),this.enableClick(),this.prepareCyclic(),this.run(this.options),void 0)},createFilm:function(){this.film=$('<div class="microfiche-film">').css({position:"absolute"}),this.el.children().appendTo(this.film).css({"float":"left"}),this.prepareFilm&&this.prepareFilm()},createScreen:function(){this.screen=$('<div class="microfiche-screen">').css({position:"relative",overflow:"hidden"}).appendTo(this.el).append(this.film)},prepareCyclic:function(){if(this.options.cyclic){var a=this.film.clone(),b=this.film.clone(),c=this.film.width();a.prependTo(this.film).css({position:"absolute",left:-c+"px"}),b.appendTo(this.film).css({position:"absolute",left:c+"px"})}},calibrate:function(a){this.screen.width(a);var b=this.film.width(),c=this.film.height();return b===a?this.calibrate(2*a):(this.film.width(b).height(c),this.screen.width("auto").height(c),void 0)},createControls:function(){var a=this;this.controls=$('<span class="microfiche-controls" />').appendTo(this.el),this.controls.on("click","a, button",function(b){a.didClickControl(b)}),this.options.bullets&&this.createBullets(),this.options.buttons&&this.createButtons(),this.updateControls()},createBullets:function(){for(var a=$('<span class="microfiche-bullets" />').appendTo(this.controls),b=0;b<this.totalPageCount();b++)$("<button>").addClass("microfiche-bullet").attr("data-microfiche-page",b).data("action","slideToPage").data("arguments",[b]).html(b+1).appendTo(a)},createButtons:function(){$("<button>").addClass("microfiche-button microfiche-prev-button").attr("rel","prev").data("action","prev").data("arguments",[]).html(this.options.prevButtonLabel).prependTo(this.controls),$("<button>").addClass("microfiche-button microfiche-next-button").attr("rel","next").data("action","next").data("arguments",[]).html(this.options.nextButtonLabel).appendTo(this.controls)},enableTouch:function(){if(this.options.swipe){var a=this,b=this.touchstart,c=this.touchmove,d=this.touchend;this.touchstart=function(){b.apply(a,arguments)},this.touchmove=function(){c.apply(a,arguments)},this.touchend=function(){d.apply(a,arguments)},this.film.on("touchstart",this.touchstart)}},enableKeyboard:function(){if(this.options.keyboard){var a=this;this.screen.attr("data-microfiche-keyboard",!0);var b=this.onkeydown;this.onkeydown=function(){b.apply(a,arguments)},$(document).on("keydown",this.onkeydown)}},enableClick:function(){if(this.options.clickToAdvance){var a=this,b=this.onmousedown;this.onmousedown=function(){b.apply(a,arguments)},this.film.on("mousedown",this.onmousedown)}},didClickControl:function(a){a.preventDefault();var b=$(a.target),c=b.data("action"),d=b.data("arguments");this[c].apply(this,d)},touchstart:function(a){var b=a.originalEvent.targetTouches;!b||b.length>1||(this.touchState={then:new Date,ox:b[0].pageX,oy:b[0].pageY,isDrag:!1},$(document).on("touchmove",this.touchmove).on("touchend",this.touchend))},touchmove:function(a){var b=a.originalEvent.targetTouches[0],c=b.pageX-this.touchState.ox,d=b.pageY-this.touchState.oy;if(!this.touchState.isDrag){if(Math.abs(d)>=this.options.dragThreshold)return this.touchend(),void 0;Math.abs(c)>=this.options.dragThreshold&&(this.touchState.isDrag=!0)}if(this.touchState.isDrag){a.preventDefault();var e=new Date,b=e-this.touchState.then;if(this.touchState.vx=(c-this.touchState.dx)/b,this.touchState.vy=(d-this.touchState.dy)/b,this.touchState.dx=c,this.touchState.dy=d,this.touchState.then=e,this.touchState.cx=this.x-c,!this.options.cyclic){if(this.touchState.cx<this.min()){var f=this.min()-this.touchState.cx;f*=this.options.elasticity,this.touchState.cx=this.min()-f}if(this.touchState.cx>this.max()){var f=this.touchState.cx-this.max();f*=this.options.elasticity,this.touchState.cx=this.max()+f}}this.film.css({WebkitTransition:"none",WebkitTransform:"translate3d("+-this.touchState.cx+"px, 0px, 0px)"})}},touchend:function(){if($(document).off("touchmove",this.touchmove).off("touchend",this.touchend),this.touchState.isDrag){var b=this.touchState.dx,c=this.screenWidth(),d=this.touchState.vx,e=this.options.swipeThreshold;-c*e>=b?this.slideByPages(1,d):b>=c*e?this.slideByPages(-1,d):this.slideByPages(0)}},onkeydown:function(a){37!==a.keyCode&&39!==a.keyCode||!this.isCentral("[data-microfiche-keyboard]")||(37===a.keyCode?this.slideByPages(-1):39===a.keyCode&&this.slideByPages(1))},onmousedown:function(){this.slideByPages(1)},updateControls:function(){this.options.bullets&&this.updateBullets(),this.options.buttons&&this.updateButtons()},updateBullets:function(){this.controls.find(".microfiche-bullet").removeClass("selected"),this.controls.find('[data-microfiche-page="'+this.currentPageIndex()+'"]').addClass("selected")},updateButtons:function(){this.options.cyclic||(this.controls.find('[rel="prev"]').attr("disabled",this.x<=this.min()),this.controls.find('[rel="next"]').attr("disabled",this.x>=this.max()))},round:function(a){var b=this.screenWidth();return Math.round(a/b)*b},constrain:function(a,b,c){return void 0===b&&(b=this.min()),void 0===c&&(c=this.max()),Math.max(b,Math.min(a,c))},roundAndConstrain:function(a,b,c){return this.constrain(this.round(a),b,c)},withinBounds:function(a){return this.min()<=a&&a<=this.max()},min:function(){return 0},max:function(){return this.film.width()-this.screenWidth()},currentPageIndex:function(){return Math.round(this.x/this.screenWidth())},totalPageCount:function(){return Math.ceil(this.film.width()/this.screenWidth())},screenWidth:function(){return this.el.width()},isCentral:function(a){var b=$($(a||".microfiche-screen").sort(function(a,b){return Math.abs(1-($(window).scrollTop()+$(window).height()/2-$(a).height()/2)/$(a).offset().top)-Math.abs(1-($(window).scrollTop()+$(window).height()/2-$(b).height()/2)/$(b).offset().top)})[0]).parent().data("microfiche");return b===this},jump:function(){this.el.trigger("microfiche:willMove"),this.performJump(),this.updateControls(),this.el.trigger("microfiche:didMove")},performJump:function(){this.film.css({left:-this.x})},transition:function(a){var b=this;this.options.cyclic&&this.handleWrappingTransition(),null==a&&(a=this.options.duration);var c=function(){b.afterTransition()};this.el.trigger("microfiche:willMove"),setTimeout(function(){b.performTransition(a,c)})},handleWrappingTransition:function(){this.x>this.max()?(this.x=this.min()-this.screenWidth(),this.touchState&&this.touchState.dx&&(this.x-=this.touchState.dx),this.jump(),this.x=this.min(),this.updateControls()):this.x<this.min()&&(this.x=this.max()+this.screenWidth(),this.touchState&&this.touchState.dx&&(this.x-=this.touchState.dx),this.jump(),this.x=this.max(),this.updateControls())},performTransition:function(a,b){this.film.stop().animate({left:-this.x+"px"},a,b)},afterTransition:function(){delete this.touchState,this.el.trigger("microfiche:didMove")},slideByPages:function(a,b){var c=this.x,d=this.screenWidth();if(this.x=this.constrain(Math.round((this.x/d+a)*d)),this.options.cyclic&&this.x==c&&(this.x+=a*d),b)var e=this.constrain(Math.abs((this.x-c)/b),this.options.minDuration,this.options.maxDuration);else var e=this.options.duration;this.updateControls(),this.transition(e)},slideToPage:function(a){this.x=this.constrain(a*this.screenWidth()),this.updateControls(),this.transition()},slideToPoint:function(a){this.x=this.roundAndConstrain(a),this.updateControls(),this.transition()},jumpToPage:function(a){this.x=this.constrain(a*this.screenWidth()),this.updateControls(),this.jump()},jumpToPoint:function(a){this.x=this.roundAndConstrain(a),this.updateControls(),this.jump()},prev:function(){this.slideByPages(-1)},next:function(){this.slideByPages(1)},autoplay:function(a){if(this.autoplayTimeout&&(clearInterval(this.autoplayTimeout),delete this.autoplayTimeout),a=+a,!(isNaN(a)||0>=a)){var b=this;this.autoplayTimeout=setInterval(function(){b.pause||b.next()},1e3*a)}},autopause:function(){var a=this;this.el.hover(function(){a.pause=!0},function(){a.pause=!1})},destroy:function(){this.clearListeners(),this.el.empty(),this.el.removeData("microfiche")},refresh:function(){var a=this.el.data("microfiche").options,b=this.getContents();return this.destroy(),this.el.append(b),new Microfiche($.extend({el:this.el},a)),this.el},getContents:function(){return this.contentsChanged()?this.el.html():this.el.data("microfiche").initialContents},contentsChanged:function(){return 0===this.el.find(".microfiche-screen").length},refreshOnResize:function(a){function e(){b.refresh()}function f(){var a=$(window).width();a!==c&&(c=a,e())}function g(){d&&clearTimeout(d),d=setTimeout(f,a)}if(this.options.refreshOnResize=a,this.resizeHandler&&this.clearResizeHandler(),a!==!1){a===!0&&(a=250);var d,b=this,c=0;this.resizeHandler=g,$(window).on("resize",this.resizeHandler)}},clearListeners:function(){$(document).off("keydown",this.onkeydown).off("touchmove",this.touchmove).off("touchend",this.touchend),this.clearResizeHandler()},clearResizeHandler:function(){$(window).off("resize",this.resizeHandler)},noScrollAlign:function(a){if(!(this.film.width()>this.screen.width()))switch(a){case"center":this.film.css({left:"50%",marginLeft:-1*(this.film.width()/2)+"px",right:"auto"});break;case"right":this.film.css({left:"auto",marginLeft:"auto",right:0});break;default:this.film.css({left:0,marginLeft:"auto",right:"auto"})}},run:function(a){for(var b in a){var c=this[b];$.isFunction(c)&&c.call(this,a[b])}}}),"WebKitCSSMatrix"in window&&"m11"in new WebKitCSSMatrix&&$.extend(Microfiche.prototype,{prepareFilm:function(){this.film.css({WebkitTransform:"translate3d(0px, 0px, 0px)"})},performTransition:function(a,b){this.film.one("webkitTransitionEnd",b).css({WebkitTransition:"-webkit-transform "+a+"ms",WebkitTransform:"translate3d("+-this.x+"px, 0px, 0px)"})},performJump:function(){this.film.css({WebkitTransition:"-webkit-transform 0ms",WebkitTransform:"translate3d("+-this.x+"px, 0px, 0px)"})}}),jQuery.fn.microfiche=function(a){return this.each(function(){var b=$(this).data("microfiche");b?b.run(a):new Microfiche($.extend({el:this},a))})}}();