reveal.js 184 KB

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