Skip to content

Commit 8a27ef9

Browse files
committed
πŸ›  enabled checkLastPage: 'string'
πŸ›  don't checkLastPage if not document πŸ›  checkLastPage after append
1 parent c39614f commit 8a27ef9

File tree

5 files changed

+104
-12
lines changed

5 files changed

+104
-12
lines changed

β€ŽREADME.mdβ€Ž

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ var infScroll = new InfiniteScroll( '.container', {
7777

7878
checkLastPage: true,
7979
// Checks if page has path selector element
80+
// Set to string if path is not set as selector string:
81+
// checkLastPage: '.pagination__next'
8082

8183
prefill: false,
8284
// Loads and appends pages on intialization until scroll requirement is met.

β€Žjs/page-load.jsβ€Ž

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ proto.appendNextPage = function( response, path ) {
9494
this.appendItems( items, fragment );
9595
this.isLoading = false;
9696
this.dispatchEvent( 'append', null, [ response, path, items ] );
97+
this.checkLastPage( response, path );
9798
}.bind( this );
9899

99100
// TODO add hook for option to trigger appendReady
@@ -102,8 +103,6 @@ proto.appendNextPage = function( response, path ) {
102103
} else {
103104
appendReady();
104105
}
105-
106-
this.checkLastPage( response, path );
107106
};
108107

109108
proto.appendItems = function( items, fragment ) {
@@ -166,26 +165,35 @@ proto.onAppendOutlayer = function( response, path, items ) {
166165
this.options.outlayer.appended( items );
167166
};
168167

169-
// ----- ----- //
168+
// ----- checkLastPage ----- //
170169

171-
// check response for next element, set with path selector
170+
// check response for next element
172171
proto.checkLastPage = function( response, path ) {
173-
// only works if path is selector
174-
var cannotCheck = !this.options.checkLastPage ||
175-
!this.isPathSelector;
176-
if ( cannotCheck ) {
172+
var checkLastPage = this.options.checkLastPage;
173+
// get selector from checkLastPage or path option
174+
var selector;
175+
if ( typeof checkLastPage == 'string' ) {
176+
selector = checkLastPage;
177+
} else if ( this.isPathSelector ) {
178+
selector = this.options.path;
179+
}
180+
// bail if checkLastPage disabled or no selector or not document response
181+
if ( !checkLastPage || !selector || !response.querySelector ) {
177182
return;
178183
}
179-
var pathElem = response.querySelector( this.options.path );
180-
if ( pathElem ) {
181-
// page has next element, keep going
184+
// check if response has selector
185+
var nextElem = response.querySelector( selector );
186+
if ( nextElem ) {
187+
// page has selector element, keep going
182188
return;
183189
}
184190
// no next selector, last page hit
185191
this.canLoad = false;
186192
this.dispatchEvent( 'last', null, [ response, path ] );
187193
};
188194

195+
// ----- error ----- //
196+
189197
proto.onPageError = function( error, path ) {
190198
this.isLoading = false;
191199
this.canLoad = false;

β€Žtest/index.htmlβ€Ž

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
<script src="unit/path.js"></script>
3232
<script src="unit/page-load.js"></script>
3333
<script src="unit/page-load-error.js"></script>
34+
<script src="unit/check-last-page.js"></script>
3435
<script src="unit/prefill-window.js"></script>
3536
<script src="unit/prefill-element.js"></script>
3637
<script src="unit/scroll-watch-window.js"></script>
@@ -65,6 +66,12 @@ <h2>page load error</h2>
6566
<div class="post">page 1, post 1</div>
6667
</div>
6768

69+
<h2>check last page</h2>
70+
<div class="demo demo--check-last-page">
71+
<div class="post">page 1, post 1</div>
72+
</div>
73+
<p><a class="check-last-page-next-link" href="page/2.html">Next</a></p>
74+
6875
<h2>prefill, window</h2>
6976
<div class="demo demo--prefill-window demo--big-posts">
7077
</div>

β€Žtest/page/2.htmlβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ <h1>test page 2</h1>
1515

1616
<div class="post">page 2, post 2</div>
1717

18-
<a class="page-load-next-link" href="3.html">Page 3</a>
18+
<a class="page-load-next-link check-last-page-next-link" href="3.html">Page 3</a>
1919

2020
</body>
2121
</html>

β€Žtest/unit/check-last-page.jsβ€Ž

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
QUnit.test( 'checkLastPage', function( assert ) {
2+
3+
var done = assert.async();
4+
5+
// ----- checkLastPage: true ----- //
6+
7+
var infScroll = new InfiniteScroll( '.demo--check-last-page', {
8+
path: '.check-last-page-next-link',
9+
append: '.post',
10+
scrollThreshold: false,
11+
history: false,
12+
});
13+
14+
infScroll.on( 'last', onTrueLast1 );
15+
infScroll.once( 'append', onTrueAppend1 );
16+
17+
function onTrueLast1() {
18+
assert.ok( false, 'last should not trigger on 2nd page' );
19+
}
20+
21+
function onTrueAppend1() {
22+
infScroll.off( 'last', onTrueLast1 );
23+
loadTruePage3();
24+
}
25+
26+
infScroll.loadNextPage();
27+
28+
function loadTruePage3() {
29+
infScroll.once( 'last', function() {
30+
assert.ok( true, 'checkLastPage: true, last triggered on 3rd page' );
31+
checkString();
32+
});
33+
34+
infScroll.loadNextPage();
35+
}
36+
37+
// ----- checkLastPage: 'string' ----- //
38+
39+
function checkString() {
40+
// reset
41+
infScroll.destroy();
42+
infScroll = new InfiniteScroll( '.demo--check-last-page', {
43+
path: 'page/{{#}}.html',
44+
checkLastPage: '.next-page-link',
45+
append: '.post',
46+
scrollThreshold: false,
47+
history: false,
48+
});
49+
50+
infScroll.on( 'last', onStringLast1 );
51+
infScroll.once( 'append', onStringAppend1 );
52+
infScroll.loadNextPage();
53+
}
54+
55+
function onStringLast1() {
56+
assert.ok( false, 'last should not trigger on 2nd page' );
57+
}
58+
59+
function onStringAppend1() {
60+
infScroll.off( 'last', onStringLast1 );
61+
loadStringPage3();
62+
}
63+
64+
infScroll.loadNextPage();
65+
66+
function loadStringPage3() {
67+
infScroll.once( 'last', function() {
68+
assert.ok( true, 'checkLastPage: \'string\', last triggered on 3rd page' );
69+
done();
70+
});
71+
72+
infScroll.loadNextPage();
73+
}
74+
75+
});

0 commit comments

Comments
Β (0)