Fixing accidentally-global variable.
[deck.js.git] / extensions / navigation / deck.navigation.js
blob0969527edc0f89d7057a6c551ab981e6ea1b46b7
1 /*!
2 Deck JS - deck.navigation
3 Copyright (c) 2011 Caleb Troughton
4 Dual licensed under the MIT license and GPL license.
5 https://github.com/imakewebthings/deck.js/blob/master/MIT-license.txt
6 https://github.com/imakewebthings/deck.js/blob/master/GPL-license.txt
7 */
9 /*
10 This module adds clickable previous and next links to the deck.
12 (function($, deck, undefined) {
13         var $d = $(document),
14         
15         /* Updates link hrefs, and disabled states if last/first slide */
16         updateButtons = function(e, from, to) {
17                 var opts = $[deck]('getOptions'),
18                 last = $[deck]('getSlides').length - 1,
19                 prevSlide = $[deck]('getSlide', to - 1),
20                 nextSlide = $[deck]('getSlide', to + 1),
21                 prevId = prevSlide ? prevSlide.attr('id') : undefined,
22                 nextId = nextSlide ? nextSlide.attr('id') : undefined;
23                 
24                 $(opts.selectors.previousLink)
25                         .toggleClass(opts.classes.navDisabled, !to)
26                         .attr('href', '#' + (prevId ? prevId : ''));
27                 $(opts.selectors.nextLink)
28                         .toggleClass(opts.classes.navDisabled, to === last)
29                         .attr('href', '#' + (nextId ? nextId : ''));
30         };
31         
32         /*
33         Extends defaults/options.
34         
35         options.classes.navDisabled
36                 This class is added to a navigation link when that action is disabled.
37                 It is added to the previous link when on the first slide, and to the
38                 next link when on the last slide.
39                 
40         options.selectors.nextLink
41                 The elements that match this selector will move the deck to the next
42                 slide when clicked.
43                 
44         options.selectors.previousLink
45                 The elements that match this selector will move to deck to the previous
46                 slide when clicked.
47         */
48         $.extend(true, $[deck].defaults, {
49                 classes: {
50                         navDisabled: 'deck-nav-disabled'
51                 },
52                 
53                 selectors: {
54                         nextLink: '.deck-next-link',
55                         previousLink: '.deck-prev-link'
56                 }
57         });
59         $d.bind('deck.init', function() {
60                 var opts = $[deck]('getOptions'),
61                 slides = $[deck]('getSlides'),
62                 $current = $[deck]('getSlide'),
63                 ndx;
64                 
65                 // Setup prev/next link events
66                 $(opts.selectors.previousLink)
67                 .unbind('click.decknavigation')
68                 .bind('click.decknavigation', function(e) {
69                         $[deck]('prev');
70                         e.preventDefault();
71                 });
72                 
73                 $(opts.selectors.nextLink)
74                 .unbind('click.decknavigation')
75                 .bind('click.decknavigation', function(e) {
76                         $[deck]('next');
77                         e.preventDefault();
78                 });
79                 
80                 // Find where we started in the deck and set initial states
81                 $.each(slides, function(i, $slide) {
82                         if ($slide === $current) {
83                                 ndx = i;
84                                 return false;
85                         }
86                 });
87                 updateButtons(null, ndx, ndx);
88         })
89         .bind('deck.change', updateButtons);
90 })(jQuery, 'deck');