reveal.js 29 KB

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