reveal.js 25 KB

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