reveal.js 27 KB

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