reveal.js 19 KB

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