Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add page URLs #25

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions resources/navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ function goBack() {

wrapper.removeChild(lastSlide);
wrapper.insertBefore(lastSlide, wrapper.firstChild);

updateURL();
}

/**
Expand All @@ -24,10 +26,72 @@ function goForward() {

wrapper.removeChild(firstSlide);
wrapper.appendChild(firstSlide);

updateURL();
}

/**
* Updates the current URL to include a hashtag of the current page number.
*/
function updateURL() {
window.history.replaceState( {} , null, '#' + currentPage() );
}

/**
* Returns the current page number of the presentation.
*/
function currentPage() {
return document.querySelector('#wrapper .slide').dataset.page;
}

/**
* Returns a NodeList of each .slide element.
*/
function allSlides() {
return document.querySelectorAll('#wrapper .slide');
}

/**
* Give each slide a "page" data attribute.
*/
function setPageNumbers() {
var wrapper = document.querySelector('#wrapper');
var pages = wrapper.querySelectorAll('section');

for (var i = 0; i < pages.length; ++i) {
var page = pages[i];
page.dataset.page = i;
}
}

/**
* Go to the specified page of content.
*/
function goToPage(page) {
// Try to find the target slide.
var targetSlide = document.querySelector('#wrapper .slide[data-page="' + page + '"]');

// If it actually exists, go forward until we find it.
if (targetSlide) {
var numSlides = allSlides().length;

for (var i = 0; currentPage() != page && i < numSlides; i++) {
goForward();
}
}
}

window.onload = function () {

// Give each slide a "page" data attribute.
setPageNumbers();

// If the location hash specifies a page number, go to it.
var page = window.location.hash.slice(1);
if (page) {
goToPage(page);
}

document.onkeydown = function (e) {
var kc = e.keyCode;

Expand Down