reveal.js 22 KB

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