3 Copyright (c) 2011-2014 Caleb Troughton
4 Dual licensed under the MIT license.
5 https://github.com/imakewebthings/deck.js/blob/master/MIT-license.txt
9 This module adds automatic scaling to the deck. Slides are scaled down
10 using CSS transforms to fit within the deck container. If the container is
11 big enough to hold the slides without scaling, no scaling occurs. The user
12 can disable and enable scaling with a keyboard shortcut.
14 Note: CSS transforms may make Flash videos render incorrectly. Presenters
15 that need to use video may want to disable scaling to play them. HTML5 video
18 (function($, undefined) {
19 var $document = $(document);
20 var $window = $(window);
21 var baseHeight, timer, rootSlides;
24 Internal function to do all the dirty work of scaling the slides.
26 var scaleDeck = function() {
27 var options = $.deck('getOptions');
28 var $container = $.deck('getContainer');
29 var baseHeight = options.baseHeight;
32 baseHeight = $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 var $scaler = $slide.find('.' + options.classes.scaleSlideWrapper);
39 var shouldScale = $container.hasClass(options.classes.scale);
40 var scale = shouldScale ? baseHeight / slideHeight : 1;
43 $scaler.css('transform', '');
46 $scaler.css('transform', 'scale(' + scale + ')');
47 window.setTimeout(function() {
48 $container.scrollTop(0)
54 var populateRootSlides = function() {
55 var options = $.deck('getOptions');
56 var slideTest = $.map([
57 options.classes.before,
58 options.classes.previous,
59 options.classes.current,
67 $.each($.deck('getSlides'), function(i, $slide) {
68 var $parentSlides = $slide.parentsUntil(
69 options.selectors.container,
72 if (!$parentSlides.length) {
73 rootSlides.push($slide);
78 var wrapRootSlideContent = function() {
79 var options = $.deck('getOptions');
80 var wrap = '<div class="' + options.classes.scaleSlideWrapper + '"/>';
81 $.each(rootSlides, function(i, $slide) {
82 $slide.children().wrapAll(wrap);
86 var scaleOnResizeAndLoad = function() {
87 var options = $.deck('getOptions');
89 $window.unbind('resize.deckscale');
90 $window.bind('resize.deckscale', function() {
91 window.clearTimeout(timer);
92 timer = window.setTimeout(scaleDeck, options.scaleDebounce);
94 $.deck('enableScale');
95 $window.unbind('load.deckscale');
96 $window.bind('load.deckscale', scaleDeck);
99 var bindKeyEvents = function() {
100 var options = $.deck('getOptions');
101 $document.unbind('keydown.deckscale');
102 $document.bind('keydown.deckscale', function(event) {
103 var isKey = event.which === options.keys.scale;
104 isKey = isKey || $.inArray(event.which, options.keys.scale) > -1;
106 $.deck('toggleScale');
107 event.preventDefault();
113 Extends defaults/options.
115 options.classes.scale
116 This class is added to the deck container when scaling is enabled.
117 It is enabled by default when the module is included.
119 options.classes.scaleSlideWrapper
120 Scaling is done using a wrapper around the contents of each slide. This
121 class is applied to that wrapper.
124 The numeric keycode used to toggle enabling and disabling scaling.
127 When baseHeight is falsy, as it is by default, the deck is scaled in
128 proportion to the height of the deck container. You may instead specify
129 a height as a number of px, and slides will be scaled against this
130 height regardless of the container size.
132 options.scaleDebounce
133 Scaling on the browser resize event is debounced. This number is the
134 threshold in milliseconds. You can learn more about debouncing here:
135 http://unscriptable.com/index.php/2009/03/20/debouncing-javascript-methods/
138 $.extend(true, $.deck.defaults, {
141 scaleSlideWrapper: 'deck-slide-scaler'
153 jQuery.deck('disableScale')
155 Disables scaling and removes the scale class from the deck container.
157 $.deck('extend', 'disableScale', function() {
158 $.deck('getContainer').removeClass($.deck('getOptions').classes.scale);
163 jQuery.deck('enableScale')
165 Enables scaling and adds the scale class to the deck container.
167 $.deck('extend', 'enableScale', function() {
168 $.deck('getContainer').addClass($.deck('getOptions').classes.scale);
173 jQuery.deck('toggleScale')
175 Toggles between enabling and disabling scaling.
177 $.deck('extend', 'toggleScale', function() {
178 var $container = $.deck('getContainer');
179 var isScaled = $container.hasClass($.deck('getOptions').classes.scale);
180 $.deck(isScaled? 'disableScale' : 'enableScale');
183 $document.bind('deck.init', function() {
184 populateRootSlides();
185 wrapRootSlideContent();
186 scaleOnResizeAndLoad();
189 })(jQuery, 'deck', this);