reveal.js 86 KB

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