reveal.js 143 KB

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