Skip to content

Commit

Permalink
πŸ”” checkLastPage for path: function
Browse files Browse the repository at this point in the history
  • Loading branch information
desandro committed Jun 22, 2017
1 parent 0fea4e6 commit 07ef13b
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 8 deletions.
30 changes: 23 additions & 7 deletions js/page-load.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,24 +167,40 @@ proto.onAppendOutlayer = function( response, path, items ) {
// check response for next element
proto.checkLastPage = function( response, path ) {
var checkLastPage = this.options.checkLastPage;
if ( !checkLastPage ) {
return;
}

var pathOpt = this.options.path;
// if path is function, check if next path is truthy
if ( typeof pathOpt == 'function' ) {
var nextPath = this.getPath();
if ( !nextPath ) {
this.lastPageReached( response, path );
return;
}
}
// get selector from checkLastPage or path option
var selector;
if ( typeof checkLastPage == 'string' ) {
selector = checkLastPage;
} else if ( this.isPathSelector ) {
selector = this.options.path;
// path option is selector string
selector = pathOpt;
}
// bail if checkLastPage disabled or no selector or not document response
if ( !checkLastPage || !selector || !response.querySelector ) {
// check last page for selector
// bail if no selector or not document response
if ( !selector || !response.querySelector ) {
return;
}
// check if response has selector
var nextElem = response.querySelector( selector );
if ( nextElem ) {
// page has selector element, keep going
return;
if ( !nextElem ) {
this.lastPageReached( response, path );
}
// no next selector, last page hit
};

proto.lastPageReached = function( response, path ) {
this.canLoad = false;
this.dispatchEvent( 'last', null, [ response, path ] );
};
Expand Down
43 changes: 42 additions & 1 deletion test/unit/check-last-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ QUnit.test( 'checkLastPage', function( assert ) {
scrollThreshold: false,
history: false,
});

infScroll.on( 'last', onStringLast1 );
infScroll.once( 'append', onStringAppend1 );
infScroll.loadNextPage();
Expand All @@ -66,6 +66,47 @@ QUnit.test( 'checkLastPage', function( assert ) {
function loadStringPage3() {
infScroll.once( 'last', function() {
assert.ok( true, 'checkLastPage: \'string\', last triggered on 3rd page' );
setTimeout( checkPathFunction );
});

infScroll.loadNextPage();
}

// ----- path: function ----- //

function checkPathFunction() {
infScroll.destroy();
infScroll = new InfiniteScroll( '.demo--check-last-page', {
// provide only page/2.html, then falsy
path: function() {
if ( this.loadCount === 0 ) {
var nextIndex = this.loadCount + 2;
return 'page/' + nextIndex + '.html';
}
},
checkLastPage: true,
append: '.post',
scrollThreshold: false,
history: false,
});

infScroll.on( 'last', onFunctionLast2 );
infScroll.once( 'append', onFunctionAppend2 );

infScroll.loadNextPage();
}

function onFunctionLast2() {
assert.ok( false, 'last should not trigger on function page 2' );
}

function onFunctionAppend2() {
infScroll.off( 'last', onFunctionLast2 );

infScroll.on( 'last', function( response, path ) {
assert.ok( true, 'path: function, last triggered' );
assert.ok( response, 'path: function, response there on last' );
assert.ok( path, 'path: function, path there on last' );
done();
});

Expand Down

0 comments on commit 07ef13b

Please sign in to comment.