Sha256: 016bdd66f147200e436d129b91d61c56424916cc286ae9c532f27207b2e477eb

Contents?: true

Size: 1.69 KB

Versions: 8

Compression:

Stored size: 1.69 KB

Contents

class InfiniteScroll {
    constructor (path, wrapperId, pTotal) {
        if (path === undefined || wrapperId === undefined || pTotal === undefined) throw Error ('no parameter.');
        this.path = path;
        this.pNum = 2;
        this.pTotal = pTotal;
        this.wNode = document.getElementById(wrapperId);
        this.wrapperId = wrapperId;
        this.enable = true;

        this.detectScroll();
    }

    detectScroll() {
        window.onscroll = (ev) => {
            if ((window.innerHeight + window.pageYOffset) >= document.body.offsetHeight){
                if(this.pNum <= this.pTotal )
                    this.getNewPost();
            }
        };
    }
    getNewPost() {
        if (this.enable === false) return false;
        this.enable = false;
        const xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = () => {
            if (xmlhttp.readyState == XMLHttpRequest.DONE) {
                if (xmlhttp.status == 200) {
                    this.pNum++;
                    const childItems = this.getChildItemsByAjaxHTML(xmlhttp.responseText);
                    this.appendNewItems(childItems);
                }
                return this.enable = true;
            }
        }
        xmlhttp.open("GET", `${location.origin + this.path + this.pNum}/index.html`, true);
        xmlhttp.send();
    }

    getChildItemsByAjaxHTML(HTMLText) {
        const newHTML = document.createElement('html');
        newHTML.innerHTML = HTMLText;
        const childItems = newHTML.querySelectorAll(`#${this.wrapperId} > *`);
        return childItems;
    }

    appendNewItems(items) {
        items.forEach(item => {
            this.wNode.appendChild(item);
        });
    }
}

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
domain-jekyll-1.5.0 assets/vendor/infinitescroll/InfiniteScroll.js
domain-jekyll-1.4.0 assets/vendor/infinitescroll/InfiniteScroll.js
domain-jekyll-1.3.0 assets/vendor/infinitescroll/InfiniteScroll.js
domain-jekyll-1.2.1 assets/vendor/infinitescroll/InfiniteScroll.js
domain-jekyll-1.2.0 assets/vendor/infinitescroll/InfiniteScroll.js
domain-jekyll-1.1.0 assets/vendor/infinitescroll/InfiniteScroll.js
domain-jekyll-1.0.4 assets/vendor/infinitescroll/InfiniteScroll.js
domain-jekyll-1.0.3 assets/vendor/infinitescroll/InfiniteScroll.js