reveal.js 137 KB

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