reveal.js 145 KB

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