reveal.js 147 KB

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