reveal.js 158 KB

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