reveal.js 96 KB

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