PageRenderTime 25ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/ajax/libs/deck.js/1.0.0/extensions/scale/deck.scale.js

https://gitlab.com/Mirros/cdnjs
JavaScript | 190 lines | 110 code | 19 blank | 61 comment | 6 complexity | c80be2ffe334f799bbcd9d036ead2927 MD5 | raw file
  1. /*!
  2. Deck JS - deck.scale
  3. Copyright (c) 2011-2013 Caleb Troughton
  4. Dual licensed under the MIT license.
  5. https://github.com/imakewebthings/deck.js/blob/master/MIT-license.txt
  6. */
  7. /*
  8. This module adds automatic scaling to the deck. Slides are scaled down
  9. using CSS transforms to fit within the deck container. If the container is
  10. big enough to hold the slides without scaling, no scaling occurs. The user
  11. can disable and enable scaling with a keyboard shortcut.
  12. Note: CSS transforms may make Flash videos render incorrectly. Presenters
  13. that need to use video may want to disable scaling to play them. HTML5 video
  14. works fine.
  15. */
  16. (function($, undefined) {
  17. var $document = $(document);
  18. var $window = $(window);
  19. var baseHeight, timer, rootSlides;
  20. /*
  21. Internal function to do all the dirty work of scaling the slides.
  22. */
  23. var scaleDeck = function() {
  24. var options = $.deck('getOptions');
  25. var $container = $.deck('getContainer');
  26. var baseHeight = options.baseHeight;
  27. if (!baseHeight) {
  28. baseHeight = $container.height();
  29. }
  30. // Scale each slide down if necessary (but don't scale up)
  31. $.each(rootSlides, function(i, $slide) {
  32. var slideHeight = $slide.innerHeight();
  33. var $scaler = $slide.find('.' + options.classes.scaleSlideWrapper);
  34. var shouldScale = $container.hasClass(options.classes.scale);
  35. var scale = shouldScale ? baseHeight / slideHeight : 1;
  36. if (scale === 1) {
  37. $scaler.css('transform', '');
  38. }
  39. else {
  40. $scaler.css('transform', 'scale(' + scale + ')');
  41. window.setTimeout(function() {
  42. $container.scrollTop(0)
  43. }, 1);
  44. }
  45. });
  46. };
  47. var populateRootSlides = function() {
  48. var options = $.deck('getOptions');
  49. var slideTest = $.map([
  50. options.classes.before,
  51. options.classes.previous,
  52. options.classes.current,
  53. options.classes.next,
  54. options.classes.after
  55. ], function(el, i) {
  56. return '.' + el;
  57. }).join(', ');
  58. rootSlides = [];
  59. $.each($.deck('getSlides'), function(i, $slide) {
  60. var $parentSlides = $slide.parentsUntil(
  61. options.selectors.container,
  62. slideTest
  63. );
  64. if (!$parentSlides.length) {
  65. rootSlides.push($slide);
  66. }
  67. });
  68. };
  69. var wrapRootSlideContent = function() {
  70. var options = $.deck('getOptions');
  71. var wrap = '<div class="' + options.classes.scaleSlideWrapper + '"/>';
  72. $.each(rootSlides, function(i, $slide) {
  73. $slide.children().wrapAll(wrap);
  74. });
  75. };
  76. var scaleOnResizeAndLoad = function() {
  77. var options = $.deck('getOptions');
  78. $window.unbind('resize.deckscale');
  79. $window.bind('resize.deckscale', function() {
  80. window.clearTimeout(timer);
  81. timer = window.setTimeout(scaleDeck, options.scaleDebounce);
  82. });
  83. $.deck('enableScale');
  84. $window.unbind('load.deckscale');
  85. $window.bind('load.deckscale', scaleDeck);
  86. };
  87. var bindKeyEvents = function() {
  88. var options = $.deck('getOptions');
  89. $document.unbind('keydown.deckscale');
  90. $document.bind('keydown.deckscale', function(event) {
  91. var isKey = event.which === options.keys.scale;
  92. isKey = isKey || $.inArray(event.which, options.keys.scale) > -1;
  93. if (isKey) {
  94. $.deck('toggleScale');
  95. event.preventDefault();
  96. }
  97. });
  98. };
  99. /*
  100. Extends defaults/options.
  101. options.classes.scale
  102. This class is added to the deck container when scaling is enabled.
  103. It is enabled by default when the module is included.
  104. options.classes.scaleSlideWrapper
  105. Scaling is done using a wrapper around the contents of each slide. This
  106. class is applied to that wrapper.
  107. options.keys.scale
  108. The numeric keycode used to toggle enabling and disabling scaling.
  109. options.baseHeight
  110. When baseHeight is falsy, as it is by default, the deck is scaled in
  111. proportion to the height of the deck container. You may instead specify
  112. a height as a number of px, and slides will be scaled against this
  113. height regardless of the container size.
  114. options.scaleDebounce
  115. Scaling on the browser resize event is debounced. This number is the
  116. threshold in milliseconds. You can learn more about debouncing here:
  117. http://unscriptable.com/index.php/2009/03/20/debouncing-javascript-methods/
  118. */
  119. $.extend(true, $.deck.defaults, {
  120. classes: {
  121. scale: 'deck-scale',
  122. scaleSlideWrapper: 'deck-slide-scaler'
  123. },
  124. keys: {
  125. scale: 83 // s
  126. },
  127. baseHeight: null,
  128. scaleDebounce: 200
  129. });
  130. /*
  131. jQuery.deck('disableScale')
  132. Disables scaling and removes the scale class from the deck container.
  133. */
  134. $.deck('extend', 'disableScale', function() {
  135. $.deck('getContainer').removeClass($.deck('getOptions').classes.scale);
  136. scaleDeck();
  137. });
  138. /*
  139. jQuery.deck('enableScale')
  140. Enables scaling and adds the scale class to the deck container.
  141. */
  142. $.deck('extend', 'enableScale', function() {
  143. $.deck('getContainer').addClass($.deck('getOptions').classes.scale);
  144. scaleDeck();
  145. });
  146. /*
  147. jQuery.deck('toggleScale')
  148. Toggles between enabling and disabling scaling.
  149. */
  150. $.deck('extend', 'toggleScale', function() {
  151. var $container = $.deck('getContainer');
  152. var isScaled = $container.hasClass($.deck('getOptions').classes.scale);
  153. $.deck(isScaled? 'disableScale' : 'enableScale');
  154. });
  155. $document.bind('deck.init', function() {
  156. populateRootSlides();
  157. wrapRootSlideContent();
  158. scaleOnResizeAndLoad();
  159. bindKeyEvents();
  160. });
  161. })(jQuery, 'deck', this);