reveal.js 91 KB

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