summaryrefslogtreecommitdiff
path: root/deck.js/extensions/automatic/deck.automatic.js
blob: 6a3220941a452e6d22c34eb26b7843303e2debbe (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
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
/*!
Deck JS - deck.navigation
Copyright (c) 2012 Romain Champourlier
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 adds automatic control of the deck.
*/
(function($, deck, undefined) {
	var $d = $(document);
	var running = false;
	
	clearAutomaticTimeout = function() {
		if ($[deck].automatic && $[deck].automatic.timeout) {
			window.clearTimeout($[deck].automatic.timeout);
		}
	};
	
	// from and to are set with the values of the slide event calling
	// this method, not the changes this method should trigger.
	setTimeoutIfNeeded = function(e, from, to) {
		// Clear previous timeout (necessary in cases the user generates deck's change
		// events, for example by changing slides manually).
		clearAutomaticTimeout();
		
		var opts = $[deck]('getOptions');

		if (running) {
			// Slideshow running.

			var elem = $[deck]('getSlide', to);
			
			var duration = opts.automatic.slideDuration;

      // Iterate over element's classes to
      // match against classdata
      $.each(elem.attr('class').split(/\s+/), function(idx, cls){
        $.each(opts.classdata, function(feat_cls, features){
          if(cls == feat_cls && features.duration){
            duration = features.duration;
          }
        });
      });

			var customDuration = elem.attr('data-duration');
			if(customDuration){
			  duration = customDuration;
			}

      // If duration is negative, don't set a timeout
			if(duration >= 0){
			  if (to == $[deck]('getSlides').length-1) {
				  // setTimeout... called when going to last slide. 
				  // If cycling, set a timeout to go to first slide, else don't set a timeout, and set
				  // state to stopped.
				  if (opts.automatic.cycle) {
					  $[deck].automatic = {
						  timeout: window.setTimeout(function() {
							  $[deck]('go', 0);
							  if (e) e.preventDefault();
						  }, duration)
					  };
				  }
				  else {
					  $(opts.selectors.automaticLink).removeClass(opts.classes.automaticRunning);
					  $(opts.selectors.automaticLink).addClass(opts.classes.automaticStopped);
				  }
			  }
			  else {
				  // Running, not yet on last slide.
				  $[deck].automatic = {
					  timeout: window.setTimeout(function() {
						  $[deck]('next');
						  if (e) e.preventDefault();
					  }, duration)
				  };
			  }
      }
		}
	};
	
	/*
	Extends defaults/options.
	
	options.classes.automaticRunning
		This class is added to the automatic link when the deck is currently in running
		state.
		
	options.classes.automaticStopped
		This class is added to the automatic link when the deck is currently in stopped
		state.
		
	options.selectors.automaticLink
		The elements that match this selector will toggle automatic run of the deck
		when clicked.
	*/
	$.extend(true, $[deck].defaults, {
		classes: {
			automaticRunning: 'deck-automatic-running',
			automaticStopped: 'deck-automatic-stopped'
		},
		
		selectors: {
			automaticLink: '.deck-automatic-link'
		},
		
		classdata: {
		/* // Example duration class-feature
		 * // Sets the duration of all elements with
		 * // bullet-point-timing class to 500ms.
		 * 'bullet-point-timing': {
		 *   duration: 500
	   * }
	   */
		},
		
		automatic: {
			startRunning: true,
			cycle: true,
			slideDuration: 3000
		}
	});

  // Lets others detect when slideshow is running automatically
  $[deck]('extend', 'isRunning', function(){
    return running;
  });

	$d.bind('deck.init', function() {
		var opts = $[deck]('getOptions'),
		slides = $[deck]('getSlides'),
		$current = $[deck]('getSlide'),
		ndx;
		
		// Extension function to play the slideshow
		$[deck]('extend', 'play', function(){
      var slides = $[deck]('getSlides');
		  if (slides[slides.length-1] == $[deck]('getSlide')) {
			  // Stopped on last slide. Clicking to play/pause will rewind to first slide, and play.
			  $.deck('go', 0);
		  }
		  running = true;
		  $(opts.selectors.automaticLink).addClass(opts.classes.automaticRunning);
		  $(opts.selectors.automaticLink).removeClass(opts.classes.automaticStopped);
      $d.trigger('deck.onPlayToggle', true);
		  $d.trigger('deck.onPlay');
		  setTimeoutIfNeeded(null, slides.length, 0);
    });
    
    // Extension function to pause the slideshow
    $[deck]('extend', 'pause', function(){
      running = false;
		  $(opts.selectors.automaticLink).addClass(opts.classes.automaticStopped);
      $(opts.selectors.automaticLink).removeClass(opts.classes.automaticRunning);
      $d.trigger('deck.onPlayToggle', true);
      $d.trigger('deck.onPause');
		  clearAutomaticTimeout();
    });
		
		// Setup initial state
		if (opts.automatic.startRunning) {
      $[deck]('play');
		}
		else {
		  $[deck]('pause');
		}
		setTimeoutIfNeeded(null, ndx, 0);
		
		// Setup automatic link toggle events
		$(opts.selectors.automaticLink)
		.unbind('click.deckautomatic')
		.bind('click.deckautomatic', function(e) {
			if (!running) {
				$[deck]('play');
			}
			else {
				$[deck]('pause');
			}
		});
	})
	.bind('deck.change', setTimeoutIfNeeded);
})(jQuery, 'deck');