slideshow.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /**
  2. * Copyright (C) 2011 Hakim El Hattab, http://hakim.se
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. */
  22. /**
  23. * Handles the very minimal navigation logic involved in the
  24. * slideshow. Including keyboard navigation, touch interaction
  25. * and URL history behavior.
  26. *
  27. * Slides are given unique hash based URL's so that they can be
  28. * opened directly. I didn't use the HTML5 History API for this
  29. * as it would have required the addition of server side rewrite
  30. * rules and hence require more effort for anyone to set up.
  31. *
  32. * This component can be called from other scripts via a tiny API:
  33. * - Slideshow.navigateTo( indexh, indexv );
  34. * - Slideshow.navigateLeft();
  35. * - Slideshow.navigateRight();
  36. * - Slideshow.navigateUp();
  37. * - Slideshow.navigateDown();
  38. *
  39. *
  40. * version 0.1:
  41. * - First release
  42. *
  43. * version 0.2:
  44. * - Refactored code and added inline documentation
  45. * - Slides now have unique URL's
  46. * - A basic API to invoke navigation was added
  47. *
  48. * version 0.3:
  49. * - Added licensing terms
  50. *
  51. *
  52. * @author Hakim El Hattab
  53. * @version 0.3
  54. */
  55. var Slideshow = (function(){
  56. var indexh = 0,
  57. indexv = 0;
  58. /**
  59. * Activates the main program logic.
  60. */
  61. function initialize() {
  62. document.addEventListener('keydown', onDocumentKeyDown, false);
  63. document.addEventListener('touchstart', onDocumentTouchStart, false);
  64. window.addEventListener('hashchange', onWindowHashChange, false);
  65. // Read the initial state of the URL (hash)
  66. readURL();
  67. }
  68. /**
  69. * Handler for the document level 'keydown' event.
  70. *
  71. * @param {Object} event
  72. */
  73. function onDocumentKeyDown( event ) {
  74. if( event.keyCode >= 37 && event.keyCode <= 40 ) {
  75. switch( event.keyCode ) {
  76. case 37: navigateLeft(); break; // left
  77. case 39: navigateRight(); break; // right
  78. case 38: navigateUp(); break; // up
  79. case 40: navigateDown(); break; // down
  80. }
  81. slide();
  82. event.preventDefault();
  83. }
  84. }
  85. /**
  86. * Handler for the document level 'touchstart' event.
  87. *
  88. * This enables very basic tap interaction for touch
  89. * devices. Added mainly for performance testing of 3D
  90. * transforms on iOS but was so happily surprised with
  91. * how smoothly it runs so I left it in here. Apple +1
  92. *
  93. * @param {Object} event
  94. */
  95. function onDocumentTouchStart( event ) {
  96. // We're only interested in one point taps
  97. if (event.touches.length == 1) {
  98. event.preventDefault();
  99. var point = {
  100. x: event.touches[0].clientX,
  101. y: event.touches[0].clientY
  102. };
  103. // Define the extent of the areas that may be tapped
  104. // to navigate
  105. var wt = window.innerWidth * 0.3;
  106. var ht = window.innerHeight * 0.3;
  107. if( point.x < wt ) {
  108. navigateLeft();
  109. }
  110. else if( point.x > window.innerWidth - wt ) {
  111. navigateRight();
  112. }
  113. else if( point.y < ht ) {
  114. navigateUp();
  115. }
  116. else if( point.y > window.innerHeight - ht ) {
  117. navigateDown();
  118. }
  119. slide();
  120. }
  121. }
  122. /**
  123. * Handler for the window level 'hashchange' event.
  124. *
  125. * @param {Object} event
  126. */
  127. function onWindowHashChange( event ) {
  128. readURL();
  129. }
  130. /**
  131. * Updates one dimension of slides by showing the slide
  132. * with the specified index.
  133. *
  134. * @param {String} selector A CSS selector that will fetch
  135. * the group of slides we are working with
  136. * @param {Number} index The index of the slide that should be
  137. * shown
  138. *
  139. * @return {Number} The index of the slide that is now shown,
  140. * might differ from the passed in index if it was out of
  141. * bounds.
  142. */
  143. function updateSlides( selector, index ) {
  144. // Select all slides and convert the NodeList result to
  145. // an array
  146. var slides = Array.prototype.slice.call( document.querySelectorAll( selector ) );
  147. if( slides.length ) {
  148. // Enforce max and minimum index bounds
  149. index = Math.max(Math.min(index, slides.length - 1), 0);
  150. slides[index].setAttribute('class', 'present');
  151. // Any element previous to index is given the 'past' class
  152. slides.slice(0, index).map(function(element){
  153. element.setAttribute('class', 'past');
  154. });
  155. // Any element subsequent to index is given the 'future' class
  156. slides.slice(index + 1).map(function(element){
  157. element.setAttribute('class', 'future');
  158. });
  159. }
  160. else {
  161. // Since there are no slides we can't be anywhere beyond the
  162. // zeroth index
  163. index = 0;
  164. }
  165. return index;
  166. }
  167. /**
  168. * Updates the visual slides to represent the currently
  169. * set indices.
  170. */
  171. function slide() {
  172. indexh = updateSlides( '#main>section', indexh );
  173. indexv = updateSlides( 'section.present>section', indexv );
  174. writeURL();
  175. }
  176. /**
  177. * Reads the current URL (hash) and navigates accordingly.
  178. */
  179. function readURL() {
  180. // Break the hash down to separate components
  181. var bits = window.location.hash.slice(2).split('/');
  182. // Read the index components of the hash
  183. indexh = bits[0] ? parseInt( bits[0] ) : 0;
  184. indexv = bits[1] ? parseInt( bits[1] ) : 0;
  185. navigateTo( indexh, indexv );
  186. }
  187. /**
  188. * Updates the page URL (hash) to reflect the current
  189. * navigational state.
  190. */
  191. function writeURL() {
  192. var url = '/';
  193. // Only include the minimum possible number of components in
  194. // the URL
  195. if( indexh > 0 || indexv > 0 ) url += indexh
  196. if( indexv > 0 ) url += '/' + indexv
  197. window.location.hash = url;
  198. }
  199. /**
  200. * Triggers a navigation to the specified indices.
  201. *
  202. * @param {Number} h The horizontal index of the slide to show
  203. * @param {Number} v The vertical index of the slide to show
  204. */
  205. function navigateTo( h, v ) {
  206. indexh = h === undefined ? indexh : h;
  207. indexv = v === undefined ? indexv : v;
  208. slide();
  209. }
  210. function navigateLeft() {
  211. indexh --;
  212. indexv = 0;
  213. slide();
  214. }
  215. function navigateRight() {
  216. indexh ++;
  217. indexv = 0;
  218. slide();
  219. }
  220. function navigateUp() {
  221. indexv --;
  222. slide();
  223. }
  224. function navigateDown() {
  225. indexv ++;
  226. slide();
  227. }
  228. // Initialize the program. Done right before returning to ensure
  229. // that any inline variable definitions are available to all
  230. // functions
  231. initialize();
  232. // Expose some methods publicly
  233. return {
  234. navigateTo: navigateTo,
  235. navigateLeft: navigateLeft,
  236. navigateRight: navigateRight,
  237. navigateUp: navigateUp,
  238. navigateDown: navigateDown
  239. };
  240. })();