reveal.js 129 KB

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