JS

add infinite scroll to FacetWP pager

(function($) {
    window.fwp_is_paging = false;

    $(document).on('facetwp-refresh', function() {
        if (! window.fwp_is_paging) {
            window.fwp_page = 1;
            FWP.extras.per_page = 'default';
        }
    
        window.fwp_is_paging = false;
    });
    
    $(document).on('facetwp-loaded', function() {
        window.fwp_total_rows = FWP.settings.pager.total_rows;
        
        if (! FWP.loaded) {
            window.fwp_default_per_page = FWP.settings.pager.per_page;
    
            $(window).scroll(function() {
                var careersListing = document.querySelector('.careers-listing');
                if (careersListing) {
                    var careersListingBottom = careersListing.getBoundingClientRect().bottom;
                    var windowHeight = window.innerHeight;
                    if (careersListingBottom <= windowHeight) {
                        var rows_loaded = (window.fwp_page * window.fwp_default_per_page);
                        if (rows_loaded < window.fwp_total_rows) {
                            console.log(rows_loaded + ' of ' + window.fwp_total_rows + ' rows');
                            window.fwp_page++;
                            window.fwp_is_paging = true;
                            FWP.extras.per_page = (window.fwp_page * window.fwp_default_per_page);
                            FWP.soft_refresh = true;
                            FWP.refresh();
                        }
                    }
                }
            });
        }
    });
 
})(jQuery);