reveal.js 144 KB

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