slideshow.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. // Never prevent taps on anchors and images
  99. if( event.target.tagName.toLowerCase() === 'a' || event.target.tagName.toLowerCase() === 'img' ) {
  100. return;
  101. }
  102. event.preventDefault();
  103. var point = {
  104. x: event.touches[0].clientX,
  105. y: event.touches[0].clientY
  106. };
  107. // Define the extent of the areas that may be tapped
  108. // to navigate
  109. var wt = window.innerWidth * 0.3;
  110. var ht = window.innerHeight * 0.3;
  111. if( point.x < wt ) {
  112. navigateLeft();
  113. }
  114. else if( point.x > window.innerWidth - wt ) {
  115. navigateRight();
  116. }
  117. else if( point.y < ht ) {
  118. navigateUp();
  119. }
  120. else if( point.y > window.innerHeight - ht ) {
  121. navigateDown();
  122. }
  123. slide();
  124. }
  125. }
  126. /**
  127. * Handler for the window level 'hashchange' event.
  128. *
  129. * @param {Object} event
  130. */
  131. function onWindowHashChange( event ) {
  132. readURL();
  133. }
  134. /**
  135. * Updates one dimension of slides by showing the slide
  136. * with the specified index.
  137. *
  138. * @param {String} selector A CSS selector that will fetch
  139. * the group of slides we are working with
  140. * @param {Number} index The index of the slide that should be
  141. * shown
  142. *
  143. * @return {Number} The index of the slide that is now shown,
  144. * might differ from the passed in index if it was out of
  145. * bounds.
  146. */
  147. function updateSlides( selector, index ) {
  148. // Select all slides and convert the NodeList result to
  149. // an array
  150. var slides = Array.prototype.slice.call( document.querySelectorAll( selector ) );
  151. if( slides.length ) {
  152. // Enforce max and minimum index bounds
  153. index = Math.max(Math.min(index, slides.length - 1), 0);
  154. slides[index].setAttribute('class', 'present');
  155. // Any element previous to index is given the 'past' class
  156. slides.slice(0, index).map(function(element){
  157. element.setAttribute('class', 'past');
  158. });
  159. // Any element subsequent to index is given the 'future' class
  160. slides.slice(index + 1).map(function(element){
  161. element.setAttribute('class', 'future');
  162. });
  163. }
  164. else {
  165. // Since there are no slides we can't be anywhere beyond the
  166. // zeroth index
  167. index = 0;
  168. }
  169. return index;
  170. }
  171. /**
  172. * Updates the visual slides to represent the currently
  173. * set indices.
  174. */
  175. function slide() {
  176. indexh = updateSlides( '#main>section', indexh );
  177. indexv = updateSlides( 'section.present>section', indexv );
  178. writeURL();
  179. }
  180. /**
  181. * Reads the current URL (hash) and navigates accordingly.
  182. */
  183. function readURL() {
  184. // Break the hash down to separate components
  185. var bits = window.location.hash.slice(2).split('/');
  186. // Read the index components of the hash
  187. indexh = bits[0] ? parseInt( bits[0] ) : 0;
  188. indexv = bits[1] ? parseInt( bits[1] ) : 0;
  189. navigateTo( indexh, indexv );
  190. }
  191. /**
  192. * Updates the page URL (hash) to reflect the current
  193. * navigational state.
  194. */
  195. function writeURL() {
  196. var url = '/';
  197. // Only include the minimum possible number of components in
  198. // the URL
  199. if( indexh > 0 || indexv > 0 ) url += indexh
  200. if( indexv > 0 ) url += '/' + indexv
  201. window.location.hash = url;
  202. }
  203. /**
  204. * Triggers a navigation to the specified indices.
  205. *
  206. * @param {Number} h The horizontal index of the slide to show
  207. * @param {Number} v The vertical index of the slide to show
  208. */
  209. function navigateTo( h, v ) {
  210. indexh = h === undefined ? indexh : h;
  211. indexv = v === undefined ? indexv : v;
  212. slide();
  213. }
  214. function navigateLeft() {
  215. indexh --;
  216. indexv = 0;
  217. slide();
  218. }
  219. function navigateRight() {
  220. indexh ++;
  221. indexv = 0;
  222. slide();
  223. }
  224. function navigateUp() {
  225. indexv --;
  226. slide();
  227. }
  228. function navigateDown() {
  229. indexv ++;
  230. slide();
  231. }
  232. // Initialize the program. Done right before returning to ensure
  233. // that any inline variable definitions are available to all
  234. // functions
  235. initialize();
  236. // Expose some methods publicly
  237. return {
  238. navigateTo: navigateTo,
  239. navigateLeft: navigateLeft,
  240. navigateRight: navigateRight,
  241. navigateUp: navigateUp,
  242. navigateDown: navigateDown
  243. };
  244. })();