reveal.js 93 KB

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