1 /* eslint-env browser */
5 * Used on all pages where there is a list of posts (homepage, tag index, etc).
7 * When the page is scrolled to 300px from the bottom, the next page of posts
8 * is fetched by following the the <link rel="next" href="..."> that is output
11 * The individual post items are extracted from the fetched pages by looking for
12 * a wrapper element with the class "post-card". Any found elements are appended
13 * to the element with the class "post-feed" in the currently viewed page.
16 (function (window
, document
) {
18 var nextElement
= document
.querySelector('link[rel=next]');
24 var feedElement
= document
.querySelector('.post-feed');
34 var lastScrollY
= window
.scrollY
;
35 var lastWindowHeight
= window
.innerHeight
;
36 var lastDocumentHeight
= document
.documentElement
.scrollHeight
;
38 function onPageLoad() {
39 if (this.status
=== 404) {
40 window
.removeEventListener('scroll', onScroll
);
41 window
.removeEventListener('resize', onResize
);
46 var postElements
= this.response
.querySelectorAll('article.post-card');
47 postElements
.forEach(function (item
) {
48 // document.importNode is important, without it the item's owner
49 // document will be different which can break resizing of
50 // `object-fit: cover` images in Safari
51 feedElement
.appendChild(document
.importNode(item
, true));
55 var resNextElement
= this.response
.querySelector('link[rel=next]');
57 nextElement
.href
= resNextElement
.href
;
59 window
.removeEventListener('scroll', onScroll
);
60 window
.removeEventListener('resize', onResize
);
64 lastDocumentHeight
= document
.documentElement
.scrollHeight
;
70 // return if already loading
75 // return if not scroll to the bottom
76 if (lastScrollY
+ lastWindowHeight
<= lastDocumentHeight
- buffer
) {
83 var xhr
= new window
.XMLHttpRequest();
84 xhr
.responseType
= 'document';
86 xhr
.addEventListener('load', onPageLoad
);
88 xhr
.open('GET', nextElement
.href
);
92 function requestTick() {
93 ticking
|| window
.requestAnimationFrame(onUpdate
);
98 lastScrollY
= window
.scrollY
;
102 function onResize() {
103 lastWindowHeight
= window
.innerHeight
;
104 lastDocumentHeight
= document
.documentElement
.scrollHeight
;
108 window
.addEventListener('scroll', onScroll
, {passive
: true});
109 window
.addEventListener('resize', onResize
);
112 })(window
, document
);