reveal.js 71 KB

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