2 Deck JS - deck.core - v1.0
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
10 The deck.core module provides all the basic functionality for creating and
11 moving through a deck. It does so by applying classes to indicate the state of
12 the deck and its slides, allowing CSS to take care of the visual representation
13 of each state. It also provides methods for navigating the deck and inspecting
14 its state, as well as basic key bindings for going to the next and previous
15 slides. More functionality is provided by wholly separate extension modules
16 that use the API provided by core.
18 (function($, deck, document, undefined) {
19 var slides, // Array of all the uh, slides...
20 current, // Array index of the current slide
24 This event fires whenever the current slide changes, whether by way of
25 next, prev, or go. The callback function is passed two parameters, from
26 and to, equal to the indices of the old slide and the new slide
29 $(document).bind('deck.change', function(event, from, to) {
30 alert('Moving from slide ' + from + ' to ' + to);
33 change: 'deck.change',
36 This event fires at the end of deck initialization. Extensions should
37 implement any code that relies on user extensible options (key bindings,
38 element selectors, classes) within a handler for this event. Native
39 events associated with Deck JS should be scoped under a .deck event
40 namespace, as with the example below:
43 $.deck.defaults.keys.myExtensionKeycode = 70; // 'h'
44 $d.bind('deck.init', function() {
45 $d.bind('keydown.deck', function(event) {
46 if (event.which == $.deck.getOptions().keys.myExtensionKeycode) {
52 initialize: 'deck.init'
59 Internal function. Updates slide and container classes based on which
60 slide is the current slide.
62 updateStates = function() {
63 var oc = options.classes,
64 osc = options.selectors.container,
66 old = $container.data('onSlide'),
70 $container.removeClass(oc.onPrefix + old)
71 .addClass(oc.onPrefix + current)
72 .data('onSlide', current);
74 // Remove and re-add child-current classes for nesting
75 $('.' + oc.current).parentsUntil(osc).removeClass(oc.childCurrent);
76 slides[current].parentsUntil(osc).addClass(oc.childCurrent);
78 // Remove previous states
79 $.each(slides, function(i, el) {
90 // Add new states back in
91 slides[current].addClass(oc.current);
93 slides[current-1].addClass(oc.previous);
95 if (current + 1 < slides.length) {
96 slides[current+1].addClass(oc.next);
99 $.each(slides.slice(0, current - 1), function(i, el) {
100 el.addClass(oc.before);
103 if (current + 2 < slides.length) {
104 $.each(slides.slice(current+2), function(i, el) {
105 el.addClass(oc.after);
110 /* Methods exposed in the jQuery.deck namespace */
114 jQuery.deck(selector, options)
116 selector: string | jQuery | array
117 options: object, optional
119 Initializes the deck, using each element matched by selector as a slide.
120 May also be passed an array of string selectors or jQuery objects, in
121 which case each selector in the array is considered a slide. The second
122 parameter is an optional options object which will extend the default
135 init: function(elements, opts) {
136 $.extend(true, options, $[deck].defaults, opts);
140 // Fill slides array depending on parameter type
141 if ($.isArray(elements)) {
142 $.each(elements, function(i, e) {
147 $(elements).each(function(i, e) {
152 /* Remove any previous bindings, and rebind key events */
153 $d.unbind('keydown.deck').bind('keydown.deck', function(e) {
155 case options.keys.next:
158 case options.keys.previous:
165 $d.trigger(events.initialize);
169 jQuery.deck('go', index)
173 Moves to the slide at the specified index. Index is 0-based, so
174 $.deck('go', 0); will move to the first slide. If index is out of bounds
175 or not a number the call is ignored.
177 go: function(index) {
178 if (typeof index != 'number' || index < 0 || index >= slides.length) return;
180 $d.trigger(events.change, [current, index]);
188 Moves to the next slide. If the last slide is already active, the call
192 methods.go(current+1);
198 Moves to the previous slide. If the first slide is already active, the
202 methods.go(current-1);
206 jQuery.deck('getSlide', index)
208 index: integer, optional
210 Returns a jQuery object containing the slide at index. If index is not
211 specified, the current slide is returned.
213 getSlide: function(index) {
214 var i = index ? index : current;
215 if (typeof i != 'number' || i < 0 || i >= slides.length) return null;
220 jQuery.deck('getSlides')
222 Returns all slides as an array of jQuery objects.
224 getSlides: function() {
229 jQuery.deck('getContainer')
231 Returns a jQuery object containing the deck container as defined by the
234 getContainer: function() {
235 return $(options.selectors.container);
239 jQuery.deck('getOptions')
241 Returns the options object for the deck, including any overrides that
242 were defined at initialization.
244 getOptions: function() {
249 jQuery.deck('extend', name, method)
254 Adds method to the deck namespace with the key of name. This doesn’t
255 give access to any private member data — public methods must still be
256 used within method — but lets extension authors piggyback on the deck
257 namespace rather than pollute jQuery.
259 $.deck('extend', 'alert', function(msg) {
264 $.deck('alert', 'boom');
266 extend: function(name, method) {
267 methods[name] = method;
271 /* jQuery extension */
272 $[deck] = function(method, arg) {
273 if (methods[method]) {
274 return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
277 return methods.init(method, arg);
282 The default settings object for a deck. All deck extensions should extend
283 this object to add defaults for any of their options.
285 options.classes.after
286 This class is added to all slides that appear after the 'next' slide.
288 options.classes.before
289 This class is added to all slides that appear before the 'previous'
292 options.classes.childCurrent
293 This class is added to all elements in the DOM tree between the
294 'current' slide and the deck container. For standard slides, this is
295 mostly seen and used for nested slides.
297 options.classes.current
298 This class is added to the current slide.
301 This class is added to the slide immediately following the 'current'
304 options.classes.onPrefix
305 This prefix, concatenated with the current slide index, is added to the
306 deck container as you change slides.
308 options.classes.previous
309 This class is added to the slide immediately preceding the 'current'
312 options.selectors.container
313 Elements matched by this CSS selector will be considered the deck
314 container. The deck container is used to scope certain states of the
315 deck, as with the onPrefix option, or with extensions such as deck.goto
319 The numeric keycode used to go to the next slide.
321 options.keys.previous
322 The numeric keycode used to go to the previous slide.
327 before: 'deck-before',
328 childCurrent: 'deck-child-current',
329 current: 'deck-current',
331 onPrefix: 'on-slide-',
332 previous: 'deck-previous'
336 container: '.deck-container'
340 next: 39, // right arrow key
341 previous: 37 // left arrow key
345 $d.ready(function() {
346 $('html').addClass('ready');
348 })(jQuery, 'deck', document);