forked from metafizzy/infinite-scroll
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathjquery.infinitescroll.min.js
14 lines (11 loc) · 8.9 KB
/
jquery.infinitescroll.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/*
--------------------------------
Infinite Scroll
--------------------------------
+ https://github.com/paulirish/infinitescroll
+ version 2.0b2.110713
+ Copyright 2011 Paul Irish & Luke Shumard
+ Licensed under the MIT license
+ Documentation: http://infinite-scroll.com/
*/
(function(e,t,n){t.infinitescroll=function(n,r,i){this.element=t(i);this._create(n,r)};t.infinitescroll.defaults={loading:{finished:n,finishedMsg:"<em>Congratulations, you've reached the end of the internet.</em>",img:"http://www.infinite-scroll.com/loading.gif",msg:null,msgText:"<em>Loading the next set of posts...</em>",selector:null,speed:"fast",start:n},state:{isDuringAjax:false,isInvalidPage:false,isDestroyed:false,isDone:false,isPaused:false,currPage:1},callback:n,debug:false,behavior:n,binder:t(e),nextSelector:"div.navigation a:first",navSelector:"div.navigation",contentSelector:null,extraScrollPx:150,itemSelector:"div.post",animate:false,pathParse:n,dataType:"html",extractLink:false,appendCallback:true,bufferPx:40,errorCallback:function(){},infid:0,pixelsFromNavToBottom:n,path:n};t.infinitescroll.prototype={_binding:function(t){var r=this,i=r.options;i.v="2.0b2.111027";if(!!i.behavior&&this["_binding_"+i.behavior]!==n){this["_binding_"+i.behavior].call(this);return}if(t!=="bind"&&t!=="unbind"){this._debug("Binding value "+t+" not valid");return false}if(t=="unbind"){this.options.binder.unbind("smartscroll.infscr."+r.options.infid)}else{this.options.binder[t]("smartscroll.infscr."+r.options.infid,function(){r.scroll()})}this._debug("Binding",t)},_create:function(r,i){if(!this._validate(r)){return false}var s=this.options=t.extend(true,{},t.infinitescroll.defaults,r),o=/(.*?\/\/).*?(\/.*)/,u=t(s.nextSelector).attr("href");s.contentSelector=s.contentSelector||this.element;s.loading.selector=s.loading.selector||s.contentSelector;if(!u){this._debug("Navigation selector not found");return}s.path=this._determinepath(u);s.loading.msg=t('<div id="infscr-loading"><img alt="Loading..." src="'+s.loading.img+'" /><div>'+s.loading.msgText+"</div></div>");(new Image).src=s.loading.img;s.pixelsFromNavToBottom=t(document).height()-t(s.navSelector).offset().top;s.loading.start=s.loading.start||function(){t(s.navSelector).hide();s.loading.msg.appendTo(s.loading.selector).show(s.loading.speed,function(){beginAjax(s)})};s.loading.finished=s.loading.finished||function(){s.loading.msg.fadeOut("normal")};s.callback=function(e,r){if(!!s.behavior&&e["_callback_"+s.behavior]!==n){e["_callback_"+s.behavior].call(t(s.contentSelector)[0],r)}if(i){i.call(t(s.contentSelector)[0],r,s)}};this._setup()},_debug:function(){if(this.options&&this.options.debug){return e.console&&console.log.call(console,arguments)}},_determinepath:function(t){var r=this.options;if(r.extractLink){return t}if(!!r.behavior&&this["_determinepath_"+r.behavior]!==n){this["_determinepath_"+r.behavior].call(this,t);return}if(!!r.pathParse){this._debug("pathParse manual");return r.pathParse()}else if(t.match(/^(.*?)\b2\b(.*?$)/)){t=t.match(/^(.*?)\b2\b(.*?$)/).slice(1)}else if(t.match(/^(.*?)2(.*?$)/)){if(t.match(/^(.*?page=)2(\/.*|$)/)){t=t.match(/^(.*?page=)2(\/.*|$)/).slice(1);return t}t=t.match(/^(.*?)2(.*?$)/).slice(1)}else{if(t.match(/^(.*?page=)1(\/.*|$)/)){t=t.match(/^(.*?page=)1(\/.*|$)/).slice(1);return t}else{this._debug("Sorry, we couldn't parse your Next (Previous Posts) URL. Verify your the css selector points to the correct A tag. If you still get this error: yell, scream, and kindly ask for help at infinite-scroll.com.");r.state.isInvalidPage=true}}this._debug("determinePath",t);return t},_error:function(t){var r=this.options;if(!!r.behavior&&this["_error_"+r.behavior]!==n){this["_error_"+r.behavior].call(this,t);return}if(t!=="destroy"&&t!=="end"){t="unknown"}this._debug("Error",t);if(t=="end"){this._showdonemsg()}r.state.isDone=true;r.state.currPage=1;r.state.isPaused=false;this._binding("unbind")},_loadcallback:function(i,s){var o=this.options,u=this.options.callback,a=o.state.isDone?"done":!o.appendCallback?"no-append":"append",f;if(!!o.behavior&&this["_loadcallback_"+o.behavior]!==n){this["_loadcallback_"+o.behavior].call(this,i,s);return}switch(a){case"done":this._showdonemsg();return false;break;case"no-append":if(o.dataType=="html"){s="<div>"+s+"</div>";s=t(s).find(o.itemSelector)}break;case"append":var l=i.children();if(o.extractLink){this.options.path=t("<div>"+s+"</div>").find(o.nextSelector).attr("href")}if(l.length==0){return this._error("end")}f=document.createDocumentFragment();while(i[0].firstChild){f.appendChild(i[0].firstChild)}this._debug("contentSelector",t(o.contentSelector)[0]);t(o.contentSelector)[0].appendChild(f);s=l.get();break}o.loading.finished.call(t(o.contentSelector)[0],o);if(o.animate){var c=t(e).scrollTop()+t("#infscr-loading").height()+o.extraScrollPx+"px";t("html,body").animate({scrollTop:c},800,function(){o.state.isDuringAjax=false})}if(!o.animate)o.state.isDuringAjax=false;u(this,s)},_nearbottom:function(){var i=this.options,s=0+t(document).height()-i.binder.scrollTop()-t(e).height();if(!!i.behavior&&this["_nearbottom_"+i.behavior]!==n){this["_nearbottom_"+i.behavior].call(this);return}this._debug("math:",s,i.pixelsFromNavToBottom);return s-i.bufferPx<i.pixelsFromNavToBottom},_pausing:function(t){var r=this.options;if(!!r.behavior&&this["_pausing_"+r.behavior]!==n){this["_pausing_"+r.behavior].call(this,t);return}if(t!=="pause"&&t!=="resume"&&t!==null){this._debug("Invalid argument. Toggling pause value instead")}t=t&&(t=="pause"||t=="resume")?t:"toggle";switch(t){case"pause":r.state.isPaused=true;break;case"resume":r.state.isPaused=false;break;case"toggle":r.state.isPaused=!r.state.isPaused;break}this._debug("Paused",r.state.isPaused);return false},_setup:function(){var t=this.options;if(!!t.behavior&&this["_setup_"+t.behavior]!==n){this["_setup_"+t.behavior].call(this);return}this._binding("bind");return false},_showdonemsg:function(){var r=this.options;if(!!r.behavior&&this["_showdonemsg_"+r.behavior]!==n){this["_showdonemsg_"+r.behavior].call(this);return}r.loading.msg.find("img").hide().parent().find("div").html(r.loading.finishedMsg).animate({opacity:1},2e3,function(){t(this).parent().fadeOut("normal")});r.errorCallback.call(t(r.contentSelector)[0],"done")},_validate:function(n){for(var r in n){if(r.indexOf&&r.indexOf("Selector")>-1&&t(n[r]).length===0){this._debug("Your "+r+" found no elements.");return false}return true}},bind:function(){this._binding("bind")},destroy:function(){this.options.state.isDestroyed=true;return this._error("destroy")},pause:function(){this._pausing("pause")},resume:function(){this._pausing("resume")},retrieve:function(r){var i=this,s=i.options,o=s.path,u,a,f,l,c,r=r||null,h=!!r?r:s.state.currPage;beginAjax=function(n){n.state.currPage++;i._debug("heading into ajax",o);u=t(n.contentSelector).is("table")?t("<tbody/>"):t("<div/>");f=n.extractLink?o:o.join(n.state.currPage);if(!f){i._error("end");return}l=n.dataType=="html"||n.dataType=="json"?n.dataType:"html+callback";if(n.appendCallback&&n.dataType=="html")l+="+callback";switch(l){case"html+callback":i._debug("Using HTML via .load() method");u.load(f+" "+n.itemSelector,null,function(t){i._loadcallback(u,t)});break;case"html":case"json":i._debug("Using "+l.toUpperCase()+" via $.ajax() method");t.ajax({url:f,dataType:n.dataType,complete:function(t,n){c=typeof t.isResolved!=="undefined"?t.isResolved():n==="success"||n==="notmodified";c?i._loadcallback(u,t.responseText):i._error("end")}});break}};if(!!s.behavior&&this["retrieve_"+s.behavior]!==n){this["retrieve_"+s.behavior].call(this,r);return}if(s.state.isDestroyed){this._debug("Instance is destroyed");return false}s.state.isDuringAjax=true;s.loading.start.call(t(s.contentSelector)[0],s)},scroll:function(){var t=this.options,r=t.state;if(!!t.behavior&&this["scroll_"+t.behavior]!==n){this["scroll_"+t.behavior].call(this);return}if(r.isDuringAjax||r.isInvalidPage||r.isDone||r.isDestroyed||r.isPaused)return;if(!this._nearbottom())return;this.retrieve()},toggle:function(){this._pausing()},unbind:function(){this._binding("unbind")},update:function(n){if(t.isPlainObject(n)){this.options=t.extend(true,this.options,n)}}};t.fn.infinitescroll=function(n,r){var i=typeof n;switch(i){case"string":var s=Array.prototype.slice.call(arguments,1);this.each(function(){var e=t.data(this,"infinitescroll");if(!e){return false}if(!t.isFunction(e[n])||n.charAt(0)==="_"){return false}e[n].apply(e,s)});break;case"object":this.each(function(){var e=t.data(this,"infinitescroll");if(e){e.update(n)}else{t.data(this,"infinitescroll",new t.infinitescroll(n,r,this))}});break}return this};var r=t.event,i;r.special.smartscroll={setup:function(){t(this).bind("scroll",r.special.smartscroll.handler)},teardown:function(){t(this).unbind("scroll",r.special.smartscroll.handler)},handler:function(e,n){var r=this,s=arguments;e.type="smartscroll";if(i){clearTimeout(i)}i=setTimeout(function(){t(r).trigger("smartscroll",s)},n==="execAsap"?0:100)}};t.fn.smartscroll=function(e){return e?this.bind("smartscroll",e):this.trigger("smartscroll",["execAsap"])}})(window,jQuery)