reveal.js 69 KB

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