reveal.js 136 KB

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