Add deck.js slide deck with basic layout
[kai/lca12.git] / extensions / menu / deck.menu.js
1 /*!
2 Deck JS - deck.menu
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 the methods and key binding to show and hide a menu of all
11 slides in the deck. The deck menu state is indicated by the presence of a class
12 on the deck container.
13 */
14 (function($, deck, undefined) {
15         var $d = $(document),
16         rootSlides; // Array of top level slides
17         
18         /*
19         Extends defaults/options.
20         
21         options.classes.menu
22                 This class is added to the deck container when showing the slide menu.
23         
24         options.keys.menu
25                 The numeric keycode used to toggle between showing and hiding the slide
26                 menu.
27                 
28         options.touch.doubletapWindow
29                 Two consecutive touch events within this number of milliseconds will
30                 be considered a double tap, and will toggle the menu on touch devices.
31         */
32         $.extend(true, $[deck].defaults, {
33                 classes: {
34                         menu: 'deck-menu'
35                 },
36                 
37                 keys: {
38                         menu: 77 // m
39                 },
40                 
41                 touch: {
42                         doubletapWindow: 400
43                 }
44         });
45
46         /*
47         jQuery.deck('showMenu')
48         
49         Shows the slide menu by adding the class specified by the menu class option
50         to the deck container.
51         */
52         $[deck]('extend', 'showMenu', function() {
53                 var $c = $[deck]('getContainer'),
54                 opts = $[deck]('getOptions');
55                 
56                 // Hide through loading class to short-circuit transitions (perf)
57                 $c.addClass([opts.classes.loading, opts.classes.menu].join(' '));
58                 
59                 /* Forced to do this in JS until CSS learns second-grade math. Save old
60                 style value for restoration when menu is hidden. */
61                 if (Modernizr.csstransforms) {
62                         $.each(rootSlides, function(i, $slide) {
63                                 $slide.data('oldStyle', $slide.attr('style'));
64                                 $slide.css({
65                                         'position': 'absolute',
66                                         'left': ((i % 4) * 25) + '%',
67                                         'top': (Math.floor(i / 4) * 25) + '%'
68                                 });
69                         });
70                 }
71                 
72                 // Need to ensure the loading class renders first, then remove
73                 window.setTimeout(function() {
74                         $c.removeClass(opts.classes.loading)
75                                 .scrollTop($[deck]('getSlide').offset().top);
76                 }, 0);
77         });
78
79         /*
80         jQuery.deck('hideMenu')
81         
82         Hides the slide menu by removing the class specified by the menu class
83         option from the deck container.
84         */
85         $[deck]('extend', 'hideMenu', function() {
86                 var $c = $[deck]('getContainer'),
87                 opts = $[deck]('getOptions');
88                 
89                 $c.removeClass(opts.classes.menu);
90                 $c.addClass(opts.classes.loading);
91                 
92                 /* Restore old style value */
93                 if (Modernizr.csstransforms) {
94                         $.each(rootSlides, function(i, $slide) {
95                                 var oldStyle = $slide.data('oldStyle');
96
97                                 $slide.attr('style', oldStyle ? oldStyle : '');
98                         });
99                 }
100                 
101                 window.setTimeout(function() {
102                         $c.removeClass(opts.classes.loading).scrollTop(0);
103                 }, 0);
104         });
105
106         /*
107         jQuery.deck('toggleMenu')
108         
109         Toggles between showing and hiding the slide menu.
110         */
111         $[deck]('extend', 'toggleMenu', function() {
112                 $[deck]('getContainer').hasClass($[deck]('getOptions').classes.menu) ?
113                 $[deck]('hideMenu') : $[deck]('showMenu');
114         });
115
116         $d.bind('deck.init', function() {
117                 var opts = $[deck]('getOptions'),
118                 touchEndTime = 0,
119                 currentSlide,
120                 slideTest = $.map([
121                         opts.classes.before,
122                         opts.classes.previous,
123                         opts.classes.current,
124                         opts.classes.next,
125                         opts.classes.after
126                 ], function(el, i) {
127                         return '.' + el;
128                 }).join(', ');
129                 
130                 // Build top level slides array
131                 rootSlides = [];
132                 $.each($[deck]('getSlides'), function(i, $el) {
133                         if (!$el.parentsUntil(opts.selectors.container, slideTest).length) {
134                                 rootSlides.push($el);
135                         }
136                 });
137                 
138                 // Bind key events
139                 $d.unbind('keydown.deckmenu').bind('keydown.deckmenu', function(e) {
140                         if (e.which === opts.keys.menu || $.inArray(e.which, opts.keys.menu) > -1) {
141                                 $[deck]('toggleMenu');
142                                 e.preventDefault();
143                         }
144                 });
145                 
146                 // Double tap to toggle slide menu for touch devices
147                 $[deck]('getContainer').unbind('touchstart.deckmenu').bind('touchstart.deckmenu', function(e) {
148                         currentSlide = $[deck]('getSlide');
149                 })
150                 .unbind('touchend.deckmenu').bind('touchend.deckmenu', function(e) {
151                         var now = Date.now();
152                         
153                         // Ignore this touch event if it caused a nav change (swipe)
154                         if (currentSlide !== $[deck]('getSlide')) return;
155                         
156                         if (now - touchEndTime < opts.touch.doubletapWindow) {
157                                 $[deck]('toggleMenu');
158                                 e.preventDefault();
159                         }
160                         touchEndTime = now;
161                 });
162                 
163                 // Selecting slides from the menu
164                 $.each($[deck]('getSlides'), function(i, $s) {
165                         $s.unbind('click.deckmenu').bind('click.deckmenu', function(e) {
166                                 if (!$[deck]('getContainer').hasClass(opts.classes.menu)) return;
167
168                                 $[deck]('go', i);
169                                 $[deck]('hideMenu');
170                                 e.stopPropagation();
171                                 e.preventDefault();
172                         });
173                 });
174         })
175         .bind('deck.change', function(e, from, to) {
176                 var container = $[deck]('getContainer');
177                 
178                 if (container.hasClass($[deck]('getOptions').classes.menu)) {
179                         container.scrollTop($[deck]('getSlide', to).offset().top);
180                 }
181         });
182 })(jQuery, 'deck');
183