reveal.js 32 KB

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