reveal.js 143 KB

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