Permalink
Browse files

Fix current status number on mid-deck init, closes #56

  • Loading branch information...
1 parent c4e57fb commit 0eea53deb36db6e4d789b8c80939e4e89a865d5e @imakewebthings imakewebthings committed Dec 4, 2011
Showing with 30 additions and 15 deletions.
  1. +26 −14 extensions/status/deck.status.js
  2. +4 −1 test/spec.status.js
@@ -10,7 +10,16 @@ https://github.com/imakewebthings/deck.js/blob/master/GPL-license.txt
This module adds a (current)/(total) style status indicator to the deck.
*/
(function($, deck, undefined) {
- var $d = $(document);
+ var $d = $(document),
+
+ updateCurrent = function(e, from, to) {
+ var opts = $[deck]('getOptions');
+
+ $(opts.selectors.statusCurrent).text(opts.countNested ?
+ to + 1 :
+ $[deck]('getSlide', to).data('rootSlide')
+ );
+ };
/*
Extends defaults/options.
@@ -35,13 +44,14 @@ This module adds a (current)/(total) style status indicator to the deck.
});
$d.bind('deck.init', function() {
- var opts = $[deck]('getOptions');
+ var opts = $[deck]('getOptions'),
+ slides = $[deck]('getSlides'),
+ $current = $[deck]('getSlide'),
+ ndx;
- // Start on first slide
- $(opts.selectors.statusCurrent).text(1);
// Set total slides once
if (opts.countNested) {
- $(opts.selectors.statusTotal).text($[deck]('getSlides').length);
+ $(opts.selectors.statusTotal).text(slides.length);
}
else {
/* Determine root slides by checking each slide's ancestor tree for
@@ -58,7 +68,7 @@ This module adds a (current)/(total) style status indicator to the deck.
}).join(', ');
/* Store the 'real' root slide number for use during slide changes. */
- $.each($[deck]('getSlides'), function(i, $el) {
+ $.each(slides, function(i, $el) {
var $parentSlides = $el.parentsUntil(opts.selectors.container, slideTest);
$el.data('rootSlide', $parentSlides.length ?
@@ -69,15 +79,17 @@ This module adds a (current)/(total) style status indicator to the deck.
$(opts.selectors.statusTotal).text(rootIndex - 1);
}
+
+ // Find where we started in the deck and set initial state
+ $.each(slides, function(i, $el) {
+ if ($el === $current) {
+ ndx = i;
+ return false;
+ }
+ });
+ updateCurrent(null, ndx, ndx);
})
/* Update current slide number with each change event */
- .bind('deck.change', function(e, from, to) {
- var opts = $[deck]('getOptions');
-
- $(opts.selectors.statusCurrent).text(opts.countNested ?
- to + 1 :
- $[deck]('getSlide', to).data('rootSlide')
- );
- });
+ .bind('deck.change', updateCurrent);
})(jQuery, 'deck');
View
@@ -14,8 +14,11 @@ describe('Deck JS Status Indicator', function() {
expect($(defaults.selectors.statusTotal)).toHaveText($.deck('getSlides').length);
});
- it('should start at 1 of X', function() {
+ it('should start at the right current slide', function() {
expect($(defaults.selectors.statusCurrent)).toHaveText(1);
+ $.deck('go', 2);
+ $.deck('.slide');
+ expect($(defaults.selectors.statusCurrent)).toHaveText(3);
});
it('should update to the correct number on slide change', function() {

0 comments on commit 0eea53d

Please sign in to comment.