reveal.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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. * Reveal.js is an easy to use HTML based slideshow enhanced by
  24. * sexy CSS 3D transforms.
  25. *
  26. * Slides are given unique hash based URL's so that they can be
  27. * opened directly. I didn't use the HTML5 History API for this
  28. * as it would have required the addition of server side rewrite
  29. * rules and hence require more effort for anyone to set up.
  30. *
  31. * Public facing methods:
  32. * - Reveal.initialize( { ... options ... } );
  33. * - Reveal.navigateTo( indexh, indexv );
  34. * - Reveal.navigateLeft();
  35. * - Reveal.navigateRight();
  36. * - Reveal.navigateUp();
  37. * - Reveal.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. * version 0.4:
  52. * - Fixed broken links on touch devices.
  53. *
  54. * version 1.0:
  55. * - Added controls
  56. * - Added initialization options
  57. * - Reveal views in fragments
  58. * - Revamped, darker, theme
  59. * - Tweaked markup styles (a, em, strong, b, i, blockquote, q, pre, ul, ol)
  60. * - Support for themes at initialization (default/linear/concave)
  61. * - Code highlighting via highlight.js
  62. *
  63. * TODO:
  64. * - Touch/swipe interactions
  65. *
  66. * @author Hakim El Hattab
  67. * @version 1.0
  68. */
  69. var Reveal = (function(){
  70. var HORIZONTAL_SLIDES_SELECTOR = '#main>section',
  71. VERTICAL_SLIDES_SELECTOR = 'section.present>section',
  72. indexh = 0,
  73. indexv = 0,
  74. config = {},
  75. dom = {};
  76. /**
  77. * Activates the main program logic.
  78. */
  79. function initialize( options ) {
  80. // Gather references to DOM elements
  81. dom.controls = document.querySelector( '.controls' );
  82. dom.controlsLeft = document.querySelector( '.controls .left' );
  83. dom.controlsRight = document.querySelector( '.controls .right' );
  84. dom.controlsUp = document.querySelector( '.controls .up' );
  85. dom.controlsDown = document.querySelector( '.controls .down' );
  86. // Add event listeners
  87. document.addEventListener('keydown', onDocumentKeyDown, false);
  88. document.addEventListener('touchstart', onDocumentTouchStart, false);
  89. window.addEventListener('hashchange', onWindowHashChange, false);
  90. dom.controlsLeft.addEventListener('click', preventAndForward( navigateLeft ), false);
  91. dom.controlsRight.addEventListener('click', preventAndForward( navigateRight ), false);
  92. dom.controlsUp.addEventListener('click', preventAndForward( navigateUp ), false);
  93. dom.controlsDown.addEventListener('click', preventAndForward( navigateDown ), false);
  94. // Set default configuration
  95. config.rollingLinks = options.rollingLinks === undefined ? true : options.rollingLinks;
  96. config.controls = options.controls === undefined ? false : options.controls;
  97. config.theme = options.theme === undefined ? 'default' : options.theme;
  98. if( config.controls ) {
  99. dom.controls.style.display = 'block';
  100. }
  101. if( config.theme !== 'default' ) {
  102. document.body.classList.add( config.theme );
  103. }
  104. if( config.rollingLinks ) {
  105. // Add some 3D magic to our anchors
  106. linkify();
  107. }
  108. // Read the initial state of the URL (hash)
  109. readURL();
  110. }
  111. /**
  112. * Prevents an events defaults behavior calls the
  113. * specified delegate.
  114. */
  115. function preventAndForward( delegate ) {
  116. return function( event ) {
  117. event.preventDefault();
  118. delegate.call();
  119. }
  120. }
  121. /**
  122. * Handler for the document level 'keydown' event.
  123. *
  124. * @param {Object} event
  125. */
  126. function onDocumentKeyDown( event ) {
  127. if( event.keyCode >= 37 && event.keyCode <= 40 ) {
  128. switch( event.keyCode ) {
  129. case 37: navigateLeft(); break; // left
  130. case 39: navigateRight(); break; // right
  131. case 38: navigateUp(); break; // up
  132. case 40: navigateDown(); break; // down
  133. }
  134. slide();
  135. event.preventDefault();
  136. }
  137. }
  138. /**
  139. * Handler for the document level 'touchstart' event.
  140. *
  141. * This enables very basic tap interaction for touch
  142. * devices. Added mainly for performance testing of 3D
  143. * transforms on iOS but was so happily surprised with
  144. * how smoothly it runs so I left it in here. Apple +1
  145. *
  146. * @param {Object} event
  147. */
  148. function onDocumentTouchStart( event ) {
  149. // We're only interested in one point taps
  150. if (event.touches.length === 1) {
  151. // Never prevent taps on anchors and images
  152. if( event.target.tagName.toLowerCase() === 'a' || event.target.tagName.toLowerCase() === 'img' ) {
  153. return;
  154. }
  155. event.preventDefault();
  156. var point = {
  157. x: event.touches[0].clientX,
  158. y: event.touches[0].clientY
  159. };
  160. // Define the extent of the areas that may be tapped
  161. // to navigate
  162. var wt = window.innerWidth * 0.3;
  163. var ht = window.innerHeight * 0.3;
  164. if( point.x < wt ) {
  165. navigateLeft();
  166. }
  167. else if( point.x > window.innerWidth - wt ) {
  168. navigateRight();
  169. }
  170. else if( point.y < ht ) {
  171. navigateUp();
  172. }
  173. else if( point.y > window.innerHeight - ht ) {
  174. navigateDown();
  175. }
  176. slide();
  177. }
  178. }
  179. /**
  180. * Handler for the window level 'hashchange' event.
  181. *
  182. * @param {Object} event
  183. */
  184. function onWindowHashChange( event ) {
  185. readURL();
  186. }
  187. /**
  188. * Wrap all links in 3D goodness.
  189. */
  190. function linkify() {
  191. var supports3DTransforms = document.body.style['webkitPerspective'] !== undefined ||
  192. document.body.style['MozPerspective'] !== undefined ||
  193. document.body.style['perspective'] !== undefined;
  194. if( supports3DTransforms ) {
  195. var nodes = document.querySelectorAll( 'section a:not(.image)' );
  196. for( var i = 0, len = nodes.length; i < len; i++ ) {
  197. var node = nodes[i];
  198. if( !node.className || !node.className.match( /roll/g ) ) {
  199. node.className += ' roll';
  200. node.innerHTML = '<span data-title="'+ node.text +'">' + node.innerHTML + '</span>';
  201. }
  202. };
  203. }
  204. }
  205. /**
  206. * Updates one dimension of slides by showing the slide
  207. * with the specified index.
  208. *
  209. * @param {String} selector A CSS selector that will fetch
  210. * the group of slides we are working with
  211. * @param {Number} index The index of the slide that should be
  212. * shown
  213. *
  214. * @return {Number} The index of the slide that is now shown,
  215. * might differ from the passed in index if it was out of
  216. * bounds.
  217. */
  218. function updateSlides( selector, index ) {
  219. // Select all slides and convert the NodeList result to
  220. // an array
  221. var slides = Array.prototype.slice.call( document.querySelectorAll( selector ) );
  222. if( slides.length ) {
  223. // Enforce max and minimum index bounds
  224. index = Math.max(Math.min(index, slides.length - 1), 0);
  225. slides[index].setAttribute('class', 'present');
  226. // Any element previous to index is given the 'past' class
  227. slides.slice(0, index).map(function(element){
  228. element.setAttribute('class', 'past');
  229. });
  230. // Any element subsequent to index is given the 'future' class
  231. slides.slice(index + 1).map(function(element){
  232. element.setAttribute('class', 'future');
  233. });
  234. }
  235. else {
  236. // Since there are no slides we can't be anywhere beyond the
  237. // zeroth index
  238. index = 0;
  239. }
  240. return index;
  241. }
  242. /**
  243. * Updates the visual slides to represent the currently
  244. * set indices.
  245. */
  246. function slide() {
  247. indexh = updateSlides( HORIZONTAL_SLIDES_SELECTOR, indexh );
  248. indexv = updateSlides( VERTICAL_SLIDES_SELECTOR, indexv );
  249. updateControls();
  250. writeURL();
  251. }
  252. /**
  253. * Updates the state and link pointers of the controls.
  254. */
  255. function updateControls() {
  256. var routes = availableRoutes();
  257. // Remove the 'enabled' class from all directions
  258. [ dom.controlsLeft, dom.controlsRight, dom.controlsUp, dom.controlsDown ].forEach( function( node ) {
  259. node.classList.remove( 'enabled' );
  260. } )
  261. if( routes.left ) dom.controlsLeft.classList.add( 'enabled' );
  262. if( routes.right ) dom.controlsRight.classList.add( 'enabled' );
  263. if( routes.up ) dom.controlsUp.classList.add( 'enabled' );
  264. if( routes.down ) dom.controlsDown.classList.add( 'enabled' );
  265. }
  266. /**
  267. *
  268. */
  269. function availableRoutes() {
  270. var horizontalSlides = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR );
  271. var verticalSlides = document.querySelectorAll( VERTICAL_SLIDES_SELECTOR );
  272. return {
  273. left: indexh > 0,
  274. right: indexh < horizontalSlides.length - 1,
  275. up: indexv > 0,
  276. down: indexv < verticalSlides.length - 1
  277. };
  278. }
  279. /**
  280. * Reads the current URL (hash) and navigates accordingly.
  281. */
  282. function readURL() {
  283. // Break the hash down to separate components
  284. var bits = window.location.hash.slice(2).split('/');
  285. // Read the index components of the hash
  286. indexh = bits[0] ? parseInt( bits[0] ) : 0;
  287. indexv = bits[1] ? parseInt( bits[1] ) : 0;
  288. navigateTo( indexh, indexv );
  289. }
  290. /**
  291. * Updates the page URL (hash) to reflect the current
  292. * navigational state.
  293. */
  294. function writeURL() {
  295. var url = '/';
  296. // Only include the minimum possible number of components in
  297. // the URL
  298. if( indexh > 0 || indexv > 0 ) url += indexh;
  299. if( indexv > 0 ) url += '/' + indexv;
  300. window.location.hash = url;
  301. }
  302. /**
  303. * Navigate to the nexy slide fragment.
  304. *
  305. * @return {Boolean} true if there was a next fragment,
  306. * false otherwise
  307. */
  308. function nextFragment() {
  309. var fragments = document.querySelectorAll( '.present .fragment:not(.visible)' );
  310. if( fragments.length ) {
  311. fragments[0].classList.add( 'visible' );
  312. return true;
  313. }
  314. return false;
  315. }
  316. /**
  317. * Navigate to the previous slide fragment.
  318. *
  319. * @return {Boolean} true if there was a previous fragment,
  320. * false otherwise
  321. */
  322. function previousFragment() {
  323. var fragments = document.querySelectorAll( '.present .fragment.visible' );
  324. if( fragments.length ) {
  325. fragments[ fragments.length - 1 ].classList.remove( 'visible' );
  326. return true;
  327. }
  328. return false;
  329. }
  330. /**
  331. * Triggers a navigation to the specified indices.
  332. *
  333. * @param {Number} h The horizontal index of the slide to show
  334. * @param {Number} v The vertical index of the slide to show
  335. */
  336. function navigateTo( h, v ) {
  337. indexh = h === undefined ? indexh : h;
  338. indexv = v === undefined ? indexv : v;
  339. slide();
  340. }
  341. function navigateLeft() {
  342. // Prioritize hiding fragments
  343. if( previousFragment() === false ) {
  344. indexh --;
  345. indexv = 0;
  346. slide();
  347. }
  348. }
  349. function navigateRight() {
  350. // Prioritize revealing fragments
  351. if( nextFragment() === false ) {
  352. indexh ++;
  353. indexv = 0;
  354. slide();
  355. }
  356. }
  357. function navigateUp() {
  358. // Prioritize hiding fragments
  359. if( previousFragment() === false ) {
  360. indexv --;
  361. slide();
  362. }
  363. }
  364. function navigateDown() {
  365. // Prioritize revealing fragments
  366. if( nextFragment() === false ) {
  367. indexv ++;
  368. slide();
  369. }
  370. }
  371. // Expose some methods publicly
  372. return {
  373. initialize: initialize,
  374. navigateTo: navigateTo,
  375. navigateLeft: navigateLeft,
  376. navigateRight: navigateRight,
  377. navigateUp: navigateUp,
  378. navigateDown: navigateDown
  379. };
  380. })();