reveal.js 129 KB

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