-
Notifications
You must be signed in to change notification settings - Fork 605
Expand file tree
/
Copy pathdeck.goto.js
More file actions
190 lines (161 loc) · 5.13 KB
/
deck.goto.js
File metadata and controls
190 lines (161 loc) · 5.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*!
Deck JS - deck.goto
Copyright (c) 2011-2014 Caleb Troughton
Dual licensed under the MIT license.
https://github.com/imakewebthings/deck.js/blob/master/MIT-license.txt
*/
/*
This module adds the necessary methods and key bindings to show and hide a form
for jumping to any slide number/id in the deck (and processes that form
accordingly). The form-showing state is indicated by the presence of a class on
the deck container.
*/
(function($, undefined) {
var $document = $(document);
var rootCounter;
var bindKeyEvents = function() {
$document.unbind('keydown.deckgoto');
$document.bind('keydown.deckgoto', function(event) {
var key = $.deck('getOptions').keys.goto;
if (event.which === key || $.inArray(event.which, key) > -1) {
event.preventDefault();
$.deck('toggleGoTo');
}
});
};
var populateDatalist = function() {
var options = $.deck('getOptions');
var $datalist = $(options.selectors.gotoDatalist);
$.each($.deck('getSlides'), function(i, $slide) {
var id = $slide.attr('id');
if (id) {
$datalist.append('<option value="' + id + '">');
}
});
};
var markRootSlides = function() {
var options = $.deck('getOptions');
var slideTest = $.map([
options.classes.before,
options.classes.previous,
options.classes.current,
options.classes.next,
options.classes.after
], function(el, i) {
return '.' + el;
}).join(', ');
rootCounter = 0;
$.each($.deck('getSlides'), function(i, $slide) {
var $parentSlides = $slide.parentsUntil(
options.selectors.container,
slideTest
);
if ($parentSlides.length) {
$slide.removeData('rootIndex');
}
else if (!options.countNested) {
++rootCounter;
$slide.data('rootIndex', rootCounter);
}
});
};
var handleFormSubmit = function() {
var options = $.deck('getOptions');
var $form = $(options.selectors.gotoForm);
$form.unbind('submit.deckgoto');
$form.bind('submit.deckgoto', function(event) {
var $field = $(options.selectors.gotoInput);
var indexOrId = $field.val();
var index = parseInt(indexOrId, 10);
if (!options.countNested) {
if (!isNaN(index) && (index > rootCounter || index <= 0)) {
return false;
}
$.each($.deck('getSlides'), function(i, $slide) {
if ($slide.data('rootIndex') === index) {
index = i + 1;
return false;
}
});
}
$.deck('go', isNaN(index) ? indexOrId : index - 1);
$.deck('hideGoTo');
$field.val('');
event.preventDefault();
});
};
/*
Extends defaults/options.
options.classes.goto
This class is added to the deck container when showing the Go To Slide
form.
options.selectors.gotoDatalist
The element that matches this selector is the datalist element that will
be populated with options for each of the slide ids. In browsers that
support the datalist element, this provides a drop list of slide ids to
aid the user in selecting a slide.
options.selectors.gotoForm
The element that matches this selector is the form that is submitted
when a user hits enter after typing a slide number/id in the gotoInput
element.
options.selectors.gotoInput
The element that matches this selector is the text input field for
entering a slide number/id in the Go To Slide form.
options.keys.goto
The numeric keycode used to show the Go To Slide form.
options.countNested
If false, only top level slides will be counted when entering a
slide number.
*/
$.extend(true, $.deck.defaults, {
classes: {
goto: 'deck-goto'
},
selectors: {
gotoDatalist: '#goto-datalist',
gotoForm: '.goto-form',
gotoInput: '#goto-slide'
},
keys: {
goto: 71 // g
},
countNested: true
});
/*
jQuery.deck('showGoTo')
Shows the Go To Slide form by adding the class specified by the goto class
option to the deck container.
*/
$.deck('extend', 'showGoTo', function() {
var options = $.deck('getOptions');
$.deck('getContainer').addClass(options.classes.goto);
$(options.selectors.gotoForm).attr('aria-hidden', false);
$(options.selectors.gotoInput).focus();
});
/*
jQuery.deck('hideGoTo')
Hides the Go To Slide form by removing the class specified by the goto class
option from the deck container.
*/
$.deck('extend', 'hideGoTo', function() {
var options = $.deck('getOptions');
$(options.selectors.gotoInput).blur();
$.deck('getContainer').removeClass(options.classes.goto);
$(options.selectors.gotoForm).attr('aria-hidden', true);
});
/*
jQuery.deck('toggleGoTo')
Toggles between showing and hiding the Go To Slide form.
*/
$.deck('extend', 'toggleGoTo', function() {
var options = $.deck('getOptions');
var hasGotoClass = $.deck('getContainer').hasClass(options.classes.goto);
$.deck(hasGotoClass ? 'hideGoTo' : 'showGoTo');
});
$document.bind('deck.init', function() {
bindKeyEvents();
populateDatalist();
markRootSlides();
handleFormSubmit();
});
})(jQuery);