reveal.js 72 KB

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