reveal.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  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. swipeDistance: 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. // We're only interested in one point taps
  208. if (event.touches.length === 1) {
  209. // Never prevent taps on anchors and images
  210. if( event.target.tagName.toLowerCase() === 'a' || event.target.tagName.toLowerCase() === 'img' ) {
  211. return;
  212. }
  213. event.preventDefault();
  214. touchStart = {
  215. x: event.touches[0].clientX,
  216. y: event.touches[0].clientY
  217. };
  218. slide();
  219. }
  220. }
  221. function onDocumentTouchMove( event ) {
  222. event.preventDefault();
  223. if(!gesture) {
  224. var touch = {
  225. x: event.touches[0].clientX,
  226. y: event.touches[0].clientY
  227. };
  228. if((touch.x - touchStart.x) > config.swipeDistance){
  229. gesture = true;
  230. navigateLeft();
  231. } else if((touch.x - touchStart.x) < -config.swipeDistance){
  232. gesture = true;
  233. navigateRight();
  234. } else if((touch.y - touchStart.y) > config.swipeDistance){
  235. gesture = true;
  236. navigateUp();
  237. } else if((touch.y - touchStart.y) < -config.swipeDistance){
  238. gesture = true;
  239. navigateDown();
  240. }
  241. }
  242. }
  243. function onDocumentTouchEnd( event ) {
  244. event.preventDefault();
  245. if(!gesture){//only check for control tap if no gesture is performed
  246. // Define the extent of the areas that may be tapped
  247. // to navigate
  248. var wt = window.innerWidth * 0.3;
  249. var ht = window.innerHeight * 0.3;
  250. if( touchStart.x < wt ) {
  251. navigateLeft();
  252. }
  253. else if( touchStart.x > window.innerWidth - wt ) {
  254. navigateRight();
  255. }
  256. else if( touchStart.y < ht ) {
  257. navigateUp();
  258. }
  259. else if( touchStart.y > window.innerHeight - ht ) {
  260. navigateDown();
  261. }
  262. }
  263. gesture = false;
  264. }
  265. /**
  266. * Handles mouse wheel scrolling, throttled to avoid
  267. * skipping multiple slides.
  268. */
  269. function onDocumentMouseScroll( event ){
  270. clearTimeout( mouseWheelTimeout );
  271. mouseWheelTimeout = setTimeout( function() {
  272. var delta = event.detail || -event.wheelDelta;
  273. if( delta > 0 ) {
  274. navigateNext();
  275. }
  276. else {
  277. navigatePrev();
  278. }
  279. }, 100 );
  280. }
  281. /**
  282. * Handler for the window level 'hashchange' event.
  283. *
  284. * @param {Object} event
  285. */
  286. function onWindowHashChange( event ) {
  287. readURL();
  288. }
  289. /**
  290. * Wrap all links in 3D goodness.
  291. */
  292. function linkify() {
  293. if( supports3DTransforms ) {
  294. var nodes = document.querySelectorAll( '#reveal .slides section a:not(.image)' );
  295. for( var i = 0, len = nodes.length; i < len; i++ ) {
  296. var node = nodes[i];
  297. if( node.textContent && !node.querySelector( 'img' ) && ( !node.className || !node.classList.contains( node, 'roll' ) ) ) {
  298. node.classList.add( 'roll' );
  299. node.innerHTML = '<span data-title="'+ node.text +'">' + node.innerHTML + '</span>';
  300. }
  301. };
  302. }
  303. }
  304. /**
  305. * Displays the overview of slides (quick nav) by
  306. * scaling down and arranging all slide elements.
  307. *
  308. * Experimental feature, might be dropped if perf
  309. * can't be improved.
  310. */
  311. function activateOverview() {
  312. dom.wrapper.classList.add( 'overview' );
  313. var horizontalSlides = Array.prototype.slice.call( document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) );
  314. for( var i = 0, len1 = horizontalSlides.length; i < len1; i++ ) {
  315. var hslide = horizontalSlides[i],
  316. htransform = 'translateZ(-2500px) translate(' + ( ( i - indexh ) * 105 ) + '%, 0%)';
  317. hslide.setAttribute( 'data-index-h', i );
  318. hslide.style.display = 'block';
  319. hslide.style.WebkitTransform = htransform;
  320. hslide.style.MozTransform = htransform;
  321. hslide.style.msTransform = htransform;
  322. hslide.style.OTransform = htransform;
  323. hslide.style.transform = htransform;
  324. if( !hslide.classList.contains( 'stack' ) ) {
  325. // Navigate to this slide on click
  326. hslide.addEventListener( 'click', onOverviewSlideClicked, true );
  327. }
  328. var verticalSlides = Array.prototype.slice.call( hslide.querySelectorAll( 'section' ) );
  329. for( var j = 0, len2 = verticalSlides.length; j < len2; j++ ) {
  330. var vslide = verticalSlides[j],
  331. vtransform = 'translate(0%, ' + ( ( j - indexv ) * 105 ) + '%)';
  332. vslide.setAttribute( 'data-index-h', i );
  333. vslide.setAttribute( 'data-index-v', j );
  334. vslide.style.display = 'block';
  335. vslide.style.WebkitTransform = vtransform;
  336. vslide.style.MozTransform = vtransform;
  337. vslide.style.msTransform = vtransform;
  338. vslide.style.OTransform = vtransform;
  339. vslide.style.transform = vtransform;
  340. // Navigate to this slide on click
  341. vslide.addEventListener( 'click', onOverviewSlideClicked, true );
  342. }
  343. }
  344. }
  345. /**
  346. * Exits the slide overview and enters the currently
  347. * active slide.
  348. */
  349. function deactivateOverview() {
  350. dom.wrapper.classList.remove( 'overview' );
  351. var slides = Array.prototype.slice.call( document.querySelectorAll( '#reveal .slides section' ) );
  352. for( var i = 0, len = slides.length; i < len; i++ ) {
  353. var element = slides[i];
  354. // Resets all transforms to use the external styles
  355. element.style.WebkitTransform = '';
  356. element.style.MozTransform = '';
  357. element.style.msTransform = '';
  358. element.style.OTransform = '';
  359. element.style.transform = '';
  360. element.removeEventListener( 'click', onOverviewSlideClicked );
  361. }
  362. slide();
  363. }
  364. /**
  365. * Checks if the overview is currently active.
  366. *
  367. * @return {Boolean} true if the overview is active,
  368. * false otherwise
  369. */
  370. function overviewIsActive() {
  371. return dom.wrapper.classList.contains( 'overview' );
  372. }
  373. /**
  374. * Invoked when a slide is and we're in the overview.
  375. */
  376. function onOverviewSlideClicked( event ) {
  377. // TODO There's a bug here where the event listeners are not
  378. // removed after deactivating the overview.
  379. if( overviewIsActive() ) {
  380. event.preventDefault();
  381. deactivateOverview();
  382. indexh = this.getAttribute( 'data-index-h' );
  383. indexv = this.getAttribute( 'data-index-v' );
  384. slide();
  385. }
  386. }
  387. /**
  388. * Updates one dimension of slides by showing the slide
  389. * with the specified index.
  390. *
  391. * @param {String} selector A CSS selector that will fetch
  392. * the group of slides we are working with
  393. * @param {Number} index The index of the slide that should be
  394. * shown
  395. *
  396. * @return {Number} The index of the slide that is now shown,
  397. * might differ from the passed in index if it was out of
  398. * bounds.
  399. */
  400. function updateSlides( selector, index ) {
  401. // Select all slides and convert the NodeList result to
  402. // an array
  403. var slides = Array.prototype.slice.call( document.querySelectorAll( selector ) ),
  404. slidesLength = slides.length;
  405. if( slidesLength ) {
  406. // Should the index loop?
  407. if( config.loop ) {
  408. index %= slidesLength;
  409. if( index < 0 ) {
  410. index = slidesLength + index;
  411. }
  412. }
  413. // Enforce max and minimum index bounds
  414. index = Math.max( Math.min( index, slidesLength - 1 ), 0 );
  415. for( var i = 0; i < slidesLength; i++ ) {
  416. var slide = slides[i];
  417. // Optimization; hide all slides that are three or more steps
  418. // away from the present slide
  419. if( overviewIsActive() === false ) {
  420. // The distance loops so that it measures 1 between the first
  421. // and last slides
  422. var distance = Math.abs( ( index - i ) % ( slidesLength - 3 ) ) || 0;
  423. slide.style.display = distance > 3 ? 'none' : 'block';
  424. }
  425. slides[i].classList.remove( 'past' );
  426. slides[i].classList.remove( 'present' );
  427. slides[i].classList.remove( 'future' );
  428. if( i < index ) {
  429. // Any element previous to index is given the 'past' class
  430. slides[i].classList.add( 'past' );
  431. }
  432. else if( i > index ) {
  433. // Any element subsequent to index is given the 'future' class
  434. slides[i].classList.add( 'future' );
  435. }
  436. // If this element contains vertical slides
  437. if( slide.querySelector( 'section' ) ) {
  438. slides[i].classList.add( 'stack' );
  439. }
  440. }
  441. // Mark the current slide as present
  442. slides[index].classList.add( 'present' );
  443. // If this slide has a state associated with it, add it
  444. // onto the current state of the deck
  445. var slideState = slides[index].getAttribute( 'data-state' );
  446. if( slideState ) {
  447. state = state.concat( slideState.split( ' ' ) );
  448. }
  449. }
  450. else {
  451. // Since there are no slides we can't be anywhere beyond the
  452. // zeroth index
  453. index = 0;
  454. }
  455. return index;
  456. }
  457. /**
  458. * Updates the visual slides to represent the currently
  459. * set indices.
  460. */
  461. function slide() {
  462. // Remember the state before this slide
  463. var stateBefore = state.concat();
  464. // Reset the state array
  465. state.length = 0;
  466. // Activate and transition to the new slide
  467. indexh = updateSlides( HORIZONTAL_SLIDES_SELECTOR, indexh );
  468. indexv = updateSlides( VERTICAL_SLIDES_SELECTOR, indexv );
  469. // Apply the new state
  470. stateLoop: for( var i = 0, len = state.length; i < len; i++ ) {
  471. // Check if this state existed on the previous slide. If it
  472. // did, we will avoid adding it repeatedly.
  473. for( var j = 0; j < stateBefore.length; j++ ) {
  474. if( stateBefore[j] === state[i] ) {
  475. stateBefore.splice( j, 1 );
  476. continue stateLoop;
  477. }
  478. }
  479. document.documentElement.classList.add( state[i] );
  480. // Dispatch custom event matching the state's name
  481. dispatchEvent( state[i] );
  482. }
  483. // Clean up the remaints of the previous state
  484. while( stateBefore.length ) {
  485. document.documentElement.classList.remove( stateBefore.pop() );
  486. }
  487. // Update progress if enabled
  488. if( config.progress ) {
  489. dom.progressbar.style.width = ( indexh / ( document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ).length - 1 ) ) * window.innerWidth + 'px';
  490. }
  491. // Close the overview if it's active
  492. if( overviewIsActive() ) {
  493. activateOverview();
  494. }
  495. updateControls();
  496. clearTimeout( writeURLTimeout );
  497. writeURLTimeout = setTimeout( writeURL, 1500 );
  498. // Dispatch an event notifying observers of the change in slide
  499. dispatchEvent( 'slidechanged', {
  500. 'indexh': indexh,
  501. 'indexv': indexv
  502. } );
  503. }
  504. /**
  505. * Updates the state and link pointers of the controls.
  506. */
  507. function updateControls() {
  508. var routes = availableRoutes();
  509. // Remove the 'enabled' class from all directions
  510. [ dom.controlsLeft, dom.controlsRight, dom.controlsUp, dom.controlsDown ].forEach( function( node ) {
  511. node.classList.remove( 'enabled' );
  512. } )
  513. if( routes.left ) dom.controlsLeft.classList.add( 'enabled' );
  514. if( routes.right ) dom.controlsRight.classList.add( 'enabled' );
  515. if( routes.up ) dom.controlsUp.classList.add( 'enabled' );
  516. if( routes.down ) dom.controlsDown.classList.add( 'enabled' );
  517. }
  518. /**
  519. * Determine what available routes there are for navigation.
  520. *
  521. * @return {Object} containing four booleans: left/right/up/down
  522. */
  523. function availableRoutes() {
  524. var horizontalSlides = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR );
  525. var verticalSlides = document.querySelectorAll( VERTICAL_SLIDES_SELECTOR );
  526. return {
  527. left: indexh > 0,
  528. right: indexh < horizontalSlides.length - 1,
  529. up: indexv > 0,
  530. down: indexv < verticalSlides.length - 1
  531. };
  532. }
  533. /**
  534. * Reads the current URL (hash) and navigates accordingly.
  535. */
  536. function readURL() {
  537. // Break the hash down to separate components
  538. var bits = window.location.hash.slice(2).split('/');
  539. // Read the index components of the hash
  540. indexh = parseInt( bits[0] ) || 0 ;
  541. indexv = parseInt( bits[1] ) || 0 ;
  542. navigateTo( indexh, indexv );
  543. }
  544. /**
  545. * Updates the page URL (hash) to reflect the current
  546. * state.
  547. */
  548. function writeURL() {
  549. if( config.history ) {
  550. var url = '/';
  551. // Only include the minimum possible number of components in
  552. // the URL
  553. if( indexh > 0 || indexv > 0 ) url += indexh;
  554. if( indexv > 0 ) url += '/' + indexv;
  555. window.location.hash = url;
  556. }
  557. }
  558. /**
  559. * Dispatches an event of the specified type from the
  560. * #reveal DOM element.
  561. */
  562. function dispatchEvent( type, properties ) {
  563. var event = document.createEvent( "HTMLEvents", 1, 2 );
  564. event.initEvent( type, true, true );
  565. extend( event, properties );
  566. dom.wrapper.dispatchEvent( event );
  567. }
  568. /**
  569. * Navigate to the next slide fragment.
  570. *
  571. * @return {Boolean} true if there was a next fragment,
  572. * false otherwise
  573. */
  574. function nextFragment() {
  575. // Vertical slides:
  576. if( document.querySelector( VERTICAL_SLIDES_SELECTOR + '.present' ) ) {
  577. var verticalFragments = document.querySelectorAll( VERTICAL_SLIDES_SELECTOR + '.present .fragment:not(.visible)' );
  578. if( verticalFragments.length ) {
  579. verticalFragments[0].classList.add( 'visible' );
  580. return true;
  581. }
  582. }
  583. // Horizontal slides:
  584. else {
  585. var horizontalFragments = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR + '.present .fragment:not(.visible)' );
  586. if( horizontalFragments.length ) {
  587. horizontalFragments[0].classList.add( 'visible' );
  588. return true;
  589. }
  590. }
  591. return false;
  592. }
  593. /**
  594. * Navigate to the previous slide fragment.
  595. *
  596. * @return {Boolean} true if there was a previous fragment,
  597. * false otherwise
  598. */
  599. function previousFragment() {
  600. // Vertical slides:
  601. if( document.querySelector( VERTICAL_SLIDES_SELECTOR + '.present' ) ) {
  602. var verticalFragments = document.querySelectorAll( VERTICAL_SLIDES_SELECTOR + '.present .fragment.visible' );
  603. if( verticalFragments.length ) {
  604. verticalFragments[ verticalFragments.length - 1 ].classList.remove( 'visible' );
  605. return true;
  606. }
  607. }
  608. // Horizontal slides:
  609. else {
  610. var horizontalFragments = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR + '.present .fragment.visible' );
  611. if( horizontalFragments.length ) {
  612. horizontalFragments[ horizontalFragments.length - 1 ].classList.remove( 'visible' );
  613. return true;
  614. }
  615. }
  616. return false;
  617. }
  618. /**
  619. * Triggers a navigation to the specified indices.
  620. *
  621. * @param {Number} h The horizontal index of the slide to show
  622. * @param {Number} v The vertical index of the slide to show
  623. */
  624. function navigateTo( h, v ) {
  625. indexh = h === undefined ? indexh : h;
  626. indexv = v === undefined ? indexv : v;
  627. slide();
  628. }
  629. function navigateLeft() {
  630. // Prioritize hiding fragments
  631. if( overviewIsActive() || previousFragment() === false ) {
  632. indexh --;
  633. indexv = 0;
  634. slide();
  635. }
  636. }
  637. function navigateRight() {
  638. // Prioritize revealing fragments
  639. if( overviewIsActive() || nextFragment() === false ) {
  640. indexh ++;
  641. indexv = 0;
  642. slide();
  643. }
  644. }
  645. function navigateUp() {
  646. // Prioritize hiding fragments
  647. if( overviewIsActive() || previousFragment() === false ) {
  648. indexv --;
  649. slide();
  650. }
  651. }
  652. function navigateDown() {
  653. // Prioritize revealing fragments
  654. if( overviewIsActive() || nextFragment() === false ) {
  655. indexv ++;
  656. slide();
  657. }
  658. }
  659. /**
  660. * Navigates backwards, prioritized in the following order:
  661. * 1) Previous fragment
  662. * 2) Previous vertical slide
  663. * 3) Previous horizontal slide
  664. */
  665. function navigatePrev() {
  666. // Prioritize revealing fragments
  667. if( previousFragment() === false ) {
  668. if( availableRoutes().up ) {
  669. navigateUp();
  670. }
  671. else {
  672. // Fetch the previous horizontal slide, if there is one
  673. var previousSlide = document.querySelector( '#reveal .slides>section.past:nth-child(' + indexh + ')' );
  674. if( previousSlide ) {
  675. indexv = ( previousSlide.querySelectorAll('section').length + 1 ) || 0;
  676. indexh --;
  677. slide();
  678. }
  679. }
  680. }
  681. }
  682. /**
  683. * Same as #navigatePrev() but navigates forwards.
  684. */
  685. function navigateNext() {
  686. // Prioritize revealing fragments
  687. if( nextFragment() === false ) {
  688. availableRoutes().down ? navigateDown() : navigateRight();
  689. }
  690. }
  691. // Expose some methods publicly
  692. return {
  693. initialize: initialize,
  694. navigateTo: navigateTo,
  695. navigateLeft: navigateLeft,
  696. navigateRight: navigateRight,
  697. navigateUp: navigateUp,
  698. navigateDown: navigateDown,
  699. // Forward event binding to the reveal DOM element
  700. addEventListener: function( type, listener, useCapture ) {
  701. ( dom.wrapper || document.querySelector( '#reveal' ) ).addEventListener( type, listener, useCapture );
  702. },
  703. removeEventListener: function( type, listener, useCapture ) {
  704. ( dom.wrapper || document.querySelector( '#reveal' ) ).removeEventListener( type, listener, useCapture );
  705. }
  706. };
  707. })();