reveal.js 120 KB

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