reveal.js 96 KB

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