reveal.js 74 KB

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