summaryrefslogtreecommitdiffstats
path: root/doc/backends/deckjs/deck.js/extensions/status/deck.status.js
blob: dca0734f3694567888a1ea4cde44d3de57ca71fd (plain)
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
/*!
Deck JS - deck.status
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 a (current)/(total) style status indicator to the deck.
*/
(function($, undefined) {
  var $document = $(document);
  var rootCounter;

  var updateCurrent = function(event, from, to) {
    var options = $.deck('getOptions');
    var currentSlideNumber = to + 1;
    if (!options.countNested) {
      currentSlideNumber = $.deck('getSlide', to).data('rootSlide');
    }
    $(options.selectors.statusCurrent).text(currentSlideNumber);
  };

  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.data('rootSlide', $parentSlides.last().data('rootSlide'));
      }
      else {
        ++rootCounter;
        $slide.data('rootSlide', rootCounter);
      }
    });
  };

  var setInitialSlideNumber = function() {
    var slides = $.deck('getSlides');
    var $currentSlide = $.deck('getSlide');
    var index;

    $.each(slides, function(i, $slide) {
      if ($slide === $currentSlide) {
        index = i;
        return false;
      }
    });
    updateCurrent(null, index, index);
  };

  var setTotalSlideNumber = function() {
    var options = $.deck('getOptions');
    var slides = $.deck('getSlides');

    if (options.countNested) {
      $(options.selectors.statusTotal).text(slides.length);
    }
    else {
      $(options.selectors.statusTotal).text(rootCounter);
    }
  };

  /*
  Extends defaults/options.

  options.selectors.statusCurrent
    The element matching this selector displays the current slide number.

  options.selectors.statusTotal
    The element matching this selector displays the total number of slides.

  options.countNested
    If false, only top level slides will be counted in the current and
    total numbers.
  */
  $.extend(true, $.deck.defaults, {
    selectors: {
      statusCurrent: '.deck-status-current',
      statusTotal: '.deck-status-total'
    },

    countNested: true
  });

  $document.bind('deck.init', function() {
    markRootSlides();
    setInitialSlideNumber();
    setTotalSlideNumber();
  });
  $document.bind('deck.change', updateCurrent);
})(jQuery, 'deck');