Add deck.js slide deck with basic layout
[kai/lca12.git] / extensions / goto / deck.goto.js
1 /*!
2 Deck JS - deck.goto
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 necessary methods and key bindings to show and hide a form
11 for jumping to any slide number/id in the deck (and processes that form
12 accordingly). The form-showing state is indicated by the presence of a class on
13 the deck container.
14 */
15 (function($, deck, undefined) {
16         var $d = $(document);
17         
18         /*
19         Extends defaults/options.
20         
21         options.classes.goto
22                 This class is added to the deck container when showing the Go To Slide
23                 form.
24                 
25         options.selectors.gotoDatalist
26                 The element that matches this selector is the datalist element that will
27                 be populated with options for each of the slide ids.  In browsers that
28                 support the datalist element, this provides a drop list of slide ids to
29                 aid the user in selecting a slide.
30                 
31         options.selectors.gotoForm
32                 The element that matches this selector is the form that is submitted
33                 when a user hits enter after typing a slide number/id in the gotoInput
34                 element.
35         
36         options.selectors.gotoInput
37                 The element that matches this selector is the text input field for
38                 entering a slide number/id in the Go To Slide form.
39                 
40         options.keys.goto
41                 The numeric keycode used to show the Go To Slide form.
42         */
43         $.extend(true, $[deck].defaults, {
44                 classes: {
45                         goto: 'deck-goto'
46                 },
47                 
48                 selectors: {
49                         gotoDatalist: '#goto-datalist',
50                         gotoForm: '.goto-form',
51                         gotoInput: '#goto-slide'
52                 },
53                 
54                 keys: {
55                         goto: 71 // g
56                 }
57         });
58
59         /*
60         jQuery.deck('showGoTo')
61         
62         Shows the Go To Slide form by adding the class specified by the goto class
63         option to the deck container.
64         */
65         $[deck]('extend', 'showGoTo', function() {
66                 $[deck]('getContainer').addClass($[deck]('getOptions').classes.goto);
67                 $($[deck]('getOptions').selectors.gotoInput).focus();
68         });
69
70         /*
71         jQuery.deck('hideGoTo')
72         
73         Hides the Go To Slide form by removing the class specified by the goto class
74         option from the deck container.
75         */
76         $[deck]('extend', 'hideGoTo', function() {
77                 $($[deck]('getOptions').selectors.gotoInput).blur();
78                 $[deck]('getContainer').removeClass($[deck]('getOptions').classes.goto);
79         });
80
81         /*
82         jQuery.deck('toggleGoTo')
83         
84         Toggles between showing and hiding the Go To Slide form.
85         */
86         $[deck]('extend', 'toggleGoTo', function() {
87                 $[deck]($[deck]('getContainer').hasClass($[deck]('getOptions').classes.goto) ? 'hideGoTo' : 'showGoTo');
88         });
89         
90         $d.bind('deck.init', function() {
91                 var opts = $[deck]('getOptions'),
92                 $datalist = $(opts.selectors.gotoDatalist);
93                 
94                 // Bind key events
95                 $d.unbind('keydown.deckgoto').bind('keydown.deckgoto', function(e) {
96                         var key = $[deck]('getOptions').keys.goto;
97                         
98                         if (e.which === key || $.inArray(e.which, key) > -1) {
99                                 e.preventDefault();
100                                 $[deck]('toggleGoTo');
101                         }
102                 });
103                 
104                 /* Populate datalist */
105                 $.each($[deck]('getSlides'), function(i, $slide) {
106                         var id = $slide.attr('id');
107                         
108                         if (id) {
109                                 $datalist.append('<option value="' + id + '">');
110                         }
111                 });
112                 
113                 // Process form submittal, go to the slide entered
114                 $(opts.selectors.gotoForm)
115                 .unbind('submit.deckgoto')
116                 .bind('submit.deckgoto', function(e) {
117                         var $field = $($[deck]('getOptions').selectors.gotoInput),
118                         i = parseInt($field.val(), 10);
119                         
120                         $[deck]('go', isNaN(i) ? $field.val() : i - 1);
121                         $[deck]('hideGoTo');
122                         $field.val('');
123                         
124                         e.preventDefault();
125                 });
126                 
127                 $(opts.selectors.gotoInput)
128                 .unbind('keydown.deckgoto')
129                 .bind('keydown.deckgoto', function(e) {
130                         e.stopPropagation();
131                 });
132         });
133 })(jQuery, 'deck');
134