reveal.js 63 KB

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