reveal.js 36 KB

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