2 Deck JS - deck.navigation
3 Copyright (c) 2011-2014 Caleb Troughton
4 Dual licensed under the MIT license.
5 https://github.com/imakewebthings/deck.js/blob/master/MIT-license.txt
9 This module adds clickable previous and next links to the deck.
11 (function($, undefined) {
12 var $document
= $(document
);
14 /* Updates link hrefs, and disabled states if last/first slide */
15 var updateButtons = function(event, from, to) {
16 var options = $.deck('getOptions');
17 var lastIndex = $.deck('getSlides').length - 1;
18 var $prevSlide = $.deck('getSlide', to - 1);
19 var $nextSlide = $.deck('getSlide', to + 1);
20 var hrefBase = window.location.href.replace(/#.*/, '');
21 var prevId = $prevSlide ? $prevSlide.attr('id') : undefined;
22 var nextId = $nextSlide ? $nextSlide.attr('id') : undefined;
23 var $prevButton = $(options.selectors.previousLink);
24 var $nextButton = $(options.selectors.nextLink);
26 $prevButton.toggleClass(options.classes.navDisabled, to === 0);
27 $prevButton.attr('aria-disabled', to === 0);
28 $prevButton.attr('href', hrefBase + '#' + (prevId ? prevId : ''));
29 $nextButton.toggleClass(options.classes.navDisabled, to === lastIndex);
30 $nextButton.attr('aria-disabled', to === lastIndex);
31 $nextButton.attr('href', hrefBase + '#' + (nextId ? nextId : ''));
35 Extends defaults/options.
37 options.classes.navDisabled
38 This class is added to a navigation link when that action is disabled.
39 It is added to the previous link when on the first slide, and to the
40 next link when on the last slide.
42 options.selectors.nextLink
43 The elements that match this selector will move the deck to the next
46 options.selectors.previousLink
47 The elements that match this selector will move to deck to the previous
50 $.extend(true, $.deck
.defaults
, {
52 navDisabled
: 'deck-nav-disabled'
56 nextLink
: '.deck-next-link',
57 previousLink
: '.deck-prev-link'
61 $document
.bind('deck.init', function() {
62 var options
= $.deck('getOptions');
63 var slides
= $.deck('getSlides');
64 var $current
= $.deck('getSlide');
65 var $prevButton
= $(options
.selectors
.previousLink
);
66 var $nextButton
= $(options
.selectors
.nextLink
);
69 // Setup prev/next link events
70 $prevButton
.unbind('click.decknavigation');
71 $prevButton
.bind('click.decknavigation', function(event
) {
73 event
.preventDefault();
76 $nextButton
.unbind('click.decknavigation');
77 $nextButton
.bind('click.decknavigation', function(event
) {
79 event
.preventDefault();
82 // Find where we started in the deck and set initial states
83 $.each(slides
, function(i
, $slide
) {
84 if ($slide
=== $current
) {
89 updateButtons(null, index
, index
);
92 $document
.bind('deck.change', updateButtons
);