reveal.js 128 KB

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