reveal.js 184 KB

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