Skip to content

Instantly share code, notes, and snippets.

@alison-mk
Created December 7, 2016 23:50
Show Gist options
  • Save alison-mk/10b24361e5cb2a2167ef635ebf850a21 to your computer and use it in GitHub Desktop.
Save alison-mk/10b24361e5cb2a2167ef635ebf850a21 to your computer and use it in GitHub Desktop.
Custom script for Coffee Fest. Activates prev/next pagination for each schedule day when more than 6 events exist per day. Visible in action here: https://www.coffeefest.com/venues/details/coffee-fest-nashville-2017#schedule
// Activate pagination when more than 6 events display per day
function pagination() {
var schedulerEvent = $('#events .event');
var prevButton = $('#events .prev');
var nextButton = $('#events .next');
var currentPage = "page_1"; // begin as statically defined, will always start on page_1
// If user is on page 1, add inactive class to Prev link
function disablePrev() {
if (currentPage === "page_1") {
prevButton.addClass("inactive");
}
}
// Calculate number of event pages, disable nextButton on last page
function disableNext() {
var numberOfPages = Math.ceil(schedulerEvent.length/6);
var disableNext = "page_" + numberOfPages;
disableNext.toString();
if (currentPage === disableNext) {
nextButton.addClass("inactive");
}
}
// Run both button activity checks on page load, also run on button clicks below
disablePrev();
disableNext();
// Trigger previous page of events when Prev link is clicked
prevButton.on('click', function(e) {
e.preventDefault();
disablePrev();
if (prevButton.hasClass('inactive')) {
return false; // do nothing if prev link has inactive class
} else {
// If user successfully clicks prev, always enable next link
nextButton.removeClass("inactive");
// If user is on page 2 and clicks prev, always add inactive class
if (currentPage === "page_2") {
prevButton.addClass("inactive");
}
// Turn page value into an object and hide matching elements
var currentPageObject = "." + currentPage;
$("" + currentPageObject + "").hide();
// Do some string manipulation to extract number value from string
var pageNumberString = currentPage.split('_')[1]; // get just the number
var pageNumber = parseInt(pageNumberString); // change string to a number
// Decrement page number by 1 and then turn back into a string for use as class name
pageNumber--;
pageNumber.toString();
// Make events with new matching page number appear
var prevPage = ".page_" + pageNumber;
$("" + prevPage + "").show();
// Override current page setting to reflect new page number, return to beginning of function
currentPage = "page_" + pageNumber;
return currentPage;
}
});
// Trigger next page of events when Next link is clicked
nextButton.on('click', function(e) {
e.preventDefault();
disableNext();
// When user successfully clicks next, always enable prev link
prevButton.removeClass("inactive");
if (nextButton.hasClass('inactive')) {
return false; // do nothing if next link has inactive class
} else {
// Hide events with current matching number
var currentPageObject = "." + currentPage;
$("" + currentPageObject + "").hide();
// Do some string manipulation to pull page number from classname
var pageNumberString = currentPage.split('_')[1]; // get just the number
var pageNumber = parseInt(pageNumberString); // change string to a number
// Increment page number by 1 and then turn back into a string for use as class name
pageNumber++;
pageNumber.toString();
// Make events with new matching number appear
var nextPage = ".page_" + pageNumber;
$("" + nextPage + "").show();
// Override current page setting to reflect new page number, return to beginning of function
currentPage = "page_" + pageNumber;
return currentPage;
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment