reveal.js 69 KB

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