123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
-
- ;(function($){
-
- $.fn.infinitescroll = function(options,callback){
-
-
- function debug(){
- if (opts.debug) { window.console && console.log.call(console,arguments)}
- }
-
-
- function areSelectorsValid(opts){
- for (var key in opts){
- if (key.indexOf && (key.indexOf('Selector') != -1) && $(opts[key]).length === 0){
- debug('Your ' + key + ' found no elements.');
- return false;
- }
- return true;
- }
- }
-
- function determinePath(path){
-
- path.match(relurl) ? path.match(relurl)[2] : path;
-
- if ( path.match(/^(.*?)\b2\b(.*?$)/) ){
- path = path.match(/^(.*?)\b2\b(.*?$)/).slice(1);
- } else
-
- if (path.match(/^(.*?)2(.*?$)/)){
- debug('Trying backup next selector parse technique. Treacherous waters here, matey.');
- path = path.match(/^(.*?)2(.*?$)/).slice(1);
- } else {
- 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.');
- props.isInvalidPage = true;
- }
-
- return path;
- }
-
- function getDocumentHeight(){
-
- return opts.localMode ? ($(props.container)[0].scrollHeight && $(props.container)[0].scrollHeight)
-
- : $(document).height()
- }
-
-
-
- function isNearBottom(opts,props){
-
-
-
- var pixelsFromWindowBottomToBottom = getDocumentHeight() -
- (opts.localMode ? $(props.container).scrollTop() :
-
- ($(props.container).scrollTop() || $(props.container.ownerDocument.body).scrollTop())) -
- $(opts.localMode ? props.container : window).height();
-
- debug('math:',pixelsFromWindowBottomToBottom, props.pixelsFromNavToBottom);
-
-
- return (pixelsFromWindowBottomToBottom - opts.bufferPx < props.pixelsFromNavToBottom);
- }
-
- function showDoneMsg(){
- props.loadingMsg
- .find('img').hide()
- .parent()
- .find('div').html(opts.donetext).animate({opacity: 1},2000).fadeOut('normal');
-
-
- opts.errorCallback();
- }
-
- function infscrSetup(path,opts,props,callback){
-
- if (props.isDuringAjax || props.isInvalidPage || props.isDone) return;
-
- if ( opts.infiniteScroll && !isNearBottom(opts,props) ) return;
-
-
- props.isDuringAjax = true;
-
-
- props.loadingMsg.appendTo( opts.contentSelector ).show();
- if(opts.infiniteScroll) $( opts.navSelector ).hide();
-
-
- props.currPage++;
-
- debug('heading into ajax',path);
-
-
- var box = $(opts.contentSelector).is('table') ? $('<tbody/>') : $('<div/>');
-
- box
- .attr('id','infscr-page-'+props.currPage)
- .addClass('infscr-pages')
- .appendTo( opts.contentSelector )
- .load( path.join( props.currPage ) + ' ' + opts.itemSelector,null,function(){
-
-
- if (props.isDone){
- showDoneMsg();
- return false;
-
- } else {
-
-
- if (box.children().length == 0){
-
- $.event.trigger( "ajaxError", [{status:404}] );
- }
-
-
- props.loadingMsg.fadeOut('normal' );
-
-
- if (opts.animate){
- var scrollTo = $(window).scrollTop() + $('#infscr-loading').height() + opts.extraScrollPx + 'px';
- $('html,body').animate({scrollTop: scrollTo}, 800,function(){ props.isDuringAjax = false; });
- }
-
-
- callback.call( box[0] );
-
- if (!opts.animate) props.isDuringAjax = false;
- }
- });
-
-
- }
-
-
-
-
-
-
- var opts = $.extend({}, $.infinitescroll.defaults, options);
- var props = $.infinitescroll;
- callback = callback || function(){};
-
- if (!areSelectorsValid(opts)){ return false; }
-
-
- props.container = opts.localMode ? this : document.documentElement;
-
-
- opts.contentSelector = opts.contentSelector || this;
-
-
-
- var relurl = /(.*?\/\/).*?(\/.*)/;
- var path = $(opts.nextSelector).attr('href');
-
-
- if (!path) { debug('Navigation selector not found'); return; }
-
-
- path = determinePath(path);
-
-
- if (opts.localMode) $(props.container)[0].scrollTop = 0;
-
-
- props.pixelsFromNavToBottom = getDocumentHeight() +
- $(props.container).offset().top -
- $(opts.navSelector).offset().top;
-
-
- props.loadingMsg = $('<div id="infscr-loading" style="text-align: center;"><img alt="Loading..." src="'+
- opts.loadingImg+'" /><div>'+opts.loadingText+'</div></div>');
-
- (new Image()).src = opts.loadingImg;
-
-
-
- $(document).ajaxError(function(e,xhr,opt){
- debug('Page not found. Self-destructing...');
-
-
- if (xhr.status == 404){
- showDoneMsg();
- props.isDone = true;
- $(opts.localMode ? this : window).unbind('scroll.infscr');
- }
- });
-
- if(opts.infiniteScroll){
-
- $(opts.localMode ? this : window)
- .bind('scroll.infscr', function(){ infscrSetup(path,opts,props,callback); } )
- .trigger('scroll.infscr');
- }else{
- $(opts.nextSelector).click(
- function(){
- infscrSetup(path,opts,props,callback);
- return false;
- }
- );
- }
-
-
- return this;
-
- }
-
-
-
-
- $.infinitescroll = {
- defaults : {
- debug : false,
- infiniteScroll : true,
- preload : false,
- nextSelector : "div.navigation a:first",
- loadingImg : "http://www.infinite-scroll.com/loading.gif",
- loadingText : "<em>Loading the next set of posts...</em>",
- donetext : "<em>Congratulations, you've reached the end of the internet.</em>",
- navSelector : "div.navigation",
- contentSelector : null,
- extraScrollPx : 150,
- itemSelector : "div.post",
- animate : false,
- localMode : false,
- bufferPx : 40,
- errorCallback : function(){}
- },
- loadingImg : undefined,
- loadingMsg : undefined,
- container : undefined,
- currPage : 1,
- currDOMChunk : null,
- isDuringAjax : false,
- isInvalidPage : false,
- isDone : false
- };
-
- })(jQuery);
|