From 1bac41de46547adce7a047f680a92ad75823e535 Mon Sep 17 00:00:00 2001 From: Barney Boisvert Date: Sat, 14 May 2016 08:37:18 -0700 Subject: [PATCH 1/8] don't loop backwards from the first slide to the last --- resources/script.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/resources/script.js b/resources/script.js index 49384dc..89b3632 100644 --- a/resources/script.js +++ b/resources/script.js @@ -13,6 +13,9 @@ function currentPosition() { function navigate(n) { var position = currentPosition(); var numSlides = document.getElementsByClassName('slide').length; + if (n < 0 && position <= 1) { + return; + } /* Positions are 1-indexed, so we need to add and subtract 1 */ var nextPosition = (position - 1 + n) % numSlides + 1; From 7c371c8cb37a2a54db5f8507a9ce4e6bf2fdd769 Mon Sep 17 00:00:00 2001 From: Barney Boisvert Date: Sat, 14 May 2016 08:37:34 -0700 Subject: [PATCH 2/8] don't loop forwards from the last slide to the first --- resources/script.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/resources/script.js b/resources/script.js index 89b3632..30fbbb4 100644 --- a/resources/script.js +++ b/resources/script.js @@ -16,6 +16,9 @@ function navigate(n) { if (n < 0 && position <= 1) { return; } + if (n > 0 && position >= numSlides) { + return; + } /* Positions are 1-indexed, so we need to add and subtract 1 */ var nextPosition = (position - 1 + n) % numSlides + 1; From 312af87be8427a4024698df7069626502ed5c4be Mon Sep 17 00:00:00 2001 From: Barney Boisvert Date: Sat, 14 May 2016 08:46:55 -0700 Subject: [PATCH 3/8] use Home and End keys to jump to the first and last slides, respectively --- README.md | 2 ++ resources/script.js | 44 +++++++++++++++++++++++++++++++++----------- 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 7110efd..810ec9e 100644 --- a/README.md +++ b/README.md @@ -272,6 +272,8 @@ To navigate the slideshow: * **forward**: K, L, UP, RIGHT, PgDn, and Space * **reverse**: H, J, LEFT, DOWN, PgUp, and Backspace +* **first slide**: Home +* **last slide**: End The toggle fullscreen mode, press the **ENTER** key. diff --git a/resources/script.js b/resources/script.js index 30fbbb4..1f94aec 100644 --- a/resources/script.js +++ b/resources/script.js @@ -5,6 +5,17 @@ function currentPosition() { return parseInt(document.querySelector('.slide:not(.hidden)').id.slice(6)); } +/** + * If passed to navigate, will move to the first slide, independent of the + * current location, rather than a relative amount. + */ +var FIRST_SLIDE = -9999999; + +/** + * If passed to navigate, will move to the last slide, independent of the + * current location, rather than a relative amount. + */ +var LAST_SLIDE = 9999999; /** * Navigates forward n pages @@ -13,19 +24,26 @@ function currentPosition() { function navigate(n) { var position = currentPosition(); var numSlides = document.getElementsByClassName('slide').length; - if (n < 0 && position <= 1) { - return; - } - if (n > 0 && position >= numSlides) { - return; + var nextPosition; + if (n == FIRST_SLIDE) { + nextPosition = 1; + } else if (n == LAST_SLIDE) { + nextPosition = numSlides; + } else { + if (n < 0 && position <= 1) { + return; + } + if (n > 0 && position >= numSlides) { + return; + } + + /* Positions are 1-indexed, so we need to add and subtract 1 */ + nextPosition = (position - 1 + n) % numSlides + 1; + + /* Normalize nextPosition in-case of a negative modulo result */ + nextPosition = (nextPosition - 1 + numSlides) % numSlides + 1; } - /* Positions are 1-indexed, so we need to add and subtract 1 */ - var nextPosition = (position - 1 + n) % numSlides + 1; - - /* Normalize nextPosition in-case of a negative modulo result */ - nextPosition = (nextPosition - 1 + numSlides) % numSlides + 1; - document.getElementById('slide-' + position).classList.add('hidden'); document.getElementById('slide-' + nextPosition).classList.remove('hidden'); @@ -136,6 +154,10 @@ document.addEventListener('DOMContentLoaded', function () { navigate(-1); } else if (kc === 38 || kc === 39 || kc === 32 || kc === 75 || kc === 76 || kc === 34) { navigate(1); + } else if (kc == 36) { + navigate(FIRST_SLIDE); + } else if (kc == 35) { + navigate(LAST_SLIDE); } else if (kc === 13) { toggleFullScreen(); } From 5eb8308a0eb1d4cd834883bad6fffba6a4a5c58b Mon Sep 17 00:00:00 2001 From: Barney Boisvert Date: Sat, 14 May 2016 08:51:21 -0700 Subject: [PATCH 4/8] changed == to === --- resources/script.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/resources/script.js b/resources/script.js index 1f94aec..539385c 100644 --- a/resources/script.js +++ b/resources/script.js @@ -25,9 +25,9 @@ function navigate(n) { var position = currentPosition(); var numSlides = document.getElementsByClassName('slide').length; var nextPosition; - if (n == FIRST_SLIDE) { + if (n === FIRST_SLIDE) { nextPosition = 1; - } else if (n == LAST_SLIDE) { + } else if (n === LAST_SLIDE) { nextPosition = numSlides; } else { if (n < 0 && position <= 1) { @@ -154,9 +154,9 @@ document.addEventListener('DOMContentLoaded', function () { navigate(-1); } else if (kc === 38 || kc === 39 || kc === 32 || kc === 75 || kc === 76 || kc === 34) { navigate(1); - } else if (kc == 36) { + } else if (kc === 36) { navigate(FIRST_SLIDE); - } else if (kc == 35) { + } else if (kc === 35) { navigate(LAST_SLIDE); } else if (kc === 13) { toggleFullScreen(); From e276b95034f380095265540bae67fb015c8ce53e Mon Sep 17 00:00:00 2001 From: Barney Boisvert Date: Fri, 20 May 2016 09:15:49 -0700 Subject: [PATCH 5/8] added support for a 'loop' option (defaulting true) for whether to allow looping around the end of the presentation (in either direction) --- lib/index.js | 2 ++ resources/script.js | 12 +++++++----- templates/layout.mustache | 1 + 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/index.js b/lib/index.js index 723b7bb..ea8bf7b 100644 --- a/lib/index.js +++ b/lib/index.js @@ -211,6 +211,7 @@ Cleaver.prototype.loadStaticAssets = function () { Cleaver.prototype.renderSlideshow = function () { var putControls = this.options.controls || (this.options.controls === undefined); var putProgress = this.options.progress || (this.options.progress === undefined); + var allowLooping = this.options.loop || (this.options.loop === undefined); var style, script, output; // Render the slides in a template (maybe as specified by the user) @@ -253,6 +254,7 @@ Cleaver.prototype.renderSlideshow = function () { style: style, author: this.options.author, script: script, + allowLooping: allowLooping, options: this.options }; diff --git a/resources/script.js b/resources/script.js index 539385c..6c42c1b 100644 --- a/resources/script.js +++ b/resources/script.js @@ -30,11 +30,13 @@ function navigate(n) { } else if (n === LAST_SLIDE) { nextPosition = numSlides; } else { - if (n < 0 && position <= 1) { - return; - } - if (n > 0 && position >= numSlides) { - return; + if (! ALLOW_LOOPING) { + if (n < 0 && position <= 1) { + return; + } + if (n > 0 && position >= numSlides) { + return; + } } /* Positions are 1-indexed, so we need to add and subtract 1 */ diff --git a/templates/layout.mustache b/templates/layout.mustache index 3b841fc..bc2db76 100644 --- a/templates/layout.mustache +++ b/templates/layout.mustache @@ -15,6 +15,7 @@ {{{slideshow}}} From 7b01d2a7ec1fa3e230e2e04f38e519b8518ca09b Mon Sep 17 00:00:00 2001 From: Barney Boisvert Date: Fri, 20 May 2016 09:19:39 -0700 Subject: [PATCH 6/8] linter fixes --- resources/script.js | 6 ++++++ templates/layout.mustache | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/resources/script.js b/resources/script.js index 6c42c1b..b8f6a7b 100644 --- a/resources/script.js +++ b/resources/script.js @@ -17,6 +17,12 @@ var FIRST_SLIDE = -9999999; */ var LAST_SLIDE = 9999999; +/** + * Whether to allow looping from the last back to the first or backwards from + * the first to the last. + */ +var ALLOW_LOOPING = true; + /** * Navigates forward n pages * If n is negative, we will navigate in reverse diff --git a/templates/layout.mustache b/templates/layout.mustache index bc2db76..c61e702 100644 --- a/templates/layout.mustache +++ b/templates/layout.mustache @@ -15,8 +15,8 @@ {{{slideshow}}} From 3d3c863f9a436f9d2595feb922f99543f5d01cfc Mon Sep 17 00:00:00 2001 From: Barney Boisvert Date: Fri, 20 May 2016 09:21:42 -0700 Subject: [PATCH 7/8] Added the new 'loop' option to the docs --- docs/options.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/docs/options.md b/docs/options.md index 08fdcef..c072442 100644 --- a/docs/options.md +++ b/docs/options.md @@ -82,6 +82,13 @@ Displays a small progress bar at the top of your document. **Default**: true +### loop + +Allows looping from the last slide to the first, or backwards from the first to +the last. + +**Default**: true + ### encoding Content encoding to use on the rendered document. From 4b4837a51cd1c03dfc21e4da4b9470e5bf88fd80 Mon Sep 17 00:00:00 2001 From: Barney Boisvert Date: Fri, 10 Jun 2016 08:12:53 -0700 Subject: [PATCH 8/8] use two spaces to indent, rather than four --- resources/script.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/resources/script.js b/resources/script.js index b8f6a7b..706fdbf 100644 --- a/resources/script.js +++ b/resources/script.js @@ -36,20 +36,20 @@ function navigate(n) { } else if (n === LAST_SLIDE) { nextPosition = numSlides; } else { - if (! ALLOW_LOOPING) { - if (n < 0 && position <= 1) { - return; - } - if (n > 0 && position >= numSlides) { - return; - } + if (! ALLOW_LOOPING) { + if (n < 0 && position <= 1) { + return; } + if (n > 0 && position >= numSlides) { + return; + } + } - /* Positions are 1-indexed, so we need to add and subtract 1 */ - nextPosition = (position - 1 + n) % numSlides + 1; + /* Positions are 1-indexed, so we need to add and subtract 1 */ + nextPosition = (position - 1 + n) % numSlides + 1; - /* Normalize nextPosition in-case of a negative modulo result */ - nextPosition = (nextPosition - 1 + numSlides) % numSlides + 1; + /* Normalize nextPosition in-case of a negative modulo result */ + nextPosition = (nextPosition - 1 + numSlides) % numSlides + 1; } document.getElementById('slide-' + position).classList.add('hidden');