reveal.js 93 KB

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