summaryrefslogtreecommitdiff
path: root/deck.js/extensions/step
diff options
context:
space:
mode:
authorChristian Franke <nobody@nowhere.ws>2013-04-09 14:31:22 +0200
committerChristian Franke <nobody@nowhere.ws>2013-04-09 14:31:22 +0200
commit5a774ef6f3b38b19b26913a34b1388530c72334e (patch)
tree543127427771d75f652fc9e726cc39aa1a72571c /deck.js/extensions/step
Initial Commit
Diffstat (limited to 'deck.js/extensions/step')
-rw-r--r--deck.js/extensions/step/deck.step.js119
1 files changed, 119 insertions, 0 deletions
diff --git a/deck.js/extensions/step/deck.step.js b/deck.js/extensions/step/deck.step.js
new file mode 100644
index 0000000..1b1ac70
--- /dev/null
+++ b/deck.js/extensions/step/deck.step.js
@@ -0,0 +1,119 @@
+/*!
+Deck JS - deck.step
+Copyright (c) 2011 RĂ©mi Emonet
+Dual licensed under the MIT license and GPL license.
+https://github.com/imakewebthings/deck.js/blob/master/MIT-license.txt
+https://github.com/imakewebthings/deck.js/blob/master/GPL-license.txt
+*/
+
+/*
+This module provides new methods for stepping without considering sub-slides, together with tools for finding toplevel slides etc.
+It also overrides the defaults keybinding and countNested value (so it is better to include it after "goto" and "status" extensions).
+*/
+
+(function($, deck, undefined) {
+ var $d = $(document);
+ // undo the defaults (to be sure jquery behaves properly when overriding it)
+ $.extend(true, $[deck].defaults, { keys: {next:null, previous:null}});
+ // and go on
+ $.extend(true, $[deck].defaults, {
+ selectors: {
+ subslidesToNotify: ".slide,.onshowtoplevel"
+ },
+ // Here we redefined the defaults:
+ // - we avoid counting nested slides
+ // - we keep up/down for top-level slides
+ // - we still use pgup/pgdown for inner slides as they are sent by some pluggable remote controls
+ keys: {
+ // backspace, left arrow, page down
+ previous: [8, 37, 34],
+ // enter, space, right arrow, page up
+ next: [13, 32, 39, 33],
+ // up arrow
+ previousTopLevel: [38],
+ // down arrow,
+ nextTopLevel: [40]
+ },
+ countNested: false
+ });
+ var myInArray = function(el, arr) {
+ for (i in arr) if (arr[i].is(el)) return i*1; // cast to int
+ return -1;
+ };
+ $[deck]('extend', 'getToplevelSlideOf', function(node) {
+ var opts = $[deck]('getOptions');
+ var slides = $[deck]('getSlides');
+ var last = {node: $(node), index: myInArray(node, slides)};
+ $(node).parentsUntil(opts.selectors.container).each( function(ii, v) {
+ var ind = myInArray(v, slides);
+ if (ind != -1) {
+ last = {node: $(v), index: ind};
+ }
+ });
+ return last;
+ });
+ $[deck]('extend', 'getToplevelSlideOfIndex', function(ind) {
+ return $[deck]('getToplevelSlideOf', $[deck]('getSlide', ind));
+ });
+ $[deck]('extend', 'previousTopLevelSlide', function() {
+ /* Find the real previous parent */
+ var current = $[deck]('getSlide');
+ var currentParent = $[deck]('getToplevelSlideOf', current);
+ var toGo = currentParent.index;
+ if (current.is(currentParent.node) && toGo > 0) {
+ // This is already toplevel slide, just go to the previous toplevel one (parent of the previous one)
+ toGo = $[deck]('getToplevelSlideOfIndex', toGo-1).index;
+ }
+ $[deck]('go', toGo);
+
+ });
+ $[deck]('extend', 'nextTopLevelSlide', function() {
+ /* Find the real next parent */
+ var current = $[deck]('getSlide');
+ var currentParent = $[deck]('getToplevelSlideOf', current);
+ var icur = currentParent.index;
+ for (; icur < $[deck]('getSlides').length; icur++) {
+ var cursorParent = $[deck]('getToplevelSlideOfIndex', icur).node;
+ if (!cursorParent.is(currentParent.node)) {
+ $[deck]('go', icur);
+ break;
+ }
+ }
+ });
+ $d.bind('deck.init', function() {
+ $d.unbind('keydown.decknexttoplevel').bind('keydown.decknexttoplevel', function(e) {
+ var $opts = $[deck]('getOptions');
+ var key = $opts.keys.nextTopLevel;
+ if (e.which === key || $.inArray(e.which, key) > -1) {
+ e.preventDefault();
+ $[deck]('nextTopLevelSlide');
+ }
+ });
+ $d.unbind('keydown.deckprevioustoplevel').bind('keydown.deckprevioustoplevel', function(e) {
+ var $opts = $[deck]('getOptions');
+ var key = $opts.keys.previousTopLevel;
+ if (e.which === key || $.inArray(e.which, key) > -1) {
+ e.preventDefault();
+ $[deck]('previousTopLevelSlide');
+ }
+ });
+ });
+ // When jumping (not steping), we will init the subslides (in case they are animations), in a backward order, and then fast forward necessary animations
+ var bigJump = function(from, to) {
+ var direction = "forward";
+ if (from > to){
+ direction = "reverse";
+ }
+ var opts = $[deck]('getOptions');
+ $($[deck]('getToplevelSlideOfIndex', to).node.find(opts.selectors.subslidesToNotify).get().reverse()).trigger('deck.toplevelBecameCurrent', direction);
+ for (icur = $[deck]('getToplevelSlideOfIndex', to).index + 1; icur < to+1; icur++) {
+ $[deck]('getSlides')[icur].trigger('deck.afterToplevelBecameCurrent', 'forward');
+ }
+ }
+ $d.bind('deck.change', function(e, from, to) {
+ if ( Math.abs(from - to) > 1 || ! $[deck]('getToplevelSlideOfIndex', to).node.is($[deck]('getToplevelSlideOfIndex', from).node)) {
+ // consider natural jumps and the case where we actually changed (top level) slide (even with a step)
+ bigJump(from, to);
+ }
+ });
+})(jQuery, 'deck');