reveal.js 33 KB

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