reveal.js 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336
  1. /*!
  2. * reveal.js
  3. * http://lab.hakim.se/reveal-js
  4. * MIT licensed
  5. *
  6. * Copyright (C) 2013 Hakim El Hattab, http://hakim.se
  7. */
  8. var Reveal = (function(){
  9. 'use strict';
  10. var SLIDES_SELECTOR = '.reveal .slides section',
  11. HORIZONTAL_SLIDES_SELECTOR = '.reveal .slides>section',
  12. VERTICAL_SLIDES_SELECTOR = '.reveal .slides>section.present>section',
  13. HOME_SLIDE_SELECTOR = '.reveal .slides>section:first-child',
  14. // Configurations defaults, can be overridden at initialization time
  15. config = {
  16. // The "normal" size of the presentation, aspect ratio will be preserved
  17. // when the presentation is scaled to fit different resolutions
  18. width: 960,
  19. height: 700,
  20. // Factor of the display size that should remain empty around the content
  21. margin: 0.1,
  22. // Bounds for smallest/largest possible scale to apply to content
  23. minScale: 0.2,
  24. maxScale: 1.0,
  25. // Display controls in the bottom right corner
  26. controls: true,
  27. // Display a presentation progress bar
  28. progress: true,
  29. // Push each slide change to the browser history
  30. history: false,
  31. // Enable keyboard shortcuts for navigation
  32. keyboard: true,
  33. // Enable the slide overview mode
  34. overview: true,
  35. // Vertical centring of slides
  36. center: true,
  37. // Enables touch navigation on devices with touch input
  38. touch: true,
  39. // Loop the presentation
  40. loop: false,
  41. // Change the presentation direction to be RTL
  42. rtl: false,
  43. // Turns fragments on and off globally
  44. fragments: true,
  45. // Number of milliseconds between automatically proceeding to the
  46. // next slide, disabled when set to 0, this value can be overwritten
  47. // by using a data-autoslide attribute on your slides
  48. autoSlide: 0,
  49. // Enable slide navigation via mouse wheel
  50. mouseWheel: false,
  51. // Apply a 3D roll to links on hover
  52. rollingLinks: true,
  53. // Theme (see /css/theme)
  54. theme: null,
  55. // Transition style
  56. transition: 'default', // default/cube/page/concave/zoom/linear/fade/none
  57. // Transition speed
  58. transitionSpeed: 'default', // default/fast/slow
  59. // Script dependencies to load
  60. dependencies: []
  61. },
  62. // The current auto-slide duration
  63. autoSlide = 0,
  64. // The horizontal and vertical index of the currently active slide
  65. indexh = 0,
  66. indexv = 0,
  67. // The previous and current slide HTML elements
  68. previousSlide,
  69. currentSlide,
  70. // Slides may hold a data-state attribute which we pick up and apply
  71. // as a class to the body. This list contains the combined state of
  72. // all current slides.
  73. state = [],
  74. // The current scale of the presentation (see width/height config)
  75. scale = 1,
  76. // Cached references to DOM elements
  77. dom = {},
  78. // Detect support for CSS 3D transforms
  79. supports3DTransforms = 'WebkitPerspective' in document.body.style ||
  80. 'MozPerspective' in document.body.style ||
  81. 'msPerspective' in document.body.style ||
  82. 'OPerspective' in document.body.style ||
  83. 'perspective' in document.body.style,
  84. // Detect support for CSS 2D transforms
  85. supports2DTransforms = 'WebkitTransform' in document.body.style ||
  86. 'MozTransform' in document.body.style ||
  87. 'msTransform' in document.body.style ||
  88. 'OTransform' in document.body.style ||
  89. 'transform' in document.body.style,
  90. // Throttles mouse wheel navigation
  91. mouseWheelTimeout = 0,
  92. // An interval used to automatically move on to the next slide
  93. autoSlideTimeout = 0,
  94. // Delays updates to the URL due to a Chrome thumbnailer bug
  95. writeURLTimeout = 0,
  96. // A delay used to activate the overview mode
  97. activateOverviewTimeout = 0,
  98. // A delay used to deactivate the overview mode
  99. deactivateOverviewTimeout = 0,
  100. // Flags if the interaction event listeners are bound
  101. eventsAreBound = false,
  102. // Holds information about the currently ongoing touch input
  103. touch = {
  104. startX: 0,
  105. startY: 0,
  106. startSpan: 0,
  107. startCount: 0,
  108. handled: false,
  109. threshold: 80
  110. };
  111. /**
  112. * Starts up the presentation if the client is capable.
  113. */
  114. function initialize( options ) {
  115. if( !supports2DTransforms && !supports3DTransforms ) {
  116. document.body.setAttribute( 'class', 'no-transforms' );
  117. // If the browser doesn't support core features we won't be
  118. // using JavaScript to control the presentation
  119. return;
  120. }
  121. // Force a layout when the whole page, incl fonts, has loaded
  122. window.addEventListener( 'load', layout, false );
  123. // Copy options over to our config object
  124. extend( config, options );
  125. // Hide the address bar in mobile browsers
  126. hideAddressBar();
  127. // Loads the dependencies and continues to #start() once done
  128. load();
  129. }
  130. /**
  131. * Finds and stores references to DOM elements which are
  132. * required by the presentation. If a required element is
  133. * not found, it is created.
  134. */
  135. function setupDOM() {
  136. // Cache references to key DOM elements
  137. dom.theme = document.querySelector( '#theme' );
  138. dom.wrapper = document.querySelector( '.reveal' );
  139. dom.slides = document.querySelector( '.reveal .slides' );
  140. // Background element
  141. if( !document.querySelector( '.reveal .background' ) ) {
  142. dom.background = document.createElement( 'div' );
  143. dom.background.classList.add( 'background' );
  144. dom.wrapper.appendChild( dom.background );
  145. }
  146. // Progress bar
  147. if( !dom.wrapper.querySelector( '.progress' ) ) {
  148. var progressElement = document.createElement( 'div' );
  149. progressElement.classList.add( 'progress' );
  150. progressElement.innerHTML = '<span></span>';
  151. dom.wrapper.appendChild( progressElement );
  152. }
  153. // Arrow controls
  154. if( !dom.wrapper.querySelector( '.controls' ) ) {
  155. var controlsElement = document.createElement( 'aside' );
  156. controlsElement.classList.add( 'controls' );
  157. controlsElement.innerHTML = '<div class="navigate-left"></div>' +
  158. '<div class="navigate-right"></div>' +
  159. '<div class="navigate-up"></div>' +
  160. '<div class="navigate-down"></div>';
  161. dom.wrapper.appendChild( controlsElement );
  162. }
  163. // State background element [DEPRECATED]
  164. if( !dom.wrapper.querySelector( '.state-background' ) ) {
  165. var stateBackgroundElement = document.createElement( 'div' );
  166. stateBackgroundElement.classList.add( 'state-background' );
  167. dom.wrapper.appendChild( stateBackgroundElement );
  168. }
  169. // Overlay graphic which is displayed during the paused mode
  170. if( !dom.wrapper.querySelector( '.pause-overlay' ) ) {
  171. var pausedElement = document.createElement( 'div' );
  172. pausedElement.classList.add( 'pause-overlay' );
  173. dom.wrapper.appendChild( pausedElement );
  174. }
  175. // Cache references to elements
  176. dom.progress = document.querySelector( '.reveal .progress' );
  177. dom.progressbar = document.querySelector( '.reveal .progress span' );
  178. if ( config.controls ) {
  179. dom.controls = document.querySelector( '.reveal .controls' );
  180. // There can be multiple instances of controls throughout the page
  181. dom.controlsLeft = toArray( document.querySelectorAll( '.navigate-left' ) );
  182. dom.controlsRight = toArray( document.querySelectorAll( '.navigate-right' ) );
  183. dom.controlsUp = toArray( document.querySelectorAll( '.navigate-up' ) );
  184. dom.controlsDown = toArray( document.querySelectorAll( '.navigate-down' ) );
  185. dom.controlsPrev = toArray( document.querySelectorAll( '.navigate-prev' ) );
  186. dom.controlsNext = toArray( document.querySelectorAll( '.navigate-next' ) );
  187. }
  188. }
  189. /**
  190. * Creates the slide background elements and appends them
  191. * to the background container.
  192. */
  193. function createBackgrounds() {
  194. // Clear prior backgrounds
  195. dom.background.innerHTML = '';
  196. dom.background.classList.add( 'no-transition' );
  197. // Helper method for creating a background element for the
  198. // given slide
  199. function _createBackground( slide, container ) {
  200. var background = slide.getAttribute( 'data-background' );
  201. var element = document.createElement( 'div' );
  202. if( background ) {
  203. // Auto-wrap image urls in url(...)
  204. if( /\.(png|jpg|jpeg|gif|bmp|)$/gi.test( background ) ) {
  205. element.style.backgroundImage = 'url('+ background +')';
  206. }
  207. else {
  208. element.style.background = background;
  209. }
  210. }
  211. container.appendChild( element );
  212. return element;
  213. }
  214. // Iterate over all horizontal slides
  215. toArray( document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) ).forEach( function( slideh ) {
  216. var backgroundStack = _createBackground( slideh, dom.background );
  217. // Iterate over all vertical slides
  218. toArray( slideh.querySelectorAll( 'section' ) ).forEach( function( slidev ) {
  219. _createBackground( slidev, backgroundStack );
  220. } );
  221. } );
  222. }
  223. /**
  224. * Hides the address bar if we're on a mobile device.
  225. */
  226. function hideAddressBar() {
  227. if( /iphone|ipod|android/gi.test( navigator.userAgent ) && !/crios/gi.test( navigator.userAgent ) ) {
  228. // Events that should trigger the address bar to hide
  229. window.addEventListener( 'load', removeAddressBar, false );
  230. window.addEventListener( 'orientationchange', removeAddressBar, false );
  231. }
  232. }
  233. /**
  234. * Loads the dependencies of reveal.js. Dependencies are
  235. * defined via the configuration option 'dependencies'
  236. * and will be loaded prior to starting/binding reveal.js.
  237. * Some dependencies may have an 'async' flag, if so they
  238. * will load after reveal.js has been started up.
  239. */
  240. function load() {
  241. var scripts = [],
  242. scriptsAsync = [];
  243. for( var i = 0, len = config.dependencies.length; i < len; i++ ) {
  244. var s = config.dependencies[i];
  245. // Load if there's no condition or the condition is truthy
  246. if( !s.condition || s.condition() ) {
  247. if( s.async ) {
  248. scriptsAsync.push( s.src );
  249. }
  250. else {
  251. scripts.push( s.src );
  252. }
  253. // Extension may contain callback functions
  254. if( typeof s.callback === 'function' ) {
  255. head.ready( s.src.match( /([\w\d_\-]*)\.?js$|[^\\\/]*$/i )[0], s.callback );
  256. }
  257. }
  258. }
  259. // Called once synchronous scripts finish loading
  260. function proceed() {
  261. if( scriptsAsync.length ) {
  262. // Load asynchronous scripts
  263. head.js.apply( null, scriptsAsync );
  264. }
  265. start();
  266. }
  267. if( scripts.length ) {
  268. head.ready( proceed );
  269. // Load synchronous scripts
  270. head.js.apply( null, scripts );
  271. }
  272. else {
  273. proceed();
  274. }
  275. }
  276. /**
  277. * Starts up reveal.js by binding input events and navigating
  278. * to the current URL deeplink if there is one.
  279. */
  280. function start() {
  281. // Make sure we've got all the DOM elements we need
  282. setupDOM();
  283. // Updates the presentation to match the current configuration values
  284. configure();
  285. // Read the initial hash
  286. readURL();
  287. // Notify listeners that the presentation is ready but use a 1ms
  288. // timeout to ensure it's not fired synchronously after #initialize()
  289. setTimeout( function() {
  290. dispatchEvent( 'ready', {
  291. 'indexh': indexh,
  292. 'indexv': indexv,
  293. 'currentSlide': currentSlide
  294. } );
  295. }, 1 );
  296. }
  297. /**
  298. * Applies the configuration settings from the config
  299. * object. May be called multiple times.
  300. */
  301. function configure( options ) {
  302. dom.wrapper.classList.remove( config.transition );
  303. // New config options may be passed when this method
  304. // is invoked through the API after initialization
  305. if( typeof options === 'object' ) extend( config, options );
  306. // Force linear transition based on browser capabilities
  307. if( supports3DTransforms === false ) config.transition = 'linear';
  308. dom.wrapper.classList.add( config.transition );
  309. dom.wrapper.setAttribute( 'data-transition-speed', config.transitionSpeed );
  310. if( dom.controls ) {
  311. dom.controls.style.display = ( config.controls && dom.controls ) ? 'block' : 'none';
  312. }
  313. if( dom.progress ) {
  314. dom.progress.style.display = ( config.progress && dom.progress ) ? 'block' : 'none';
  315. }
  316. if( config.rtl ) {
  317. dom.wrapper.classList.add( 'rtl' );
  318. }
  319. else {
  320. dom.wrapper.classList.remove( 'rtl' );
  321. }
  322. if( config.center ) {
  323. dom.wrapper.classList.add( 'center' );
  324. }
  325. else {
  326. dom.wrapper.classList.remove( 'center' );
  327. }
  328. if( config.mouseWheel ) {
  329. document.addEventListener( 'DOMMouseScroll', onDocumentMouseScroll, false ); // FF
  330. document.addEventListener( 'mousewheel', onDocumentMouseScroll, false );
  331. }
  332. else {
  333. document.removeEventListener( 'DOMMouseScroll', onDocumentMouseScroll, false ); // FF
  334. document.removeEventListener( 'mousewheel', onDocumentMouseScroll, false );
  335. }
  336. // 3D links
  337. if( config.rollingLinks ) {
  338. enable3DLinks();
  339. }
  340. else {
  341. disable3DLinks();
  342. }
  343. // Load the theme in the config, if it's not already loaded
  344. if( config.theme && dom.theme ) {
  345. var themeURL = dom.theme.getAttribute( 'href' );
  346. var themeFinder = /[^\/]*?(?=\.css)/;
  347. var themeName = themeURL.match(themeFinder)[0];
  348. if( config.theme !== themeName ) {
  349. themeURL = themeURL.replace(themeFinder, config.theme);
  350. dom.theme.setAttribute( 'href', themeURL );
  351. }
  352. }
  353. sync();
  354. }
  355. /**
  356. * Binds all event listeners.
  357. */
  358. function addEventListeners() {
  359. eventsAreBound = true;
  360. window.addEventListener( 'hashchange', onWindowHashChange, false );
  361. window.addEventListener( 'resize', onWindowResize, false );
  362. if( config.touch ) {
  363. dom.wrapper.addEventListener( 'touchstart', onTouchStart, false );
  364. dom.wrapper.addEventListener( 'touchmove', onTouchMove, false );
  365. dom.wrapper.addEventListener( 'touchend', onTouchEnd, false );
  366. // Support pointer-style touch interaction as well
  367. if( window.navigator.msPointerEnabled ) {
  368. dom.wrapper.addEventListener( 'MSPointerDown', onPointerDown, false );
  369. dom.wrapper.addEventListener( 'MSPointerMove', onPointerMove, false );
  370. dom.wrapper.addEventListener( 'MSPointerUp', onPointerUp, false );
  371. }
  372. }
  373. if( config.keyboard ) {
  374. document.addEventListener( 'keydown', onDocumentKeyDown, false );
  375. }
  376. if ( config.progress && dom.progress ) {
  377. dom.progress.addEventListener( 'click', onProgressClicked, false );
  378. }
  379. if ( config.controls && dom.controls ) {
  380. [ 'touchstart', 'click' ].forEach( function( eventName ) {
  381. dom.controlsLeft.forEach( function( el ) { el.addEventListener( eventName, onNavigateLeftClicked, false ); } );
  382. dom.controlsRight.forEach( function( el ) { el.addEventListener( eventName, onNavigateRightClicked, false ); } );
  383. dom.controlsUp.forEach( function( el ) { el.addEventListener( eventName, onNavigateUpClicked, false ); } );
  384. dom.controlsDown.forEach( function( el ) { el.addEventListener( eventName, onNavigateDownClicked, false ); } );
  385. dom.controlsPrev.forEach( function( el ) { el.addEventListener( eventName, onNavigatePrevClicked, false ); } );
  386. dom.controlsNext.forEach( function( el ) { el.addEventListener( eventName, onNavigateNextClicked, false ); } );
  387. } );
  388. }
  389. }
  390. /**
  391. * Unbinds all event listeners.
  392. */
  393. function removeEventListeners() {
  394. eventsAreBound = false;
  395. document.removeEventListener( 'keydown', onDocumentKeyDown, false );
  396. window.removeEventListener( 'hashchange', onWindowHashChange, false );
  397. window.removeEventListener( 'resize', onWindowResize, false );
  398. dom.wrapper.removeEventListener( 'touchstart', onTouchStart, false );
  399. dom.wrapper.removeEventListener( 'touchmove', onTouchMove, false );
  400. dom.wrapper.removeEventListener( 'touchend', onTouchEnd, false );
  401. if( window.navigator.msPointerEnabled ) {
  402. dom.wrapper.removeEventListener( 'MSPointerDown', onPointerDown, false );
  403. dom.wrapper.removeEventListener( 'MSPointerMove', onPointerMove, false );
  404. dom.wrapper.removeEventListener( 'MSPointerUp', onPointerUp, false );
  405. }
  406. if ( config.progress && dom.progress ) {
  407. dom.progress.removeEventListener( 'click', onProgressClicked, false );
  408. }
  409. if ( config.controls && dom.controls ) {
  410. [ 'touchstart', 'click' ].forEach( function( eventName ) {
  411. dom.controlsLeft.forEach( function( el ) { el.removeEventListener( eventName, onNavigateLeftClicked, false ); } );
  412. dom.controlsRight.forEach( function( el ) { el.removeEventListener( eventName, onNavigateRightClicked, false ); } );
  413. dom.controlsUp.forEach( function( el ) { el.removeEventListener( eventName, onNavigateUpClicked, false ); } );
  414. dom.controlsDown.forEach( function( el ) { el.removeEventListener( eventName, onNavigateDownClicked, false ); } );
  415. dom.controlsPrev.forEach( function( el ) { el.removeEventListener( eventName, onNavigatePrevClicked, false ); } );
  416. dom.controlsNext.forEach( function( el ) { el.removeEventListener( eventName, onNavigateNextClicked, false ); } );
  417. } );
  418. }
  419. }
  420. /**
  421. * Extend object a with the properties of object b.
  422. * If there's a conflict, object b takes precedence.
  423. */
  424. function extend( a, b ) {
  425. for( var i in b ) {
  426. a[ i ] = b[ i ];
  427. }
  428. }
  429. /**
  430. * Converts the target object to an array.
  431. */
  432. function toArray( o ) {
  433. return Array.prototype.slice.call( o );
  434. }
  435. /**
  436. * Measures the distance in pixels between point a
  437. * and point b.
  438. *
  439. * @param {Object} a point with x/y properties
  440. * @param {Object} b point with x/y properties
  441. */
  442. function distanceBetween( a, b ) {
  443. var dx = a.x - b.x,
  444. dy = a.y - b.y;
  445. return Math.sqrt( dx*dx + dy*dy );
  446. }
  447. /**
  448. * Causes the address bar to hide on mobile devices,
  449. * more vertical space ftw.
  450. */
  451. function removeAddressBar() {
  452. if( window.orientation === 0 ) {
  453. document.documentElement.style.overflow = 'scroll';
  454. document.body.style.height = '120%';
  455. }
  456. else {
  457. document.documentElement.style.overflow = '';
  458. document.body.style.height = '100%';
  459. }
  460. setTimeout( function() {
  461. window.scrollTo( 0, 1 );
  462. }, 10 );
  463. }
  464. /**
  465. * Dispatches an event of the specified type from the
  466. * reveal DOM element.
  467. */
  468. function dispatchEvent( type, properties ) {
  469. var event = document.createEvent( "HTMLEvents", 1, 2 );
  470. event.initEvent( type, true, true );
  471. extend( event, properties );
  472. dom.wrapper.dispatchEvent( event );
  473. }
  474. /**
  475. * Wrap all links in 3D goodness.
  476. */
  477. function enable3DLinks() {
  478. if( supports3DTransforms && !( 'msPerspective' in document.body.style ) ) {
  479. var anchors = document.querySelectorAll( SLIDES_SELECTOR + ' a:not(.image)' );
  480. for( var i = 0, len = anchors.length; i < len; i++ ) {
  481. var anchor = anchors[i];
  482. if( anchor.textContent && !anchor.querySelector( '*' ) && ( !anchor.className || !anchor.classList.contains( anchor, 'roll' ) ) ) {
  483. var span = document.createElement('span');
  484. span.setAttribute('data-title', anchor.text);
  485. span.innerHTML = anchor.innerHTML;
  486. anchor.classList.add( 'roll' );
  487. anchor.innerHTML = '';
  488. anchor.appendChild(span);
  489. }
  490. }
  491. }
  492. }
  493. /**
  494. * Unwrap all 3D links.
  495. */
  496. function disable3DLinks() {
  497. var anchors = document.querySelectorAll( SLIDES_SELECTOR + ' a.roll' );
  498. for( var i = 0, len = anchors.length; i < len; i++ ) {
  499. var anchor = anchors[i];
  500. var span = anchor.querySelector( 'span' );
  501. if( span ) {
  502. anchor.classList.remove( 'roll' );
  503. anchor.innerHTML = span.innerHTML;
  504. }
  505. }
  506. }
  507. /**
  508. * Return a sorted fragments list, ordered by an increasing
  509. * "data-fragment-index" attribute.
  510. *
  511. * Fragments will be revealed in the order that they are returned by
  512. * this function, so you can use the index attributes to control the
  513. * order of fragment appearance.
  514. *
  515. * To maintain a sensible default fragment order, fragments are presumed
  516. * to be passed in document order. This function adds a "fragment-index"
  517. * attribute to each node if such an attribute is not already present,
  518. * and sets that attribute to an integer value which is the position of
  519. * the fragment within the fragments list.
  520. */
  521. function sortFragments( fragments ) {
  522. var a = toArray( fragments );
  523. a.forEach( function( el, idx ) {
  524. if( !el.hasAttribute( 'data-fragment-index' ) ) {
  525. el.setAttribute( 'data-fragment-index', idx );
  526. }
  527. } );
  528. a.sort( function( l, r ) {
  529. return l.getAttribute( 'data-fragment-index' ) - r.getAttribute( 'data-fragment-index');
  530. } );
  531. return a;
  532. }
  533. /**
  534. * Applies JavaScript-controlled layout rules to the
  535. * presentation.
  536. */
  537. function layout() {
  538. if( dom.wrapper ) {
  539. // Available space to scale within
  540. var availableWidth = dom.wrapper.offsetWidth,
  541. availableHeight = dom.wrapper.offsetHeight;
  542. // Reduce available space by margin
  543. availableWidth -= ( availableHeight * config.margin );
  544. availableHeight -= ( availableHeight * config.margin );
  545. // Dimensions of the content
  546. var slideWidth = config.width,
  547. slideHeight = config.height;
  548. // Slide width may be a percentage of available width
  549. if( typeof slideWidth === 'string' && /%$/.test( slideWidth ) ) {
  550. slideWidth = parseInt( slideWidth, 10 ) / 100 * availableWidth;
  551. }
  552. // Slide height may be a percentage of available height
  553. if( typeof slideHeight === 'string' && /%$/.test( slideHeight ) ) {
  554. slideHeight = parseInt( slideHeight, 10 ) / 100 * availableHeight;
  555. }
  556. dom.slides.style.width = slideWidth + 'px';
  557. dom.slides.style.height = slideHeight + 'px';
  558. // Determine scale of content to fit within available space
  559. scale = Math.min( availableWidth / slideWidth, availableHeight / slideHeight );
  560. // Respect max/min scale settings
  561. scale = Math.max( scale, config.minScale );
  562. scale = Math.min( scale, config.maxScale );
  563. // Prefer applying scale via zoom since Chrome blurs scaled content
  564. // with nested transforms
  565. if( typeof dom.slides.style.zoom !== 'undefined' && !navigator.userAgent.match( /(iphone|ipod|ipad|android)/gi ) ) {
  566. dom.slides.style.zoom = scale;
  567. }
  568. // Apply scale transform as a fallback
  569. else {
  570. var transform = 'translate(-50%, -50%) scale('+ scale +') translate(50%, 50%)';
  571. dom.slides.style.WebkitTransform = transform;
  572. dom.slides.style.MozTransform = transform;
  573. dom.slides.style.msTransform = transform;
  574. dom.slides.style.OTransform = transform;
  575. dom.slides.style.transform = transform;
  576. }
  577. // Select all slides, vertical and horizontal
  578. var slides = toArray( document.querySelectorAll( SLIDES_SELECTOR ) );
  579. for( var i = 0, len = slides.length; i < len; i++ ) {
  580. var slide = slides[ i ];
  581. // Don't bother updating invisible slides
  582. if( slide.style.display === 'none' ) {
  583. continue;
  584. }
  585. if( config.center ) {
  586. // Vertical stacks are not centred since their section
  587. // children will be
  588. if( slide.classList.contains( 'stack' ) ) {
  589. slide.style.top = 0;
  590. }
  591. else {
  592. slide.style.top = Math.max( - ( slide.offsetHeight / 2 ) - 20, -slideHeight / 2 ) + 'px';
  593. }
  594. }
  595. else {
  596. slide.style.top = '';
  597. }
  598. }
  599. updateProgress();
  600. }
  601. }
  602. /**
  603. * Stores the vertical index of a stack so that the same
  604. * vertical slide can be selected when navigating to and
  605. * from the stack.
  606. *
  607. * @param {HTMLElement} stack The vertical stack element
  608. * @param {int} v Index to memorize
  609. */
  610. function setPreviousVerticalIndex( stack, v ) {
  611. if( typeof stack === 'object' && typeof stack.setAttribute === 'function' ) {
  612. stack.setAttribute( 'data-previous-indexv', v || 0 );
  613. }
  614. }
  615. /**
  616. * Retrieves the vertical index which was stored using
  617. * #setPreviousVerticalIndex() or 0 if no previous index
  618. * exists.
  619. *
  620. * @param {HTMLElement} stack The vertical stack element
  621. */
  622. function getPreviousVerticalIndex( stack ) {
  623. if( typeof stack === 'object' && typeof stack.setAttribute === 'function' && stack.classList.contains( 'stack' ) ) {
  624. // Prefer manually defined start-indexv
  625. var attributeName = stack.hasAttribute( 'data-start-indexv' ) ? 'data-start-indexv' : 'data-previous-indexv';
  626. return parseInt( stack.getAttribute( attributeName ) || 0, 10 );
  627. }
  628. return 0;
  629. }
  630. /**
  631. * Displays the overview of slides (quick nav) by
  632. * scaling down and arranging all slide elements.
  633. *
  634. * Experimental feature, might be dropped if perf
  635. * can't be improved.
  636. */
  637. function activateOverview() {
  638. // Only proceed if enabled in config
  639. if( config.overview ) {
  640. // Don't auto-slide while in overview mode
  641. cancelAutoSlide();
  642. var wasActive = dom.wrapper.classList.contains( 'overview' );
  643. dom.wrapper.classList.add( 'overview' );
  644. dom.wrapper.classList.remove( 'exit-overview' );
  645. clearTimeout( activateOverviewTimeout );
  646. clearTimeout( deactivateOverviewTimeout );
  647. // Not the pretties solution, but need to let the overview
  648. // class apply first so that slides are measured accurately
  649. // before we can position them
  650. activateOverviewTimeout = setTimeout( function(){
  651. var horizontalSlides = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR );
  652. for( var i = 0, len1 = horizontalSlides.length; i < len1; i++ ) {
  653. var hslide = horizontalSlides[i],
  654. hoffset = config.rtl ? -105 : 105,
  655. htransform = 'translateZ(-2500px) translate(' + ( ( i - indexh ) * hoffset ) + '%, 0%)';
  656. hslide.setAttribute( 'data-index-h', i );
  657. hslide.style.display = 'block';
  658. hslide.style.WebkitTransform = htransform;
  659. hslide.style.MozTransform = htransform;
  660. hslide.style.msTransform = htransform;
  661. hslide.style.OTransform = htransform;
  662. hslide.style.transform = htransform;
  663. if( hslide.classList.contains( 'stack' ) ) {
  664. var verticalSlides = hslide.querySelectorAll( 'section' );
  665. for( var j = 0, len2 = verticalSlides.length; j < len2; j++ ) {
  666. var verticalIndex = i === indexh ? indexv : getPreviousVerticalIndex( hslide );
  667. var vslide = verticalSlides[j],
  668. vtransform = 'translate(0%, ' + ( ( j - verticalIndex ) * 105 ) + '%)';
  669. vslide.setAttribute( 'data-index-h', i );
  670. vslide.setAttribute( 'data-index-v', j );
  671. vslide.style.display = 'block';
  672. vslide.style.WebkitTransform = vtransform;
  673. vslide.style.MozTransform = vtransform;
  674. vslide.style.msTransform = vtransform;
  675. vslide.style.OTransform = vtransform;
  676. vslide.style.transform = vtransform;
  677. // Navigate to this slide on click
  678. vslide.addEventListener( 'click', onOverviewSlideClicked, true );
  679. }
  680. }
  681. else {
  682. // Navigate to this slide on click
  683. hslide.addEventListener( 'click', onOverviewSlideClicked, true );
  684. }
  685. }
  686. layout();
  687. if( !wasActive ) {
  688. // Notify observers of the overview showing
  689. dispatchEvent( 'overviewshown', {
  690. 'indexh': indexh,
  691. 'indexv': indexv,
  692. 'currentSlide': currentSlide
  693. } );
  694. }
  695. }, 10 );
  696. }
  697. }
  698. /**
  699. * Exits the slide overview and enters the currently
  700. * active slide.
  701. */
  702. function deactivateOverview() {
  703. // Only proceed if enabled in config
  704. if( config.overview ) {
  705. clearTimeout( activateOverviewTimeout );
  706. clearTimeout( deactivateOverviewTimeout );
  707. dom.wrapper.classList.remove( 'overview' );
  708. // Temporarily add a class so that transitions can do different things
  709. // depending on whether they are exiting/entering overview, or just
  710. // moving from slide to slide
  711. dom.wrapper.classList.add( 'exit-overview' );
  712. deactivateOverviewTimeout = setTimeout( function () {
  713. dom.wrapper.classList.remove( 'exit-overview' );
  714. }, 10);
  715. // Select all slides
  716. var slides = toArray( document.querySelectorAll( SLIDES_SELECTOR ) );
  717. for( var i = 0, len = slides.length; i < len; i++ ) {
  718. var element = slides[i];
  719. element.style.display = '';
  720. // Resets all transforms to use the external styles
  721. element.style.WebkitTransform = '';
  722. element.style.MozTransform = '';
  723. element.style.msTransform = '';
  724. element.style.OTransform = '';
  725. element.style.transform = '';
  726. element.removeEventListener( 'click', onOverviewSlideClicked, true );
  727. }
  728. slide( indexh, indexv );
  729. cueAutoSlide();
  730. // Notify observers of the overview hiding
  731. dispatchEvent( 'overviewhidden', {
  732. 'indexh': indexh,
  733. 'indexv': indexv,
  734. 'currentSlide': currentSlide
  735. } );
  736. }
  737. }
  738. /**
  739. * Toggles the slide overview mode on and off.
  740. *
  741. * @param {Boolean} override Optional flag which overrides the
  742. * toggle logic and forcibly sets the desired state. True means
  743. * overview is open, false means it's closed.
  744. */
  745. function toggleOverview( override ) {
  746. if( typeof override === 'boolean' ) {
  747. override ? activateOverview() : deactivateOverview();
  748. }
  749. else {
  750. isOverview() ? deactivateOverview() : activateOverview();
  751. }
  752. }
  753. /**
  754. * Checks if the overview is currently active.
  755. *
  756. * @return {Boolean} true if the overview is active,
  757. * false otherwise
  758. */
  759. function isOverview() {
  760. return dom.wrapper.classList.contains( 'overview' );
  761. }
  762. /**
  763. * Checks if the current or specified slide is vertical
  764. * (nested within another slide).
  765. *
  766. * @param {HTMLElement} slide [optional] The slide to check
  767. * orientation of
  768. */
  769. function isVerticalSlide( slide ) {
  770. // Prefer slide argument, otherwise use current slide
  771. slide = slide ? slide : currentSlide;
  772. return slide && !!slide.parentNode.nodeName.match( /section/i );
  773. }
  774. /**
  775. * Handling the fullscreen functionality via the fullscreen API
  776. *
  777. * @see http://fullscreen.spec.whatwg.org/
  778. * @see https://developer.mozilla.org/en-US/docs/DOM/Using_fullscreen_mode
  779. */
  780. function enterFullscreen() {
  781. var element = document.body;
  782. // Check which implementation is available
  783. var requestMethod = element.requestFullScreen ||
  784. element.webkitRequestFullscreen ||
  785. element.webkitRequestFullScreen ||
  786. element.mozRequestFullScreen ||
  787. element.msRequestFullScreen;
  788. if( requestMethod ) {
  789. requestMethod.apply( element );
  790. }
  791. }
  792. /**
  793. * Enters the paused mode which fades everything on screen to
  794. * black.
  795. */
  796. function pause() {
  797. var wasPaused = dom.wrapper.classList.contains( 'paused' );
  798. cancelAutoSlide();
  799. dom.wrapper.classList.add( 'paused' );
  800. if( wasPaused === false ) {
  801. dispatchEvent( 'paused' );
  802. }
  803. }
  804. /**
  805. * Exits from the paused mode.
  806. */
  807. function resume() {
  808. var wasPaused = dom.wrapper.classList.contains( 'paused' );
  809. dom.wrapper.classList.remove( 'paused' );
  810. cueAutoSlide();
  811. if( wasPaused ) {
  812. dispatchEvent( 'resumed' );
  813. }
  814. }
  815. /**
  816. * Toggles the paused mode on and off.
  817. */
  818. function togglePause() {
  819. if( isPaused() ) {
  820. resume();
  821. }
  822. else {
  823. pause();
  824. }
  825. }
  826. /**
  827. * Checks if we are currently in the paused mode.
  828. */
  829. function isPaused() {
  830. return dom.wrapper.classList.contains( 'paused' );
  831. }
  832. /**
  833. * Steps from the current point in the presentation to the
  834. * slide which matches the specified horizontal and vertical
  835. * indices.
  836. *
  837. * @param {int} h Horizontal index of the target slide
  838. * @param {int} v Vertical index of the target slide
  839. * @param {int} f Optional index of a fragment within the
  840. * target slide to activate
  841. * @param {int} o Optional origin for use in multimaster environments
  842. */
  843. function slide( h, v, f, o ) {
  844. // Remember where we were at before
  845. previousSlide = currentSlide;
  846. // Query all horizontal slides in the deck
  847. var horizontalSlides = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR );
  848. // If no vertical index is specified and the upcoming slide is a
  849. // stack, resume at its previous vertical index
  850. if( v === undefined ) {
  851. v = getPreviousVerticalIndex( horizontalSlides[ h ] );
  852. }
  853. // If we were on a vertical stack, remember what vertical index
  854. // it was on so we can resume at the same position when returning
  855. if( previousSlide && previousSlide.parentNode && previousSlide.parentNode.classList.contains( 'stack' ) ) {
  856. setPreviousVerticalIndex( previousSlide.parentNode, indexv );
  857. }
  858. // Remember the state before this slide
  859. var stateBefore = state.concat();
  860. // Reset the state array
  861. state.length = 0;
  862. var indexhBefore = indexh,
  863. indexvBefore = indexv;
  864. // Activate and transition to the new slide
  865. indexh = updateSlides( HORIZONTAL_SLIDES_SELECTOR, h === undefined ? indexh : h );
  866. indexv = updateSlides( VERTICAL_SLIDES_SELECTOR, v === undefined ? indexv : v );
  867. layout();
  868. // Apply the new state
  869. stateLoop: for( var i = 0, len = state.length; i < len; i++ ) {
  870. // Check if this state existed on the previous slide. If it
  871. // did, we will avoid adding it repeatedly
  872. for( var j = 0; j < stateBefore.length; j++ ) {
  873. if( stateBefore[j] === state[i] ) {
  874. stateBefore.splice( j, 1 );
  875. continue stateLoop;
  876. }
  877. }
  878. document.documentElement.classList.add( state[i] );
  879. // Dispatch custom event matching the state's name
  880. dispatchEvent( state[i] );
  881. }
  882. // Clean up the remains of the previous state
  883. while( stateBefore.length ) {
  884. document.documentElement.classList.remove( stateBefore.pop() );
  885. }
  886. // If the overview is active, re-activate it to update positions
  887. if( isOverview() ) {
  888. activateOverview();
  889. }
  890. // Update the URL hash after a delay since updating it mid-transition
  891. // is likely to cause visual lag
  892. writeURL( 1500 );
  893. // Find the current horizontal slide and any possible vertical slides
  894. // within it
  895. var currentHorizontalSlide = horizontalSlides[ indexh ],
  896. currentVerticalSlides = currentHorizontalSlide.querySelectorAll( 'section' );
  897. // Store references to the previous and current slides
  898. currentSlide = currentVerticalSlides[ indexv ] || currentHorizontalSlide;
  899. // Show fragment, if specified
  900. if( typeof f !== 'undefined' ) {
  901. var fragments = sortFragments( currentSlide.querySelectorAll( '.fragment' ) );
  902. toArray( fragments ).forEach( function( fragment, indexf ) {
  903. if( indexf < f ) {
  904. fragment.classList.add( 'visible' );
  905. }
  906. else {
  907. fragment.classList.remove( 'visible' );
  908. }
  909. } );
  910. }
  911. // Dispatch an event if the slide changed
  912. if( indexh !== indexhBefore || indexv !== indexvBefore ) {
  913. dispatchEvent( 'slidechanged', {
  914. 'indexh': indexh,
  915. 'indexv': indexv,
  916. 'previousSlide': previousSlide,
  917. 'currentSlide': currentSlide,
  918. 'origin': o
  919. } );
  920. }
  921. else {
  922. // Ensure that the previous slide is never the same as the current
  923. previousSlide = null;
  924. }
  925. // Solves an edge case where the previous slide maintains the
  926. // 'present' class when navigating between adjacent vertical
  927. // stacks
  928. if( previousSlide ) {
  929. previousSlide.classList.remove( 'present' );
  930. // Reset all slides upon navigate to home
  931. // Issue: #285
  932. if ( document.querySelector( HOME_SLIDE_SELECTOR ).classList.contains( 'present' ) ) {
  933. // Launch async task
  934. setTimeout( function () {
  935. var slides = toArray( document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR + '.stack') ), i;
  936. for( i in slides ) {
  937. if( slides[i] ) {
  938. // Reset stack
  939. setPreviousVerticalIndex( slides[i], 0 );
  940. }
  941. }
  942. }, 0 );
  943. }
  944. }
  945. // Handle embedded content
  946. stopEmbeddedContent( previousSlide );
  947. startEmbeddedContent( currentSlide );
  948. updateControls();
  949. updateProgress();
  950. updateBackground();
  951. }
  952. /**
  953. * Syncs the presentation with the current DOM. Useful
  954. * when new slides or control elements are added or when
  955. * the configuration has changed.
  956. */
  957. function sync() {
  958. // Subscribe to input
  959. removeEventListeners();
  960. addEventListeners();
  961. // Force a layout to make sure the current config is accounted for
  962. layout();
  963. // Reflect the current autoSlide value
  964. autoSlide = config.autoSlide;
  965. // Start auto-sliding if it's enabled
  966. cueAutoSlide();
  967. // Re-create the slide backgrounds
  968. createBackgrounds();
  969. updateControls();
  970. updateProgress();
  971. updateBackground();
  972. }
  973. /**
  974. * Updates one dimension of slides by showing the slide
  975. * with the specified index.
  976. *
  977. * @param {String} selector A CSS selector that will fetch
  978. * the group of slides we are working with
  979. * @param {Number} index The index of the slide that should be
  980. * shown
  981. *
  982. * @return {Number} The index of the slide that is now shown,
  983. * might differ from the passed in index if it was out of
  984. * bounds.
  985. */
  986. function updateSlides( selector, index ) {
  987. // Select all slides and convert the NodeList result to
  988. // an array
  989. var slides = toArray( document.querySelectorAll( selector ) ),
  990. slidesLength = slides.length;
  991. if( slidesLength ) {
  992. // Should the index loop?
  993. if( config.loop ) {
  994. index %= slidesLength;
  995. if( index < 0 ) {
  996. index = slidesLength + index;
  997. }
  998. }
  999. // Enforce max and minimum index bounds
  1000. index = Math.max( Math.min( index, slidesLength - 1 ), 0 );
  1001. for( var i = 0; i < slidesLength; i++ ) {
  1002. var element = slides[i];
  1003. // Optimization; hide all slides that are three or more steps
  1004. // away from the present slide
  1005. if( isOverview() === false ) {
  1006. // The distance loops so that it measures 1 between the first
  1007. // and last slides
  1008. var distance = Math.abs( ( index - i ) % ( slidesLength - 3 ) ) || 0;
  1009. element.style.display = distance > 3 ? 'none' : 'block';
  1010. }
  1011. var reverse = config.rtl && !isVerticalSlide( element );
  1012. element.classList.remove( 'past' );
  1013. element.classList.remove( 'present' );
  1014. element.classList.remove( 'future' );
  1015. if( i < index ) {
  1016. // Any element previous to index is given the 'past' class
  1017. element.classList.add( reverse ? 'future' : 'past' );
  1018. }
  1019. else if( i > index ) {
  1020. // Any element subsequent to index is given the 'future' class
  1021. element.classList.add( reverse ? 'past' : 'future' );
  1022. }
  1023. // If this element contains vertical slides
  1024. if( element.querySelector( 'section' ) ) {
  1025. element.classList.add( 'stack' );
  1026. }
  1027. }
  1028. // Mark the current slide as present
  1029. slides[index].classList.add( 'present' );
  1030. // If this slide has a state associated with it, add it
  1031. // onto the current state of the deck
  1032. var slideState = slides[index].getAttribute( 'data-state' );
  1033. if( slideState ) {
  1034. state = state.concat( slideState.split( ' ' ) );
  1035. }
  1036. // If this slide has a data-autoslide attribtue associated use this as
  1037. // autoSlide value otherwise use the global configured time
  1038. var slideAutoSlide = slides[index].getAttribute( 'data-autoslide' );
  1039. if( slideAutoSlide ) {
  1040. autoSlide = parseInt( slideAutoSlide, 10 );
  1041. }
  1042. else {
  1043. autoSlide = config.autoSlide;
  1044. }
  1045. }
  1046. else {
  1047. // Since there are no slides we can't be anywhere beyond the
  1048. // zeroth index
  1049. index = 0;
  1050. }
  1051. return index;
  1052. }
  1053. /**
  1054. * Updates the progress bar to reflect the current slide.
  1055. */
  1056. function updateProgress() {
  1057. // Update progress if enabled
  1058. if( config.progress && dom.progress ) {
  1059. var horizontalSlides = toArray( document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) );
  1060. // The number of past and total slides
  1061. var totalCount = document.querySelectorAll( SLIDES_SELECTOR + ':not(.stack)' ).length;
  1062. var pastCount = 0;
  1063. // Step through all slides and count the past ones
  1064. mainLoop: for( var i = 0; i < horizontalSlides.length; i++ ) {
  1065. var horizontalSlide = horizontalSlides[i];
  1066. var verticalSlides = toArray( horizontalSlide.querySelectorAll( 'section' ) );
  1067. for( var j = 0; j < verticalSlides.length; j++ ) {
  1068. // Stop as soon as we arrive at the present
  1069. if( verticalSlides[j].classList.contains( 'present' ) ) {
  1070. break mainLoop;
  1071. }
  1072. pastCount++;
  1073. }
  1074. // Stop as soon as we arrive at the present
  1075. if( horizontalSlide.classList.contains( 'present' ) ) {
  1076. break;
  1077. }
  1078. // Don't count the wrapping section for vertical slides
  1079. if( horizontalSlide.classList.contains( 'stack' ) === false ) {
  1080. pastCount++;
  1081. }
  1082. }
  1083. dom.progressbar.style.width = ( pastCount / ( totalCount - 1 ) ) * window.innerWidth + 'px';
  1084. }
  1085. }
  1086. /**
  1087. * Updates the state of all control/navigation arrows.
  1088. */
  1089. function updateControls() {
  1090. if ( config.controls && dom.controls ) {
  1091. var routes = availableRoutes();
  1092. var fragments = availableFragments();
  1093. // Remove the 'enabled' class from all directions
  1094. dom.controlsLeft.concat( dom.controlsRight )
  1095. .concat( dom.controlsUp )
  1096. .concat( dom.controlsDown )
  1097. .concat( dom.controlsPrev )
  1098. .concat( dom.controlsNext ).forEach( function( node ) {
  1099. node.classList.remove( 'enabled' );
  1100. node.classList.remove( 'fragmented' );
  1101. } );
  1102. // Add the 'enabled' class to the available routes
  1103. if( routes.left ) dom.controlsLeft.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  1104. if( routes.right ) dom.controlsRight.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  1105. if( routes.up ) dom.controlsUp.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  1106. if( routes.down ) dom.controlsDown.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  1107. // Prev/next buttons
  1108. if( routes.left || routes.up ) dom.controlsPrev.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  1109. if( routes.right || routes.down ) dom.controlsNext.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  1110. // Highlight fragment directions
  1111. if( currentSlide ) {
  1112. // Always apply fragment decorator to prev/next buttons
  1113. if( fragments.prev ) dom.controlsPrev.forEach( function( el ) { el.classList.add( 'fragmented', 'enabled' ); } );
  1114. if( fragments.next ) dom.controlsNext.forEach( function( el ) { el.classList.add( 'fragmented', 'enabled' ); } );
  1115. // Apply fragment decorators to directional buttons based on
  1116. // what slide axis they are in
  1117. if( isVerticalSlide( currentSlide ) ) {
  1118. if( fragments.prev ) dom.controlsUp.forEach( function( el ) { el.classList.add( 'fragmented', 'enabled' ); } );
  1119. if( fragments.next ) dom.controlsDown.forEach( function( el ) { el.classList.add( 'fragmented', 'enabled' ); } );
  1120. }
  1121. else {
  1122. if( fragments.prev ) dom.controlsLeft.forEach( function( el ) { el.classList.add( 'fragmented', 'enabled' ); } );
  1123. if( fragments.next ) dom.controlsRight.forEach( function( el ) { el.classList.add( 'fragmented', 'enabled' ); } );
  1124. }
  1125. }
  1126. }
  1127. }
  1128. /**
  1129. * Updates the background elements to reflect the current
  1130. * slide.
  1131. */
  1132. function updateBackground() {
  1133. // Update the classes of all backgrounds to match the
  1134. // states of their slides (past/present/future)
  1135. toArray( dom.background.childNodes ).forEach( function( backgroundh, h ) {
  1136. backgroundh.className = ( h < indexh ? 'past' : h > indexh ? 'future' : 'present' );
  1137. toArray( backgroundh.childNodes ).forEach( function( backgroundv, v ) {
  1138. backgroundv.className = ( v < indexv ? 'past' : v > indexv ? 'future' : 'present' );
  1139. } );
  1140. } );
  1141. // Allow the first background to apply without transition
  1142. setTimeout( function() {
  1143. dom.background.classList.remove( 'no-transition' );
  1144. }, 1 );
  1145. }
  1146. /**
  1147. * Determine what available routes there are for navigation.
  1148. *
  1149. * @return {Object} containing four booleans: left/right/up/down
  1150. */
  1151. function availableRoutes() {
  1152. var horizontalSlides = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ),
  1153. verticalSlides = document.querySelectorAll( VERTICAL_SLIDES_SELECTOR );
  1154. var routes = {
  1155. left: indexh > 0 || config.loop,
  1156. right: indexh < horizontalSlides.length - 1 || config.loop,
  1157. up: indexv > 0,
  1158. down: indexv < verticalSlides.length - 1
  1159. };
  1160. // reverse horizontal controls for rtl
  1161. if( config.rtl ) {
  1162. var left = routes.left;
  1163. routes.left = routes.right;
  1164. routes.right = left;
  1165. }
  1166. return routes;
  1167. }
  1168. /**
  1169. * Returns an object describing the available fragment
  1170. * directions.
  1171. *
  1172. * @return {Object} two boolean properties: prev/next
  1173. */
  1174. function availableFragments() {
  1175. if( currentSlide && config.fragments ) {
  1176. var fragments = currentSlide.querySelectorAll( '.fragment' );
  1177. var hiddenFragments = currentSlide.querySelectorAll( '.fragment:not(.visible)' );
  1178. return {
  1179. prev: fragments.length - hiddenFragments.length > 0,
  1180. next: !!hiddenFragments.length
  1181. };
  1182. }
  1183. else {
  1184. return { prev: false, next: false };
  1185. }
  1186. }
  1187. /**
  1188. * Start playback of any embedded content inside of
  1189. * the targeted slide.
  1190. */
  1191. function startEmbeddedContent( slide ) {
  1192. if( slide ) {
  1193. // HTML5 media elements
  1194. toArray( slide.querySelectorAll( 'video, audio' ) ).forEach( function( el ) {
  1195. if( el.hasAttribute( 'data-autoplay' ) ) {
  1196. el.play();
  1197. }
  1198. } );
  1199. // YouTube embeds
  1200. toArray( slide.querySelectorAll( 'iframe[src*="youtube.com/embed/"]' ) ).forEach( function( el ) {
  1201. if( el.hasAttribute( 'data-autoplay' ) ) {
  1202. el.contentWindow.postMessage('{"event":"command","func":"playVideo","args":""}', '*');
  1203. }
  1204. });
  1205. }
  1206. }
  1207. /**
  1208. * Stop playback of any embedded content inside of
  1209. * the targeted slide.
  1210. */
  1211. function stopEmbeddedContent( slide ) {
  1212. if( slide ) {
  1213. // HTML5 media elements
  1214. toArray( slide.querySelectorAll( 'video, audio' ) ).forEach( function( el ) {
  1215. if( !el.hasAttribute( 'data-ignore' ) ) {
  1216. el.pause();
  1217. }
  1218. } );
  1219. // YouTube embeds
  1220. toArray( slide.querySelectorAll( 'iframe[src*="youtube.com/embed/"]' ) ).forEach( function( el ) {
  1221. if( !el.hasAttribute( 'data-ignore' ) && typeof el.contentWindow.postMessage === 'function' ) {
  1222. el.contentWindow.postMessage('{"event":"command","func":"pauseVideo","args":""}', '*');
  1223. }
  1224. });
  1225. }
  1226. }
  1227. /**
  1228. * Reads the current URL (hash) and navigates accordingly.
  1229. */
  1230. function readURL() {
  1231. var hash = window.location.hash;
  1232. // Attempt to parse the hash as either an index or name
  1233. var bits = hash.slice( 2 ).split( '/' ),
  1234. name = hash.replace( /#|\//gi, '' );
  1235. // If the first bit is invalid and there is a name we can
  1236. // assume that this is a named link
  1237. if( isNaN( parseInt( bits[0], 10 ) ) && name.length ) {
  1238. // Find the slide with the specified name
  1239. var element = document.querySelector( '#' + name );
  1240. if( element ) {
  1241. // Find the position of the named slide and navigate to it
  1242. var indices = Reveal.getIndices( element );
  1243. slide( indices.h, indices.v );
  1244. }
  1245. // If the slide doesn't exist, navigate to the current slide
  1246. else {
  1247. slide( indexh, indexv );
  1248. }
  1249. }
  1250. else {
  1251. // Read the index components of the hash
  1252. var h = parseInt( bits[0], 10 ) || 0,
  1253. v = parseInt( bits[1], 10 ) || 0;
  1254. slide( h, v );
  1255. }
  1256. }
  1257. /**
  1258. * Updates the page URL (hash) to reflect the current
  1259. * state.
  1260. *
  1261. * @param {Number} delay The time in ms to wait before
  1262. * writing the hash
  1263. */
  1264. function writeURL( delay ) {
  1265. if( config.history ) {
  1266. // Make sure there's never more than one timeout running
  1267. clearTimeout( writeURLTimeout );
  1268. // If a delay is specified, timeout this call
  1269. if( typeof delay === 'number' ) {
  1270. writeURLTimeout = setTimeout( writeURL, delay );
  1271. }
  1272. else {
  1273. var url = '/';
  1274. // If the current slide has an ID, use that as a named link
  1275. if( currentSlide && typeof currentSlide.getAttribute( 'id' ) === 'string' ) {
  1276. url = '/' + currentSlide.getAttribute( 'id' );
  1277. }
  1278. // Otherwise use the /h/v index
  1279. else {
  1280. if( indexh > 0 || indexv > 0 ) url += indexh;
  1281. if( indexv > 0 ) url += '/' + indexv;
  1282. }
  1283. window.location.hash = url;
  1284. }
  1285. }
  1286. }
  1287. /**
  1288. * Retrieves the h/v location of the current, or specified,
  1289. * slide.
  1290. *
  1291. * @param {HTMLElement} slide If specified, the returned
  1292. * index will be for this slide rather than the currently
  1293. * active one
  1294. *
  1295. * @return {Object} { h: <int>, v: <int>, f: <int> }
  1296. */
  1297. function getIndices( slide ) {
  1298. // By default, return the current indices
  1299. var h = indexh,
  1300. v = indexv,
  1301. f;
  1302. // If a slide is specified, return the indices of that slide
  1303. if( slide ) {
  1304. var isVertical = isVerticalSlide( slide );
  1305. var slideh = isVertical ? slide.parentNode : slide;
  1306. // Select all horizontal slides
  1307. var horizontalSlides = toArray( document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) );
  1308. // Now that we know which the horizontal slide is, get its index
  1309. h = Math.max( horizontalSlides.indexOf( slideh ), 0 );
  1310. // If this is a vertical slide, grab the vertical index
  1311. if( isVertical ) {
  1312. v = Math.max( toArray( slide.parentNode.querySelectorAll( 'section' ) ).indexOf( slide ), 0 );
  1313. }
  1314. }
  1315. if( !slide && currentSlide ) {
  1316. var visibleFragments = currentSlide.querySelectorAll( '.fragment.visible' );
  1317. if( visibleFragments.length ) {
  1318. f = visibleFragments.length;
  1319. }
  1320. }
  1321. return { h: h, v: v, f: f };
  1322. }
  1323. /**
  1324. * Navigate to the next slide fragment.
  1325. *
  1326. * @return {Boolean} true if there was a next fragment,
  1327. * false otherwise
  1328. */
  1329. function nextFragment() {
  1330. if( currentSlide && config.fragments ) {
  1331. var fragments = sortFragments( currentSlide.querySelectorAll( '.fragment:not(.visible)' ) );
  1332. if( fragments.length ) {
  1333. // Find the index of the next fragment
  1334. var index = fragments[0].getAttribute( 'data-fragment-index' );
  1335. // Find all fragments with the same index
  1336. fragments = currentSlide.querySelectorAll( '.fragment[data-fragment-index="'+ index +'"]' );
  1337. toArray( fragments ).forEach( function( element ) {
  1338. element.classList.add( 'visible' );
  1339. // Notify subscribers of the change
  1340. dispatchEvent( 'fragmentshown', { fragment: element } );
  1341. } );
  1342. updateControls();
  1343. return true;
  1344. }
  1345. }
  1346. return false;
  1347. }
  1348. /**
  1349. * Navigate to the previous slide fragment.
  1350. *
  1351. * @return {Boolean} true if there was a previous fragment,
  1352. * false otherwise
  1353. */
  1354. function previousFragment() {
  1355. if( currentSlide && config.fragments ) {
  1356. var fragments = sortFragments( currentSlide.querySelectorAll( '.fragment.visible' ) );
  1357. if( fragments.length ) {
  1358. // Find the index of the previous fragment
  1359. var index = fragments[ fragments.length - 1 ].getAttribute( 'data-fragment-index' );
  1360. // Find all fragments with the same index
  1361. fragments = currentSlide.querySelectorAll( '.fragment[data-fragment-index="'+ index +'"]' );
  1362. toArray( fragments ).forEach( function( f ) {
  1363. f.classList.remove( 'visible' );
  1364. // Notify subscribers of the change
  1365. dispatchEvent( 'fragmenthidden', { fragment: f } );
  1366. } );
  1367. updateControls();
  1368. return true;
  1369. }
  1370. }
  1371. return false;
  1372. }
  1373. /**
  1374. * Cues a new automated slide if enabled in the config.
  1375. */
  1376. function cueAutoSlide() {
  1377. clearTimeout( autoSlideTimeout );
  1378. // Cue the next auto-slide if enabled
  1379. if( autoSlide && !isPaused() && !isOverview() ) {
  1380. autoSlideTimeout = setTimeout( navigateNext, autoSlide );
  1381. }
  1382. }
  1383. /**
  1384. * Cancels any ongoing request to auto-slide.
  1385. */
  1386. function cancelAutoSlide() {
  1387. clearTimeout( autoSlideTimeout );
  1388. }
  1389. function navigateLeft() {
  1390. // Reverse for RTL
  1391. if( config.rtl ) {
  1392. if( ( isOverview() || nextFragment() === false ) && availableRoutes().left ) {
  1393. slide( indexh + 1 );
  1394. }
  1395. }
  1396. // Normal navigation
  1397. else if( ( isOverview() || previousFragment() === false ) && availableRoutes().left ) {
  1398. slide( indexh - 1 );
  1399. }
  1400. }
  1401. function navigateRight() {
  1402. // Reverse for RTL
  1403. if( config.rtl ) {
  1404. if( ( isOverview() || previousFragment() === false ) && availableRoutes().right ) {
  1405. slide( indexh - 1 );
  1406. }
  1407. }
  1408. // Normal navigation
  1409. else if( ( isOverview() || nextFragment() === false ) && availableRoutes().right ) {
  1410. slide( indexh + 1 );
  1411. }
  1412. }
  1413. function navigateUp() {
  1414. // Prioritize hiding fragments
  1415. if( ( isOverview() || previousFragment() === false ) && availableRoutes().up ) {
  1416. slide( indexh, indexv - 1 );
  1417. }
  1418. }
  1419. function navigateDown() {
  1420. // Prioritize revealing fragments
  1421. if( ( isOverview() || nextFragment() === false ) && availableRoutes().down ) {
  1422. slide( indexh, indexv + 1 );
  1423. }
  1424. }
  1425. /**
  1426. * Navigates backwards, prioritized in the following order:
  1427. * 1) Previous fragment
  1428. * 2) Previous vertical slide
  1429. * 3) Previous horizontal slide
  1430. */
  1431. function navigatePrev() {
  1432. // Prioritize revealing fragments
  1433. if( previousFragment() === false ) {
  1434. if( availableRoutes().up ) {
  1435. navigateUp();
  1436. }
  1437. else {
  1438. // Fetch the previous horizontal slide, if there is one
  1439. var previousSlide = document.querySelector( HORIZONTAL_SLIDES_SELECTOR + '.past:nth-child(' + indexh + ')' );
  1440. if( previousSlide ) {
  1441. indexv = ( previousSlide.querySelectorAll( 'section' ).length + 1 ) || undefined;
  1442. indexh --;
  1443. slide( indexh, indexv );
  1444. }
  1445. }
  1446. }
  1447. }
  1448. /**
  1449. * Same as #navigatePrev() but navigates forwards.
  1450. */
  1451. function navigateNext() {
  1452. // Prioritize revealing fragments
  1453. if( nextFragment() === false ) {
  1454. availableRoutes().down ? navigateDown() : navigateRight();
  1455. }
  1456. // If auto-sliding is enabled we need to cue up
  1457. // another timeout
  1458. cueAutoSlide();
  1459. }
  1460. // --------------------------------------------------------------------//
  1461. // ----------------------------- EVENTS -------------------------------//
  1462. // --------------------------------------------------------------------//
  1463. /**
  1464. * Handler for the document level 'keydown' event.
  1465. *
  1466. * @param {Object} event
  1467. */
  1468. function onDocumentKeyDown( event ) {
  1469. // Check if there's a focused element that could be using
  1470. // the keyboard
  1471. var activeElement = document.activeElement;
  1472. var hasFocus = !!( document.activeElement && ( document.activeElement.type || document.activeElement.href || document.activeElement.contentEditable !== 'inherit' ) );
  1473. // Disregard the event if there's a focused element or a
  1474. // keyboard modifier key is present
  1475. if( hasFocus || (event.shiftKey && event.keyCode !== 32) || event.altKey || event.ctrlKey || event.metaKey ) return;
  1476. var triggered = true;
  1477. // while paused only allow "unpausing" keyboard events (b and .)
  1478. if( isPaused() && [66,190,191].indexOf( event.keyCode ) === -1 ) {
  1479. return false;
  1480. }
  1481. switch( event.keyCode ) {
  1482. // p, page up
  1483. case 80: case 33: navigatePrev(); break;
  1484. // n, page down
  1485. case 78: case 34: navigateNext(); break;
  1486. // h, left
  1487. case 72: case 37: navigateLeft(); break;
  1488. // l, right
  1489. case 76: case 39: navigateRight(); break;
  1490. // k, up
  1491. case 75: case 38: navigateUp(); break;
  1492. // j, down
  1493. case 74: case 40: navigateDown(); break;
  1494. // home
  1495. case 36: slide( 0 ); break;
  1496. // end
  1497. case 35: slide( Number.MAX_VALUE ); break;
  1498. // space
  1499. case 32: isOverview() ? deactivateOverview() : event.shiftKey ? navigatePrev() : navigateNext(); break;
  1500. // return
  1501. case 13: isOverview() ? deactivateOverview() : triggered = false; break;
  1502. // b, period, Logitech presenter tools "black screen" button
  1503. case 66: case 190: case 191: togglePause(); break;
  1504. // f
  1505. case 70: enterFullscreen(); break;
  1506. default:
  1507. triggered = false;
  1508. }
  1509. // If the input resulted in a triggered action we should prevent
  1510. // the browsers default behavior
  1511. if( triggered ) {
  1512. event.preventDefault();
  1513. }
  1514. else if ( event.keyCode === 27 && supports3DTransforms ) {
  1515. toggleOverview();
  1516. event.preventDefault();
  1517. }
  1518. // If auto-sliding is enabled we need to cue up
  1519. // another timeout
  1520. cueAutoSlide();
  1521. }
  1522. /**
  1523. * Handler for the 'touchstart' event, enables support for
  1524. * swipe and pinch gestures.
  1525. */
  1526. function onTouchStart( event ) {
  1527. touch.startX = event.touches[0].clientX;
  1528. touch.startY = event.touches[0].clientY;
  1529. touch.startCount = event.touches.length;
  1530. // If there's two touches we need to memorize the distance
  1531. // between those two points to detect pinching
  1532. if( event.touches.length === 2 && config.overview ) {
  1533. touch.startSpan = distanceBetween( {
  1534. x: event.touches[1].clientX,
  1535. y: event.touches[1].clientY
  1536. }, {
  1537. x: touch.startX,
  1538. y: touch.startY
  1539. } );
  1540. }
  1541. }
  1542. /**
  1543. * Handler for the 'touchmove' event.
  1544. */
  1545. function onTouchMove( event ) {
  1546. // Each touch should only trigger one action
  1547. if( !touch.handled ) {
  1548. var currentX = event.touches[0].clientX;
  1549. var currentY = event.touches[0].clientY;
  1550. // If the touch started off with two points and still has
  1551. // two active touches; test for the pinch gesture
  1552. if( event.touches.length === 2 && touch.startCount === 2 && config.overview ) {
  1553. // The current distance in pixels between the two touch points
  1554. var currentSpan = distanceBetween( {
  1555. x: event.touches[1].clientX,
  1556. y: event.touches[1].clientY
  1557. }, {
  1558. x: touch.startX,
  1559. y: touch.startY
  1560. } );
  1561. // If the span is larger than the desire amount we've got
  1562. // ourselves a pinch
  1563. if( Math.abs( touch.startSpan - currentSpan ) > touch.threshold ) {
  1564. touch.handled = true;
  1565. if( currentSpan < touch.startSpan ) {
  1566. activateOverview();
  1567. }
  1568. else {
  1569. deactivateOverview();
  1570. }
  1571. }
  1572. event.preventDefault();
  1573. }
  1574. // There was only one touch point, look for a swipe
  1575. else if( event.touches.length === 1 && touch.startCount !== 2 ) {
  1576. var deltaX = currentX - touch.startX,
  1577. deltaY = currentY - touch.startY;
  1578. if( deltaX > touch.threshold && Math.abs( deltaX ) > Math.abs( deltaY ) ) {
  1579. touch.handled = true;
  1580. navigateLeft();
  1581. }
  1582. else if( deltaX < -touch.threshold && Math.abs( deltaX ) > Math.abs( deltaY ) ) {
  1583. touch.handled = true;
  1584. navigateRight();
  1585. }
  1586. else if( deltaY > touch.threshold ) {
  1587. touch.handled = true;
  1588. navigateUp();
  1589. }
  1590. else if( deltaY < -touch.threshold ) {
  1591. touch.handled = true;
  1592. navigateDown();
  1593. }
  1594. event.preventDefault();
  1595. }
  1596. }
  1597. // There's a bug with swiping on some Android devices unless
  1598. // the default action is always prevented
  1599. else if( navigator.userAgent.match( /android/gi ) ) {
  1600. event.preventDefault();
  1601. }
  1602. }
  1603. /**
  1604. * Handler for the 'touchend' event.
  1605. */
  1606. function onTouchEnd( event ) {
  1607. touch.handled = false;
  1608. }
  1609. /**
  1610. * Convert pointer down to touch start.
  1611. */
  1612. function onPointerDown( event ) {
  1613. if( event.pointerType === event.MSPOINTER_TYPE_TOUCH ) {
  1614. event.touches = [{ clientX: event.clientX, clientY: event.clientY }];
  1615. onTouchStart( event );
  1616. }
  1617. }
  1618. /**
  1619. * Convert pointer move to touch move.
  1620. */
  1621. function onPointerMove( event ) {
  1622. if( event.pointerType === event.MSPOINTER_TYPE_TOUCH ) {
  1623. event.touches = [{ clientX: event.clientX, clientY: event.clientY }];
  1624. onTouchMove( event );
  1625. }
  1626. }
  1627. /**
  1628. * Convert pointer up to touch end.
  1629. */
  1630. function onPointerUp( event ) {
  1631. if( event.pointerType === event.MSPOINTER_TYPE_TOUCH ) {
  1632. event.touches = [{ clientX: event.clientX, clientY: event.clientY }];
  1633. onTouchEnd( event );
  1634. }
  1635. }
  1636. /**
  1637. * Handles mouse wheel scrolling, throttled to avoid skipping
  1638. * multiple slides.
  1639. */
  1640. function onDocumentMouseScroll( event ) {
  1641. clearTimeout( mouseWheelTimeout );
  1642. mouseWheelTimeout = setTimeout( function() {
  1643. var delta = event.detail || -event.wheelDelta;
  1644. if( delta > 0 ) {
  1645. navigateNext();
  1646. }
  1647. else {
  1648. navigatePrev();
  1649. }
  1650. }, 100 );
  1651. }
  1652. /**
  1653. * Clicking on the progress bar results in a navigation to the
  1654. * closest approximate horizontal slide using this equation:
  1655. *
  1656. * ( clickX / presentationWidth ) * numberOfSlides
  1657. */
  1658. function onProgressClicked( event ) {
  1659. event.preventDefault();
  1660. var slidesTotal = toArray( document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) ).length;
  1661. var slideIndex = Math.floor( ( event.clientX / dom.wrapper.offsetWidth ) * slidesTotal );
  1662. slide( slideIndex );
  1663. }
  1664. /**
  1665. * Event handler for navigation control buttons.
  1666. */
  1667. function onNavigateLeftClicked( event ) { event.preventDefault(); navigateLeft(); }
  1668. function onNavigateRightClicked( event ) { event.preventDefault(); navigateRight(); }
  1669. function onNavigateUpClicked( event ) { event.preventDefault(); navigateUp(); }
  1670. function onNavigateDownClicked( event ) { event.preventDefault(); navigateDown(); }
  1671. function onNavigatePrevClicked( event ) { event.preventDefault(); navigatePrev(); }
  1672. function onNavigateNextClicked( event ) { event.preventDefault(); navigateNext(); }
  1673. /**
  1674. * Handler for the window level 'hashchange' event.
  1675. */
  1676. function onWindowHashChange( event ) {
  1677. readURL();
  1678. }
  1679. /**
  1680. * Handler for the window level 'resize' event.
  1681. */
  1682. function onWindowResize( event ) {
  1683. layout();
  1684. }
  1685. /**
  1686. * Invoked when a slide is and we're in the overview.
  1687. */
  1688. function onOverviewSlideClicked( event ) {
  1689. // TODO There's a bug here where the event listeners are not
  1690. // removed after deactivating the overview.
  1691. if( eventsAreBound && isOverview() ) {
  1692. event.preventDefault();
  1693. var element = event.target;
  1694. while( element && !element.nodeName.match( /section/gi ) ) {
  1695. element = element.parentNode;
  1696. }
  1697. if( element && !element.classList.contains( 'disabled' ) ) {
  1698. deactivateOverview();
  1699. if( element.nodeName.match( /section/gi ) ) {
  1700. var h = parseInt( element.getAttribute( 'data-index-h' ), 10 ),
  1701. v = parseInt( element.getAttribute( 'data-index-v' ), 10 );
  1702. slide( h, v );
  1703. }
  1704. }
  1705. }
  1706. }
  1707. // --------------------------------------------------------------------//
  1708. // ------------------------------- API --------------------------------//
  1709. // --------------------------------------------------------------------//
  1710. return {
  1711. initialize: initialize,
  1712. configure: configure,
  1713. sync: sync,
  1714. // Navigation methods
  1715. slide: slide,
  1716. left: navigateLeft,
  1717. right: navigateRight,
  1718. up: navigateUp,
  1719. down: navigateDown,
  1720. prev: navigatePrev,
  1721. next: navigateNext,
  1722. prevFragment: previousFragment,
  1723. nextFragment: nextFragment,
  1724. // Deprecated aliases
  1725. navigateTo: slide,
  1726. navigateLeft: navigateLeft,
  1727. navigateRight: navigateRight,
  1728. navigateUp: navigateUp,
  1729. navigateDown: navigateDown,
  1730. navigatePrev: navigatePrev,
  1731. navigateNext: navigateNext,
  1732. // Forces an update in slide layout
  1733. layout: layout,
  1734. // Returns an object with the available routes as booleans (left/right/top/bottom)
  1735. availableRoutes: availableRoutes,
  1736. // Returns an object with the available fragments as booleans (prev/next)
  1737. availableFragments: availableFragments,
  1738. // Toggles the overview mode on/off
  1739. toggleOverview: toggleOverview,
  1740. // Toggles the "black screen" mode on/off
  1741. togglePause: togglePause,
  1742. // State checks
  1743. isOverview: isOverview,
  1744. isPaused: isPaused,
  1745. // Adds or removes all internal event listeners (such as keyboard)
  1746. addEventListeners: addEventListeners,
  1747. removeEventListeners: removeEventListeners,
  1748. // Returns the indices of the current, or specified, slide
  1749. getIndices: getIndices,
  1750. // Returns the slide at the specified index, y is optional
  1751. getSlide: function( x, y ) {
  1752. var horizontalSlide = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR )[ x ];
  1753. var verticalSlides = horizontalSlide && horizontalSlide.querySelectorAll( 'section' );
  1754. if( typeof y !== 'undefined' ) {
  1755. return verticalSlides ? verticalSlides[ y ] : undefined;
  1756. }
  1757. return horizontalSlide;
  1758. },
  1759. // Returns the previous slide element, may be null
  1760. getPreviousSlide: function() {
  1761. return previousSlide;
  1762. },
  1763. // Returns the current slide element
  1764. getCurrentSlide: function() {
  1765. return currentSlide;
  1766. },
  1767. // Returns the current scale of the presentation content
  1768. getScale: function() {
  1769. return scale;
  1770. },
  1771. // Returns the current configuration object
  1772. getConfig: function() {
  1773. return config;
  1774. },
  1775. // Helper method, retrieves query string as a key/value hash
  1776. getQueryHash: function() {
  1777. var query = {};
  1778. location.search.replace( /[A-Z0-9]+?=(\w*)/gi, function(a) {
  1779. query[ a.split( '=' ).shift() ] = a.split( '=' ).pop();
  1780. } );
  1781. return query;
  1782. },
  1783. // Returns true if we're currently on the first slide
  1784. isFirstSlide: function() {
  1785. return document.querySelector( SLIDES_SELECTOR + '.past' ) == null ? true : false;
  1786. },
  1787. // Returns true if we're currently on the last slide
  1788. isLastSlide: function() {
  1789. if( currentSlide && currentSlide.classList.contains( '.stack' ) ) {
  1790. return currentSlide.querySelector( SLIDES_SELECTOR + '.future' ) == null ? true : false;
  1791. }
  1792. else {
  1793. return document.querySelector( SLIDES_SELECTOR + '.future' ) == null ? true : false;
  1794. }
  1795. },
  1796. // Forward event binding to the reveal DOM element
  1797. addEventListener: function( type, listener, useCapture ) {
  1798. if( 'addEventListener' in window ) {
  1799. ( dom.wrapper || document.querySelector( '.reveal' ) ).addEventListener( type, listener, useCapture );
  1800. }
  1801. },
  1802. removeEventListener: function( type, listener, useCapture ) {
  1803. if( 'addEventListener' in window ) {
  1804. ( dom.wrapper || document.querySelector( '.reveal' ) ).removeEventListener( type, listener, useCapture );
  1805. }
  1806. }
  1807. };
  1808. })();