reveal.js 129 KB

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