reveal.js 141 KB

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