reveal.js 64 KB

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