reveal.js 122 KB

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