reveal.js 121 KB

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