reveal.js 147 KB

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