reveal.js 19 KB

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