reveal.js 128 KB

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