reveal.js 18 KB

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