reveal.js 14 KB

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