reveal.js 126 KB

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