-
Notifications
You must be signed in to change notification settings - Fork 0
/
deck.core.js
498 lines (423 loc) · 13.7 KB
/
deck.core.js
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/*!
Deck JS - deck.core
Copyright (c) 2011 Caleb Troughton
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
*/
/*
The deck.core module provides all the basic functionality for creating and
moving through a deck. It does so by applying classes to indicate the state of
the deck and its slides, allowing CSS to take care of the visual representation
of each state. It also provides methods for navigating the deck and inspecting
its state, as well as basic key bindings for going to the next and previous
slides. More functionality is provided by wholly separate extension modules
that use the API provided by core.
*/
(function($, deck, document, undefined) {
var slides, // Array of all the uh, slides...
current, // Array index of the current slide
$container, // Keeping this cached
events = {
/*
This event fires whenever the current slide changes, whether by way of
next, prev, or go. The callback function is passed two parameters, from
and to, equal to the indices of the old slide and the new slide
respectively. If preventDefault is called on the event within this handler
the slide change does not occur.
$(document).bind('deck.change', function(event, from, to) {
alert('Moving from slide ' + from + ' to ' + to);
});
*/
change: 'deck.change',
/*
This event fires at the beginning of deck initialization, after the options
are set but before the slides array is created. This event makes a good hook
for preprocessing extensions looking to modify the deck.
*/
beforeInitialize: 'deck.beforeInit',
/*
This event fires at the end of deck initialization. Extensions should
implement any code that relies on user extensible options (key bindings,
element selectors, classes) within a handler for this event. Native
events associated with Deck JS should be scoped under a .deck event
namespace, as with the example below:
var $d = $(document);
$.deck.defaults.keys.myExtensionKeycode = 70; // 'h'
$d.bind('deck.init', function() {
$d.bind('keydown.deck', function(event) {
if (event.which === $.deck.getOptions().keys.myExtensionKeycode) {
// Rock out
}
});
});
*/
initialize: 'deck.init'
},
options = {},
$d = $(document),
/*
Internal function. Updates slide and container classes based on which
slide is the current slide.
*/
updateStates = function() {
var oc = options.classes,
osc = options.selectors.container,
old = $container.data('onSlide'),
$all = $();
// Container state
$container.removeClass(oc.onPrefix + old)
.addClass(oc.onPrefix + current)
.data('onSlide', current);
// Remove and re-add child-current classes for nesting
$('.' + oc.current).parentsUntil(osc).removeClass(oc.childCurrent);
slides[current].parentsUntil(osc).addClass(oc.childCurrent);
// Remove previous states
$.each(slides, function(i, el) {
$all = $all.add(el);
});
$all.removeClass([
oc.before,
oc.previous,
oc.current,
oc.next,
oc.after
].join(" "));
// Add new states back in
slides[current].addClass(oc.current);
if (current > 0) {
slides[current-1].addClass(oc.previous);
}
if (current + 1 < slides.length) {
slides[current+1].addClass(oc.next);
}
if (current > 1) {
$.each(slides.slice(0, current - 1), function(i, el) {
el.addClass(oc.before);
});
}
if (current + 2 < slides.length) {
$.each(slides.slice(current+2), function(i, el) {
el.addClass(oc.after);
});
}
},
/* Methods exposed in the jQuery.deck namespace */
methods = {
/*
jQuery.deck(selector, options)
selector: string | jQuery | array
options: object, optional
Initializes the deck, using each element matched by selector as a slide.
May also be passed an array of string selectors or jQuery objects, in
which case each selector in the array is considered a slide. The second
parameter is an optional options object which will extend the default
values.
$.deck('.slide');
or
$.deck([
'#first-slide',
'#second-slide',
'#etc'
]);
*/
init: function(elements, opts) {
var startTouch,
tolerance,
esp = function(e) {
e.stopPropagation();
};
options = $.extend(true, {}, $[deck].defaults, opts);
slides = [];
current = 0;
$container = $(options.selectors.container);
tolerance = options.touch.swipeTolerance;
// Pre init event for preprocessing hooks
$d.trigger(events.beforeInitialize);
// Hide the deck while states are being applied to kill transitions
$container.addClass(options.classes.loading);
// Fill slides array depending on parameter type
if ($.isArray(elements)) {
$.each(elements, function(i, e) {
slides.push($(e));
});
}
else {
$(elements).each(function(i, e) {
slides.push($(e));
});
}
/* Remove any previous bindings, and rebind key events */
$d.unbind('keydown.deck').bind('keydown.deck', function(e) {
if (e.which === options.keys.next || $.inArray(e.which, options.keys.next) > -1) {
methods.next();
e.preventDefault();
}
else if (e.which === options.keys.previous || $.inArray(e.which, options.keys.previous) > -1) {
methods.prev();
e.preventDefault();
}
})
/* Stop propagation of key events within editable elements */
.undelegate('input, textarea, select, button, meter, progress, [contentEditable]', 'keydown', esp)
.delegate('input, textarea, select, button, meter, progress, [contentEditable]', 'keydown', esp);
/* Bind touch events for swiping between slides on touch devices */
$container.unbind('touchstart.deck').bind('touchstart.deck', function(e) {
if (!startTouch) {
startTouch = $.extend({}, e.originalEvent.targetTouches[0]);
}
})
.unbind('touchmove.deck').bind('touchmove.deck', function(e) {
$.each(e.originalEvent.changedTouches, function(i, t) {
if (startTouch && t.identifier === startTouch.identifier) {
if (t.screenX - startTouch.screenX > tolerance || t.screenY - startTouch.screenY > tolerance) {
$[deck]('prev');
startTouch = undefined;
}
else if (t.screenX - startTouch.screenX < -1 * tolerance || t.screenY - startTouch.screenY < -1 * tolerance) {
$[deck]('next');
startTouch = undefined;
}
return false;
}
});
e.preventDefault();
})
.unbind('touchend.deck').bind('touchend.deck', function(t) {
$.each(t.originalEvent.changedTouches, function(i, t) {
if (startTouch && t.identifier === startTouch.identifier) {
startTouch = undefined;
}
});
})
.scrollLeft(0).scrollTop(0);
/*
Kick iframe videos, which dont like to redraw w/ transforms.
Remove this if Webkit ever fixes it.
*/
$.each(slides, function(i, $el) {
$el.unbind('webkitTransitionEnd.deck').bind('webkitTransitionEnd.deck',
function(event) {
if ($el.hasClass($[deck]('getOptions').classes.current)) {
var embeds = $(this).find('iframe').css('opacity', 0);
window.setTimeout(function() {
embeds.css('opacity', 1);
}, 100);
}
});
});
if (slides.length) {
updateStates();
}
// Show deck again now that slides are in place
$container.removeClass(options.classes.loading);
$d.trigger(events.initialize);
},
/*
jQuery.deck('go', index)
index: integer | string
Moves to the slide at the specified index if index is a number. Index is
0-based, so $.deck('go', 0); will move to the first slide. If index is a
string this will move to the slide with the specified id. If index is out
of bounds or doesn't match a slide id the call is ignored.
*/
go: function(index) {
var e = $.Event(events.change),
ndx;
/* Number index, easy. */
if (typeof index === 'number' && index >= 0 && index < slides.length) {
ndx = index;
}
/* Id string index, search for it and set integer index */
else if (typeof index === 'string') {
$.each(slides, function(i, $slide) {
if ($slide.attr('id') === index) {
ndx = i;
return false;
}
});
};
/* Out of bounds, id doesn't exist, illegal input, eject */
if (typeof ndx === 'undefined') return;
$d.trigger(e, [current, ndx]);
if (e.isDefaultPrevented()) {
/* Trigger the event again and undo the damage done by extensions. */
$d.trigger(events.change, [ndx, current]);
}
else {
current = ndx;
updateStates();
}
},
/*
jQuery.deck('next')
Moves to the next slide. If the last slide is already active, the call
is ignored.
*/
next: function() {
methods.go(current+1);
},
/*
jQuery.deck('prev')
Moves to the previous slide. If the first slide is already active, the
call is ignored.
*/
prev: function() {
methods.go(current-1);
},
/*
jQuery.deck('getSlide', index)
index: integer, optional
Returns a jQuery object containing the slide at index. If index is not
specified, the current slide is returned.
*/
getSlide: function(index) {
var i = typeof index !== 'undefined' ? index : current;
if (typeof i != 'number' || i < 0 || i >= slides.length) return null;
return slides[i];
},
/*
jQuery.deck('getSlides')
Returns all slides as an array of jQuery objects.
*/
getSlides: function() {
return slides;
},
/*
jQuery.deck('getContainer')
Returns a jQuery object containing the deck container as defined by the
container option.
*/
getContainer: function() {
return $container;
},
/*
jQuery.deck('getOptions')
Returns the options object for the deck, including any overrides that
were defined at initialization.
*/
getOptions: function() {
return options;
},
/*
jQuery.deck('extend', name, method)
name: string
method: function
Adds method to the deck namespace with the key of name. This doesn’t
give access to any private member data — public methods must still be
used within method — but lets extension authors piggyback on the deck
namespace rather than pollute jQuery.
$.deck('extend', 'alert', function(msg) {
alert(msg);
});
// Alerts 'boom'
$.deck('alert', 'boom');
*/
extend: function(name, method) {
methods[name] = method;
}
};
/* jQuery extension */
$[deck] = function(method, arg) {
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
}
else {
return methods.init(method, arg);
}
};
/*
The default settings object for a deck. All deck extensions should extend
this object to add defaults for any of their options.
options.classes.after
This class is added to all slides that appear after the 'next' slide.
options.classes.before
This class is added to all slides that appear before the 'previous'
slide.
options.classes.childCurrent
This class is added to all elements in the DOM tree between the
'current' slide and the deck container. For standard slides, this is
mostly seen and used for nested slides.
options.classes.current
This class is added to the current slide.
options.classes.loading
This class is applied to the deck container during loading phases and is
primarily used as a way to short circuit transitions between states
where such transitions are distracting or unwanted. For example, this
class is applied during deck initialization and then removed to prevent
all the slides from appearing stacked and transitioning into place
on load.
options.classes.next
This class is added to the slide immediately following the 'current'
slide.
options.classes.onPrefix
This prefix, concatenated with the current slide index, is added to the
deck container as you change slides.
options.classes.previous
This class is added to the slide immediately preceding the 'current'
slide.
options.selectors.container
Elements matched by this CSS selector will be considered the deck
container. The deck container is used to scope certain states of the
deck, as with the onPrefix option, or with extensions such as deck.goto
and deck.menu.
options.keys.next
The numeric keycode used to go to the next slide.
options.keys.previous
The numeric keycode used to go to the previous slide.
options.touch.swipeTolerance
The number of pixels the users finger must travel to produce a swipe
gesture.
*/
$[deck].defaults = {
classes: {
after: 'deck-after',
before: 'deck-before',
childCurrent: 'deck-child-current',
current: 'deck-current',
loading: 'deck-loading',
next: 'deck-next',
onPrefix: 'on-slide-',
previous: 'deck-previous'
},
selectors: {
container: '.deck-container'
},
keys: {
// enter, space, page down, right arrow, down arrow,
next: [13, 32, 34, 39, 40],
// backspace, page up, left arrow, up arrow
previous: [8, 33, 37, 38]
},
touch: {
swipeTolerance: 60
}
};
$d.ready(function() {
$('html').addClass('ready');
});
/*
FF + Transforms + Flash video don't get along...
Firefox will reload and start playing certain videos after a
transform. Blanking the src when a previously shown slide goes out
of view prevents this.
*/
$d.bind('deck.change', function(e, from, to) {
var oldFrames = $[deck]('getSlide', from).find('iframe'),
newFrames = $[deck]('getSlide', to).find('iframe');
oldFrames.each(function() {
var $this = $(this),
curSrc = $this.attr('src');
if(curSrc) {
$this.data('deck-src', curSrc).attr('src', '');
}
});
newFrames.each(function() {
var $this = $(this),
originalSrc = $this.data('deck-src');
if (originalSrc) {
$this.attr('src', originalSrc);
}
});
});
})(jQuery, 'deck', document);