reveal.js 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675
  1. /*!
  2. * reveal.js
  3. * http://lab.hakim.se/reveal-js
  4. * MIT licensed
  5. *
  6. * Copyright (C) 2013 Hakim El Hattab, http://hakim.se
  7. */
  8. var Reveal = (function(){
  9. 'use strict';
  10. var SLIDES_SELECTOR = '.reveal .slides section',
  11. HORIZONTAL_SLIDES_SELECTOR = '.reveal .slides>section',
  12. VERTICAL_SLIDES_SELECTOR = '.reveal .slides>section.present>section',
  13. HOME_SLIDE_SELECTOR = '.reveal .slides>section:first-child',
  14. // Configurations defaults, can be overridden at initialization time
  15. config = {
  16. // The "normal" size of the presentation, aspect ratio will be preserved
  17. // when the presentation is scaled to fit different resolutions
  18. width: 960,
  19. height: 700,
  20. // Factor of the display size that should remain empty around the content
  21. margin: 0.1,
  22. // Bounds for smallest/largest possible scale to apply to content
  23. minScale: 0.2,
  24. maxScale: 1.0,
  25. // Display controls in the bottom right corner
  26. controls: true,
  27. // Display a presentation progress bar
  28. progress: true,
  29. // Push each slide change to the browser history
  30. history: false,
  31. // Enable keyboard shortcuts for navigation
  32. keyboard: true,
  33. // Enable the slide overview mode
  34. overview: true,
  35. // Vertical centring of slides
  36. center: true,
  37. // Enables touch navigation on devices with touch input
  38. touch: true,
  39. // Loop the presentation
  40. loop: false,
  41. // Change the presentation direction to be RTL
  42. rtl: false,
  43. // Turns fragments on and off globally
  44. fragments: true,
  45. // Number of milliseconds between automatically proceeding to the
  46. // next slide, disabled when set to 0, this value can be overwritten
  47. // by using a data-autoslide attribute on your slides
  48. autoSlide: 0,
  49. // Enable slide navigation via mouse wheel
  50. mouseWheel: false,
  51. // Apply a 3D roll to links on hover
  52. rollingLinks: false,
  53. // Opens links in an iframe preview overlay
  54. previewLinks: false,
  55. // Theme (see /css/theme)
  56. theme: null,
  57. // Transition style
  58. transition: 'default', // default/cube/page/concave/zoom/linear/fade/none
  59. // Transition speed
  60. transitionSpeed: 'default', // default/fast/slow
  61. // Transition style for full page slide backgrounds
  62. backgroundTransition: 'default', // default/linear/none
  63. // Number of slides away from the current that are visible
  64. viewDistance: 3,
  65. // Script dependencies to load
  66. dependencies: []
  67. },
  68. // The current auto-slide duration
  69. autoSlide = 0,
  70. // The horizontal and vertical index of the currently active slide
  71. indexh,
  72. indexv,
  73. // The previous and current slide HTML elements
  74. previousSlide,
  75. currentSlide,
  76. // Slides may hold a data-state attribute which we pick up and apply
  77. // as a class to the body. This list contains the combined state of
  78. // all current slides.
  79. state = [],
  80. // The current scale of the presentation (see width/height config)
  81. scale = 1,
  82. // Cached references to DOM elements
  83. dom = {},
  84. // Client support for CSS 3D transforms, see #checkCapabilities()
  85. supports3DTransforms,
  86. // Client support for CSS 2D transforms, see #checkCapabilities()
  87. supports2DTransforms,
  88. // Client is a mobile device, see #checkCapabilities()
  89. isMobileDevice,
  90. // Throttles mouse wheel navigation
  91. lastMouseWheelStep = 0,
  92. // An interval used to automatically move on to the next slide
  93. autoSlideTimeout = 0,
  94. // Delays updates to the URL due to a Chrome thumbnailer bug
  95. writeURLTimeout = 0,
  96. // A delay used to activate the overview mode
  97. activateOverviewTimeout = 0,
  98. // A delay used to deactivate the overview mode
  99. deactivateOverviewTimeout = 0,
  100. // Flags if the interaction event listeners are bound
  101. eventsAreBound = false,
  102. // Holds information about the currently ongoing touch input
  103. touch = {
  104. startX: 0,
  105. startY: 0,
  106. startSpan: 0,
  107. startCount: 0,
  108. handled: false,
  109. threshold: 80
  110. };
  111. /**
  112. * Starts up the presentation if the client is capable.
  113. */
  114. function initialize( options ) {
  115. checkCapabilities();
  116. if( !supports2DTransforms && !supports3DTransforms ) {
  117. document.body.setAttribute( 'class', 'no-transforms' );
  118. // If the browser doesn't support core features we won't be
  119. // using JavaScript to control the presentation
  120. return;
  121. }
  122. // Force a layout when the whole page, incl fonts, has loaded
  123. window.addEventListener( 'load', layout, false );
  124. // Copy options over to our config object
  125. extend( config, options );
  126. // Hide the address bar in mobile browsers
  127. hideAddressBar();
  128. // Loads the dependencies and continues to #start() once done
  129. load();
  130. }
  131. /**
  132. * Inspect the client to see what it's capable of, this
  133. * should only happens once per runtime.
  134. */
  135. function checkCapabilities() {
  136. supports3DTransforms = 'WebkitPerspective' in document.body.style ||
  137. 'MozPerspective' in document.body.style ||
  138. 'msPerspective' in document.body.style ||
  139. 'OPerspective' in document.body.style ||
  140. 'perspective' in document.body.style;
  141. supports2DTransforms = 'WebkitTransform' in document.body.style ||
  142. 'MozTransform' in document.body.style ||
  143. 'msTransform' in document.body.style ||
  144. 'OTransform' in document.body.style ||
  145. 'transform' in document.body.style;
  146. isMobileDevice = navigator.userAgent.match( /(iphone|ipod|android)/gi );
  147. }
  148. /**
  149. * Finds and stores references to DOM elements which are
  150. * required by the presentation. If a required element is
  151. * not found, it is created.
  152. */
  153. function setupDOM() {
  154. // Cache references to key DOM elements
  155. dom.theme = document.querySelector( '#theme' );
  156. dom.wrapper = document.querySelector( '.reveal' );
  157. dom.slides = document.querySelector( '.reveal .slides' );
  158. // Background element
  159. if( !document.querySelector( '.reveal .backgrounds' ) ) {
  160. dom.background = document.createElement( 'div' );
  161. dom.background.classList.add( 'backgrounds' );
  162. dom.wrapper.appendChild( dom.background );
  163. }
  164. // Progress bar
  165. if( !dom.wrapper.querySelector( '.progress' ) ) {
  166. var progressElement = document.createElement( 'div' );
  167. progressElement.classList.add( 'progress' );
  168. progressElement.innerHTML = '<span></span>';
  169. dom.wrapper.appendChild( progressElement );
  170. }
  171. // Arrow controls
  172. if( !dom.wrapper.querySelector( '.controls' ) ) {
  173. var controlsElement = document.createElement( 'aside' );
  174. controlsElement.classList.add( 'controls' );
  175. controlsElement.innerHTML = '<div class="navigate-left"></div>' +
  176. '<div class="navigate-right"></div>' +
  177. '<div class="navigate-up"></div>' +
  178. '<div class="navigate-down"></div>';
  179. dom.wrapper.appendChild( controlsElement );
  180. }
  181. // State background element [DEPRECATED]
  182. if( !dom.wrapper.querySelector( '.state-background' ) ) {
  183. var stateBackgroundElement = document.createElement( 'div' );
  184. stateBackgroundElement.classList.add( 'state-background' );
  185. dom.wrapper.appendChild( stateBackgroundElement );
  186. }
  187. // Overlay graphic which is displayed during the paused mode
  188. if( !dom.wrapper.querySelector( '.pause-overlay' ) ) {
  189. var pausedElement = document.createElement( 'div' );
  190. pausedElement.classList.add( 'pause-overlay' );
  191. dom.wrapper.appendChild( pausedElement );
  192. }
  193. // Cache references to elements
  194. dom.progress = document.querySelector( '.reveal .progress' );
  195. dom.progressbar = document.querySelector( '.reveal .progress span' );
  196. if ( config.controls ) {
  197. dom.controls = document.querySelector( '.reveal .controls' );
  198. // There can be multiple instances of controls throughout the page
  199. dom.controlsLeft = toArray( document.querySelectorAll( '.navigate-left' ) );
  200. dom.controlsRight = toArray( document.querySelectorAll( '.navigate-right' ) );
  201. dom.controlsUp = toArray( document.querySelectorAll( '.navigate-up' ) );
  202. dom.controlsDown = toArray( document.querySelectorAll( '.navigate-down' ) );
  203. dom.controlsPrev = toArray( document.querySelectorAll( '.navigate-prev' ) );
  204. dom.controlsNext = toArray( document.querySelectorAll( '.navigate-next' ) );
  205. }
  206. }
  207. /**
  208. * Creates the slide background elements and appends them
  209. * to the background container. One element is created per
  210. * slide no matter if the given slide has visible background.
  211. */
  212. function createBackgrounds() {
  213. if( isPrintingPDF() ) {
  214. document.body.classList.add( 'print-pdf' );
  215. }
  216. // Clear prior backgrounds
  217. dom.background.innerHTML = '';
  218. dom.background.classList.add( 'no-transition' );
  219. // Helper method for creating a background element for the
  220. // given slide
  221. function _createBackground( slide, container ) {
  222. var data = {
  223. background: slide.getAttribute( 'data-background' ),
  224. backgroundSize: slide.getAttribute( 'data-background-size' ),
  225. backgroundImage: slide.getAttribute( 'data-background-image' ),
  226. backgroundColor: slide.getAttribute( 'data-background-color' ),
  227. backgroundRepeat: slide.getAttribute( 'data-background-repeat' ),
  228. backgroundPosition: slide.getAttribute( 'data-background-position' ),
  229. backgroundTransition: slide.getAttribute( 'data-background-transition' )
  230. };
  231. var element = document.createElement( 'div' );
  232. element.className = 'slide-background';
  233. if( data.background ) {
  234. // Auto-wrap image urls in url(...)
  235. if( /^(http|file|\/\/)/gi.test( data.background ) || /\.(png|jpg|jpeg|gif|bmp)$/gi.test( data.background ) ) {
  236. element.style.backgroundImage = 'url('+ data.background +')';
  237. }
  238. else {
  239. element.style.background = data.background;
  240. }
  241. }
  242. // Additional and optional background properties
  243. if( data.backgroundSize ) element.style.backgroundSize = data.backgroundSize;
  244. if( data.backgroundImage ) element.style.backgroundImage = 'url("' + data.backgroundImage + '")';
  245. if( data.backgroundColor ) element.style.backgroundColor = data.backgroundColor;
  246. if( data.backgroundRepeat ) element.style.backgroundRepeat = data.backgroundRepeat;
  247. if( data.backgroundPosition ) element.style.backgroundPosition = data.backgroundPosition;
  248. if( data.backgroundTransition ) element.setAttribute( 'data-background-transition', data.backgroundTransition );
  249. container.appendChild( element );
  250. return element;
  251. }
  252. // Iterate over all horizontal slides
  253. toArray( document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) ).forEach( function( slideh ) {
  254. var backgroundStack;
  255. if( isPrintingPDF() ) {
  256. backgroundStack = _createBackground( slideh, slideh );
  257. }
  258. else {
  259. backgroundStack = _createBackground( slideh, dom.background );
  260. }
  261. // Iterate over all vertical slides
  262. toArray( slideh.querySelectorAll( 'section' ) ).forEach( function( slidev ) {
  263. if( isPrintingPDF() ) {
  264. _createBackground( slidev, slidev );
  265. }
  266. else {
  267. _createBackground( slidev, backgroundStack );
  268. }
  269. } );
  270. } );
  271. }
  272. /**
  273. * Hides the address bar if we're on a mobile device.
  274. */
  275. function hideAddressBar() {
  276. if( /iphone|ipod|android/gi.test( navigator.userAgent ) && !/crios/gi.test( navigator.userAgent ) ) {
  277. // Events that should trigger the address bar to hide
  278. window.addEventListener( 'load', removeAddressBar, false );
  279. window.addEventListener( 'orientationchange', removeAddressBar, false );
  280. }
  281. }
  282. /**
  283. * Loads the dependencies of reveal.js. Dependencies are
  284. * defined via the configuration option 'dependencies'
  285. * and will be loaded prior to starting/binding reveal.js.
  286. * Some dependencies may have an 'async' flag, if so they
  287. * will load after reveal.js has been started up.
  288. */
  289. function load() {
  290. var scripts = [],
  291. scriptsAsync = [];
  292. for( var i = 0, len = config.dependencies.length; i < len; i++ ) {
  293. var s = config.dependencies[i];
  294. // Load if there's no condition or the condition is truthy
  295. if( !s.condition || s.condition() ) {
  296. if( s.async ) {
  297. scriptsAsync.push( s.src );
  298. }
  299. else {
  300. scripts.push( s.src );
  301. }
  302. // Extension may contain callback functions
  303. if( typeof s.callback === 'function' ) {
  304. head.ready( s.src.match( /([\w\d_\-]*)\.?js$|[^\\\/]*$/i )[0], s.callback );
  305. }
  306. }
  307. }
  308. // Called once synchronous scripts finish loading
  309. function proceed() {
  310. if( scriptsAsync.length ) {
  311. // Load asynchronous scripts
  312. head.js.apply( null, scriptsAsync );
  313. }
  314. start();
  315. }
  316. if( scripts.length ) {
  317. head.ready( proceed );
  318. // Load synchronous scripts
  319. head.js.apply( null, scripts );
  320. }
  321. else {
  322. proceed();
  323. }
  324. }
  325. /**
  326. * Starts up reveal.js by binding input events and navigating
  327. * to the current URL deeplink if there is one.
  328. */
  329. function start() {
  330. // Make sure we've got all the DOM elements we need
  331. setupDOM();
  332. // Updates the presentation to match the current configuration values
  333. configure();
  334. // Read the initial hash
  335. readURL();
  336. // Notify listeners that the presentation is ready but use a 1ms
  337. // timeout to ensure it's not fired synchronously after #initialize()
  338. setTimeout( function() {
  339. dispatchEvent( 'ready', {
  340. 'indexh': indexh,
  341. 'indexv': indexv,
  342. 'currentSlide': currentSlide
  343. } );
  344. }, 1 );
  345. }
  346. /**
  347. * Applies the configuration settings from the config
  348. * object. May be called multiple times.
  349. */
  350. function configure( options ) {
  351. dom.wrapper.classList.remove( config.transition );
  352. // New config options may be passed when this method
  353. // is invoked through the API after initialization
  354. if( typeof options === 'object' ) extend( config, options );
  355. // Force linear transition based on browser capabilities
  356. if( supports3DTransforms === false ) config.transition = 'linear';
  357. dom.wrapper.classList.add( config.transition );
  358. dom.wrapper.setAttribute( 'data-transition-speed', config.transitionSpeed );
  359. dom.wrapper.setAttribute( 'data-background-transition', config.backgroundTransition );
  360. if( dom.controls ) {
  361. dom.controls.style.display = ( config.controls && dom.controls ) ? 'block' : 'none';
  362. }
  363. if( dom.progress ) {
  364. dom.progress.style.display = ( config.progress && dom.progress ) ? 'block' : 'none';
  365. }
  366. if( config.rtl ) {
  367. dom.wrapper.classList.add( 'rtl' );
  368. }
  369. else {
  370. dom.wrapper.classList.remove( 'rtl' );
  371. }
  372. if( config.center ) {
  373. dom.wrapper.classList.add( 'center' );
  374. }
  375. else {
  376. dom.wrapper.classList.remove( 'center' );
  377. }
  378. if( config.mouseWheel ) {
  379. document.addEventListener( 'DOMMouseScroll', onDocumentMouseScroll, false ); // FF
  380. document.addEventListener( 'mousewheel', onDocumentMouseScroll, false );
  381. }
  382. else {
  383. document.removeEventListener( 'DOMMouseScroll', onDocumentMouseScroll, false ); // FF
  384. document.removeEventListener( 'mousewheel', onDocumentMouseScroll, false );
  385. }
  386. // Rolling 3D links
  387. if( config.rollingLinks ) {
  388. enableRollingLinks();
  389. }
  390. else {
  391. disableRollingLinks();
  392. }
  393. // Iframe link previews
  394. if( config.previewLinks ) {
  395. enablePreviewLinks();
  396. }
  397. else {
  398. disablePreviewLinks();
  399. enablePreviewLinks( '[data-preview-link]' );
  400. }
  401. // Load the theme in the config, if it's not already loaded
  402. if( config.theme && dom.theme ) {
  403. var themeURL = dom.theme.getAttribute( 'href' );
  404. var themeFinder = /[^\/]*?(?=\.css)/;
  405. var themeName = themeURL.match(themeFinder)[0];
  406. if( config.theme !== themeName ) {
  407. themeURL = themeURL.replace(themeFinder, config.theme);
  408. dom.theme.setAttribute( 'href', themeURL );
  409. }
  410. }
  411. sync();
  412. }
  413. /**
  414. * Binds all event listeners.
  415. */
  416. function addEventListeners() {
  417. eventsAreBound = true;
  418. window.addEventListener( 'hashchange', onWindowHashChange, false );
  419. window.addEventListener( 'resize', onWindowResize, false );
  420. if( config.touch ) {
  421. dom.wrapper.addEventListener( 'touchstart', onTouchStart, false );
  422. dom.wrapper.addEventListener( 'touchmove', onTouchMove, false );
  423. dom.wrapper.addEventListener( 'touchend', onTouchEnd, false );
  424. // Support pointer-style touch interaction as well
  425. if( window.navigator.msPointerEnabled ) {
  426. dom.wrapper.addEventListener( 'MSPointerDown', onPointerDown, false );
  427. dom.wrapper.addEventListener( 'MSPointerMove', onPointerMove, false );
  428. dom.wrapper.addEventListener( 'MSPointerUp', onPointerUp, false );
  429. }
  430. }
  431. if( config.keyboard ) {
  432. document.addEventListener( 'keydown', onDocumentKeyDown, false );
  433. }
  434. if ( config.progress && dom.progress ) {
  435. dom.progress.addEventListener( 'click', onProgressClicked, false );
  436. }
  437. if ( config.controls && dom.controls ) {
  438. [ 'touchstart', 'click' ].forEach( function( eventName ) {
  439. dom.controlsLeft.forEach( function( el ) { el.addEventListener( eventName, onNavigateLeftClicked, false ); } );
  440. dom.controlsRight.forEach( function( el ) { el.addEventListener( eventName, onNavigateRightClicked, false ); } );
  441. dom.controlsUp.forEach( function( el ) { el.addEventListener( eventName, onNavigateUpClicked, false ); } );
  442. dom.controlsDown.forEach( function( el ) { el.addEventListener( eventName, onNavigateDownClicked, false ); } );
  443. dom.controlsPrev.forEach( function( el ) { el.addEventListener( eventName, onNavigatePrevClicked, false ); } );
  444. dom.controlsNext.forEach( function( el ) { el.addEventListener( eventName, onNavigateNextClicked, false ); } );
  445. } );
  446. }
  447. }
  448. /**
  449. * Unbinds all event listeners.
  450. */
  451. function removeEventListeners() {
  452. eventsAreBound = false;
  453. document.removeEventListener( 'keydown', onDocumentKeyDown, false );
  454. window.removeEventListener( 'hashchange', onWindowHashChange, false );
  455. window.removeEventListener( 'resize', onWindowResize, false );
  456. dom.wrapper.removeEventListener( 'touchstart', onTouchStart, false );
  457. dom.wrapper.removeEventListener( 'touchmove', onTouchMove, false );
  458. dom.wrapper.removeEventListener( 'touchend', onTouchEnd, false );
  459. if( window.navigator.msPointerEnabled ) {
  460. dom.wrapper.removeEventListener( 'MSPointerDown', onPointerDown, false );
  461. dom.wrapper.removeEventListener( 'MSPointerMove', onPointerMove, false );
  462. dom.wrapper.removeEventListener( 'MSPointerUp', onPointerUp, false );
  463. }
  464. if ( config.progress && dom.progress ) {
  465. dom.progress.removeEventListener( 'click', onProgressClicked, false );
  466. }
  467. if ( config.controls && dom.controls ) {
  468. [ 'touchstart', 'click' ].forEach( function( eventName ) {
  469. dom.controlsLeft.forEach( function( el ) { el.removeEventListener( eventName, onNavigateLeftClicked, false ); } );
  470. dom.controlsRight.forEach( function( el ) { el.removeEventListener( eventName, onNavigateRightClicked, false ); } );
  471. dom.controlsUp.forEach( function( el ) { el.removeEventListener( eventName, onNavigateUpClicked, false ); } );
  472. dom.controlsDown.forEach( function( el ) { el.removeEventListener( eventName, onNavigateDownClicked, false ); } );
  473. dom.controlsPrev.forEach( function( el ) { el.removeEventListener( eventName, onNavigatePrevClicked, false ); } );
  474. dom.controlsNext.forEach( function( el ) { el.removeEventListener( eventName, onNavigateNextClicked, false ); } );
  475. } );
  476. }
  477. }
  478. /**
  479. * Extend object a with the properties of object b.
  480. * If there's a conflict, object b takes precedence.
  481. */
  482. function extend( a, b ) {
  483. for( var i in b ) {
  484. a[ i ] = b[ i ];
  485. }
  486. }
  487. /**
  488. * Converts the target object to an array.
  489. */
  490. function toArray( o ) {
  491. return Array.prototype.slice.call( o );
  492. }
  493. /**
  494. * Measures the distance in pixels between point a
  495. * and point b.
  496. *
  497. * @param {Object} a point with x/y properties
  498. * @param {Object} b point with x/y properties
  499. */
  500. function distanceBetween( a, b ) {
  501. var dx = a.x - b.x,
  502. dy = a.y - b.y;
  503. return Math.sqrt( dx*dx + dy*dy );
  504. }
  505. /**
  506. * Retrieves the height of the given element by looking
  507. * at the position and height of its immediate children.
  508. */
  509. function getAbsoluteHeight( element ) {
  510. var height = 0;
  511. if( element ) {
  512. var absoluteChildren = 0;
  513. toArray( element.childNodes ).forEach( function( child ) {
  514. if( typeof child.offsetTop === 'number' && child.style ) {
  515. // Count # of abs children
  516. if( child.style.position === 'absolute' ) {
  517. absoluteChildren += 1;
  518. }
  519. height = Math.max( height, child.offsetTop + child.offsetHeight );
  520. }
  521. } );
  522. // If there are no absolute children, use offsetHeight
  523. if( absoluteChildren === 0 ) {
  524. height = element.offsetHeight;
  525. }
  526. }
  527. return height;
  528. }
  529. /**
  530. * Checks if this instance is being used to print a PDF.
  531. */
  532. function isPrintingPDF() {
  533. return ( /print-pdf/gi ).test( window.location.search );
  534. }
  535. /**
  536. * Causes the address bar to hide on mobile devices,
  537. * more vertical space ftw.
  538. */
  539. function removeAddressBar() {
  540. if( window.orientation === 0 ) {
  541. document.documentElement.style.overflow = 'scroll';
  542. document.body.style.height = '120%';
  543. }
  544. else {
  545. document.documentElement.style.overflow = '';
  546. document.body.style.height = '100%';
  547. }
  548. setTimeout( function() {
  549. window.scrollTo( 0, 1 );
  550. }, 10 );
  551. }
  552. /**
  553. * Dispatches an event of the specified type from the
  554. * reveal DOM element.
  555. */
  556. function dispatchEvent( type, properties ) {
  557. var event = document.createEvent( "HTMLEvents", 1, 2 );
  558. event.initEvent( type, true, true );
  559. extend( event, properties );
  560. dom.wrapper.dispatchEvent( event );
  561. }
  562. /**
  563. * Wrap all links in 3D goodness.
  564. */
  565. function enableRollingLinks() {
  566. if( supports3DTransforms && !( 'msPerspective' in document.body.style ) ) {
  567. var anchors = document.querySelectorAll( SLIDES_SELECTOR + ' a:not(.image)' );
  568. for( var i = 0, len = anchors.length; i < len; i++ ) {
  569. var anchor = anchors[i];
  570. if( anchor.textContent && !anchor.querySelector( '*' ) && ( !anchor.className || !anchor.classList.contains( anchor, 'roll' ) ) ) {
  571. var span = document.createElement('span');
  572. span.setAttribute('data-title', anchor.text);
  573. span.innerHTML = anchor.innerHTML;
  574. anchor.classList.add( 'roll' );
  575. anchor.innerHTML = '';
  576. anchor.appendChild(span);
  577. }
  578. }
  579. }
  580. }
  581. /**
  582. * Unwrap all 3D links.
  583. */
  584. function disableRollingLinks() {
  585. var anchors = document.querySelectorAll( SLIDES_SELECTOR + ' a.roll' );
  586. for( var i = 0, len = anchors.length; i < len; i++ ) {
  587. var anchor = anchors[i];
  588. var span = anchor.querySelector( 'span' );
  589. if( span ) {
  590. anchor.classList.remove( 'roll' );
  591. anchor.innerHTML = span.innerHTML;
  592. }
  593. }
  594. }
  595. /**
  596. * Bind preview frame links.
  597. */
  598. function enablePreviewLinks( selector ) {
  599. var anchors = toArray( document.querySelectorAll( selector ? selector : 'a' ) );
  600. anchors.forEach( function( element ) {
  601. if( /^(http|www)/gi.test( element.getAttribute( 'href' ) ) ) {
  602. element.addEventListener( 'click', onPreviewLinkClicked, false );
  603. }
  604. } );
  605. }
  606. /**
  607. * Unbind preview frame links.
  608. */
  609. function disablePreviewLinks() {
  610. var anchors = toArray( document.querySelectorAll( 'a' ) );
  611. anchors.forEach( function( element ) {
  612. if( /^(http|www)/gi.test( element.getAttribute( 'href' ) ) ) {
  613. element.removeEventListener( 'click', onPreviewLinkClicked, false );
  614. }
  615. } );
  616. }
  617. /**
  618. * Opens a preview window for the target URL.
  619. */
  620. function openPreview( url ) {
  621. closePreview();
  622. dom.preview = document.createElement( 'div' );
  623. dom.preview.classList.add( 'preview-link-overlay' );
  624. dom.wrapper.appendChild( dom.preview );
  625. dom.preview.innerHTML = [
  626. '<header>',
  627. '<a class="close" href="#"><span class="icon"></span></a>',
  628. '<a class="external" href="'+ url +'" target="_blank"><span class="icon"></span></a>',
  629. '</header>',
  630. '<div class="spinner"></div>',
  631. '<div class="viewport">',
  632. '<iframe src="'+ url +'"></iframe>',
  633. '</div>'
  634. ].join('');
  635. dom.preview.querySelector( 'iframe' ).addEventListener( 'load', function( event ) {
  636. dom.preview.classList.add( 'loaded' );
  637. }, false );
  638. dom.preview.querySelector( '.close' ).addEventListener( 'click', function( event ) {
  639. closePreview();
  640. event.preventDefault();
  641. }, false );
  642. dom.preview.querySelector( '.external' ).addEventListener( 'click', function( event ) {
  643. closePreview();
  644. }, false );
  645. setTimeout( function() {
  646. dom.preview.classList.add( 'visible' );
  647. }, 1 );
  648. }
  649. /**
  650. * Closes the iframe preview window.
  651. */
  652. function closePreview() {
  653. if( dom.preview ) {
  654. dom.preview.setAttribute( 'src', '' );
  655. dom.preview.parentNode.removeChild( dom.preview );
  656. dom.preview = null;
  657. }
  658. }
  659. /**
  660. * Return a sorted fragments list, ordered by an increasing
  661. * "data-fragment-index" attribute.
  662. *
  663. * Fragments will be revealed in the order that they are returned by
  664. * this function, so you can use the index attributes to control the
  665. * order of fragment appearance.
  666. *
  667. * To maintain a sensible default fragment order, fragments are presumed
  668. * to be passed in document order. This function adds a "fragment-index"
  669. * attribute to each node if such an attribute is not already present,
  670. * and sets that attribute to an integer value which is the position of
  671. * the fragment within the fragments list.
  672. */
  673. function sortFragments( fragments ) {
  674. var a = toArray( fragments );
  675. a.forEach( function( el, idx ) {
  676. if( !el.hasAttribute( 'data-fragment-index' ) ) {
  677. el.setAttribute( 'data-fragment-index', idx );
  678. }
  679. } );
  680. a.sort( function( l, r ) {
  681. return l.getAttribute( 'data-fragment-index' ) - r.getAttribute( 'data-fragment-index');
  682. } );
  683. return a;
  684. }
  685. /**
  686. * Applies JavaScript-controlled layout rules to the
  687. * presentation.
  688. */
  689. function layout() {
  690. if( dom.wrapper && !isPrintingPDF() ) {
  691. // Available space to scale within
  692. var availableWidth = dom.wrapper.offsetWidth,
  693. availableHeight = dom.wrapper.offsetHeight;
  694. // Reduce available space by margin
  695. availableWidth -= ( availableHeight * config.margin );
  696. availableHeight -= ( availableHeight * config.margin );
  697. // Dimensions of the content
  698. var slideWidth = config.width,
  699. slideHeight = config.height;
  700. // Slide width may be a percentage of available width
  701. if( typeof slideWidth === 'string' && /%$/.test( slideWidth ) ) {
  702. slideWidth = parseInt( slideWidth, 10 ) / 100 * availableWidth;
  703. }
  704. // Slide height may be a percentage of available height
  705. if( typeof slideHeight === 'string' && /%$/.test( slideHeight ) ) {
  706. slideHeight = parseInt( slideHeight, 10 ) / 100 * availableHeight;
  707. }
  708. dom.slides.style.width = slideWidth + 'px';
  709. dom.slides.style.height = slideHeight + 'px';
  710. // Determine scale of content to fit within available space
  711. scale = Math.min( availableWidth / slideWidth, availableHeight / slideHeight );
  712. // Respect max/min scale settings
  713. scale = Math.max( scale, config.minScale );
  714. scale = Math.min( scale, config.maxScale );
  715. // Prefer applying scale via zoom since Chrome blurs scaled content
  716. // with nested transforms
  717. if( typeof dom.slides.style.zoom !== 'undefined' && !navigator.userAgent.match( /(iphone|ipod|ipad|android)/gi ) ) {
  718. dom.slides.style.zoom = scale;
  719. }
  720. // Apply scale transform as a fallback
  721. else {
  722. var transform = 'translate(-50%, -50%) scale('+ scale +') translate(50%, 50%)';
  723. dom.slides.style.WebkitTransform = transform;
  724. dom.slides.style.MozTransform = transform;
  725. dom.slides.style.msTransform = transform;
  726. dom.slides.style.OTransform = transform;
  727. dom.slides.style.transform = transform;
  728. }
  729. // Select all slides, vertical and horizontal
  730. var slides = toArray( document.querySelectorAll( SLIDES_SELECTOR ) );
  731. for( var i = 0, len = slides.length; i < len; i++ ) {
  732. var slide = slides[ i ];
  733. // Don't bother updating invisible slides
  734. if( slide.style.display === 'none' ) {
  735. continue;
  736. }
  737. if( config.center ) {
  738. // Vertical stacks are not centred since their section
  739. // children will be
  740. if( slide.classList.contains( 'stack' ) ) {
  741. slide.style.top = 0;
  742. }
  743. else {
  744. slide.style.top = Math.max( - ( getAbsoluteHeight( slide ) / 2 ) - 20, -slideHeight / 2 ) + 'px';
  745. }
  746. }
  747. else {
  748. slide.style.top = '';
  749. }
  750. }
  751. updateProgress();
  752. }
  753. }
  754. /**
  755. * Stores the vertical index of a stack so that the same
  756. * vertical slide can be selected when navigating to and
  757. * from the stack.
  758. *
  759. * @param {HTMLElement} stack The vertical stack element
  760. * @param {int} v Index to memorize
  761. */
  762. function setPreviousVerticalIndex( stack, v ) {
  763. if( typeof stack === 'object' && typeof stack.setAttribute === 'function' ) {
  764. stack.setAttribute( 'data-previous-indexv', v || 0 );
  765. }
  766. }
  767. /**
  768. * Retrieves the vertical index which was stored using
  769. * #setPreviousVerticalIndex() or 0 if no previous index
  770. * exists.
  771. *
  772. * @param {HTMLElement} stack The vertical stack element
  773. */
  774. function getPreviousVerticalIndex( stack ) {
  775. if( typeof stack === 'object' && typeof stack.setAttribute === 'function' && stack.classList.contains( 'stack' ) ) {
  776. // Prefer manually defined start-indexv
  777. var attributeName = stack.hasAttribute( 'data-start-indexv' ) ? 'data-start-indexv' : 'data-previous-indexv';
  778. return parseInt( stack.getAttribute( attributeName ) || 0, 10 );
  779. }
  780. return 0;
  781. }
  782. /**
  783. * Displays the overview of slides (quick nav) by
  784. * scaling down and arranging all slide elements.
  785. *
  786. * Experimental feature, might be dropped if perf
  787. * can't be improved.
  788. */
  789. function activateOverview() {
  790. // Only proceed if enabled in config
  791. if( config.overview ) {
  792. // Don't auto-slide while in overview mode
  793. cancelAutoSlide();
  794. var wasActive = dom.wrapper.classList.contains( 'overview' );
  795. var depth = 2500;
  796. if( window.innerWidth < 400 ) {
  797. depth = 1000;
  798. }
  799. dom.wrapper.classList.add( 'overview' );
  800. dom.wrapper.classList.remove( 'exit-overview' );
  801. clearTimeout( activateOverviewTimeout );
  802. clearTimeout( deactivateOverviewTimeout );
  803. // Not the pretties solution, but need to let the overview
  804. // class apply first so that slides are measured accurately
  805. // before we can position them
  806. activateOverviewTimeout = setTimeout( function(){
  807. var horizontalSlides = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR );
  808. for( var i = 0, len1 = horizontalSlides.length; i < len1; i++ ) {
  809. var hslide = horizontalSlides[i],
  810. hoffset = config.rtl ? -105 : 105,
  811. htransform = 'translateZ(-'+ depth +'px) translate(' + ( ( i - indexh ) * hoffset ) + '%, 0%)';
  812. hslide.setAttribute( 'data-index-h', i );
  813. hslide.style.WebkitTransform = htransform;
  814. hslide.style.MozTransform = htransform;
  815. hslide.style.msTransform = htransform;
  816. hslide.style.OTransform = htransform;
  817. hslide.style.transform = htransform;
  818. if( hslide.classList.contains( 'stack' ) ) {
  819. var verticalSlides = hslide.querySelectorAll( 'section' );
  820. for( var j = 0, len2 = verticalSlides.length; j < len2; j++ ) {
  821. var verticalIndex = i === indexh ? indexv : getPreviousVerticalIndex( hslide );
  822. var vslide = verticalSlides[j],
  823. vtransform = 'translate(0%, ' + ( ( j - verticalIndex ) * 105 ) + '%)';
  824. vslide.setAttribute( 'data-index-h', i );
  825. vslide.setAttribute( 'data-index-v', j );
  826. vslide.style.WebkitTransform = vtransform;
  827. vslide.style.MozTransform = vtransform;
  828. vslide.style.msTransform = vtransform;
  829. vslide.style.OTransform = vtransform;
  830. vslide.style.transform = vtransform;
  831. // Navigate to this slide on click
  832. vslide.addEventListener( 'click', onOverviewSlideClicked, true );
  833. }
  834. }
  835. else {
  836. // Navigate to this slide on click
  837. hslide.addEventListener( 'click', onOverviewSlideClicked, true );
  838. }
  839. }
  840. updateSlidesVisibility();
  841. layout();
  842. if( !wasActive ) {
  843. // Notify observers of the overview showing
  844. dispatchEvent( 'overviewshown', {
  845. 'indexh': indexh,
  846. 'indexv': indexv,
  847. 'currentSlide': currentSlide
  848. } );
  849. }
  850. }, 10 );
  851. }
  852. }
  853. /**
  854. * Exits the slide overview and enters the currently
  855. * active slide.
  856. */
  857. function deactivateOverview() {
  858. // Only proceed if enabled in config
  859. if( config.overview ) {
  860. clearTimeout( activateOverviewTimeout );
  861. clearTimeout( deactivateOverviewTimeout );
  862. dom.wrapper.classList.remove( 'overview' );
  863. // Temporarily add a class so that transitions can do different things
  864. // depending on whether they are exiting/entering overview, or just
  865. // moving from slide to slide
  866. dom.wrapper.classList.add( 'exit-overview' );
  867. deactivateOverviewTimeout = setTimeout( function () {
  868. dom.wrapper.classList.remove( 'exit-overview' );
  869. }, 10);
  870. // Select all slides
  871. var slides = toArray( document.querySelectorAll( SLIDES_SELECTOR ) );
  872. for( var i = 0, len = slides.length; i < len; i++ ) {
  873. var element = slides[i];
  874. element.style.display = '';
  875. // Resets all transforms to use the external styles
  876. element.style.WebkitTransform = '';
  877. element.style.MozTransform = '';
  878. element.style.msTransform = '';
  879. element.style.OTransform = '';
  880. element.style.transform = '';
  881. element.removeEventListener( 'click', onOverviewSlideClicked, true );
  882. }
  883. slide( indexh, indexv );
  884. cueAutoSlide();
  885. // Notify observers of the overview hiding
  886. dispatchEvent( 'overviewhidden', {
  887. 'indexh': indexh,
  888. 'indexv': indexv,
  889. 'currentSlide': currentSlide
  890. } );
  891. }
  892. }
  893. /**
  894. * Toggles the slide overview mode on and off.
  895. *
  896. * @param {Boolean} override Optional flag which overrides the
  897. * toggle logic and forcibly sets the desired state. True means
  898. * overview is open, false means it's closed.
  899. */
  900. function toggleOverview( override ) {
  901. if( typeof override === 'boolean' ) {
  902. override ? activateOverview() : deactivateOverview();
  903. }
  904. else {
  905. isOverview() ? deactivateOverview() : activateOverview();
  906. }
  907. }
  908. /**
  909. * Checks if the overview is currently active.
  910. *
  911. * @return {Boolean} true if the overview is active,
  912. * false otherwise
  913. */
  914. function isOverview() {
  915. return dom.wrapper.classList.contains( 'overview' );
  916. }
  917. /**
  918. * Checks if the current or specified slide is vertical
  919. * (nested within another slide).
  920. *
  921. * @param {HTMLElement} slide [optional] The slide to check
  922. * orientation of
  923. */
  924. function isVerticalSlide( slide ) {
  925. // Prefer slide argument, otherwise use current slide
  926. slide = slide ? slide : currentSlide;
  927. return slide && !!slide.parentNode.nodeName.match( /section/i );
  928. }
  929. /**
  930. * Handling the fullscreen functionality via the fullscreen API
  931. *
  932. * @see http://fullscreen.spec.whatwg.org/
  933. * @see https://developer.mozilla.org/en-US/docs/DOM/Using_fullscreen_mode
  934. */
  935. function enterFullscreen() {
  936. var element = document.body;
  937. // Check which implementation is available
  938. var requestMethod = element.requestFullScreen ||
  939. element.webkitRequestFullscreen ||
  940. element.webkitRequestFullScreen ||
  941. element.mozRequestFullScreen ||
  942. element.msRequestFullScreen;
  943. if( requestMethod ) {
  944. requestMethod.apply( element );
  945. }
  946. }
  947. /**
  948. * Enters the paused mode which fades everything on screen to
  949. * black.
  950. */
  951. function pause() {
  952. var wasPaused = dom.wrapper.classList.contains( 'paused' );
  953. cancelAutoSlide();
  954. dom.wrapper.classList.add( 'paused' );
  955. if( wasPaused === false ) {
  956. dispatchEvent( 'paused' );
  957. }
  958. }
  959. /**
  960. * Exits from the paused mode.
  961. */
  962. function resume() {
  963. var wasPaused = dom.wrapper.classList.contains( 'paused' );
  964. dom.wrapper.classList.remove( 'paused' );
  965. cueAutoSlide();
  966. if( wasPaused ) {
  967. dispatchEvent( 'resumed' );
  968. }
  969. }
  970. /**
  971. * Toggles the paused mode on and off.
  972. */
  973. function togglePause() {
  974. if( isPaused() ) {
  975. resume();
  976. }
  977. else {
  978. pause();
  979. }
  980. }
  981. /**
  982. * Checks if we are currently in the paused mode.
  983. */
  984. function isPaused() {
  985. return dom.wrapper.classList.contains( 'paused' );
  986. }
  987. /**
  988. * Steps from the current point in the presentation to the
  989. * slide which matches the specified horizontal and vertical
  990. * indices.
  991. *
  992. * @param {int} h Horizontal index of the target slide
  993. * @param {int} v Vertical index of the target slide
  994. * @param {int} f Optional index of a fragment within the
  995. * target slide to activate
  996. * @param {int} o Optional origin for use in multimaster environments
  997. */
  998. function slide( h, v, f, o ) {
  999. // Remember where we were at before
  1000. previousSlide = currentSlide;
  1001. // Query all horizontal slides in the deck
  1002. var horizontalSlides = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR );
  1003. // If no vertical index is specified and the upcoming slide is a
  1004. // stack, resume at its previous vertical index
  1005. if( v === undefined ) {
  1006. v = getPreviousVerticalIndex( horizontalSlides[ h ] );
  1007. }
  1008. // If we were on a vertical stack, remember what vertical index
  1009. // it was on so we can resume at the same position when returning
  1010. if( previousSlide && previousSlide.parentNode && previousSlide.parentNode.classList.contains( 'stack' ) ) {
  1011. setPreviousVerticalIndex( previousSlide.parentNode, indexv );
  1012. }
  1013. // Remember the state before this slide
  1014. var stateBefore = state.concat();
  1015. // Reset the state array
  1016. state.length = 0;
  1017. var indexhBefore = indexh,
  1018. indexvBefore = indexv;
  1019. // Activate and transition to the new slide
  1020. indexh = updateSlides( HORIZONTAL_SLIDES_SELECTOR, h === undefined ? indexh : h );
  1021. indexv = updateSlides( VERTICAL_SLIDES_SELECTOR, v === undefined ? indexv : v );
  1022. // Update the visibility of slides now that the indices have changed
  1023. updateSlidesVisibility();
  1024. layout();
  1025. // Apply the new state
  1026. stateLoop: for( var i = 0, len = state.length; i < len; i++ ) {
  1027. // Check if this state existed on the previous slide. If it
  1028. // did, we will avoid adding it repeatedly
  1029. for( var j = 0; j < stateBefore.length; j++ ) {
  1030. if( stateBefore[j] === state[i] ) {
  1031. stateBefore.splice( j, 1 );
  1032. continue stateLoop;
  1033. }
  1034. }
  1035. document.documentElement.classList.add( state[i] );
  1036. // Dispatch custom event matching the state's name
  1037. dispatchEvent( state[i] );
  1038. }
  1039. // Clean up the remains of the previous state
  1040. while( stateBefore.length ) {
  1041. document.documentElement.classList.remove( stateBefore.pop() );
  1042. }
  1043. // If the overview is active, re-activate it to update positions
  1044. if( isOverview() ) {
  1045. activateOverview();
  1046. }
  1047. // Find the current horizontal slide and any possible vertical slides
  1048. // within it
  1049. var currentHorizontalSlide = horizontalSlides[ indexh ],
  1050. currentVerticalSlides = currentHorizontalSlide.querySelectorAll( 'section' );
  1051. // Store references to the previous and current slides
  1052. currentSlide = currentVerticalSlides[ indexv ] || currentHorizontalSlide;
  1053. // Show fragment, if specified
  1054. if( typeof f !== 'undefined' ) {
  1055. var fragments = sortFragments( currentSlide.querySelectorAll( '.fragment' ) );
  1056. toArray( fragments ).forEach( function( fragment, indexf ) {
  1057. if( indexf < f ) {
  1058. fragment.classList.add( 'visible' );
  1059. }
  1060. else {
  1061. fragment.classList.remove( 'visible' );
  1062. }
  1063. } );
  1064. }
  1065. // Dispatch an event if the slide changed
  1066. var slideChanged = ( indexh !== indexhBefore || indexv !== indexvBefore );
  1067. if( slideChanged ) {
  1068. dispatchEvent( 'slidechanged', {
  1069. 'indexh': indexh,
  1070. 'indexv': indexv,
  1071. 'previousSlide': previousSlide,
  1072. 'currentSlide': currentSlide,
  1073. 'origin': o
  1074. } );
  1075. }
  1076. else {
  1077. // Ensure that the previous slide is never the same as the current
  1078. previousSlide = null;
  1079. }
  1080. // Solves an edge case where the previous slide maintains the
  1081. // 'present' class when navigating between adjacent vertical
  1082. // stacks
  1083. if( previousSlide ) {
  1084. previousSlide.classList.remove( 'present' );
  1085. // Reset all slides upon navigate to home
  1086. // Issue: #285
  1087. if ( document.querySelector( HOME_SLIDE_SELECTOR ).classList.contains( 'present' ) ) {
  1088. // Launch async task
  1089. setTimeout( function () {
  1090. var slides = toArray( document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR + '.stack') ), i;
  1091. for( i in slides ) {
  1092. if( slides[i] ) {
  1093. // Reset stack
  1094. setPreviousVerticalIndex( slides[i], 0 );
  1095. }
  1096. }
  1097. }, 0 );
  1098. }
  1099. }
  1100. // Handle embedded content
  1101. if( slideChanged ) {
  1102. stopEmbeddedContent( previousSlide );
  1103. startEmbeddedContent( currentSlide );
  1104. }
  1105. updateControls();
  1106. updateProgress();
  1107. updateBackground();
  1108. // Update the URL hash
  1109. writeURL();
  1110. }
  1111. /**
  1112. * Syncs the presentation with the current DOM. Useful
  1113. * when new slides or control elements are added or when
  1114. * the configuration has changed.
  1115. */
  1116. function sync() {
  1117. // Subscribe to input
  1118. removeEventListeners();
  1119. addEventListeners();
  1120. // Force a layout to make sure the current config is accounted for
  1121. layout();
  1122. // Reflect the current autoSlide value
  1123. autoSlide = config.autoSlide;
  1124. // Start auto-sliding if it's enabled
  1125. cueAutoSlide();
  1126. // Re-create the slide backgrounds
  1127. createBackgrounds();
  1128. updateControls();
  1129. updateProgress();
  1130. updateBackground();
  1131. }
  1132. /**
  1133. * Updates one dimension of slides by showing the slide
  1134. * with the specified index.
  1135. *
  1136. * @param {String} selector A CSS selector that will fetch
  1137. * the group of slides we are working with
  1138. * @param {Number} index The index of the slide that should be
  1139. * shown
  1140. *
  1141. * @return {Number} The index of the slide that is now shown,
  1142. * might differ from the passed in index if it was out of
  1143. * bounds.
  1144. */
  1145. function updateSlides( selector, index ) {
  1146. // Select all slides and convert the NodeList result to
  1147. // an array
  1148. var slides = toArray( document.querySelectorAll( selector ) ),
  1149. slidesLength = slides.length;
  1150. if( slidesLength ) {
  1151. // Should the index loop?
  1152. if( config.loop ) {
  1153. index %= slidesLength;
  1154. if( index < 0 ) {
  1155. index = slidesLength + index;
  1156. }
  1157. }
  1158. // Enforce max and minimum index bounds
  1159. index = Math.max( Math.min( index, slidesLength - 1 ), 0 );
  1160. for( var i = 0; i < slidesLength; i++ ) {
  1161. var element = slides[i];
  1162. var reverse = config.rtl && !isVerticalSlide( element );
  1163. element.classList.remove( 'past' );
  1164. element.classList.remove( 'present' );
  1165. element.classList.remove( 'future' );
  1166. // http://www.w3.org/html/wg/drafts/html/master/editing.html#the-hidden-attribute
  1167. element.setAttribute( 'hidden', '' );
  1168. if( i < index ) {
  1169. // Any element previous to index is given the 'past' class
  1170. element.classList.add( reverse ? 'future' : 'past' );
  1171. }
  1172. else if( i > index ) {
  1173. // Any element subsequent to index is given the 'future' class
  1174. element.classList.add( reverse ? 'past' : 'future' );
  1175. var fragments = toArray( element.querySelectorAll( '.fragment.visible' ) );
  1176. // No fragments in future slides should be visible ahead of time
  1177. while( fragments.length ) {
  1178. fragments.pop().classList.remove( 'visible' );
  1179. }
  1180. }
  1181. // If this element contains vertical slides
  1182. if( element.querySelector( 'section' ) ) {
  1183. element.classList.add( 'stack' );
  1184. }
  1185. }
  1186. // Mark the current slide as present
  1187. slides[index].classList.add( 'present' );
  1188. slides[index].removeAttribute( 'hidden' );
  1189. // If this slide has a state associated with it, add it
  1190. // onto the current state of the deck
  1191. var slideState = slides[index].getAttribute( 'data-state' );
  1192. if( slideState ) {
  1193. state = state.concat( slideState.split( ' ' ) );
  1194. }
  1195. // If this slide has a data-autoslide attribute associated use this as
  1196. // autoSlide value otherwise use the global configured time
  1197. var slideAutoSlide = slides[index].getAttribute( 'data-autoslide' );
  1198. if( slideAutoSlide ) {
  1199. autoSlide = parseInt( slideAutoSlide, 10 );
  1200. }
  1201. else {
  1202. autoSlide = config.autoSlide;
  1203. }
  1204. cueAutoSlide();
  1205. }
  1206. else {
  1207. // Since there are no slides we can't be anywhere beyond the
  1208. // zeroth index
  1209. index = 0;
  1210. }
  1211. return index;
  1212. }
  1213. /**
  1214. * Optimization method; hide all slides that are far away
  1215. * from the present slide.
  1216. */
  1217. function updateSlidesVisibility() {
  1218. // Select all slides and convert the NodeList result to
  1219. // an array
  1220. var horizontalSlides = toArray( document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) ),
  1221. horizontalSlidesLength = horizontalSlides.length,
  1222. distance;
  1223. if( horizontalSlidesLength ) {
  1224. // The number of steps away from the present slide that will
  1225. // be visible
  1226. var viewDistance = config.viewDistance;
  1227. // Heavily limited on weaker devices
  1228. if( isMobileDevice ) {
  1229. viewDistance = 1;
  1230. }
  1231. if( isOverview() ) {
  1232. viewDistance = 6;
  1233. }
  1234. for( var x = 0; x < horizontalSlidesLength; x++ ) {
  1235. var horizontalSlide = horizontalSlides[x];
  1236. var verticalSlides = toArray( horizontalSlide.querySelectorAll( 'section' ) ),
  1237. verticalSlidesLength = verticalSlides.length;
  1238. if( verticalSlidesLength ) {
  1239. // Always show the vertical stack itself, even if its child
  1240. // slides are invisible
  1241. horizontalSlide.style.display = 'block';
  1242. for( var y = 0; y < verticalSlidesLength; y++ ) {
  1243. var verticalSlide = verticalSlides[y];
  1244. var dx = x - indexh,
  1245. dy = y - indexv;
  1246. distance = Math.sqrt( dx*dx + dy*dy );
  1247. verticalSlide.style.display = distance > viewDistance ? 'none' : 'block';
  1248. }
  1249. }
  1250. else {
  1251. // Loops so that it measures 1 between the first and last slides
  1252. distance = Math.abs( ( indexh - x ) % ( horizontalSlidesLength - viewDistance ) ) || 0;
  1253. horizontalSlide.style.display = distance > 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, indexv );
  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.handled ) {
  1783. var currentX = event.touches[0].clientX;
  1784. var currentY = event.touches[0].clientY;
  1785. // If the touch started off 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.handled = 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.handled = true;
  1815. navigateLeft();
  1816. }
  1817. else if( deltaX < -touch.threshold && Math.abs( deltaX ) > Math.abs( deltaY ) ) {
  1818. touch.handled = true;
  1819. navigateRight();
  1820. }
  1821. else if( deltaY > touch.threshold ) {
  1822. touch.handled = true;
  1823. navigateUp();
  1824. }
  1825. else if( deltaY < -touch.threshold ) {
  1826. touch.handled = true;
  1827. navigateDown();
  1828. }
  1829. event.preventDefault();
  1830. }
  1831. }
  1832. // There's a bug with swiping on some Android devices unless
  1833. // the default action is always prevented
  1834. else if( navigator.userAgent.match( /android/gi ) ) {
  1835. event.preventDefault();
  1836. }
  1837. }
  1838. /**
  1839. * Handler for the 'touchend' event.
  1840. */
  1841. function onTouchEnd( event ) {
  1842. touch.handled = false;
  1843. }
  1844. /**
  1845. * Convert pointer down to touch start.
  1846. */
  1847. function onPointerDown( event ) {
  1848. if( event.pointerType === event.MSPOINTER_TYPE_TOUCH ) {
  1849. event.touches = [{ clientX: event.clientX, clientY: event.clientY }];
  1850. onTouchStart( event );
  1851. }
  1852. }
  1853. /**
  1854. * Convert pointer move to touch move.
  1855. */
  1856. function onPointerMove( event ) {
  1857. if( event.pointerType === event.MSPOINTER_TYPE_TOUCH ) {
  1858. event.touches = [{ clientX: event.clientX, clientY: event.clientY }];
  1859. onTouchMove( event );
  1860. }
  1861. }
  1862. /**
  1863. * Convert pointer up to touch end.
  1864. */
  1865. function onPointerUp( event ) {
  1866. if( event.pointerType === event.MSPOINTER_TYPE_TOUCH ) {
  1867. event.touches = [{ clientX: event.clientX, clientY: event.clientY }];
  1868. onTouchEnd( event );
  1869. }
  1870. }
  1871. /**
  1872. * Handles mouse wheel scrolling, throttled to avoid skipping
  1873. * multiple slides.
  1874. */
  1875. function onDocumentMouseScroll( event ) {
  1876. if( Date.now() - lastMouseWheelStep > 600 ) {
  1877. lastMouseWheelStep = Date.now();
  1878. var delta = event.detail || -event.wheelDelta;
  1879. if( delta > 0 ) {
  1880. navigateNext();
  1881. }
  1882. else {
  1883. navigatePrev();
  1884. }
  1885. }
  1886. }
  1887. /**
  1888. * Clicking on the progress bar results in a navigation to the
  1889. * closest approximate horizontal slide using this equation:
  1890. *
  1891. * ( clickX / presentationWidth ) * numberOfSlides
  1892. */
  1893. function onProgressClicked( event ) {
  1894. event.preventDefault();
  1895. var slidesTotal = toArray( document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) ).length;
  1896. var slideIndex = Math.floor( ( event.clientX / dom.wrapper.offsetWidth ) * slidesTotal );
  1897. slide( slideIndex );
  1898. }
  1899. /**
  1900. * Event handler for navigation control buttons.
  1901. */
  1902. function onNavigateLeftClicked( event ) { event.preventDefault(); navigateLeft(); }
  1903. function onNavigateRightClicked( event ) { event.preventDefault(); navigateRight(); }
  1904. function onNavigateUpClicked( event ) { event.preventDefault(); navigateUp(); }
  1905. function onNavigateDownClicked( event ) { event.preventDefault(); navigateDown(); }
  1906. function onNavigatePrevClicked( event ) { event.preventDefault(); navigatePrev(); }
  1907. function onNavigateNextClicked( event ) { event.preventDefault(); navigateNext(); }
  1908. /**
  1909. * Handler for the window level 'hashchange' event.
  1910. */
  1911. function onWindowHashChange( event ) {
  1912. readURL();
  1913. }
  1914. /**
  1915. * Handler for the window level 'resize' event.
  1916. */
  1917. function onWindowResize( event ) {
  1918. layout();
  1919. }
  1920. /**
  1921. * Invoked when a slide is and we're in the overview.
  1922. */
  1923. function onOverviewSlideClicked( event ) {
  1924. // TODO There's a bug here where the event listeners are not
  1925. // removed after deactivating the overview.
  1926. if( eventsAreBound && isOverview() ) {
  1927. event.preventDefault();
  1928. var element = event.target;
  1929. while( element && !element.nodeName.match( /section/gi ) ) {
  1930. element = element.parentNode;
  1931. }
  1932. if( element && !element.classList.contains( 'disabled' ) ) {
  1933. deactivateOverview();
  1934. if( element.nodeName.match( /section/gi ) ) {
  1935. var h = parseInt( element.getAttribute( 'data-index-h' ), 10 ),
  1936. v = parseInt( element.getAttribute( 'data-index-v' ), 10 );
  1937. slide( h, v );
  1938. }
  1939. }
  1940. }
  1941. }
  1942. /**
  1943. * Handles clicks on links that are set to preview in the
  1944. * iframe overlay.
  1945. */
  1946. function onPreviewLinkClicked( event ) {
  1947. var url = event.target.getAttribute( 'href' );
  1948. if( url ) {
  1949. openPreview( url );
  1950. event.preventDefault();
  1951. }
  1952. }
  1953. // --------------------------------------------------------------------//
  1954. // ------------------------------- API --------------------------------//
  1955. // --------------------------------------------------------------------//
  1956. return {
  1957. initialize: initialize,
  1958. configure: configure,
  1959. sync: sync,
  1960. // Navigation methods
  1961. slide: slide,
  1962. left: navigateLeft,
  1963. right: navigateRight,
  1964. up: navigateUp,
  1965. down: navigateDown,
  1966. prev: navigatePrev,
  1967. next: navigateNext,
  1968. prevFragment: previousFragment,
  1969. nextFragment: nextFragment,
  1970. // Deprecated aliases
  1971. navigateTo: slide,
  1972. navigateLeft: navigateLeft,
  1973. navigateRight: navigateRight,
  1974. navigateUp: navigateUp,
  1975. navigateDown: navigateDown,
  1976. navigatePrev: navigatePrev,
  1977. navigateNext: navigateNext,
  1978. // Forces an update in slide layout
  1979. layout: layout,
  1980. // Returns an object with the available routes as booleans (left/right/top/bottom)
  1981. availableRoutes: availableRoutes,
  1982. // Returns an object with the available fragments as booleans (prev/next)
  1983. availableFragments: availableFragments,
  1984. // Toggles the overview mode on/off
  1985. toggleOverview: toggleOverview,
  1986. // Toggles the "black screen" mode on/off
  1987. togglePause: togglePause,
  1988. // State checks
  1989. isOverview: isOverview,
  1990. isPaused: isPaused,
  1991. // Adds or removes all internal event listeners (such as keyboard)
  1992. addEventListeners: addEventListeners,
  1993. removeEventListeners: removeEventListeners,
  1994. // Returns the indices of the current, or specified, slide
  1995. getIndices: getIndices,
  1996. // Returns the slide at the specified index, y is optional
  1997. getSlide: function( x, y ) {
  1998. var horizontalSlide = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR )[ x ];
  1999. var verticalSlides = horizontalSlide && horizontalSlide.querySelectorAll( 'section' );
  2000. if( typeof y !== 'undefined' ) {
  2001. return verticalSlides ? verticalSlides[ y ] : undefined;
  2002. }
  2003. return horizontalSlide;
  2004. },
  2005. // Returns the previous slide element, may be null
  2006. getPreviousSlide: function() {
  2007. return previousSlide;
  2008. },
  2009. // Returns the current slide element
  2010. getCurrentSlide: function() {
  2011. return currentSlide;
  2012. },
  2013. // Returns the current scale of the presentation content
  2014. getScale: function() {
  2015. return scale;
  2016. },
  2017. // Returns the current configuration object
  2018. getConfig: function() {
  2019. return config;
  2020. },
  2021. // Helper method, retrieves query string as a key/value hash
  2022. getQueryHash: function() {
  2023. var query = {};
  2024. location.search.replace( /[A-Z0-9]+?=(\w*)/gi, function(a) {
  2025. query[ a.split( '=' ).shift() ] = a.split( '=' ).pop();
  2026. } );
  2027. return query;
  2028. },
  2029. // Returns true if we're currently on the first slide
  2030. isFirstSlide: function() {
  2031. return document.querySelector( SLIDES_SELECTOR + '.past' ) == null ? true : false;
  2032. },
  2033. // Returns true if we're currently on the last slide
  2034. isLastSlide: function() {
  2035. if( currentSlide && currentSlide.classList.contains( '.stack' ) ) {
  2036. return currentSlide.querySelector( SLIDES_SELECTOR + '.future' ) == null ? true : false;
  2037. }
  2038. else {
  2039. return document.querySelector( SLIDES_SELECTOR + '.future' ) == null ? true : false;
  2040. }
  2041. },
  2042. // Forward event binding to the reveal DOM element
  2043. addEventListener: function( type, listener, useCapture ) {
  2044. if( 'addEventListener' in window ) {
  2045. ( dom.wrapper || document.querySelector( '.reveal' ) ).addEventListener( type, listener, useCapture );
  2046. }
  2047. },
  2048. removeEventListener: function( type, listener, useCapture ) {
  2049. if( 'addEventListener' in window ) {
  2050. ( dom.wrapper || document.querySelector( '.reveal' ) ).removeEventListener( type, listener, useCapture );
  2051. }
  2052. }
  2053. };
  2054. })();