reveal.js 88 KB

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