reveal.js 19 KB

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