reveal.js 26 KB

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