reveal.js 96 KB

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