reveal.js 147 KB

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