reveal.js 74 KB

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