reveal.js 72 KB

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