3 Copyright (c) 2011-2012 Caleb Troughton
4 Dual licensed under the MIT license and GPL license.
5 https://github.com/imakewebthings/deck.js/blob/master/MIT-license.txt
6 https://github.com/imakewebthings/deck.js/blob/master/GPL-license.txt
10 This module adds automatic scaling to the deck. Slides are scaled down
11 using CSS transforms to fit within the deck container. If the container is
12 big enough to hold the slides without scaling, no scaling occurs. The user
13 can disable and enable scaling with a keyboard shortcut.
15 Note: CSS transforms may make Flash videos render incorrectly. Presenters
16 that need to use video may want to disable scaling to play them. HTML5 video
19 (function($, deck
, window
, undefined) {
22 baseHeight
, // Value to scale against
23 timer
, // Timeout id for debouncing
27 Internal function to do all the dirty work of scaling the slides.
29 scaleDeck = function() {
30 var opts
= $[deck
]('getOptions'),
31 obh
= opts
.baseHeight
,
32 $container
= $[deck
]('getContainer'),
33 baseHeight
= obh
? obh
: $container
.height();
35 // Scale each slide down if necessary (but don't scale up)
36 $.each(rootSlides
, function(i
, $slide
) {
37 var slideHeight
= $slide
.innerHeight(),
38 $scaler
= $slide
.find('.' + opts
.classes
.scaleSlideWrapper
),
39 scale
= $container
.hasClass(opts
.classes
.scale
) ?
40 baseHeight
/ slideHeight
:
44 $scaler
.css('transform', '');
47 $scaler
.css('transform', 'scale(' + scale
+ ')');
53 Extends defaults/options.
56 This class is added to the deck container when scaling is enabled.
57 It is enabled by default when the module is included.
59 options.classes.scaleSlideWrapper
60 Scaling is done using a wrapper around the contents of each slide. This
61 class is applied to that wrapper.
64 The numeric keycode used to toggle enabling and disabling scaling.
67 When baseHeight is falsy, as it is by default, the deck is scaled in
68 proportion to the height of the deck container. You may instead specify
69 a height as a number of px, and slides will be scaled against this
70 height regardless of the container size.
73 Scaling on the browser resize event is debounced. This number is the
74 threshold in milliseconds. You can learn more about debouncing here:
75 http://unscriptable.com/index.php/2009/03/20/debouncing-javascript-methods/
78 $.extend(true, $[deck
].defaults
, {
81 scaleSlideWrapper
: 'deck-slide-scaler'
93 jQuery.deck('disableScale')
95 Disables scaling and removes the scale class from the deck container.
97 $[deck
]('extend', 'disableScale', function() {
98 $[deck
]('getContainer').removeClass($[deck
]('getOptions').classes
.scale
);
103 jQuery.deck('enableScale')
105 Enables scaling and adds the scale class to the deck container.
107 $[deck
]('extend', 'enableScale', function() {
108 $[deck
]('getContainer').addClass($[deck
]('getOptions').classes
.scale
);
113 jQuery.deck('toggleScale')
115 Toggles between enabling and disabling scaling.
117 $[deck
]('extend', 'toggleScale', function() {
118 var $c
= $[deck
]('getContainer');
119 $[deck
]($c
.hasClass($[deck
]('getOptions').classes
.scale
) ?
120 'disableScale' : 'enableScale');
123 $d
.bind('deck.init', function() {
124 var opts
= $[deck
]('getOptions'),
127 opts
.classes
.previous
,
128 opts
.classes
.current
,
135 // Build top level slides array
137 $.each($[deck
]('getSlides'), function(i
, $el
) {
138 if (!$el
.parentsUntil(opts
.selectors
.container
, slideTest
).length
) {
139 rootSlides
.push($el
);
143 // Use a wrapper on each slide to handle content scaling
144 $.each(rootSlides
, function(i
, $slide
) {
145 $slide
.children().wrapAll('<div class="' + opts
.classes
.scaleSlideWrapper
+ '"/>');
148 // Debounce the resize scaling
149 $w
.unbind('resize.deckscale').bind('resize.deckscale', function() {
150 window
.clearTimeout(timer
);
151 timer
= window
.setTimeout(scaleDeck
, opts
.scaleDebounce
);
153 // Scale once on load, in case images or something change layout
154 .unbind('load.deckscale').bind('load.deckscale', scaleDeck
);
157 $d
.unbind('keydown.deckscale').bind('keydown.deckscale', function(e
) {
158 if (e
.which
=== opts
.keys
.scale
|| $.inArray(e
.which
, opts
.keys
.scale
) > -1) {
159 $[deck
]('toggleScale');
164 // Enable scale on init
165 $[deck
]('enableScale');
167 })(jQuery
, 'deck', this);