Add deck.js slide deck with basic layout
[kai/lca12.git] / extensions / status / deck.status.js
1 /*!
2 Deck JS - deck.status
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 */
8
9 /*
10 This module adds a (current)/(total) style status indicator to the deck.
11 */
12 (function($, deck, undefined) {
13         var $d = $(document);
14         
15         /*
16         Extends defaults/options.
17         
18         options.selectors.statusCurrent
19                 The element matching this selector displays the current slide number.
20                 
21         options.selectors.statusTotal
22                 The element matching this selector displays the total number of slides.
23                 
24         options.countNested
25                 If false, only top level slides will be counted in the current and
26                 total numbers.
27         */
28         $.extend(true, $[deck].defaults, {
29                 selectors: {
30                         statusCurrent: '.deck-status-current',
31                         statusTotal: '.deck-status-total'
32                 },
33                 
34                 countNested: true
35         });
36         
37         $d.bind('deck.init', function() {
38                 var opts = $[deck]('getOptions');
39                 
40                 // Start on first slide
41                 $(opts.selectors.statusCurrent).text(1);
42                 // Set total slides once
43                 if (opts.countNested) {
44                         $(opts.selectors.statusTotal).text($[deck]('getSlides').length);
45                 }
46                 else {
47                         /* Determine root slides by checking each slide's ancestor tree for
48                         any of the slide classes. */
49                         var rootIndex = 1,
50                         slideTest = $.map([
51                                 opts.classes.before,
52                                 opts.classes.previous,
53                                 opts.classes.current,
54                                 opts.classes.next,
55                                 opts.classes.after
56                         ], function(el, i) {
57                                 return '.' + el;
58                         }).join(', ');
59                         
60                         /* Store the 'real' root slide number for use during slide changes. */
61                         $.each($[deck]('getSlides'), function(i, $el) {
62                                 var $parentSlides = $el.parentsUntil(opts.selectors.container, slideTest);
63
64                                 $el.data('rootSlide', $parentSlides.length ?
65                                         $parentSlides.last().data('rootSlide') :
66                                         rootIndex++
67                                 );
68                         });
69                         
70                         $(opts.selectors.statusTotal).text(rootIndex - 1);
71                 }
72         })
73         /* Update current slide number with each change event */
74         .bind('deck.change', function(e, from, to) {
75                 var opts = $[deck]('getOptions');
76                 
77                 $(opts.selectors.statusCurrent).text(opts.countNested ?
78                         to + 1 :
79                         $[deck]('getSlide', to).data('rootSlide')
80                 );
81         });
82 })(jQuery, 'deck');
83