Skip to content

Commit a15ed37

Browse files
committed
rewrite infinitscroll plugin
1 parent 2341780 commit a15ed37

File tree

5 files changed

+86
-44
lines changed

5 files changed

+86
-44
lines changed

lib/modules/apostrophe-assets/index.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ module.exports = {
3434
{ name: 'vendor/jquery-3.6.0.min', when: 'anon' },
3535
{ name: 'vendor/swiper-bundle.min' },
3636
{ name: 'vendor/featherlight.min' },
37-
{ name: 'vendor/infiniscroll.min' },
3837
{ name: 'vendor/svg-inject.min' },
3938
{ name: 'vendor/materialize/vendor/cash' },
4039
{ name: 'vendor/materialize/components/component' },
@@ -46,7 +45,6 @@ module.exports = {
4645
{ name: 'vendor/materialize/components/component' },
4746
{ name: 'vendor/materialize/components/dropdown' },
4847
{ name: 'vendor/materialize/components/forms' },
49-
{ name: 'vendor/materialize/components/expand' },
5048
{ name: 'vendor/materialize/components/modal' },
5149
{ name: 'vendor/materialize/components/parallax' },
5250
{ name: 'vendor/materialize/components/pushpin' },
@@ -55,7 +53,6 @@ module.exports = {
5553
{ name: 'vendor/materialize/components/slider' },
5654
{ name: 'vendor/materialize/components/tabs' },
5755
{ name: 'vendor/materialize/components/waves' },
58-
{ name: 'vendor/materialize/admin-ui', when: 'user' },
59-
{ name: 'scripts' }
56+
{ name: 'vendor/materialize/admin-ui', when: 'user' }
6057
]
6158
};

lib/modules/apostrophe-assets/public/js/scripts.js

-31
This file was deleted.

lib/modules/apostrophe-assets/public/js/vendor/infiniscroll.min.js

-8
This file was deleted.

lib/modules/galleries-pages/index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,9 @@ module.exports = {
2424
'_ordering'
2525
]
2626
}
27-
]
27+
],
28+
// Add page script
29+
construct: function(self, options) {
30+
self.pushAsset('script', 'infinity', { when: 'lean' });
31+
}
2832
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
function infiniscroll(el, options) {
2+
this.el = el;
3+
this.options = Object.assign({
4+
navSelector: '.pagination',
5+
nextSelector: '.pagination a.next',
6+
loadingSelector: '.loading',
7+
pageFragment: '.ajax-append',
8+
scrollBuffer: 200,
9+
scrollOnLoad: true,
10+
scrollOnLoadDistance: 200,
11+
scrollOnLoadSpeed: 500,
12+
onInit: function () {},
13+
beforeContentLoaded: function (link) {},
14+
afterContentLoaded: function (html) {},
15+
}, options);
16+
this._isLoading = false;
17+
this._nextLink = null;
18+
this._endReached = false;
19+
this.init();
20+
}
21+
22+
infiniscroll.prototype = {
23+
init: function () {
24+
var self = this;
25+
document.querySelector(this.options.navSelector).style.display = 'none';
26+
window.addEventListener('scroll', function () {
27+
self.doScroll.apply(self);
28+
});
29+
this.options.onInit.call(this);
30+
},
31+
doScroll: function () {
32+
if (this._isLoading) return;
33+
if (window.scrollY >= document.body.scrollHeight - window.innerHeight - this.options.scrollBuffer) {
34+
this._isLoading = true;
35+
this._nextLink || (this._nextLink = document.querySelector(this.options.nextSelector));
36+
if (!this._endReached) {
37+
this.options.beforeContentLoaded.call(this, this._nextLink);
38+
var loadingEl = document.querySelector(this.options.loadingSelector);
39+
if (loadingEl) loadingEl.style.display = 'block';
40+
var self = this;
41+
var xhr = new XMLHttpRequest();
42+
xhr.open('GET', this._nextLink.getAttribute('href'));
43+
xhr.onload = function () {
44+
var frag = document.createRange().createContextualFragment(xhr.response),
45+
content = frag.querySelector(self.options.pageFragment),
46+
s = false;
47+
self.options.scrollOnLoad && window.scrollY === document.body.scrollHeight - window.innerHeight && (s = true);
48+
content.querySelector(self.options.navSelector).style.display = 'none';
49+
self._nextLink = content.querySelector(self.options.nextSelector);
50+
if (loadingEl) loadingEl.style.display = 'none';
51+
self.el.appendChild(content);
52+
s && window.scrollTo(0, window.scrollY + self.options.scrollOnLoadDistance);
53+
self._isLoading = false;
54+
self.options.afterContentLoaded.call(self, content);
55+
if (!self._nextLink) self._endReached = true; // Stop infinite loop if no more pages to load
56+
};
57+
xhr.send();
58+
} else {
59+
this._endReached = true; // Stop infinite loop if no more pages to load
60+
}
61+
}
62+
}
63+
};
64+
65+
var el = document.querySelector('.ajax-append');
66+
if (el) {
67+
new infiniscroll(el, {
68+
navSelector: '.pagination',
69+
nextSelector: '.pagination a.next',
70+
loadingSelector: '.loading',
71+
pageFragment: '.ajax-append',
72+
scrollBuffer: 200,
73+
scrollOnLoad: true,
74+
scrollOnLoadDistance: 200,
75+
scrollOnLoadSpeed: 500,
76+
onInit: function () {},
77+
beforeContentLoaded: function (link) {},
78+
afterContentLoaded: function (html) {}
79+
});
80+
}

0 commit comments

Comments
 (0)