reveal.js 19 KB

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