reveal.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  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. * - Slide overview available via SPACE
  65. * - Added 'transition' option for specifying transition styles
  66. * - Added 'theme' option for specifying UI styles
  67. * - Slides that contain nested-slides are given the 'stack' class
  68. *
  69. * TODO:
  70. * - Touch/swipe interactions
  71. * - Presentation overview via keyboard shortcut
  72. *
  73. * @author Hakim El Hattab | http://hakim.se
  74. * @version 1.1
  75. */
  76. var Reveal = (function(){
  77. var HORIZONTAL_SLIDES_SELECTOR = '#main>section',
  78. VERTICAL_SLIDES_SELECTOR = 'section.present>section',
  79. // The horizontal and verical index of the currently active slide
  80. indexh = 0,
  81. indexv = 0,
  82. // Configurations options, including;
  83. // > {Boolean} controls
  84. // > {Boolean} progress
  85. // > {String} theme
  86. // > {String} transition
  87. // > {Boolean} rollingLinks
  88. config = {},
  89. // Cached references to DOM elements
  90. dom = {},
  91. // Detect support for CSS 3D transforms
  92. supports3DTransforms = document.body.style['perspectiveProperty'] !== undefined ||
  93. document.body.style['WebkitPerspective'] !== undefined ||
  94. document.body.style['MozPerspective'] !== undefined ||
  95. document.body.style['msTransform'] !== undefined;
  96. /**
  97. * Starts up the slideshow by applying configuration
  98. * options and binding various events.
  99. */
  100. function initialize( options ) {
  101. // Cache references to DOM elements
  102. dom.progress = document.querySelector( 'body>.progress' );
  103. dom.progressbar = document.querySelector( 'body>.progress span' );
  104. dom.controls = document.querySelector( '.controls' );
  105. dom.controlsLeft = document.querySelector( '.controls .left' );
  106. dom.controlsRight = document.querySelector( '.controls .right' );
  107. dom.controlsUp = document.querySelector( '.controls .up' );
  108. dom.controlsDown = document.querySelector( '.controls .down' );
  109. // Bind all view events
  110. document.addEventListener('keydown', onDocumentKeyDown, false);
  111. document.addEventListener('touchstart', onDocumentTouchStart, false);
  112. window.addEventListener('hashchange', onWindowHashChange, false);
  113. dom.controlsLeft.addEventListener('click', preventAndForward( navigateLeft ), false);
  114. dom.controlsRight.addEventListener('click', preventAndForward( navigateRight ), false);
  115. dom.controlsUp.addEventListener('click', preventAndForward( navigateUp ), false);
  116. dom.controlsDown.addEventListener('click', preventAndForward( navigateDown ), false);
  117. // Fall back on default options
  118. config.rollingLinks = options.rollingLinks === undefined ? true : options.rollingLinks;
  119. config.controls = options.controls === undefined ? false : options.controls;
  120. config.progress = options.progress === undefined ? false : options.progress;
  121. config.transition = options.transition === undefined ? 'default' : options.transition;
  122. config.theme = options.theme === undefined ? 'default' : options.theme;
  123. // Fall back on the 2D transform theme 'linear'
  124. if( supports3DTransforms === false ) {
  125. config.transition = 'linear';
  126. }
  127. if( config.controls ) {
  128. dom.controls.style.display = 'block';
  129. }
  130. if( config.progress ) {
  131. dom.progress.style.display = 'block';
  132. }
  133. if( config.transition !== 'default' ) {
  134. document.body.classList.add( config.transition );
  135. }
  136. if( config.theme !== 'default' ) {
  137. document.body.classList.add( config.theme );
  138. }
  139. if( config.rollingLinks ) {
  140. // Add some 3D magic to our anchors
  141. linkify();
  142. }
  143. // Read the initial hash
  144. readURL();
  145. }
  146. /**
  147. * Prevents an events defaults behavior calls the
  148. * specified delegate.
  149. *
  150. * @param {Function} delegate The method to call
  151. * after the wrapper has been executed
  152. */
  153. function preventAndForward( delegate ) {
  154. return function( event ) {
  155. event.preventDefault();
  156. delegate.call();
  157. }
  158. }
  159. /**
  160. * Handler for the document level 'keydown' event.
  161. *
  162. * @param {Object} event
  163. */
  164. function onDocumentKeyDown( event ) {
  165. // FFT: Use document.querySelector( ':focus' ) === null
  166. // instead of checking contentEditable?
  167. if( event.target.contentEditable === 'inherit' ) {
  168. if( event.keyCode >= 37 && event.keyCode <= 40 ) {
  169. switch( event.keyCode ) {
  170. case 37: navigateLeft(); break; // left
  171. case 39: navigateRight(); break; // right
  172. case 38: navigateUp(); break; // up
  173. case 40: navigateDown(); break; // down
  174. }
  175. slide();
  176. event.preventDefault();
  177. }
  178. // Space bar
  179. else if ( event.keyCode === 32 && supports3DTransforms ) {
  180. if( overviewIsActive() ) {
  181. deactivateOverview();
  182. }
  183. else {
  184. activateOverview();
  185. }
  186. }
  187. }
  188. }
  189. /**
  190. * Handler for the document level 'touchstart' event.
  191. *
  192. * This enables very basic tap interaction for touch
  193. * devices. Added mainly for performance testing of 3D
  194. * transforms on iOS but was so happily surprised with
  195. * how smoothly it runs so I left it in here. Apple +1
  196. *
  197. * @param {Object} event
  198. */
  199. function onDocumentTouchStart( event ) {
  200. // We're only interested in one point taps
  201. if (event.touches.length === 1) {
  202. // Never prevent taps on anchors and images
  203. if( event.target.tagName.toLowerCase() === 'a' || event.target.tagName.toLowerCase() === 'img' ) {
  204. return;
  205. }
  206. event.preventDefault();
  207. var point = {
  208. x: event.touches[0].clientX,
  209. y: event.touches[0].clientY
  210. };
  211. // Define the extent of the areas that may be tapped
  212. // to navigate
  213. var wt = window.innerWidth * 0.3;
  214. var ht = window.innerHeight * 0.3;
  215. if( point.x < wt ) {
  216. navigateLeft();
  217. }
  218. else if( point.x > window.innerWidth - wt ) {
  219. navigateRight();
  220. }
  221. else if( point.y < ht ) {
  222. navigateUp();
  223. }
  224. else if( point.y > window.innerHeight - ht ) {
  225. navigateDown();
  226. }
  227. slide();
  228. }
  229. }
  230. /**
  231. * Handler for the window level 'hashchange' event.
  232. *
  233. * @param {Object} event
  234. */
  235. function onWindowHashChange( event ) {
  236. readURL();
  237. }
  238. /**
  239. * Wrap all links in 3D goodness.
  240. */
  241. function linkify() {
  242. if( supports3DTransforms ) {
  243. var nodes = document.querySelectorAll( 'section a:not(.image)' );
  244. for( var i = 0, len = nodes.length; i < len; i++ ) {
  245. var node = nodes[i];
  246. if( node.textContent && ( !node.className || !node.className.match( /roll/g ) ) ) {
  247. node.className += ' roll';
  248. node.innerHTML = '<span data-title="'+ node.text +'">' + node.innerHTML + '</span>';
  249. }
  250. };
  251. }
  252. }
  253. /**
  254. * Displays the overview of slides (quick nav) by
  255. * scaling down and arranging all slide elements.
  256. *
  257. * Experimental feature, might be dropped if perf
  258. * can't be improved.
  259. */
  260. function activateOverview() {
  261. document.body.classList.add( 'overview' );
  262. var horizontalSlides = Array.prototype.slice.call( document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) );
  263. for( var i = 0, len1 = horizontalSlides.length; i < len1; i++ ) {
  264. var hslide = horizontalSlides[i],
  265. htransform = 'translateZ(-2500px) translate(' + ( ( i - indexh ) * 105 ) + '%, 0%)';
  266. hslide.setAttribute( 'data-index-h', i );
  267. hslide.style.display = 'block';
  268. hslide.style.WebkitTransform = htransform;
  269. hslide.style.MozTransform = htransform;
  270. hslide.style.msTransform = htransform;
  271. hslide.style.OTransform = htransform;
  272. hslide.style.transform = htransform;
  273. // Navigate to this slide on click
  274. hslide.addEventListener( 'click', onOverviewSlideClicked, true );
  275. var verticalSlides = Array.prototype.slice.call( hslide.querySelectorAll( 'section' ) );
  276. for( var j = 0, len2 = verticalSlides.length; j < len2; j++ ) {
  277. var vslide = verticalSlides[j],
  278. vtransform = 'translate(0%, ' + ( ( j - indexv ) * 105 ) + '%)';
  279. vslide.setAttribute( 'data-index-h', i );
  280. vslide.setAttribute( 'data-index-v', j );
  281. vslide.style.display = 'block';
  282. vslide.style.WebkitTransform = vtransform;
  283. vslide.style.MozTransform = vtransform;
  284. vslide.style.msTransform = vtransform;
  285. vslide.style.OTransform = vtransform;
  286. vslide.style.transform = vtransform;
  287. // Navigate to this slide on click
  288. vslide.addEventListener( 'click', onOverviewSlideClicked, true );
  289. }
  290. }
  291. }
  292. /**
  293. * Exits the slide overview and enters the currently
  294. * active slide.
  295. */
  296. function deactivateOverview() {
  297. document.body.classList.remove( 'overview' );
  298. var slides = Array.prototype.slice.call( document.querySelectorAll( '#main section' ) );
  299. for( var i = 0, len = slides.length; i < len; i++ ) {
  300. var element = slides[i];
  301. // Resets all transforms to use the external styles
  302. element.style.WebkitTransform = '';
  303. element.style.MozTransform = '';
  304. element.style.msTransform = '';
  305. element.style.OTransform = '';
  306. element.style.transform = '';
  307. element.removeEventListener( 'click', onOverviewSlideClicked );
  308. }
  309. slide();
  310. }
  311. /**
  312. * Checks if the overview is currently active.
  313. *
  314. * @return {Boolean} true if the overview is active,
  315. * false otherwise
  316. */
  317. function overviewIsActive() {
  318. return document.body.classList.contains( 'overview' );
  319. }
  320. /**
  321. * Invoked when a slide is and we're in the overview.
  322. */
  323. function onOverviewSlideClicked( event ) {
  324. // TODO There's a bug here where the event listeners are not
  325. // removed after deactivating the overview.
  326. if( overviewIsActive() ) {
  327. event.preventDefault();
  328. deactivateOverview();
  329. indexh = this.getAttribute( 'data-index-h' );
  330. indexv = this.getAttribute( 'data-index-v' );
  331. slide();
  332. }
  333. }
  334. /**
  335. * Updates one dimension of slides by showing the slide
  336. * with the specified index.
  337. *
  338. * @param {String} selector A CSS selector that will fetch
  339. * the group of slides we are working with
  340. * @param {Number} index The index of the slide that should be
  341. * shown
  342. *
  343. * @return {Number} The index of the slide that is now shown,
  344. * might differ from the passed in index if it was out of
  345. * bounds.
  346. */
  347. function updateSlides( selector, index ) {
  348. // Select all slides and convert the NodeList result to
  349. // an array
  350. var slides = Array.prototype.slice.call( document.querySelectorAll( selector ) );
  351. if( slides.length ) {
  352. // Enforce max and minimum index bounds
  353. index = Math.max(Math.min(index, slides.length - 1), 0);
  354. slides[index].setAttribute('class', 'present');
  355. for( var i = 0; i < slides.length; i++ ) {
  356. var slide = slides[i];
  357. // Optimization; hide all slides that are three or more steps
  358. // away from the present slide
  359. if( overviewIsActive() === false ) {
  360. slide.style.display = Math.abs( index - i ) > 3 ? 'none' : 'block';
  361. }
  362. if( i < index ) {
  363. // Any element previous to index is given the 'past' class
  364. slide.setAttribute('class', 'past');
  365. }
  366. else if( i > index ) {
  367. // Any element subsequent to index is given the 'future' class
  368. slide.setAttribute('class', 'future');
  369. }
  370. // If this element contains vertical slides
  371. if( slide.querySelector( 'section' ) ) {
  372. slide.classList.add( 'stack' );
  373. }
  374. }
  375. }
  376. else {
  377. // Since there are no slides we can't be anywhere beyond the
  378. // zeroth index
  379. index = 0;
  380. }
  381. return index;
  382. }
  383. /**
  384. * Updates the visual slides to represent the currently
  385. * set indices.
  386. */
  387. function slide() {
  388. indexh = updateSlides( HORIZONTAL_SLIDES_SELECTOR, indexh );
  389. indexv = updateSlides( VERTICAL_SLIDES_SELECTOR, indexv );
  390. // Update progress if enabled
  391. if( config.progress ) {
  392. dom.progressbar.style.width = ( indexh / ( document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ).length - 1 ) ) * window.innerWidth + 'px';
  393. }
  394. // Close the overview if it's active
  395. if( overviewIsActive() ) {
  396. activateOverview();
  397. }
  398. updateControls();
  399. writeURL();
  400. }
  401. /**
  402. * Updates the state and link pointers of the controls.
  403. */
  404. function updateControls() {
  405. var routes = availableRoutes();
  406. // Remove the 'enabled' class from all directions
  407. [ dom.controlsLeft, dom.controlsRight, dom.controlsUp, dom.controlsDown ].forEach( function( node ) {
  408. node.classList.remove( 'enabled' );
  409. } )
  410. if( routes.left ) dom.controlsLeft.classList.add( 'enabled' );
  411. if( routes.right ) dom.controlsRight.classList.add( 'enabled' );
  412. if( routes.up ) dom.controlsUp.classList.add( 'enabled' );
  413. if( routes.down ) dom.controlsDown.classList.add( 'enabled' );
  414. }
  415. /**
  416. * Determine what available routes there are for navigation.
  417. *
  418. * @return {Object} containing four booleans: left/right/up/down
  419. */
  420. function availableRoutes() {
  421. var horizontalSlides = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR );
  422. var verticalSlides = document.querySelectorAll( VERTICAL_SLIDES_SELECTOR );
  423. return {
  424. left: indexh > 0,
  425. right: indexh < horizontalSlides.length - 1,
  426. up: indexv > 0,
  427. down: indexv < verticalSlides.length - 1
  428. };
  429. }
  430. /**
  431. * Reads the current URL (hash) and navigates accordingly.
  432. */
  433. function readURL() {
  434. // Break the hash down to separate components
  435. var bits = window.location.hash.slice(2).split('/');
  436. // Read the index components of the hash
  437. indexh = bits[0] ? parseInt( bits[0] ) : 0;
  438. indexv = bits[1] ? parseInt( bits[1] ) : 0;
  439. navigateTo( indexh, indexv );
  440. }
  441. /**
  442. * Updates the page URL (hash) to reflect the current
  443. * state.
  444. */
  445. function writeURL() {
  446. var url = '/';
  447. // Only include the minimum possible number of components in
  448. // the URL
  449. if( indexh > 0 || indexv > 0 ) url += indexh;
  450. if( indexv > 0 ) url += '/' + indexv;
  451. window.location.hash = url;
  452. }
  453. /**
  454. * Navigate to the next slide fragment.
  455. *
  456. * @return {Boolean} true if there was a next fragment,
  457. * false otherwise
  458. */
  459. function nextFragment() {
  460. // Vertical slides:
  461. if( document.querySelector( VERTICAL_SLIDES_SELECTOR + '.present' ) ) {
  462. var verticalFragments = document.querySelectorAll( VERTICAL_SLIDES_SELECTOR + '.present .fragment:not(.visible)' );
  463. if( verticalFragments.length ) {
  464. verticalFragments[0].classList.add( 'visible' );
  465. return true;
  466. }
  467. }
  468. // Horizontal slides:
  469. else {
  470. var horizontalFragments = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR + '.present .fragment:not(.visible)' );
  471. if( horizontalFragments.length ) {
  472. horizontalFragments[0].classList.add( 'visible' );
  473. return true;
  474. }
  475. }
  476. return false;
  477. }
  478. /**
  479. * Navigate to the previous slide fragment.
  480. *
  481. * @return {Boolean} true if there was a previous fragment,
  482. * false otherwise
  483. */
  484. function previousFragment() {
  485. // Vertical slides:
  486. if( document.querySelector( VERTICAL_SLIDES_SELECTOR + '.present' ) ) {
  487. var verticalFragments = document.querySelectorAll( VERTICAL_SLIDES_SELECTOR + '.present .fragment.visible' );
  488. if( verticalFragments.length ) {
  489. verticalFragments[ verticalFragments.length - 1 ].classList.remove( 'visible' );
  490. return true;
  491. }
  492. }
  493. // Horizontal slides:
  494. else {
  495. var horizontalFragments = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR + '.present .fragment.visible' );
  496. if( horizontalFragments.length ) {
  497. horizontalFragments[ horizontalFragments.length - 1 ].classList.remove( 'visible' );
  498. return true;
  499. }
  500. }
  501. return false;
  502. }
  503. /**
  504. * Triggers a navigation to the specified indices.
  505. *
  506. * @param {Number} h The horizontal index of the slide to show
  507. * @param {Number} v The vertical index of the slide to show
  508. */
  509. function navigateTo( h, v ) {
  510. indexh = h === undefined ? indexh : h;
  511. indexv = v === undefined ? indexv : v;
  512. slide();
  513. }
  514. function navigateLeft() {
  515. // Prioritize hiding fragments
  516. if( overviewIsActive() || previousFragment() === false ) {
  517. indexh --;
  518. indexv = 0;
  519. slide();
  520. }
  521. }
  522. function navigateRight() {
  523. // Prioritize revealing fragments
  524. if( overviewIsActive() || nextFragment() === false ) {
  525. indexh ++;
  526. indexv = 0;
  527. slide();
  528. }
  529. }
  530. function navigateUp() {
  531. // Prioritize hiding fragments
  532. if( overviewIsActive() || previousFragment() === false ) {
  533. indexv --;
  534. slide();
  535. }
  536. }
  537. function navigateDown() {
  538. // Prioritize revealing fragments
  539. if( overviewIsActive() || nextFragment() === false ) {
  540. indexv ++;
  541. slide();
  542. }
  543. }
  544. // Expose some methods publicly
  545. return {
  546. initialize: initialize,
  547. navigateTo: navigateTo,
  548. navigateLeft: navigateLeft,
  549. navigateRight: navigateRight,
  550. navigateUp: navigateUp,
  551. navigateDown: navigateDown
  552. };
  553. })();