reveal.js 36 KB

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