reveal.js 139 KB

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