reveal.js 127 KB

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