reveal.js 71 KB

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