reveal.js 143 KB

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