reveal.js 21 KB

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