Skip to content

Instantly share code, notes, and snippets.

@hacfi
Created January 25, 2016 16:02
Show Gist options
  • Save hacfi/539dce39c539909cf1a2 to your computer and use it in GitHub Desktop.
Save hacfi/539dce39c539909cf1a2 to your computer and use it in GitHub Desktop.
GitHub Issues PJAX pagination
$(function () {
var loadedPages = [];
console.log('Initializing GitHub Issues PJAX...');
function group () {
if (loadedPages.length) {
return;
}
var currentPage = +$('.pagination > .current').text();
$('.issues-listing > .table-list-issues').wrap('<div class="issues-pages-wrapper"><div class="issues-page base-page" data-page="' + currentPage + '"></div></div>');
loadedPages.push(currentPage);
};
function inline (e) {
var $currentTarget = $(e.currentTarget);
var href = e.currentTarget.href;
var loadPage = +$(e.currentTarget).text();
if ($.inArray(loadPage, loadedPages) > -1) {
return false;
}
group();
e.preventDefault();
e.stopImmediatePropagation();
$.ajax({
url: href,
headers: {
'X-PJAX': true
},
success: function (data) {
var $page = $('<div class="issues-page" data-page="' + loadPage + '"></div>').append($(data).find('.table-list-issues'));
if (loadPage === 1) {
$('.issues-pages-wrapper').prepend($page);
return;
}
var $pages = $('.issues-pages-wrapper').children();
var pagesCount = $pages.length;
$pages.each(function (i, el) {
var $el = $(el),
pageNo = $el.data('page');
if (loadPage + 1 === pageNo) {
$el.before($page);
return false;
}
if (i === pagesCount - 1) {
$('.issues-pages-wrapper').append($page);
}
});
$currentTarget.replaceWith('<em class="current">' + loadPage + '</em>');
}
});
return false;
};
$('.pagination a').not('.previous_page, .next_page').on('click', inline);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment