reveal.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  1. /*!
  2. * reveal.js 1.3
  3. * http://lab.hakim.se/reveal-js
  4. * MIT licensed
  5. *
  6. * Copyright (C) 2012 Hakim El Hattab, http://hakim.se
  7. */
  8. var Reveal = (function(){
  9. var HORIZONTAL_SLIDES_SELECTOR = '#reveal .slides>section',
  10. VERTICAL_SLIDES_SELECTOR = '#reveal .slides>section.present>section',
  11. IS_TOUCH_DEVICE = !!( 'ontouchstart' in window ),
  12. // The horizontal and verical index of the currently active slide
  13. indexh = 0,
  14. indexv = 0,
  15. // Configurations options, can be overridden at initialization time
  16. config = {
  17. controls: false,
  18. progress: false,
  19. history: false,
  20. loop: false,
  21. mouseWheel: true,
  22. rollingLinks: true,
  23. transition: 'default',
  24. theme: 'default',
  25. swipeDist: 30
  26. },
  27. // Slides may hold a data-state attribute which we pick up and apply
  28. // as a class to the body. This list contains the combined state of
  29. // all current slides.
  30. state = [],
  31. // Cached references to DOM elements
  32. dom = {},
  33. // Detect support for CSS 3D transforms
  34. supports3DTransforms = document.body.style['perspectiveProperty'] !== undefined ||
  35. document.body.style['WebkitPerspective'] !== undefined ||
  36. document.body.style['MozPerspective'] !== undefined ||
  37. document.body.style['msPerspective'] !== undefined ||
  38. document.body.style['OPerspective'] !== undefined,
  39. supports2DTransforms = document.body.style['transformProperty'] !== undefined ||
  40. document.body.style['WebkitTransform'] !== undefined ||
  41. document.body.style['MozTransform'] !== undefined ||
  42. document.body.style['msTransform'] !== undefined ||
  43. document.body.style['OTransform'] !== undefined,
  44. // Throttles mouse wheel navigation
  45. mouseWheelTimeout = 0,
  46. // Delays updates to the URL due to a Chrome thumbnailer bug
  47. writeURLTimeout = 0;
  48. /**
  49. * Starts up the slideshow by applying configuration
  50. * options and binding various events.
  51. */
  52. function initialize( options ) {
  53. if( !supports2DTransforms && !supports3DTransforms ) {
  54. document.body.setAttribute( 'class', 'no-transforms' );
  55. // If the browser doesn't support transforms we won't be
  56. // using JavaScript to control the presentation
  57. return;
  58. }
  59. // Cache references to DOM elements
  60. dom.wrapper = document.querySelector( '#reveal' );
  61. dom.progress = document.querySelector( '#reveal .progress' );
  62. dom.progressbar = document.querySelector( '#reveal .progress span' );
  63. dom.controls = document.querySelector( '#reveal .controls' );
  64. dom.controlsLeft = document.querySelector( '#reveal .controls .left' );
  65. dom.controlsRight = document.querySelector( '#reveal .controls .right' );
  66. dom.controlsUp = document.querySelector( '#reveal .controls .up' );
  67. dom.controlsDown = document.querySelector( '#reveal .controls .down' );
  68. // Bind all view events
  69. document.addEventListener( 'keydown', onDocumentKeyDown, false );
  70. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  71. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  72. document.addEventListener( 'touchend', onDocumentTouchEnd, false );
  73. window.addEventListener( 'hashchange', onWindowHashChange, false );
  74. dom.controlsLeft.addEventListener( 'click', preventAndForward( navigateLeft ), false );
  75. dom.controlsRight.addEventListener( 'click', preventAndForward( navigateRight ), false );
  76. dom.controlsUp.addEventListener( 'click', preventAndForward( navigateUp ), false );
  77. dom.controlsDown.addEventListener( 'click', preventAndForward( navigateDown ), false );
  78. // Copy options over to our config object
  79. extend( config, options );
  80. // Fall back on the 2D transform theme 'linear'
  81. if( supports3DTransforms === false ) {
  82. config.transition = 'linear';
  83. }
  84. if( config.controls ) {
  85. dom.controls.style.display = 'block';
  86. }
  87. if( config.progress ) {
  88. dom.progress.style.display = 'block';
  89. }
  90. if( config.transition !== 'default' ) {
  91. dom.wrapper.classList.add( config.transition );
  92. }
  93. if( config.theme !== 'default' ) {
  94. dom.wrapper.classList.add( config.theme );
  95. }
  96. if( config.mouseWheel ) {
  97. document.addEventListener( 'DOMMouseScroll', onDocumentMouseScroll, false ); // FF
  98. document.addEventListener( 'mousewheel', onDocumentMouseScroll, false );
  99. }
  100. if( config.rollingLinks ) {
  101. // Add some 3D magic to our anchors
  102. linkify();
  103. }
  104. // Read the initial hash
  105. readURL();
  106. // Set up hiding of the browser address bar
  107. if( navigator.userAgent.match( /(iphone|ipod|android)/i ) ) {
  108. // Give the page some scrollable overflow
  109. document.documentElement.style.overflow = 'scroll';
  110. document.body.style.height = '120%';
  111. // Events that should trigger the address bar to hide
  112. window.addEventListener( 'load', removeAddressBar, false );
  113. window.addEventListener( 'orientationchange', removeAddressBar, false );
  114. }
  115. }
  116. /**
  117. * Extend object a with the properties of object b.
  118. * If there's a conflict, object b takes precedence.
  119. */
  120. function extend( a, b ) {
  121. for( var i in b ) {
  122. a[ i ] = b[ i ];
  123. }
  124. }
  125. /**
  126. * Prevents an events defaults behavior calls the
  127. * specified delegate.
  128. *
  129. * @param {Function} delegate The method to call
  130. * after the wrapper has been executed
  131. */
  132. function preventAndForward( delegate ) {
  133. return function( event ) {
  134. event.preventDefault();
  135. delegate.call();
  136. }
  137. }
  138. /**
  139. * Causes the address bar to hide on mobile devices,
  140. * more vertical space ftw.
  141. */
  142. function removeAddressBar() {
  143. setTimeout( function() {
  144. window.scrollTo( 0, 1 );
  145. }, 0 );
  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. // Disregard the event if the target is editable or a
  156. // modifier is present
  157. if ( event.target.contentEditable != 'inherit' || event.shiftKey || event.altKey || event.ctrlKey || event.metaKey ) return;
  158. var triggered = false;
  159. switch( event.keyCode ) {
  160. // p, page up
  161. case 80: case 33: navigatePrev(); triggered = true; break;
  162. // n, page down
  163. case 78: case 34: navigateNext(); triggered = true; break;
  164. // h, left
  165. case 72: case 37: navigateLeft(); triggered = true; break;
  166. // l, right
  167. case 76: case 39: navigateRight(); triggered = true; break;
  168. // k, up
  169. case 75: case 38: navigateUp(); triggered = true; break;
  170. // j, down
  171. case 74: case 40: navigateDown(); triggered = true; break;
  172. // home
  173. case 36: navigateTo( 0 ); triggered = true; break;
  174. // end
  175. case 35: navigateTo( Number.MAX_VALUE ); triggered = true; break;
  176. // space
  177. case 32: overviewIsActive() ? deactivateOverview() : navigateNext(); triggered = true; break;
  178. // return
  179. case 13: if( overviewIsActive() ) { deactivateOverview(); triggered = true; } break;
  180. }
  181. if( triggered ) {
  182. event.preventDefault();
  183. }
  184. else if ( event.keyCode === 27 && supports3DTransforms ) {
  185. if( overviewIsActive() ) {
  186. deactivateOverview();
  187. }
  188. else {
  189. activateOverview();
  190. }
  191. event.preventDefault();
  192. }
  193. }
  194. /**
  195. * Handler for the document level 'touchstart' event.
  196. *
  197. * This enables very basic tap interaction for touch
  198. * devices. Added mainly for performance testing of 3D
  199. * transforms on iOS but was so happily surprised with
  200. * how smoothly it runs so I left it in here. Apple +1
  201. *
  202. * @param {Object} event
  203. */
  204. var touchStart = {}
  205. var gesture = false;
  206. function onDocumentTouchStart( event ) {
  207. touchStart = {
  208. x: event.touches[0].clientX,
  209. y: event.touches[0].clientY
  210. };
  211. if( event.target.tagName.toLowerCase() === 'a' || event.target.tagName.toLowerCase() === 'img' ) {
  212. } else {
  213. event.preventDefault();
  214. }
  215. }
  216. function onDocumentTouchMove( event ) {
  217. event.preventDefault();
  218. if(!gesture) {
  219. var touch = {
  220. x: event.touches[0].clientX,
  221. y: event.touches[0].clientY
  222. };
  223. if((touch.x - touchStart.x) > config.swipeDist){
  224. gesture = true;
  225. navigateLeft();
  226. } else if((touch.x - touchStart.x) < -config.swipeDist){
  227. gesture = true;
  228. navigateRight();
  229. } else if((touch.y - touchStart.y) > config.swipeDist){
  230. gesture = true;
  231. navigateUp();
  232. } else if((touch.y - touchStart.y) < -config.swipeDist){
  233. gesture = true;
  234. navigateDown();
  235. }
  236. }
  237. }
  238. function onDocumentTouchEnd( event ) {
  239. if(!gesture){
  240. // Never prevent taps on anchors and images
  241. if( event.target.tagName.toLowerCase() === 'a' || event.target.tagName.toLowerCase() === 'img' ) {
  242. return;
  243. }
  244. // Define the extent of the areas that may be tapped
  245. // to navigate
  246. var wt = window.innerWidth * 0.3;
  247. var ht = window.innerHeight * 0.3;
  248. if( touchStart.x < wt ) {
  249. navigateLeft();
  250. }
  251. else if( touchStart.x > window.innerWidth - wt ) {
  252. navigateRight();
  253. }
  254. else if( touchStart.y < ht ) {
  255. navigateUp();
  256. }
  257. else if( touchStart.y > window.innerHeight - ht ) {
  258. navigateDown();
  259. }
  260. }
  261. gesture = false;
  262. event.preventDefault();
  263. }
  264. /**
  265. * Handles mouse wheel scrolling, throttled to avoid
  266. * skipping multiple slides.
  267. */
  268. function onDocumentMouseScroll( event ){
  269. clearTimeout( mouseWheelTimeout );
  270. mouseWheelTimeout = setTimeout( function() {
  271. var delta = event.detail || -event.wheelDelta;
  272. if( delta > 0 ) {
  273. navigateNext();
  274. }
  275. else {
  276. navigatePrev();
  277. }
  278. }, 100 );
  279. }
  280. /**
  281. * Handler for the window level 'hashchange' event.
  282. *
  283. * @param {Object} event
  284. */
  285. function onWindowHashChange( event ) {
  286. readURL();
  287. }
  288. /**
  289. * Wrap all links in 3D goodness.
  290. */
  291. function linkify() {
  292. if( supports3DTransforms ) {
  293. var nodes = document.querySelectorAll( '#reveal .slides section a:not(.image)' );
  294. for( var i = 0, len = nodes.length; i < len; i++ ) {
  295. var node = nodes[i];
  296. if( node.textContent && !node.querySelector( 'img' ) && ( !node.className || !node.classList.contains( node, 'roll' ) ) ) {
  297. node.classList.add( 'roll' );
  298. node.innerHTML = '<span data-title="'+ node.text +'">' + node.innerHTML + '</span>';
  299. }
  300. };
  301. }
  302. }
  303. /**
  304. * Displays the overview of slides (quick nav) by
  305. * scaling down and arranging all slide elements.
  306. *
  307. * Experimental feature, might be dropped if perf
  308. * can't be improved.
  309. */
  310. function activateOverview() {
  311. dom.wrapper.classList.add( 'overview' );
  312. var horizontalSlides = Array.prototype.slice.call( document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) );
  313. for( var i = 0, len1 = horizontalSlides.length; i < len1; i++ ) {
  314. var hslide = horizontalSlides[i],
  315. htransform = 'translateZ(-2500px) translate(' + ( ( i - indexh ) * 105 ) + '%, 0%)';
  316. hslide.setAttribute( 'data-index-h', i );
  317. hslide.style.display = 'block';
  318. hslide.style.WebkitTransform = htransform;
  319. hslide.style.MozTransform = htransform;
  320. hslide.style.msTransform = htransform;
  321. hslide.style.OTransform = htransform;
  322. hslide.style.transform = htransform;
  323. if( !hslide.classList.contains( 'stack' ) ) {
  324. // Navigate to this slide on click
  325. hslide.addEventListener( 'click', onOverviewSlideClicked, true );
  326. }
  327. var verticalSlides = Array.prototype.slice.call( hslide.querySelectorAll( 'section' ) );
  328. for( var j = 0, len2 = verticalSlides.length; j < len2; j++ ) {
  329. var vslide = verticalSlides[j],
  330. vtransform = 'translate(0%, ' + ( ( j - indexv ) * 105 ) + '%)';
  331. vslide.setAttribute( 'data-index-h', i );
  332. vslide.setAttribute( 'data-index-v', j );
  333. vslide.style.display = 'block';
  334. vslide.style.WebkitTransform = vtransform;
  335. vslide.style.MozTransform = vtransform;
  336. vslide.style.msTransform = vtransform;
  337. vslide.style.OTransform = vtransform;
  338. vslide.style.transform = vtransform;
  339. // Navigate to this slide on click
  340. vslide.addEventListener( 'click', onOverviewSlideClicked, true );
  341. }
  342. }
  343. }
  344. /**
  345. * Exits the slide overview and enters the currently
  346. * active slide.
  347. */
  348. function deactivateOverview() {
  349. dom.wrapper.classList.remove( 'overview' );
  350. var slides = Array.prototype.slice.call( document.querySelectorAll( '#reveal .slides section' ) );
  351. for( var i = 0, len = slides.length; i < len; i++ ) {
  352. var element = slides[i];
  353. // Resets all transforms to use the external styles
  354. element.style.WebkitTransform = '';
  355. element.style.MozTransform = '';
  356. element.style.msTransform = '';
  357. element.style.OTransform = '';
  358. element.style.transform = '';
  359. element.removeEventListener( 'click', onOverviewSlideClicked );
  360. }
  361. slide();
  362. }
  363. /**
  364. * Checks if the overview is currently active.
  365. *
  366. * @return {Boolean} true if the overview is active,
  367. * false otherwise
  368. */
  369. function overviewIsActive() {
  370. return dom.wrapper.classList.contains( 'overview' );
  371. }
  372. /**
  373. * Invoked when a slide is and we're in the overview.
  374. */
  375. function onOverviewSlideClicked( event ) {
  376. // TODO There's a bug here where the event listeners are not
  377. // removed after deactivating the overview.
  378. if( overviewIsActive() ) {
  379. event.preventDefault();
  380. deactivateOverview();
  381. indexh = this.getAttribute( 'data-index-h' );
  382. indexv = this.getAttribute( 'data-index-v' );
  383. slide();
  384. }
  385. }
  386. /**
  387. * Updates one dimension of slides by showing the slide
  388. * with the specified index.
  389. *
  390. * @param {String} selector A CSS selector that will fetch
  391. * the group of slides we are working with
  392. * @param {Number} index The index of the slide that should be
  393. * shown
  394. *
  395. * @return {Number} The index of the slide that is now shown,
  396. * might differ from the passed in index if it was out of
  397. * bounds.
  398. */
  399. function updateSlides( selector, index ) {
  400. // Select all slides and convert the NodeList result to
  401. // an array
  402. var slides = Array.prototype.slice.call( document.querySelectorAll( selector ) ),
  403. slidesLength = slides.length;
  404. if( slidesLength ) {
  405. // Should the index loop?
  406. if( config.loop ) {
  407. index %= slidesLength;
  408. if( index < 0 ) {
  409. index = slidesLength + index;
  410. }
  411. }
  412. // Enforce max and minimum index bounds
  413. index = Math.max( Math.min( index, slidesLength - 1 ), 0 );
  414. for( var i = 0; i < slidesLength; i++ ) {
  415. var slide = slides[i];
  416. // Optimization; hide all slides that are three or more steps
  417. // away from the present slide
  418. if( overviewIsActive() === false ) {
  419. // The distance loops so that it measures 1 between the first
  420. // and last slides
  421. var distance = Math.abs( ( index - i ) % ( slidesLength - 3 ) ) || 0;
  422. slide.style.display = distance > 3 ? 'none' : 'block';
  423. }
  424. slides[i].classList.remove( 'past' );
  425. slides[i].classList.remove( 'present' );
  426. slides[i].classList.remove( 'future' );
  427. if( i < index ) {
  428. // Any element previous to index is given the 'past' class
  429. slides[i].classList.add( 'past' );
  430. }
  431. else if( i > index ) {
  432. // Any element subsequent to index is given the 'future' class
  433. slides[i].classList.add( 'future' );
  434. }
  435. // If this element contains vertical slides
  436. if( slide.querySelector( 'section' ) ) {
  437. slides[i].classList.add( 'stack' );
  438. }
  439. }
  440. // Mark the current slide as present
  441. slides[index].classList.add( 'present' );
  442. // If this slide has a state associated with it, add it
  443. // onto the current state of the deck
  444. var slideState = slides[index].getAttribute( 'data-state' );
  445. if( slideState ) {
  446. state = state.concat( slideState.split( ' ' ) );
  447. }
  448. }
  449. else {
  450. // Since there are no slides we can't be anywhere beyond the
  451. // zeroth index
  452. index = 0;
  453. }
  454. return index;
  455. }
  456. /**
  457. * Updates the visual slides to represent the currently
  458. * set indices.
  459. */
  460. function slide() {
  461. // Remember the state before this slide
  462. var stateBefore = state.concat();
  463. // Reset the state array
  464. state.length = 0;
  465. // Activate and transition to the new slide
  466. indexh = updateSlides( HORIZONTAL_SLIDES_SELECTOR, indexh );
  467. indexv = updateSlides( VERTICAL_SLIDES_SELECTOR, indexv );
  468. // Apply the new state
  469. stateLoop: for( var i = 0, len = state.length; i < len; i++ ) {
  470. // Check if this state existed on the previous slide. If it
  471. // did, we will avoid adding it repeatedly.
  472. for( var j = 0; j < stateBefore.length; j++ ) {
  473. if( stateBefore[j] === state[i] ) {
  474. stateBefore.splice( j, 1 );
  475. continue stateLoop;
  476. }
  477. }
  478. document.documentElement.classList.add( state[i] );
  479. // Dispatch custom event matching the state's name
  480. dispatchEvent( state[i] );
  481. }
  482. // Clean up the remaints of the previous state
  483. while( stateBefore.length ) {
  484. document.documentElement.classList.remove( stateBefore.pop() );
  485. }
  486. // Update progress if enabled
  487. if( config.progress ) {
  488. dom.progressbar.style.width = ( indexh / ( document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ).length - 1 ) ) * window.innerWidth + 'px';
  489. }
  490. // Close the overview if it's active
  491. if( overviewIsActive() ) {
  492. activateOverview();
  493. }
  494. updateControls();
  495. clearTimeout( writeURLTimeout );
  496. writeURLTimeout = setTimeout( writeURL, 1500 );
  497. // Dispatch an event notifying observers of the change in slide
  498. dispatchEvent( 'slidechanged', {
  499. 'indexh': indexh,
  500. 'indexv': indexv
  501. } );
  502. }
  503. /**
  504. * Updates the state and link pointers of the controls.
  505. */
  506. function updateControls() {
  507. var routes = availableRoutes();
  508. // Remove the 'enabled' class from all directions
  509. [ dom.controlsLeft, dom.controlsRight, dom.controlsUp, dom.controlsDown ].forEach( function( node ) {
  510. node.classList.remove( 'enabled' );
  511. } )
  512. if( routes.left ) dom.controlsLeft.classList.add( 'enabled' );
  513. if( routes.right ) dom.controlsRight.classList.add( 'enabled' );
  514. if( routes.up ) dom.controlsUp.classList.add( 'enabled' );
  515. if( routes.down ) dom.controlsDown.classList.add( 'enabled' );
  516. }
  517. /**
  518. * Determine what available routes there are for navigation.
  519. *
  520. * @return {Object} containing four booleans: left/right/up/down
  521. */
  522. function availableRoutes() {
  523. var horizontalSlides = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR );
  524. var verticalSlides = document.querySelectorAll( VERTICAL_SLIDES_SELECTOR );
  525. return {
  526. left: indexh > 0,
  527. right: indexh < horizontalSlides.length - 1,
  528. up: indexv > 0,
  529. down: indexv < verticalSlides.length - 1
  530. };
  531. }
  532. /**
  533. * Reads the current URL (hash) and navigates accordingly.
  534. */
  535. function readURL() {
  536. // Break the hash down to separate components
  537. var bits = window.location.hash.slice(2).split('/');
  538. // Read the index components of the hash
  539. indexh = parseInt( bits[0] ) || 0 ;
  540. indexv = parseInt( bits[1] ) || 0 ;
  541. navigateTo( indexh, indexv );
  542. }
  543. /**
  544. * Updates the page URL (hash) to reflect the current
  545. * state.
  546. */
  547. function writeURL() {
  548. if( config.history ) {
  549. var url = '/';
  550. // Only include the minimum possible number of components in
  551. // the URL
  552. if( indexh > 0 || indexv > 0 ) url += indexh;
  553. if( indexv > 0 ) url += '/' + indexv;
  554. window.location.hash = url;
  555. }
  556. }
  557. /**
  558. * Dispatches an event of the specified type from the
  559. * #reveal DOM element.
  560. */
  561. function dispatchEvent( type, properties ) {
  562. var event = document.createEvent( "HTMLEvents", 1, 2 );
  563. event.initEvent( type, true, true );
  564. extend( event, properties );
  565. dom.wrapper.dispatchEvent( event );
  566. }
  567. /**
  568. * Navigate to the next slide fragment.
  569. *
  570. * @return {Boolean} true if there was a next fragment,
  571. * false otherwise
  572. */
  573. function nextFragment() {
  574. // Vertical slides:
  575. if( document.querySelector( VERTICAL_SLIDES_SELECTOR + '.present' ) ) {
  576. var verticalFragments = document.querySelectorAll( VERTICAL_SLIDES_SELECTOR + '.present .fragment:not(.visible)' );
  577. if( verticalFragments.length ) {
  578. verticalFragments[0].classList.add( 'visible' );
  579. return true;
  580. }
  581. }
  582. // Horizontal slides:
  583. else {
  584. var horizontalFragments = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR + '.present .fragment:not(.visible)' );
  585. if( horizontalFragments.length ) {
  586. horizontalFragments[0].classList.add( 'visible' );
  587. return true;
  588. }
  589. }
  590. return false;
  591. }
  592. /**
  593. * Navigate to the previous slide fragment.
  594. *
  595. * @return {Boolean} true if there was a previous fragment,
  596. * false otherwise
  597. */
  598. function previousFragment() {
  599. // Vertical slides:
  600. if( document.querySelector( VERTICAL_SLIDES_SELECTOR + '.present' ) ) {
  601. var verticalFragments = document.querySelectorAll( VERTICAL_SLIDES_SELECTOR + '.present .fragment.visible' );
  602. if( verticalFragments.length ) {
  603. verticalFragments[ verticalFragments.length - 1 ].classList.remove( 'visible' );
  604. return true;
  605. }
  606. }
  607. // Horizontal slides:
  608. else {
  609. var horizontalFragments = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR + '.present .fragment.visible' );
  610. if( horizontalFragments.length ) {
  611. horizontalFragments[ horizontalFragments.length - 1 ].classList.remove( 'visible' );
  612. return true;
  613. }
  614. }
  615. return false;
  616. }
  617. /**
  618. * Triggers a navigation to the specified indices.
  619. *
  620. * @param {Number} h The horizontal index of the slide to show
  621. * @param {Number} v The vertical index of the slide to show
  622. */
  623. function navigateTo( h, v ) {
  624. indexh = h === undefined ? indexh : h;
  625. indexv = v === undefined ? indexv : v;
  626. slide();
  627. }
  628. function navigateLeft() {
  629. // Prioritize hiding fragments
  630. if( overviewIsActive() || previousFragment() === false ) {
  631. indexh --;
  632. indexv = 0;
  633. slide();
  634. }
  635. }
  636. function navigateRight() {
  637. // Prioritize revealing fragments
  638. if( overviewIsActive() || nextFragment() === false ) {
  639. indexh ++;
  640. indexv = 0;
  641. slide();
  642. }
  643. }
  644. function navigateUp() {
  645. // Prioritize hiding fragments
  646. if( overviewIsActive() || previousFragment() === false ) {
  647. indexv --;
  648. slide();
  649. }
  650. }
  651. function navigateDown() {
  652. // Prioritize revealing fragments
  653. if( overviewIsActive() || nextFragment() === false ) {
  654. indexv ++;
  655. slide();
  656. }
  657. }
  658. /**
  659. * Navigates backwards, prioritized in the following order:
  660. * 1) Previous fragment
  661. * 2) Previous vertical slide
  662. * 3) Previous horizontal slide
  663. */
  664. function navigatePrev() {
  665. // Prioritize revealing fragments
  666. if( previousFragment() === false ) {
  667. if( availableRoutes().up ) {
  668. navigateUp();
  669. }
  670. else {
  671. // Fetch the previous horizontal slide, if there is one
  672. var previousSlide = document.querySelector( '#reveal .slides>section.past:nth-child(' + indexh + ')' );
  673. if( previousSlide ) {
  674. indexv = ( previousSlide.querySelectorAll('section').length + 1 ) || 0;
  675. indexh --;
  676. slide();
  677. }
  678. }
  679. }
  680. }
  681. /**
  682. * Same as #navigatePrev() but navigates forwards.
  683. */
  684. function navigateNext() {
  685. // Prioritize revealing fragments
  686. if( nextFragment() === false ) {
  687. availableRoutes().down ? navigateDown() : navigateRight();
  688. }
  689. }
  690. // Expose some methods publicly
  691. return {
  692. initialize: initialize,
  693. navigateTo: navigateTo,
  694. navigateLeft: navigateLeft,
  695. navigateRight: navigateRight,
  696. navigateUp: navigateUp,
  697. navigateDown: navigateDown,
  698. // Forward event binding to the reveal DOM element
  699. addEventListener: function( type, listener, useCapture ) {
  700. ( dom.wrapper || document.querySelector( '#reveal' ) ).addEventListener( type, listener, useCapture );
  701. },
  702. removeEventListener: function( type, listener, useCapture ) {
  703. ( dom.wrapper || document.querySelector( '#reveal' ) ).removeEventListener( type, listener, useCapture );
  704. }
  705. };
  706. })();