reveal.js 23 KB

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