reveal.js 147 KB

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