Skip to content

Instantly share code, notes, and snippets.

@powerwlsl
Created October 3, 2017 08:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save powerwlsl/eccb179854231c8471c32e39ff094ca8 to your computer and use it in GitHub Desktop.
Save powerwlsl/eccb179854231c8471c32e39ff094ca8 to your computer and use it in GitHub Desktop.
function init_pagination() {
var hash, loading_articles, page_regexp, pushPage, title_of_article;
page_regexp = /\d+$/;
pushPage = function(page) {
if (page == "1") {
title_of_article = $("input#article_friendly_id")[0].value;
}
history.pushState(null, "InfiniteScrolling | Page " + page, title_of_article);
};
window.preparePagination = function(el) {
el.waypoint(function(direction) {
var $this, page, page_el;
$this = $(this);
title_of_article = $this.next("input").val();
if (!($this.hasClass('first-page') && direction === 'up')) {
page = parseInt($this.data('page'), 10);
if (direction === 'up') {
page -= 1;
}
page_el = $($('#static-pagination li').get(page));
if (!page_el.hasClass('active')) {
$('#static-pagination .active').removeClass('active');
pushPage(page);
return page_el.addClass('active');
}
}
});
};
hash = window.location.hash;
if (hash.match(/page=\d+/i)) {
window.location.hash = '';
window.location.search = '?page=' + hash.match(/page=(\d+)/i)[1];
}
if ($('#infinite-scrolling').size() > 0) {
preparePagination($('.page-delimiter'));
$(window).bindWithDelay('scroll', function() {
var more_articles_url;
more_articles_url = $('#infinite-scrolling .next a').attr('href');
if (more_articles_url && $(window).scrollTop() > $(document).height() - $(window).height() - 60) {
$('#infinite-scrolling .pagination').html('<img src="/assets/ajax-loader.gif" alt="Loading..." title="Loading..." />');
$.getScript(more_articles_url, function() {
window.location.hash = more_articles_url.match(page_regexp)[0]
return pushPage(more_articles_url.match(page_regexp)[0]);
});
}
}, 100);
}
if ($('#with-button').size() > 0) {
$('.pagination').hide();
loading_articles = false;
$('#load_more_articles').show().click(function() {
var $this, more_articles_url;
if (!loading_articles) {
loading_articles = true;
more_articles_url = $('.pagination .next a').attr('href');
$this = $(this);
$this.html('<img src="/assets/ajax-loader.gif" alt="Loading..." title="Loading..." />').addClass('disabled');
$.getScript(more_articles_url, function() {
if ($this) {
$this.text(I18n.t("titles.more")).removeClass('disabled');
}
return loading_articles = false;
});
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment