reveal.js 88 KB

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