reveal.js 122 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527
  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. '<button class="navigate-left" aria-label="previous slide"></button>' +
  348. '<button class="navigate-right" aria-label="next slide"></button>' +
  349. '<button class="navigate-up" aria-label="above slide"></button>' +
  350. '<button class="navigate-down" aria-label="below slide"></button>' );
  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. // Use zoom to scale up in desktop Chrome so that content
  1189. // remains crisp. We don't use zoom to scale down since that
  1190. // can lead to shifts in text layout/line breaks.
  1191. if( scale > 1 && !isMobileDevice && /chrome/i.test( navigator.userAgent ) && typeof dom.slides.style.zoom !== 'undefined' ) {
  1192. dom.slides.style.zoom = scale;
  1193. dom.slides.style.left = '';
  1194. dom.slides.style.top = '';
  1195. dom.slides.style.bottom = '';
  1196. dom.slides.style.right = '';
  1197. transformSlides( { layout: '' } );
  1198. }
  1199. // Apply scale transform as a fallback
  1200. else {
  1201. dom.slides.style.zoom = '';
  1202. dom.slides.style.left = '50%';
  1203. dom.slides.style.top = '50%';
  1204. dom.slides.style.bottom = 'auto';
  1205. dom.slides.style.right = 'auto';
  1206. transformSlides( { layout: 'translate(-50%, -50%) scale('+ scale +')' } );
  1207. }
  1208. }
  1209. // Select all slides, vertical and horizontal
  1210. var slides = toArray( dom.wrapper.querySelectorAll( SLIDES_SELECTOR ) );
  1211. for( var i = 0, len = slides.length; i < len; i++ ) {
  1212. var slide = slides[ i ];
  1213. // Don't bother updating invisible slides
  1214. if( slide.style.display === 'none' ) {
  1215. continue;
  1216. }
  1217. if( config.center || slide.classList.contains( 'center' ) ) {
  1218. // Vertical stacks are not centred since their section
  1219. // children will be
  1220. if( slide.classList.contains( 'stack' ) ) {
  1221. slide.style.top = 0;
  1222. }
  1223. else {
  1224. slide.style.top = Math.max( ( ( size.height - getAbsoluteHeight( slide ) ) / 2 ) - slidePadding, 0 ) + 'px';
  1225. }
  1226. }
  1227. else {
  1228. slide.style.top = '';
  1229. }
  1230. }
  1231. updateProgress();
  1232. updateParallax();
  1233. }
  1234. }
  1235. /**
  1236. * Applies layout logic to the contents of all slides in
  1237. * the presentation.
  1238. */
  1239. function layoutSlideContents( width, height, padding ) {
  1240. // Handle sizing of elements with the 'stretch' class
  1241. toArray( dom.slides.querySelectorAll( 'section > .stretch' ) ).forEach( function( element ) {
  1242. // Determine how much vertical space we can use
  1243. var remainingHeight = getRemainingHeight( element, height );
  1244. // Consider the aspect ratio of media elements
  1245. if( /(img|video)/gi.test( element.nodeName ) ) {
  1246. var nw = element.naturalWidth || element.videoWidth,
  1247. nh = element.naturalHeight || element.videoHeight;
  1248. var es = Math.min( width / nw, remainingHeight / nh );
  1249. element.style.width = ( nw * es ) + 'px';
  1250. element.style.height = ( nh * es ) + 'px';
  1251. }
  1252. else {
  1253. element.style.width = width + 'px';
  1254. element.style.height = remainingHeight + 'px';
  1255. }
  1256. } );
  1257. }
  1258. /**
  1259. * Calculates the computed pixel size of our slides. These
  1260. * values are based on the width and height configuration
  1261. * options.
  1262. */
  1263. function getComputedSlideSize( presentationWidth, presentationHeight ) {
  1264. var size = {
  1265. // Slide size
  1266. width: config.width,
  1267. height: config.height,
  1268. // Presentation size
  1269. presentationWidth: presentationWidth || dom.wrapper.offsetWidth,
  1270. presentationHeight: presentationHeight || dom.wrapper.offsetHeight
  1271. };
  1272. // Reduce available space by margin
  1273. size.presentationWidth -= ( size.presentationWidth * config.margin );
  1274. size.presentationHeight -= ( size.presentationHeight * config.margin );
  1275. // Slide width may be a percentage of available width
  1276. if( typeof size.width === 'string' && /%$/.test( size.width ) ) {
  1277. size.width = parseInt( size.width, 10 ) / 100 * size.presentationWidth;
  1278. }
  1279. // Slide height may be a percentage of available height
  1280. if( typeof size.height === 'string' && /%$/.test( size.height ) ) {
  1281. size.height = parseInt( size.height, 10 ) / 100 * size.presentationHeight;
  1282. }
  1283. return size;
  1284. }
  1285. /**
  1286. * Stores the vertical index of a stack so that the same
  1287. * vertical slide can be selected when navigating to and
  1288. * from the stack.
  1289. *
  1290. * @param {HTMLElement} stack The vertical stack element
  1291. * @param {int} v Index to memorize
  1292. */
  1293. function setPreviousVerticalIndex( stack, v ) {
  1294. if( typeof stack === 'object' && typeof stack.setAttribute === 'function' ) {
  1295. stack.setAttribute( 'data-previous-indexv', v || 0 );
  1296. }
  1297. }
  1298. /**
  1299. * Retrieves the vertical index which was stored using
  1300. * #setPreviousVerticalIndex() or 0 if no previous index
  1301. * exists.
  1302. *
  1303. * @param {HTMLElement} stack The vertical stack element
  1304. */
  1305. function getPreviousVerticalIndex( stack ) {
  1306. if( typeof stack === 'object' && typeof stack.setAttribute === 'function' && stack.classList.contains( 'stack' ) ) {
  1307. // Prefer manually defined start-indexv
  1308. var attributeName = stack.hasAttribute( 'data-start-indexv' ) ? 'data-start-indexv' : 'data-previous-indexv';
  1309. return parseInt( stack.getAttribute( attributeName ) || 0, 10 );
  1310. }
  1311. return 0;
  1312. }
  1313. /**
  1314. * Displays the overview of slides (quick nav) by scaling
  1315. * down and arranging all slide elements.
  1316. */
  1317. function activateOverview() {
  1318. // Only proceed if enabled in config
  1319. if( config.overview && !isOverview() ) {
  1320. overview = true;
  1321. dom.wrapper.classList.add( 'overview' );
  1322. dom.wrapper.classList.remove( 'overview-deactivating' );
  1323. if( features.overviewTransitions ) {
  1324. setTimeout( function() {
  1325. dom.wrapper.classList.add( 'overview-animated' );
  1326. }, 1 );
  1327. }
  1328. // Don't auto-slide while in overview mode
  1329. cancelAutoSlide();
  1330. // Move the backgrounds element into the slide container to
  1331. // that the same scaling is applied
  1332. dom.slides.appendChild( dom.background );
  1333. // Clicking on an overview slide navigates to it
  1334. toArray( dom.wrapper.querySelectorAll( SLIDES_SELECTOR ) ).forEach( function( slide ) {
  1335. if( !slide.classList.contains( 'stack' ) ) {
  1336. slide.addEventListener( 'click', onOverviewSlideClicked, true );
  1337. }
  1338. } );
  1339. updateSlidesVisibility();
  1340. layoutOverview();
  1341. updateOverview();
  1342. layout();
  1343. // Notify observers of the overview showing
  1344. dispatchEvent( 'overviewshown', {
  1345. 'indexh': indexh,
  1346. 'indexv': indexv,
  1347. 'currentSlide': currentSlide
  1348. } );
  1349. }
  1350. }
  1351. /**
  1352. * Uses CSS transforms to position all slides in a grid for
  1353. * display inside of the overview mode.
  1354. */
  1355. function layoutOverview() {
  1356. var margin = 70;
  1357. var slideWidth = config.width + margin,
  1358. slideHeight = config.height + margin;
  1359. // Reverse in RTL mode
  1360. if( config.rtl ) {
  1361. slideWidth = -slideWidth;
  1362. }
  1363. // Layout slides
  1364. toArray( dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) ).forEach( function( hslide, h ) {
  1365. hslide.setAttribute( 'data-index-h', h );
  1366. transformElement( hslide, 'translate3d(' + ( h * slideWidth ) + 'px, 0, 0)' );
  1367. if( hslide.classList.contains( 'stack' ) ) {
  1368. toArray( hslide.querySelectorAll( 'section' ) ).forEach( function( vslide, v ) {
  1369. vslide.setAttribute( 'data-index-h', h );
  1370. vslide.setAttribute( 'data-index-v', v );
  1371. transformElement( vslide, 'translate3d(0, ' + ( v * slideHeight ) + 'px, 0)' );
  1372. } );
  1373. }
  1374. } );
  1375. // Layout slide backgrounds
  1376. toArray( dom.background.childNodes ).forEach( function( hbackground, h ) {
  1377. transformElement( hbackground, 'translate3d(' + ( h * slideWidth ) + 'px, 0, 0)' );
  1378. toArray( hbackground.querySelectorAll( '.slide-background' ) ).forEach( function( vbackground, v ) {
  1379. transformElement( vbackground, 'translate3d(0, ' + ( v * slideHeight ) + 'px, 0)' );
  1380. } );
  1381. } );
  1382. }
  1383. /**
  1384. * Moves the overview viewport to the current slides.
  1385. * Called each time the current slide changes.
  1386. */
  1387. function updateOverview() {
  1388. var margin = 70;
  1389. var slideWidth = config.width + margin,
  1390. slideHeight = config.height + margin;
  1391. // Reverse in RTL mode
  1392. if( config.rtl ) {
  1393. slideWidth = -slideWidth;
  1394. }
  1395. transformSlides( {
  1396. overview: [
  1397. 'translateX('+ ( -indexh * slideWidth ) +'px)',
  1398. 'translateY('+ ( -indexv * slideHeight ) +'px)',
  1399. 'translateZ('+ ( window.innerWidth < 400 ? -1000 : -2500 ) +'px)'
  1400. ].join( ' ' )
  1401. } );
  1402. }
  1403. /**
  1404. * Exits the slide overview and enters the currently
  1405. * active slide.
  1406. */
  1407. function deactivateOverview() {
  1408. // Only proceed if enabled in config
  1409. if( config.overview ) {
  1410. overview = false;
  1411. dom.wrapper.classList.remove( 'overview' );
  1412. dom.wrapper.classList.remove( 'overview-animated' );
  1413. // Temporarily add a class so that transitions can do different things
  1414. // depending on whether they are exiting/entering overview, or just
  1415. // moving from slide to slide
  1416. dom.wrapper.classList.add( 'overview-deactivating' );
  1417. setTimeout( function () {
  1418. dom.wrapper.classList.remove( 'overview-deactivating' );
  1419. }, 1 );
  1420. // Move the background element back out
  1421. dom.wrapper.appendChild( dom.background );
  1422. // Clean up changes made to slides
  1423. toArray( dom.wrapper.querySelectorAll( SLIDES_SELECTOR ) ).forEach( function( slide ) {
  1424. transformElement( slide, '' );
  1425. slide.removeEventListener( 'click', onOverviewSlideClicked, true );
  1426. } );
  1427. // Clean up changes made to backgrounds
  1428. toArray( dom.background.querySelectorAll( '.slide-background' ) ).forEach( function( background ) {
  1429. transformElement( background, '' );
  1430. } );
  1431. transformSlides( { overview: '' } );
  1432. slide( indexh, indexv );
  1433. layout();
  1434. cueAutoSlide();
  1435. // Notify observers of the overview hiding
  1436. dispatchEvent( 'overviewhidden', {
  1437. 'indexh': indexh,
  1438. 'indexv': indexv,
  1439. 'currentSlide': currentSlide
  1440. } );
  1441. }
  1442. }
  1443. /**
  1444. * Toggles the slide overview mode on and off.
  1445. *
  1446. * @param {Boolean} override Optional flag which overrides the
  1447. * toggle logic and forcibly sets the desired state. True means
  1448. * overview is open, false means it's closed.
  1449. */
  1450. function toggleOverview( override ) {
  1451. if( typeof override === 'boolean' ) {
  1452. override ? activateOverview() : deactivateOverview();
  1453. }
  1454. else {
  1455. isOverview() ? deactivateOverview() : activateOverview();
  1456. }
  1457. }
  1458. /**
  1459. * Checks if the overview is currently active.
  1460. *
  1461. * @return {Boolean} true if the overview is active,
  1462. * false otherwise
  1463. */
  1464. function isOverview() {
  1465. return overview;
  1466. }
  1467. /**
  1468. * Checks if the current or specified slide is vertical
  1469. * (nested within another slide).
  1470. *
  1471. * @param {HTMLElement} slide [optional] The slide to check
  1472. * orientation of
  1473. */
  1474. function isVerticalSlide( slide ) {
  1475. // Prefer slide argument, otherwise use current slide
  1476. slide = slide ? slide : currentSlide;
  1477. return slide && slide.parentNode && !!slide.parentNode.nodeName.match( /section/i );
  1478. }
  1479. /**
  1480. * Handling the fullscreen functionality via the fullscreen API
  1481. *
  1482. * @see http://fullscreen.spec.whatwg.org/
  1483. * @see https://developer.mozilla.org/en-US/docs/DOM/Using_fullscreen_mode
  1484. */
  1485. function enterFullscreen() {
  1486. var element = document.body;
  1487. // Check which implementation is available
  1488. var requestMethod = element.requestFullScreen ||
  1489. element.webkitRequestFullscreen ||
  1490. element.webkitRequestFullScreen ||
  1491. element.mozRequestFullScreen ||
  1492. element.msRequestFullscreen;
  1493. if( requestMethod ) {
  1494. requestMethod.apply( element );
  1495. }
  1496. }
  1497. /**
  1498. * Enters the paused mode which fades everything on screen to
  1499. * black.
  1500. */
  1501. function pause() {
  1502. if( config.pause ) {
  1503. var wasPaused = dom.wrapper.classList.contains( 'paused' );
  1504. cancelAutoSlide();
  1505. dom.wrapper.classList.add( 'paused' );
  1506. if( wasPaused === false ) {
  1507. dispatchEvent( 'paused' );
  1508. }
  1509. }
  1510. }
  1511. /**
  1512. * Exits from the paused mode.
  1513. */
  1514. function resume() {
  1515. var wasPaused = dom.wrapper.classList.contains( 'paused' );
  1516. dom.wrapper.classList.remove( 'paused' );
  1517. cueAutoSlide();
  1518. if( wasPaused ) {
  1519. dispatchEvent( 'resumed' );
  1520. }
  1521. }
  1522. /**
  1523. * Toggles the paused mode on and off.
  1524. */
  1525. function togglePause( override ) {
  1526. if( typeof override === 'boolean' ) {
  1527. override ? pause() : resume();
  1528. }
  1529. else {
  1530. isPaused() ? resume() : pause();
  1531. }
  1532. }
  1533. /**
  1534. * Checks if we are currently in the paused mode.
  1535. */
  1536. function isPaused() {
  1537. return dom.wrapper.classList.contains( 'paused' );
  1538. }
  1539. /**
  1540. * Toggles the auto slide mode on and off.
  1541. *
  1542. * @param {Boolean} override Optional flag which sets the desired state.
  1543. * True means autoplay starts, false means it stops.
  1544. */
  1545. function toggleAutoSlide( override ) {
  1546. if( typeof override === 'boolean' ) {
  1547. override ? resumeAutoSlide() : pauseAutoSlide();
  1548. }
  1549. else {
  1550. autoSlidePaused ? resumeAutoSlide() : pauseAutoSlide();
  1551. }
  1552. }
  1553. /**
  1554. * Checks if the auto slide mode is currently on.
  1555. */
  1556. function isAutoSliding() {
  1557. return !!( autoSlide && !autoSlidePaused );
  1558. }
  1559. /**
  1560. * Steps from the current point in the presentation to the
  1561. * slide which matches the specified horizontal and vertical
  1562. * indices.
  1563. *
  1564. * @param {int} h Horizontal index of the target slide
  1565. * @param {int} v Vertical index of the target slide
  1566. * @param {int} f Optional index of a fragment within the
  1567. * target slide to activate
  1568. * @param {int} o Optional origin for use in multimaster environments
  1569. */
  1570. function slide( h, v, f, o ) {
  1571. // Remember where we were at before
  1572. previousSlide = currentSlide;
  1573. // Query all horizontal slides in the deck
  1574. var horizontalSlides = dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR );
  1575. // If no vertical index is specified and the upcoming slide is a
  1576. // stack, resume at its previous vertical index
  1577. if( v === undefined && !isOverview() ) {
  1578. v = getPreviousVerticalIndex( horizontalSlides[ h ] );
  1579. }
  1580. // If we were on a vertical stack, remember what vertical index
  1581. // it was on so we can resume at the same position when returning
  1582. if( previousSlide && previousSlide.parentNode && previousSlide.parentNode.classList.contains( 'stack' ) ) {
  1583. setPreviousVerticalIndex( previousSlide.parentNode, indexv );
  1584. }
  1585. // Remember the state before this slide
  1586. var stateBefore = state.concat();
  1587. // Reset the state array
  1588. state.length = 0;
  1589. var indexhBefore = indexh || 0,
  1590. indexvBefore = indexv || 0;
  1591. // Activate and transition to the new slide
  1592. indexh = updateSlides( HORIZONTAL_SLIDES_SELECTOR, h === undefined ? indexh : h );
  1593. indexv = updateSlides( VERTICAL_SLIDES_SELECTOR, v === undefined ? indexv : v );
  1594. // Update the visibility of slides now that the indices have changed
  1595. updateSlidesVisibility();
  1596. layout();
  1597. // Apply the new state
  1598. stateLoop: for( var i = 0, len = state.length; i < len; i++ ) {
  1599. // Check if this state existed on the previous slide. If it
  1600. // did, we will avoid adding it repeatedly
  1601. for( var j = 0; j < stateBefore.length; j++ ) {
  1602. if( stateBefore[j] === state[i] ) {
  1603. stateBefore.splice( j, 1 );
  1604. continue stateLoop;
  1605. }
  1606. }
  1607. document.documentElement.classList.add( state[i] );
  1608. // Dispatch custom event matching the state's name
  1609. dispatchEvent( state[i] );
  1610. }
  1611. // Clean up the remains of the previous state
  1612. while( stateBefore.length ) {
  1613. document.documentElement.classList.remove( stateBefore.pop() );
  1614. }
  1615. // Update the overview if it's currently active
  1616. if( isOverview() ) {
  1617. updateOverview();
  1618. }
  1619. // Find the current horizontal slide and any possible vertical slides
  1620. // within it
  1621. var currentHorizontalSlide = horizontalSlides[ indexh ],
  1622. currentVerticalSlides = currentHorizontalSlide.querySelectorAll( 'section' );
  1623. // Store references to the previous and current slides
  1624. currentSlide = currentVerticalSlides[ indexv ] || currentHorizontalSlide;
  1625. // Show fragment, if specified
  1626. if( typeof f !== 'undefined' ) {
  1627. navigateFragment( f );
  1628. }
  1629. // Dispatch an event if the slide changed
  1630. var slideChanged = ( indexh !== indexhBefore || indexv !== indexvBefore );
  1631. if( slideChanged ) {
  1632. dispatchEvent( 'slidechanged', {
  1633. 'indexh': indexh,
  1634. 'indexv': indexv,
  1635. 'previousSlide': previousSlide,
  1636. 'currentSlide': currentSlide,
  1637. 'origin': o
  1638. } );
  1639. }
  1640. else {
  1641. // Ensure that the previous slide is never the same as the current
  1642. previousSlide = null;
  1643. }
  1644. // Solves an edge case where the previous slide maintains the
  1645. // 'present' class when navigating between adjacent vertical
  1646. // stacks
  1647. if( previousSlide ) {
  1648. previousSlide.classList.remove( 'present' );
  1649. previousSlide.setAttribute( 'aria-hidden', 'true' );
  1650. // Reset all slides upon navigate to home
  1651. // Issue: #285
  1652. if ( dom.wrapper.querySelector( HOME_SLIDE_SELECTOR ).classList.contains( 'present' ) ) {
  1653. // Launch async task
  1654. setTimeout( function () {
  1655. var slides = toArray( dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR + '.stack') ), i;
  1656. for( i in slides ) {
  1657. if( slides[i] ) {
  1658. // Reset stack
  1659. setPreviousVerticalIndex( slides[i], 0 );
  1660. }
  1661. }
  1662. }, 0 );
  1663. }
  1664. }
  1665. // Handle embedded content
  1666. if( slideChanged || !previousSlide ) {
  1667. stopEmbeddedContent( previousSlide );
  1668. startEmbeddedContent( currentSlide );
  1669. }
  1670. // Announce the current slide contents, for screen readers
  1671. dom.statusDiv.textContent = currentSlide.textContent;
  1672. updateControls();
  1673. updateProgress();
  1674. updateBackground();
  1675. updateParallax();
  1676. updateSlideNumber();
  1677. // Update the URL hash
  1678. writeURL();
  1679. cueAutoSlide();
  1680. }
  1681. /**
  1682. * Syncs the presentation with the current DOM. Useful
  1683. * when new slides or control elements are added or when
  1684. * the configuration has changed.
  1685. */
  1686. function sync() {
  1687. // Subscribe to input
  1688. removeEventListeners();
  1689. addEventListeners();
  1690. // Force a layout to make sure the current config is accounted for
  1691. layout();
  1692. // Reflect the current autoSlide value
  1693. autoSlide = config.autoSlide;
  1694. // Start auto-sliding if it's enabled
  1695. cueAutoSlide();
  1696. // Re-create the slide backgrounds
  1697. createBackgrounds();
  1698. // Write the current hash to the URL
  1699. writeURL();
  1700. sortAllFragments();
  1701. updateControls();
  1702. updateProgress();
  1703. updateBackground( true );
  1704. updateSlideNumber();
  1705. updateSlidesVisibility();
  1706. formatEmbeddedContent();
  1707. startEmbeddedContent( currentSlide );
  1708. if( isOverview() ) {
  1709. layoutOverview();
  1710. }
  1711. }
  1712. /**
  1713. * Resets all vertical slides so that only the first
  1714. * is visible.
  1715. */
  1716. function resetVerticalSlides() {
  1717. var horizontalSlides = toArray( dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) );
  1718. horizontalSlides.forEach( function( horizontalSlide ) {
  1719. var verticalSlides = toArray( horizontalSlide.querySelectorAll( 'section' ) );
  1720. verticalSlides.forEach( function( verticalSlide, y ) {
  1721. if( y > 0 ) {
  1722. verticalSlide.classList.remove( 'present' );
  1723. verticalSlide.classList.remove( 'past' );
  1724. verticalSlide.classList.add( 'future' );
  1725. verticalSlide.setAttribute( 'aria-hidden', 'true' );
  1726. }
  1727. } );
  1728. } );
  1729. }
  1730. /**
  1731. * Sorts and formats all of fragments in the
  1732. * presentation.
  1733. */
  1734. function sortAllFragments() {
  1735. var horizontalSlides = toArray( dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) );
  1736. horizontalSlides.forEach( function( horizontalSlide ) {
  1737. var verticalSlides = toArray( horizontalSlide.querySelectorAll( 'section' ) );
  1738. verticalSlides.forEach( function( verticalSlide, y ) {
  1739. sortFragments( verticalSlide.querySelectorAll( '.fragment' ) );
  1740. } );
  1741. if( verticalSlides.length === 0 ) sortFragments( horizontalSlide.querySelectorAll( '.fragment' ) );
  1742. } );
  1743. }
  1744. /**
  1745. * Updates one dimension of slides by showing the slide
  1746. * with the specified index.
  1747. *
  1748. * @param {String} selector A CSS selector that will fetch
  1749. * the group of slides we are working with
  1750. * @param {Number} index The index of the slide that should be
  1751. * shown
  1752. *
  1753. * @return {Number} The index of the slide that is now shown,
  1754. * might differ from the passed in index if it was out of
  1755. * bounds.
  1756. */
  1757. function updateSlides( selector, index ) {
  1758. // Select all slides and convert the NodeList result to
  1759. // an array
  1760. var slides = toArray( dom.wrapper.querySelectorAll( selector ) ),
  1761. slidesLength = slides.length;
  1762. var printMode = isPrintingPDF();
  1763. if( slidesLength ) {
  1764. // Should the index loop?
  1765. if( config.loop ) {
  1766. index %= slidesLength;
  1767. if( index < 0 ) {
  1768. index = slidesLength + index;
  1769. }
  1770. }
  1771. // Enforce max and minimum index bounds
  1772. index = Math.max( Math.min( index, slidesLength - 1 ), 0 );
  1773. for( var i = 0; i < slidesLength; i++ ) {
  1774. var element = slides[i];
  1775. var reverse = config.rtl && !isVerticalSlide( element );
  1776. element.classList.remove( 'past' );
  1777. element.classList.remove( 'present' );
  1778. element.classList.remove( 'future' );
  1779. // http://www.w3.org/html/wg/drafts/html/master/editing.html#the-hidden-attribute
  1780. element.setAttribute( 'hidden', '' );
  1781. element.setAttribute( 'aria-hidden', 'true' );
  1782. // If this element contains vertical slides
  1783. if( element.querySelector( 'section' ) ) {
  1784. element.classList.add( 'stack' );
  1785. }
  1786. // If we're printing static slides, all slides are "present"
  1787. if( printMode ) {
  1788. element.classList.add( 'present' );
  1789. continue;
  1790. }
  1791. if( i < index ) {
  1792. // Any element previous to index is given the 'past' class
  1793. element.classList.add( reverse ? 'future' : 'past' );
  1794. if( config.fragments ) {
  1795. var pastFragments = toArray( element.querySelectorAll( '.fragment' ) );
  1796. // Show all fragments on prior slides
  1797. while( pastFragments.length ) {
  1798. var pastFragment = pastFragments.pop();
  1799. pastFragment.classList.add( 'visible' );
  1800. pastFragment.classList.remove( 'current-fragment' );
  1801. }
  1802. }
  1803. }
  1804. else if( i > index ) {
  1805. // Any element subsequent to index is given the 'future' class
  1806. element.classList.add( reverse ? 'past' : 'future' );
  1807. if( config.fragments ) {
  1808. var futureFragments = toArray( element.querySelectorAll( '.fragment.visible' ) );
  1809. // No fragments in future slides should be visible ahead of time
  1810. while( futureFragments.length ) {
  1811. var futureFragment = futureFragments.pop();
  1812. futureFragment.classList.remove( 'visible' );
  1813. futureFragment.classList.remove( 'current-fragment' );
  1814. }
  1815. }
  1816. }
  1817. }
  1818. // Mark the current slide as present
  1819. slides[index].classList.add( 'present' );
  1820. slides[index].removeAttribute( 'hidden' );
  1821. slides[index].removeAttribute( 'aria-hidden' );
  1822. // If this slide has a state associated with it, add it
  1823. // onto the current state of the deck
  1824. var slideState = slides[index].getAttribute( 'data-state' );
  1825. if( slideState ) {
  1826. state = state.concat( slideState.split( ' ' ) );
  1827. }
  1828. }
  1829. else {
  1830. // Since there are no slides we can't be anywhere beyond the
  1831. // zeroth index
  1832. index = 0;
  1833. }
  1834. return index;
  1835. }
  1836. /**
  1837. * Optimization method; hide all slides that are far away
  1838. * from the present slide.
  1839. */
  1840. function updateSlidesVisibility() {
  1841. // Select all slides and convert the NodeList result to
  1842. // an array
  1843. var horizontalSlides = toArray( dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) ),
  1844. horizontalSlidesLength = horizontalSlides.length,
  1845. distanceX,
  1846. distanceY;
  1847. if( horizontalSlidesLength && typeof indexh !== 'undefined' ) {
  1848. // The number of steps away from the present slide that will
  1849. // be visible
  1850. var viewDistance = isOverview() ? 10 : config.viewDistance;
  1851. // Limit view distance on weaker devices
  1852. if( isMobileDevice ) {
  1853. viewDistance = isOverview() ? 6 : 2;
  1854. }
  1855. // All slides need to be visible when exporting to PDF
  1856. if( isPrintingPDF() ) {
  1857. viewDistance = Number.MAX_VALUE;
  1858. }
  1859. for( var x = 0; x < horizontalSlidesLength; x++ ) {
  1860. var horizontalSlide = horizontalSlides[x];
  1861. var verticalSlides = toArray( horizontalSlide.querySelectorAll( 'section' ) ),
  1862. verticalSlidesLength = verticalSlides.length;
  1863. // Determine how far away this slide is from the present
  1864. distanceX = Math.abs( ( indexh || 0 ) - x ) || 0;
  1865. // If the presentation is looped, distance should measure
  1866. // 1 between the first and last slides
  1867. if( config.loop ) {
  1868. distanceX = Math.abs( ( ( indexh || 0 ) - x ) % ( horizontalSlidesLength - viewDistance ) ) || 0;
  1869. }
  1870. // Show the horizontal slide if it's within the view distance
  1871. if( distanceX < viewDistance ) {
  1872. showSlide( horizontalSlide );
  1873. }
  1874. else {
  1875. hideSlide( horizontalSlide );
  1876. }
  1877. if( verticalSlidesLength ) {
  1878. var oy = getPreviousVerticalIndex( horizontalSlide );
  1879. for( var y = 0; y < verticalSlidesLength; y++ ) {
  1880. var verticalSlide = verticalSlides[y];
  1881. distanceY = x === ( indexh || 0 ) ? Math.abs( ( indexv || 0 ) - y ) : Math.abs( y - oy );
  1882. if( distanceX + distanceY < viewDistance ) {
  1883. showSlide( verticalSlide );
  1884. }
  1885. else {
  1886. hideSlide( verticalSlide );
  1887. }
  1888. }
  1889. }
  1890. }
  1891. }
  1892. }
  1893. /**
  1894. * Updates the progress bar to reflect the current slide.
  1895. */
  1896. function updateProgress() {
  1897. // Update progress if enabled
  1898. if( config.progress && dom.progressbar ) {
  1899. dom.progressbar.style.width = getProgress() * dom.wrapper.offsetWidth + 'px';
  1900. }
  1901. }
  1902. /**
  1903. * Updates the slide number div to reflect the current slide.
  1904. *
  1905. * Slide number format can be defined as a string using the
  1906. * following variables:
  1907. * h: current slide's horizontal index
  1908. * v: current slide's vertical index
  1909. * c: current slide index (flattened)
  1910. * t: total number of slides (flattened)
  1911. */
  1912. function updateSlideNumber() {
  1913. // Update slide number if enabled
  1914. if( config.slideNumber && dom.slideNumber) {
  1915. // Default to only showing the current slide number
  1916. var format = 'c';
  1917. // Check if a custom slide number format is available
  1918. if( typeof config.slideNumber === 'string' ) {
  1919. format = config.slideNumber;
  1920. }
  1921. dom.slideNumber.innerHTML = format.replace( /h/g, indexh )
  1922. .replace( /v/g, indexv )
  1923. .replace( /c/g, getSlidePastCount() + 1 )
  1924. .replace( /t/g, getTotalSlides() );
  1925. }
  1926. }
  1927. /**
  1928. * Updates the state of all control/navigation arrows.
  1929. */
  1930. function updateControls() {
  1931. var routes = availableRoutes();
  1932. var fragments = availableFragments();
  1933. // Remove the 'enabled' class from all directions
  1934. dom.controlsLeft.concat( dom.controlsRight )
  1935. .concat( dom.controlsUp )
  1936. .concat( dom.controlsDown )
  1937. .concat( dom.controlsPrev )
  1938. .concat( dom.controlsNext ).forEach( function( node ) {
  1939. node.classList.remove( 'enabled' );
  1940. node.classList.remove( 'fragmented' );
  1941. } );
  1942. // Add the 'enabled' class to the available routes
  1943. if( routes.left ) dom.controlsLeft.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  1944. if( routes.right ) dom.controlsRight.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  1945. if( routes.up ) dom.controlsUp.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  1946. if( routes.down ) dom.controlsDown.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  1947. // Prev/next buttons
  1948. if( routes.left || routes.up ) dom.controlsPrev.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  1949. if( routes.right || routes.down ) dom.controlsNext.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  1950. // Highlight fragment directions
  1951. if( currentSlide ) {
  1952. // Always apply fragment decorator to prev/next buttons
  1953. if( fragments.prev ) dom.controlsPrev.forEach( function( el ) { el.classList.add( 'fragmented', 'enabled' ); } );
  1954. if( fragments.next ) dom.controlsNext.forEach( function( el ) { el.classList.add( 'fragmented', 'enabled' ); } );
  1955. // Apply fragment decorators to directional buttons based on
  1956. // what slide axis they are in
  1957. if( isVerticalSlide( currentSlide ) ) {
  1958. if( fragments.prev ) dom.controlsUp.forEach( function( el ) { el.classList.add( 'fragmented', 'enabled' ); } );
  1959. if( fragments.next ) dom.controlsDown.forEach( function( el ) { el.classList.add( 'fragmented', 'enabled' ); } );
  1960. }
  1961. else {
  1962. if( fragments.prev ) dom.controlsLeft.forEach( function( el ) { el.classList.add( 'fragmented', 'enabled' ); } );
  1963. if( fragments.next ) dom.controlsRight.forEach( function( el ) { el.classList.add( 'fragmented', 'enabled' ); } );
  1964. }
  1965. }
  1966. }
  1967. /**
  1968. * Updates the background elements to reflect the current
  1969. * slide.
  1970. *
  1971. * @param {Boolean} includeAll If true, the backgrounds of
  1972. * all vertical slides (not just the present) will be updated.
  1973. */
  1974. function updateBackground( includeAll ) {
  1975. var currentBackground = null;
  1976. // Reverse past/future classes when in RTL mode
  1977. var horizontalPast = config.rtl ? 'future' : 'past',
  1978. horizontalFuture = config.rtl ? 'past' : 'future';
  1979. // Update the classes of all backgrounds to match the
  1980. // states of their slides (past/present/future)
  1981. toArray( dom.background.childNodes ).forEach( function( backgroundh, h ) {
  1982. backgroundh.classList.remove( 'past' );
  1983. backgroundh.classList.remove( 'present' );
  1984. backgroundh.classList.remove( 'future' );
  1985. if( h < indexh ) {
  1986. backgroundh.classList.add( horizontalPast );
  1987. }
  1988. else if ( h > indexh ) {
  1989. backgroundh.classList.add( horizontalFuture );
  1990. }
  1991. else {
  1992. backgroundh.classList.add( 'present' );
  1993. // Store a reference to the current background element
  1994. currentBackground = backgroundh;
  1995. }
  1996. if( includeAll || h === indexh ) {
  1997. toArray( backgroundh.querySelectorAll( '.slide-background' ) ).forEach( function( backgroundv, v ) {
  1998. backgroundv.classList.remove( 'past' );
  1999. backgroundv.classList.remove( 'present' );
  2000. backgroundv.classList.remove( 'future' );
  2001. if( v < indexv ) {
  2002. backgroundv.classList.add( 'past' );
  2003. }
  2004. else if ( v > indexv ) {
  2005. backgroundv.classList.add( 'future' );
  2006. }
  2007. else {
  2008. backgroundv.classList.add( 'present' );
  2009. // Only if this is the present horizontal and vertical slide
  2010. if( h === indexh ) currentBackground = backgroundv;
  2011. }
  2012. } );
  2013. }
  2014. } );
  2015. // Stop any currently playing video background
  2016. if( previousBackground ) {
  2017. var previousVideo = previousBackground.querySelector( 'video' );
  2018. if( previousVideo ) previousVideo.pause();
  2019. }
  2020. if( currentBackground ) {
  2021. // Start video playback
  2022. var currentVideo = currentBackground.querySelector( 'video' );
  2023. if( currentVideo ) {
  2024. currentVideo.currentTime = 0;
  2025. currentVideo.play();
  2026. }
  2027. var backgroundImageURL = currentBackground.style.backgroundImage || '';
  2028. // Restart GIFs (doesn't work in Firefox)
  2029. if( /\.gif/i.test( backgroundImageURL ) ) {
  2030. currentBackground.style.backgroundImage = '';
  2031. window.getComputedStyle( currentBackground ).opacity;
  2032. currentBackground.style.backgroundImage = backgroundImageURL;
  2033. }
  2034. // Don't transition between identical backgrounds. This
  2035. // prevents unwanted flicker.
  2036. var previousBackgroundHash = previousBackground ? previousBackground.getAttribute( 'data-background-hash' ) : null;
  2037. var currentBackgroundHash = currentBackground.getAttribute( 'data-background-hash' );
  2038. if( currentBackgroundHash && currentBackgroundHash === previousBackgroundHash && currentBackground !== previousBackground ) {
  2039. dom.background.classList.add( 'no-transition' );
  2040. }
  2041. previousBackground = currentBackground;
  2042. }
  2043. // If there's a background brightness flag for this slide,
  2044. // bubble it to the .reveal container
  2045. if( currentSlide ) {
  2046. [ 'has-light-background', 'has-dark-background' ].forEach( function( classToBubble ) {
  2047. if( currentSlide.classList.contains( classToBubble ) ) {
  2048. dom.wrapper.classList.add( classToBubble );
  2049. }
  2050. else {
  2051. dom.wrapper.classList.remove( classToBubble );
  2052. }
  2053. } );
  2054. }
  2055. // Allow the first background to apply without transition
  2056. setTimeout( function() {
  2057. dom.background.classList.remove( 'no-transition' );
  2058. }, 1 );
  2059. }
  2060. /**
  2061. * Updates the position of the parallax background based
  2062. * on the current slide index.
  2063. */
  2064. function updateParallax() {
  2065. if( config.parallaxBackgroundImage ) {
  2066. var horizontalSlides = dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ),
  2067. verticalSlides = dom.wrapper.querySelectorAll( VERTICAL_SLIDES_SELECTOR );
  2068. var backgroundSize = dom.background.style.backgroundSize.split( ' ' ),
  2069. backgroundWidth, backgroundHeight;
  2070. if( backgroundSize.length === 1 ) {
  2071. backgroundWidth = backgroundHeight = parseInt( backgroundSize[0], 10 );
  2072. }
  2073. else {
  2074. backgroundWidth = parseInt( backgroundSize[0], 10 );
  2075. backgroundHeight = parseInt( backgroundSize[1], 10 );
  2076. }
  2077. var slideWidth = dom.background.offsetWidth,
  2078. horizontalSlideCount = horizontalSlides.length,
  2079. horizontalOffsetMultiplier,
  2080. horizontalOffset;
  2081. if( typeof config.parallaxBackgroundHorizontal === 'number' ) {
  2082. horizontalOffsetMultiplier = config.parallaxBackgroundHorizontal;
  2083. }
  2084. else {
  2085. horizontalOffsetMultiplier = ( backgroundWidth - slideWidth ) / ( horizontalSlideCount-1 );
  2086. }
  2087. horizontalOffset = horizontalOffsetMultiplier * indexh * -1;
  2088. var slideHeight = dom.background.offsetHeight,
  2089. verticalSlideCount = verticalSlides.length,
  2090. verticalOffsetMultiplier,
  2091. verticalOffset;
  2092. if( typeof config.parallaxBackgroundVertical === 'number' ) {
  2093. verticalOffsetMultiplier = config.parallaxBackgroundVertical;
  2094. }
  2095. else {
  2096. verticalOffsetMultiplier = ( backgroundHeight - slideHeight ) / ( verticalSlideCount-1 );
  2097. }
  2098. verticalOffset = verticalSlideCount > 0 ? verticalOffsetMultiplier * indexv * 1 : 0;
  2099. dom.background.style.backgroundPosition = horizontalOffset + 'px ' + -verticalOffset + 'px';
  2100. }
  2101. }
  2102. /**
  2103. * Called when the given slide is within the configured view
  2104. * distance. Shows the slide element and loads any content
  2105. * that is set to load lazily (data-src).
  2106. */
  2107. function showSlide( slide ) {
  2108. // Show the slide element
  2109. slide.style.display = 'block';
  2110. // Media elements with data-src attributes
  2111. toArray( slide.querySelectorAll( 'img[data-src], video[data-src], audio[data-src]' ) ).forEach( function( element ) {
  2112. element.setAttribute( 'src', element.getAttribute( 'data-src' ) );
  2113. element.removeAttribute( 'data-src' );
  2114. } );
  2115. // Media elements with <source> children
  2116. toArray( slide.querySelectorAll( 'video, audio' ) ).forEach( function( media ) {
  2117. var sources = 0;
  2118. toArray( media.querySelectorAll( 'source[data-src]' ) ).forEach( function( source ) {
  2119. source.setAttribute( 'src', source.getAttribute( 'data-src' ) );
  2120. source.removeAttribute( 'data-src' );
  2121. sources += 1;
  2122. } );
  2123. // If we rewrote sources for this video/audio element, we need
  2124. // to manually tell it to load from its new origin
  2125. if( sources > 0 ) {
  2126. media.load();
  2127. }
  2128. } );
  2129. // Show the corresponding background element
  2130. var indices = getIndices( slide );
  2131. var background = getSlideBackground( indices.h, indices.v );
  2132. if( background ) {
  2133. background.style.display = 'block';
  2134. // If the background contains media, load it
  2135. if( background.hasAttribute( 'data-loaded' ) === false ) {
  2136. background.setAttribute( 'data-loaded', 'true' );
  2137. var backgroundImage = slide.getAttribute( 'data-background-image' ),
  2138. backgroundVideo = slide.getAttribute( 'data-background-video' ),
  2139. backgroundVideoLoop = slide.hasAttribute( 'data-background-video-loop' ),
  2140. backgroundIframe = slide.getAttribute( 'data-background-iframe' );
  2141. // Images
  2142. if( backgroundImage ) {
  2143. background.style.backgroundImage = 'url('+ backgroundImage +')';
  2144. }
  2145. // Videos
  2146. else if ( backgroundVideo && !isSpeakerNotes() ) {
  2147. var video = document.createElement( 'video' );
  2148. if( backgroundVideoLoop ) {
  2149. video.setAttribute( 'loop', '' );
  2150. }
  2151. // Support comma separated lists of video sources
  2152. backgroundVideo.split( ',' ).forEach( function( source ) {
  2153. video.innerHTML += '<source src="'+ source +'">';
  2154. } );
  2155. background.appendChild( video );
  2156. }
  2157. // Iframes
  2158. else if( backgroundIframe ) {
  2159. var iframe = document.createElement( 'iframe' );
  2160. iframe.setAttribute( 'src', backgroundIframe );
  2161. iframe.style.width = '100%';
  2162. iframe.style.height = '100%';
  2163. iframe.style.maxHeight = '100%';
  2164. iframe.style.maxWidth = '100%';
  2165. background.appendChild( iframe );
  2166. }
  2167. }
  2168. }
  2169. }
  2170. /**
  2171. * Called when the given slide is moved outside of the
  2172. * configured view distance.
  2173. */
  2174. function hideSlide( slide ) {
  2175. // Hide the slide element
  2176. slide.style.display = 'none';
  2177. // Hide the corresponding background element
  2178. var indices = getIndices( slide );
  2179. var background = getSlideBackground( indices.h, indices.v );
  2180. if( background ) {
  2181. background.style.display = 'none';
  2182. }
  2183. }
  2184. /**
  2185. * Determine what available routes there are for navigation.
  2186. *
  2187. * @return {Object} containing four booleans: left/right/up/down
  2188. */
  2189. function availableRoutes() {
  2190. var horizontalSlides = dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ),
  2191. verticalSlides = dom.wrapper.querySelectorAll( VERTICAL_SLIDES_SELECTOR );
  2192. var routes = {
  2193. left: indexh > 0 || config.loop,
  2194. right: indexh < horizontalSlides.length - 1 || config.loop,
  2195. up: indexv > 0,
  2196. down: indexv < verticalSlides.length - 1
  2197. };
  2198. // reverse horizontal controls for rtl
  2199. if( config.rtl ) {
  2200. var left = routes.left;
  2201. routes.left = routes.right;
  2202. routes.right = left;
  2203. }
  2204. return routes;
  2205. }
  2206. /**
  2207. * Returns an object describing the available fragment
  2208. * directions.
  2209. *
  2210. * @return {Object} two boolean properties: prev/next
  2211. */
  2212. function availableFragments() {
  2213. if( currentSlide && config.fragments ) {
  2214. var fragments = currentSlide.querySelectorAll( '.fragment' );
  2215. var hiddenFragments = currentSlide.querySelectorAll( '.fragment:not(.visible)' );
  2216. return {
  2217. prev: fragments.length - hiddenFragments.length > 0,
  2218. next: !!hiddenFragments.length
  2219. };
  2220. }
  2221. else {
  2222. return { prev: false, next: false };
  2223. }
  2224. }
  2225. /**
  2226. * Enforces origin-specific format rules for embedded media.
  2227. */
  2228. function formatEmbeddedContent() {
  2229. var _appendParamToIframeSource = function( sourceAttribute, sourceURL, param ) {
  2230. toArray( dom.slides.querySelectorAll( 'iframe['+ sourceAttribute +'*="'+ sourceURL +'"]' ) ).forEach( function( el ) {
  2231. var src = el.getAttribute( sourceAttribute );
  2232. if( src && src.indexOf( param ) === -1 ) {
  2233. el.setAttribute( sourceAttribute, src + ( !/\?/.test( src ) ? '?' : '&' ) + param );
  2234. }
  2235. });
  2236. };
  2237. // YouTube frames must include "?enablejsapi=1"
  2238. _appendParamToIframeSource( 'src', 'youtube.com/embed/', 'enablejsapi=1' );
  2239. _appendParamToIframeSource( 'data-src', 'youtube.com/embed/', 'enablejsapi=1' );
  2240. // Vimeo frames must include "?api=1"
  2241. _appendParamToIframeSource( 'src', 'player.vimeo.com/', 'api=1' );
  2242. _appendParamToIframeSource( 'data-src', 'player.vimeo.com/', 'api=1' );
  2243. }
  2244. /**
  2245. * Start playback of any embedded content inside of
  2246. * the targeted slide.
  2247. */
  2248. function startEmbeddedContent( slide ) {
  2249. if( slide && !isSpeakerNotes() ) {
  2250. // Restart GIFs
  2251. toArray( slide.querySelectorAll( 'img[src$=".gif"]' ) ).forEach( function( el ) {
  2252. // Setting the same unchanged source like this was confirmed
  2253. // to work in Chrome, FF & Safari
  2254. el.setAttribute( 'src', el.getAttribute( 'src' ) );
  2255. } );
  2256. // HTML5 media elements
  2257. toArray( slide.querySelectorAll( 'video, audio' ) ).forEach( function( el ) {
  2258. if( el.hasAttribute( 'data-autoplay' ) && typeof el.play === 'function' ) {
  2259. el.play();
  2260. }
  2261. } );
  2262. // Normal iframes
  2263. toArray( slide.querySelectorAll( 'iframe[src]' ) ).forEach( function( el ) {
  2264. startEmbeddedIframe( { target: el } );
  2265. } );
  2266. // Lazy loading iframes
  2267. toArray( slide.querySelectorAll( 'iframe[data-src]' ) ).forEach( function( el ) {
  2268. if( el.getAttribute( 'src' ) !== el.getAttribute( 'data-src' ) ) {
  2269. el.removeEventListener( 'load', startEmbeddedIframe ); // remove first to avoid dupes
  2270. el.addEventListener( 'load', startEmbeddedIframe );
  2271. el.setAttribute( 'src', el.getAttribute( 'data-src' ) );
  2272. }
  2273. } );
  2274. }
  2275. }
  2276. /**
  2277. * "Starts" the content of an embedded iframe using the
  2278. * postmessage API.
  2279. */
  2280. function startEmbeddedIframe( event ) {
  2281. var iframe = event.target;
  2282. // YouTube postMessage API
  2283. if( /youtube\.com\/embed\//.test( iframe.getAttribute( 'src' ) ) && iframe.hasAttribute( 'data-autoplay' ) ) {
  2284. iframe.contentWindow.postMessage( '{"event":"command","func":"playVideo","args":""}', '*' );
  2285. }
  2286. // Vimeo postMessage API
  2287. else if( /player\.vimeo\.com\//.test( iframe.getAttribute( 'src' ) ) && iframe.hasAttribute( 'data-autoplay' ) ) {
  2288. iframe.contentWindow.postMessage( '{"method":"play"}', '*' );
  2289. }
  2290. // Generic postMessage API
  2291. else {
  2292. iframe.contentWindow.postMessage( 'slide:start', '*' );
  2293. }
  2294. }
  2295. /**
  2296. * Stop playback of any embedded content inside of
  2297. * the targeted slide.
  2298. */
  2299. function stopEmbeddedContent( slide ) {
  2300. if( slide && slide.parentNode ) {
  2301. // HTML5 media elements
  2302. toArray( slide.querySelectorAll( 'video, audio' ) ).forEach( function( el ) {
  2303. if( !el.hasAttribute( 'data-ignore' ) && typeof el.pause === 'function' ) {
  2304. el.pause();
  2305. }
  2306. } );
  2307. // Generic postMessage API for non-lazy loaded iframes
  2308. toArray( slide.querySelectorAll( 'iframe' ) ).forEach( function( el ) {
  2309. el.contentWindow.postMessage( 'slide:stop', '*' );
  2310. el.removeEventListener( 'load', startEmbeddedIframe );
  2311. });
  2312. // YouTube postMessage API
  2313. toArray( slide.querySelectorAll( 'iframe[src*="youtube.com/embed/"]' ) ).forEach( function( el ) {
  2314. if( !el.hasAttribute( 'data-ignore' ) && typeof el.contentWindow.postMessage === 'function' ) {
  2315. el.contentWindow.postMessage( '{"event":"command","func":"pauseVideo","args":""}', '*' );
  2316. }
  2317. });
  2318. // Vimeo postMessage API
  2319. toArray( slide.querySelectorAll( 'iframe[src*="player.vimeo.com/"]' ) ).forEach( function( el ) {
  2320. if( !el.hasAttribute( 'data-ignore' ) && typeof el.contentWindow.postMessage === 'function' ) {
  2321. el.contentWindow.postMessage( '{"method":"pause"}', '*' );
  2322. }
  2323. });
  2324. // Lazy loading iframes
  2325. toArray( slide.querySelectorAll( 'iframe[data-src]' ) ).forEach( function( el ) {
  2326. // Only removing the src doesn't actually unload the frame
  2327. // in all browsers (Firefox) so we set it to blank first
  2328. el.setAttribute( 'src', 'about:blank' );
  2329. el.removeAttribute( 'src' );
  2330. } );
  2331. }
  2332. }
  2333. /**
  2334. * Returns the number of past slides. This can be used as a global
  2335. * flattened index for slides.
  2336. */
  2337. function getSlidePastCount() {
  2338. var horizontalSlides = toArray( dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) );
  2339. // The number of past slides
  2340. var pastCount = 0;
  2341. // Step through all slides and count the past ones
  2342. mainLoop: for( var i = 0; i < horizontalSlides.length; i++ ) {
  2343. var horizontalSlide = horizontalSlides[i];
  2344. var verticalSlides = toArray( horizontalSlide.querySelectorAll( 'section' ) );
  2345. for( var j = 0; j < verticalSlides.length; j++ ) {
  2346. // Stop as soon as we arrive at the present
  2347. if( verticalSlides[j].classList.contains( 'present' ) ) {
  2348. break mainLoop;
  2349. }
  2350. pastCount++;
  2351. }
  2352. // Stop as soon as we arrive at the present
  2353. if( horizontalSlide.classList.contains( 'present' ) ) {
  2354. break;
  2355. }
  2356. // Don't count the wrapping section for vertical slides
  2357. if( horizontalSlide.classList.contains( 'stack' ) === false ) {
  2358. pastCount++;
  2359. }
  2360. }
  2361. return pastCount;
  2362. }
  2363. /**
  2364. * Returns a value ranging from 0-1 that represents
  2365. * how far into the presentation we have navigated.
  2366. */
  2367. function getProgress() {
  2368. // The number of past and total slides
  2369. var totalCount = getTotalSlides();
  2370. var pastCount = getSlidePastCount();
  2371. if( currentSlide ) {
  2372. var allFragments = currentSlide.querySelectorAll( '.fragment' );
  2373. // If there are fragments in the current slide those should be
  2374. // accounted for in the progress.
  2375. if( allFragments.length > 0 ) {
  2376. var visibleFragments = currentSlide.querySelectorAll( '.fragment.visible' );
  2377. // This value represents how big a portion of the slide progress
  2378. // that is made up by its fragments (0-1)
  2379. var fragmentWeight = 0.9;
  2380. // Add fragment progress to the past slide count
  2381. pastCount += ( visibleFragments.length / allFragments.length ) * fragmentWeight;
  2382. }
  2383. }
  2384. return pastCount / ( totalCount - 1 );
  2385. }
  2386. /**
  2387. * Checks if this presentation is running inside of the
  2388. * speaker notes window.
  2389. */
  2390. function isSpeakerNotes() {
  2391. return !!window.location.search.match( /receiver/gi );
  2392. }
  2393. /**
  2394. * Reads the current URL (hash) and navigates accordingly.
  2395. */
  2396. function readURL() {
  2397. var hash = window.location.hash;
  2398. // Attempt to parse the hash as either an index or name
  2399. var bits = hash.slice( 2 ).split( '/' ),
  2400. name = hash.replace( /#|\//gi, '' );
  2401. // If the first bit is invalid and there is a name we can
  2402. // assume that this is a named link
  2403. if( isNaN( parseInt( bits[0], 10 ) ) && name.length ) {
  2404. var element;
  2405. // Ensure the named link is a valid HTML ID attribute
  2406. if( /^[a-zA-Z][\w:.-]*$/.test( name ) ) {
  2407. // Find the slide with the specified ID
  2408. element = document.getElementById( name );
  2409. }
  2410. if( element ) {
  2411. // Find the position of the named slide and navigate to it
  2412. var indices = Reveal.getIndices( element );
  2413. slide( indices.h, indices.v );
  2414. }
  2415. // If the slide doesn't exist, navigate to the current slide
  2416. else {
  2417. slide( indexh || 0, indexv || 0 );
  2418. }
  2419. }
  2420. else {
  2421. // Read the index components of the hash
  2422. var h = parseInt( bits[0], 10 ) || 0,
  2423. v = parseInt( bits[1], 10 ) || 0;
  2424. if( h !== indexh || v !== indexv ) {
  2425. slide( h, v );
  2426. }
  2427. }
  2428. }
  2429. /**
  2430. * Updates the page URL (hash) to reflect the current
  2431. * state.
  2432. *
  2433. * @param {Number} delay The time in ms to wait before
  2434. * writing the hash
  2435. */
  2436. function writeURL( delay ) {
  2437. if( config.history ) {
  2438. // Make sure there's never more than one timeout running
  2439. clearTimeout( writeURLTimeout );
  2440. // If a delay is specified, timeout this call
  2441. if( typeof delay === 'number' ) {
  2442. writeURLTimeout = setTimeout( writeURL, delay );
  2443. }
  2444. else if( currentSlide ) {
  2445. var url = '/';
  2446. // Attempt to create a named link based on the slide's ID
  2447. var id = currentSlide.getAttribute( 'id' );
  2448. if( id ) {
  2449. id = id.toLowerCase();
  2450. id = id.replace( /[^a-zA-Z0-9\-\_\:\.]/g, '' );
  2451. }
  2452. // If the current slide has an ID, use that as a named link
  2453. if( typeof id === 'string' && id.length ) {
  2454. url = '/' + id;
  2455. }
  2456. // Otherwise use the /h/v index
  2457. else {
  2458. if( indexh > 0 || indexv > 0 ) url += indexh;
  2459. if( indexv > 0 ) url += '/' + indexv;
  2460. }
  2461. window.location.hash = url;
  2462. }
  2463. }
  2464. }
  2465. /**
  2466. * Retrieves the h/v location of the current, or specified,
  2467. * slide.
  2468. *
  2469. * @param {HTMLElement} slide If specified, the returned
  2470. * index will be for this slide rather than the currently
  2471. * active one
  2472. *
  2473. * @return {Object} { h: <int>, v: <int>, f: <int> }
  2474. */
  2475. function getIndices( slide ) {
  2476. // By default, return the current indices
  2477. var h = indexh,
  2478. v = indexv,
  2479. f;
  2480. // If a slide is specified, return the indices of that slide
  2481. if( slide ) {
  2482. var isVertical = isVerticalSlide( slide );
  2483. var slideh = isVertical ? slide.parentNode : slide;
  2484. // Select all horizontal slides
  2485. var horizontalSlides = toArray( dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) );
  2486. // Now that we know which the horizontal slide is, get its index
  2487. h = Math.max( horizontalSlides.indexOf( slideh ), 0 );
  2488. // Assume we're not vertical
  2489. v = undefined;
  2490. // If this is a vertical slide, grab the vertical index
  2491. if( isVertical ) {
  2492. v = Math.max( toArray( slide.parentNode.querySelectorAll( 'section' ) ).indexOf( slide ), 0 );
  2493. }
  2494. }
  2495. if( !slide && currentSlide ) {
  2496. var hasFragments = currentSlide.querySelectorAll( '.fragment' ).length > 0;
  2497. if( hasFragments ) {
  2498. var currentFragment = currentSlide.querySelector( '.current-fragment' );
  2499. if( currentFragment && currentFragment.hasAttribute( 'data-fragment-index' ) ) {
  2500. f = parseInt( currentFragment.getAttribute( 'data-fragment-index' ), 10 );
  2501. }
  2502. else {
  2503. f = currentSlide.querySelectorAll( '.fragment.visible' ).length - 1;
  2504. }
  2505. }
  2506. }
  2507. return { h: h, v: v, f: f };
  2508. }
  2509. /**
  2510. * Retrieves the total number of slides in this presentation.
  2511. */
  2512. function getTotalSlides() {
  2513. return dom.wrapper.querySelectorAll( SLIDES_SELECTOR + ':not(.stack)' ).length;
  2514. }
  2515. /**
  2516. * Returns the slide element matching the specified index.
  2517. */
  2518. function getSlide( x, y ) {
  2519. var horizontalSlide = dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR )[ x ];
  2520. var verticalSlides = horizontalSlide && horizontalSlide.querySelectorAll( 'section' );
  2521. if( verticalSlides && verticalSlides.length && typeof y === 'number' ) {
  2522. return verticalSlides ? verticalSlides[ y ] : undefined;
  2523. }
  2524. return horizontalSlide;
  2525. }
  2526. /**
  2527. * Returns the background element for the given slide.
  2528. * All slides, even the ones with no background properties
  2529. * defined, have a background element so as long as the
  2530. * index is valid an element will be returned.
  2531. */
  2532. function getSlideBackground( x, y ) {
  2533. // When printing to PDF the slide backgrounds are nested
  2534. // inside of the slides
  2535. if( isPrintingPDF() ) {
  2536. var slide = getSlide( x, y );
  2537. if( slide ) {
  2538. var background = slide.querySelector( '.slide-background' );
  2539. if( background && background.parentNode === slide ) {
  2540. return background;
  2541. }
  2542. }
  2543. return undefined;
  2544. }
  2545. var horizontalBackground = dom.wrapper.querySelectorAll( '.backgrounds>.slide-background' )[ x ];
  2546. var verticalBackgrounds = horizontalBackground && horizontalBackground.querySelectorAll( '.slide-background' );
  2547. if( verticalBackgrounds && verticalBackgrounds.length && typeof y === 'number' ) {
  2548. return verticalBackgrounds ? verticalBackgrounds[ y ] : undefined;
  2549. }
  2550. return horizontalBackground;
  2551. }
  2552. /**
  2553. * Retrieves the current state of the presentation as
  2554. * an object. This state can then be restored at any
  2555. * time.
  2556. */
  2557. function getState() {
  2558. var indices = getIndices();
  2559. return {
  2560. indexh: indices.h,
  2561. indexv: indices.v,
  2562. indexf: indices.f,
  2563. paused: isPaused(),
  2564. overview: isOverview()
  2565. };
  2566. }
  2567. /**
  2568. * Restores the presentation to the given state.
  2569. *
  2570. * @param {Object} state As generated by getState()
  2571. */
  2572. function setState( state ) {
  2573. if( typeof state === 'object' ) {
  2574. slide( deserialize( state.indexh ), deserialize( state.indexv ), deserialize( state.indexf ) );
  2575. var pausedFlag = deserialize( state.paused ),
  2576. overviewFlag = deserialize( state.overview );
  2577. if( typeof pausedFlag === 'boolean' && pausedFlag !== isPaused() ) {
  2578. togglePause( pausedFlag );
  2579. }
  2580. if( typeof overviewFlag === 'boolean' && overviewFlag !== isOverview() ) {
  2581. toggleOverview( overviewFlag );
  2582. }
  2583. }
  2584. }
  2585. /**
  2586. * Return a sorted fragments list, ordered by an increasing
  2587. * "data-fragment-index" attribute.
  2588. *
  2589. * Fragments will be revealed in the order that they are returned by
  2590. * this function, so you can use the index attributes to control the
  2591. * order of fragment appearance.
  2592. *
  2593. * To maintain a sensible default fragment order, fragments are presumed
  2594. * to be passed in document order. This function adds a "fragment-index"
  2595. * attribute to each node if such an attribute is not already present,
  2596. * and sets that attribute to an integer value which is the position of
  2597. * the fragment within the fragments list.
  2598. */
  2599. function sortFragments( fragments ) {
  2600. fragments = toArray( fragments );
  2601. var ordered = [],
  2602. unordered = [],
  2603. sorted = [];
  2604. // Group ordered and unordered elements
  2605. fragments.forEach( function( fragment, i ) {
  2606. if( fragment.hasAttribute( 'data-fragment-index' ) ) {
  2607. var index = parseInt( fragment.getAttribute( 'data-fragment-index' ), 10 );
  2608. if( !ordered[index] ) {
  2609. ordered[index] = [];
  2610. }
  2611. ordered[index].push( fragment );
  2612. }
  2613. else {
  2614. unordered.push( [ fragment ] );
  2615. }
  2616. } );
  2617. // Append fragments without explicit indices in their
  2618. // DOM order
  2619. ordered = ordered.concat( unordered );
  2620. // Manually count the index up per group to ensure there
  2621. // are no gaps
  2622. var index = 0;
  2623. // Push all fragments in their sorted order to an array,
  2624. // this flattens the groups
  2625. ordered.forEach( function( group ) {
  2626. group.forEach( function( fragment ) {
  2627. sorted.push( fragment );
  2628. fragment.setAttribute( 'data-fragment-index', index );
  2629. } );
  2630. index ++;
  2631. } );
  2632. return sorted;
  2633. }
  2634. /**
  2635. * Navigate to the specified slide fragment.
  2636. *
  2637. * @param {Number} index The index of the fragment that
  2638. * should be shown, -1 means all are invisible
  2639. * @param {Number} offset Integer offset to apply to the
  2640. * fragment index
  2641. *
  2642. * @return {Boolean} true if a change was made in any
  2643. * fragments visibility as part of this call
  2644. */
  2645. function navigateFragment( index, offset ) {
  2646. if( currentSlide && config.fragments ) {
  2647. var fragments = sortFragments( currentSlide.querySelectorAll( '.fragment' ) );
  2648. if( fragments.length ) {
  2649. // If no index is specified, find the current
  2650. if( typeof index !== 'number' ) {
  2651. var lastVisibleFragment = sortFragments( currentSlide.querySelectorAll( '.fragment.visible' ) ).pop();
  2652. if( lastVisibleFragment ) {
  2653. index = parseInt( lastVisibleFragment.getAttribute( 'data-fragment-index' ) || 0, 10 );
  2654. }
  2655. else {
  2656. index = -1;
  2657. }
  2658. }
  2659. // If an offset is specified, apply it to the index
  2660. if( typeof offset === 'number' ) {
  2661. index += offset;
  2662. }
  2663. var fragmentsShown = [],
  2664. fragmentsHidden = [];
  2665. toArray( fragments ).forEach( function( element, i ) {
  2666. if( element.hasAttribute( 'data-fragment-index' ) ) {
  2667. i = parseInt( element.getAttribute( 'data-fragment-index' ), 10 );
  2668. }
  2669. // Visible fragments
  2670. if( i <= index ) {
  2671. if( !element.classList.contains( 'visible' ) ) fragmentsShown.push( element );
  2672. element.classList.add( 'visible' );
  2673. element.classList.remove( 'current-fragment' );
  2674. // Announce the fragments one by one to the Screen Reader
  2675. dom.statusDiv.textContent = element.textContent;
  2676. if( i === index ) {
  2677. element.classList.add( 'current-fragment' );
  2678. }
  2679. }
  2680. // Hidden fragments
  2681. else {
  2682. if( element.classList.contains( 'visible' ) ) fragmentsHidden.push( element );
  2683. element.classList.remove( 'visible' );
  2684. element.classList.remove( 'current-fragment' );
  2685. }
  2686. } );
  2687. if( fragmentsHidden.length ) {
  2688. dispatchEvent( 'fragmenthidden', { fragment: fragmentsHidden[0], fragments: fragmentsHidden } );
  2689. }
  2690. if( fragmentsShown.length ) {
  2691. dispatchEvent( 'fragmentshown', { fragment: fragmentsShown[0], fragments: fragmentsShown } );
  2692. }
  2693. updateControls();
  2694. updateProgress();
  2695. return !!( fragmentsShown.length || fragmentsHidden.length );
  2696. }
  2697. }
  2698. return false;
  2699. }
  2700. /**
  2701. * Navigate to the next slide fragment.
  2702. *
  2703. * @return {Boolean} true if there was a next fragment,
  2704. * false otherwise
  2705. */
  2706. function nextFragment() {
  2707. return navigateFragment( null, 1 );
  2708. }
  2709. /**
  2710. * Navigate to the previous slide fragment.
  2711. *
  2712. * @return {Boolean} true if there was a previous fragment,
  2713. * false otherwise
  2714. */
  2715. function previousFragment() {
  2716. return navigateFragment( null, -1 );
  2717. }
  2718. /**
  2719. * Cues a new automated slide if enabled in the config.
  2720. */
  2721. function cueAutoSlide() {
  2722. cancelAutoSlide();
  2723. if( currentSlide ) {
  2724. var currentFragment = currentSlide.querySelector( '.current-fragment' );
  2725. var fragmentAutoSlide = currentFragment ? currentFragment.getAttribute( 'data-autoslide' ) : null;
  2726. var parentAutoSlide = currentSlide.parentNode ? currentSlide.parentNode.getAttribute( 'data-autoslide' ) : null;
  2727. var slideAutoSlide = currentSlide.getAttribute( 'data-autoslide' );
  2728. // Pick value in the following priority order:
  2729. // 1. Current fragment's data-autoslide
  2730. // 2. Current slide's data-autoslide
  2731. // 3. Parent slide's data-autoslide
  2732. // 4. Global autoSlide setting
  2733. if( fragmentAutoSlide ) {
  2734. autoSlide = parseInt( fragmentAutoSlide, 10 );
  2735. }
  2736. else if( slideAutoSlide ) {
  2737. autoSlide = parseInt( slideAutoSlide, 10 );
  2738. }
  2739. else if( parentAutoSlide ) {
  2740. autoSlide = parseInt( parentAutoSlide, 10 );
  2741. }
  2742. else {
  2743. autoSlide = config.autoSlide;
  2744. }
  2745. // If there are media elements with data-autoplay,
  2746. // automatically set the autoSlide duration to the
  2747. // length of that media. Not applicable if the slide
  2748. // is divided up into fragments.
  2749. if( currentSlide.querySelectorAll( '.fragment' ).length === 0 ) {
  2750. toArray( currentSlide.querySelectorAll( 'video, audio' ) ).forEach( function( el ) {
  2751. if( el.hasAttribute( 'data-autoplay' ) ) {
  2752. if( autoSlide && el.duration * 1000 > autoSlide ) {
  2753. autoSlide = ( el.duration * 1000 ) + 1000;
  2754. }
  2755. }
  2756. } );
  2757. }
  2758. // Cue the next auto-slide if:
  2759. // - There is an autoSlide value
  2760. // - Auto-sliding isn't paused by the user
  2761. // - The presentation isn't paused
  2762. // - The overview isn't active
  2763. // - The presentation isn't over
  2764. if( autoSlide && !autoSlidePaused && !isPaused() && !isOverview() && ( !Reveal.isLastSlide() || availableFragments().next || config.loop === true ) ) {
  2765. autoSlideTimeout = setTimeout( navigateNext, autoSlide );
  2766. autoSlideStartTime = Date.now();
  2767. }
  2768. if( autoSlidePlayer ) {
  2769. autoSlidePlayer.setPlaying( autoSlideTimeout !== -1 );
  2770. }
  2771. }
  2772. }
  2773. /**
  2774. * Cancels any ongoing request to auto-slide.
  2775. */
  2776. function cancelAutoSlide() {
  2777. clearTimeout( autoSlideTimeout );
  2778. autoSlideTimeout = -1;
  2779. }
  2780. function pauseAutoSlide() {
  2781. if( autoSlide && !autoSlidePaused ) {
  2782. autoSlidePaused = true;
  2783. dispatchEvent( 'autoslidepaused' );
  2784. clearTimeout( autoSlideTimeout );
  2785. if( autoSlidePlayer ) {
  2786. autoSlidePlayer.setPlaying( false );
  2787. }
  2788. }
  2789. }
  2790. function resumeAutoSlide() {
  2791. if( autoSlide && autoSlidePaused ) {
  2792. autoSlidePaused = false;
  2793. dispatchEvent( 'autoslideresumed' );
  2794. cueAutoSlide();
  2795. }
  2796. }
  2797. function navigateLeft() {
  2798. // Reverse for RTL
  2799. if( config.rtl ) {
  2800. if( ( isOverview() || nextFragment() === false ) && availableRoutes().left ) {
  2801. slide( indexh + 1 );
  2802. }
  2803. }
  2804. // Normal navigation
  2805. else if( ( isOverview() || previousFragment() === false ) && availableRoutes().left ) {
  2806. slide( indexh - 1 );
  2807. }
  2808. }
  2809. function navigateRight() {
  2810. // Reverse for RTL
  2811. if( config.rtl ) {
  2812. if( ( isOverview() || previousFragment() === false ) && availableRoutes().right ) {
  2813. slide( indexh - 1 );
  2814. }
  2815. }
  2816. // Normal navigation
  2817. else if( ( isOverview() || nextFragment() === false ) && availableRoutes().right ) {
  2818. slide( indexh + 1 );
  2819. }
  2820. }
  2821. function navigateUp() {
  2822. // Prioritize hiding fragments
  2823. if( ( isOverview() || previousFragment() === false ) && availableRoutes().up ) {
  2824. slide( indexh, indexv - 1 );
  2825. }
  2826. }
  2827. function navigateDown() {
  2828. // Prioritize revealing fragments
  2829. if( ( isOverview() || nextFragment() === false ) && availableRoutes().down ) {
  2830. slide( indexh, indexv + 1 );
  2831. }
  2832. }
  2833. /**
  2834. * Navigates backwards, prioritized in the following order:
  2835. * 1) Previous fragment
  2836. * 2) Previous vertical slide
  2837. * 3) Previous horizontal slide
  2838. */
  2839. function navigatePrev() {
  2840. // Prioritize revealing fragments
  2841. if( previousFragment() === false ) {
  2842. if( availableRoutes().up ) {
  2843. navigateUp();
  2844. }
  2845. else {
  2846. // Fetch the previous horizontal slide, if there is one
  2847. var previousSlide;
  2848. if( config.rtl ) {
  2849. previousSlide = toArray( dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR + '.future' ) ).pop();
  2850. }
  2851. else {
  2852. previousSlide = toArray( dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR + '.past' ) ).pop();
  2853. }
  2854. if( previousSlide ) {
  2855. var v = ( previousSlide.querySelectorAll( 'section' ).length - 1 ) || undefined;
  2856. var h = indexh - 1;
  2857. slide( h, v );
  2858. }
  2859. }
  2860. }
  2861. }
  2862. /**
  2863. * The reverse of #navigatePrev().
  2864. */
  2865. function navigateNext() {
  2866. // Prioritize revealing fragments
  2867. if( nextFragment() === false ) {
  2868. if( availableRoutes().down ) {
  2869. navigateDown();
  2870. }
  2871. else if( config.rtl ) {
  2872. navigateLeft();
  2873. }
  2874. else {
  2875. navigateRight();
  2876. }
  2877. }
  2878. // If auto-sliding is enabled we need to cue up
  2879. // another timeout
  2880. cueAutoSlide();
  2881. }
  2882. // --------------------------------------------------------------------//
  2883. // ----------------------------- EVENTS -------------------------------//
  2884. // --------------------------------------------------------------------//
  2885. /**
  2886. * Called by all event handlers that are based on user
  2887. * input.
  2888. */
  2889. function onUserInput( event ) {
  2890. if( config.autoSlideStoppable ) {
  2891. pauseAutoSlide();
  2892. }
  2893. }
  2894. /**
  2895. * Handler for the document level 'keypress' event.
  2896. */
  2897. function onDocumentKeyPress( event ) {
  2898. // Check if the pressed key is question mark
  2899. if( event.shiftKey && event.charCode === 63 ) {
  2900. if( dom.overlay ) {
  2901. closeOverlay();
  2902. }
  2903. else {
  2904. showHelp( true );
  2905. }
  2906. }
  2907. }
  2908. /**
  2909. * Handler for the document level 'keydown' event.
  2910. */
  2911. function onDocumentKeyDown( event ) {
  2912. // If there's a condition specified and it returns false,
  2913. // ignore this event
  2914. if( typeof config.keyboardCondition === 'function' && config.keyboardCondition() === false ) {
  2915. return true;
  2916. }
  2917. // Remember if auto-sliding was paused so we can toggle it
  2918. var autoSlideWasPaused = autoSlidePaused;
  2919. onUserInput( event );
  2920. // Check if there's a focused element that could be using
  2921. // the keyboard
  2922. var activeElementIsCE = document.activeElement && document.activeElement.contentEditable !== 'inherit';
  2923. var activeElementIsInput = document.activeElement && document.activeElement.tagName && /input|textarea/i.test( document.activeElement.tagName );
  2924. // Disregard the event if there's a focused element or a
  2925. // keyboard modifier key is present
  2926. if( activeElementIsCE || activeElementIsInput || (event.shiftKey && event.keyCode !== 32) || event.altKey || event.ctrlKey || event.metaKey ) return;
  2927. // While paused only allow resume keyboard events; 'b', '.''
  2928. var resumeKeyCodes = [66,190,191];
  2929. var key;
  2930. // Custom key bindings for togglePause should be able to resume
  2931. if( typeof config.keyboard === 'object' ) {
  2932. for( key in config.keyboard ) {
  2933. if( config.keyboard[key] === 'togglePause' ) {
  2934. resumeKeyCodes.push( parseInt( key, 10 ) );
  2935. }
  2936. }
  2937. }
  2938. if( isPaused() && resumeKeyCodes.indexOf( event.keyCode ) === -1 ) {
  2939. return false;
  2940. }
  2941. var triggered = false;
  2942. // 1. User defined key bindings
  2943. if( typeof config.keyboard === 'object' ) {
  2944. for( key in config.keyboard ) {
  2945. // Check if this binding matches the pressed key
  2946. if( parseInt( key, 10 ) === event.keyCode ) {
  2947. var value = config.keyboard[ key ];
  2948. // Callback function
  2949. if( typeof value === 'function' ) {
  2950. value.apply( null, [ event ] );
  2951. }
  2952. // String shortcuts to reveal.js API
  2953. else if( typeof value === 'string' && typeof Reveal[ value ] === 'function' ) {
  2954. Reveal[ value ].call();
  2955. }
  2956. triggered = true;
  2957. }
  2958. }
  2959. }
  2960. // 2. System defined key bindings
  2961. if( triggered === false ) {
  2962. // Assume true and try to prove false
  2963. triggered = true;
  2964. switch( event.keyCode ) {
  2965. // p, page up
  2966. case 80: case 33: navigatePrev(); break;
  2967. // n, page down
  2968. case 78: case 34: navigateNext(); break;
  2969. // h, left
  2970. case 72: case 37: navigateLeft(); break;
  2971. // l, right
  2972. case 76: case 39: navigateRight(); break;
  2973. // k, up
  2974. case 75: case 38: navigateUp(); break;
  2975. // j, down
  2976. case 74: case 40: navigateDown(); break;
  2977. // home
  2978. case 36: slide( 0 ); break;
  2979. // end
  2980. case 35: slide( Number.MAX_VALUE ); break;
  2981. // space
  2982. case 32: isOverview() ? deactivateOverview() : event.shiftKey ? navigatePrev() : navigateNext(); break;
  2983. // return
  2984. case 13: isOverview() ? deactivateOverview() : triggered = false; break;
  2985. // two-spot, semicolon, b, period, Logitech presenter tools "black screen" button
  2986. case 58: case 59: case 66: case 190: case 191: togglePause(); break;
  2987. // f
  2988. case 70: enterFullscreen(); break;
  2989. // a
  2990. case 65: if ( config.autoSlideStoppable ) toggleAutoSlide( autoSlideWasPaused ); break;
  2991. default:
  2992. triggered = false;
  2993. }
  2994. }
  2995. // If the input resulted in a triggered action we should prevent
  2996. // the browsers default behavior
  2997. if( triggered ) {
  2998. event.preventDefault && event.preventDefault();
  2999. }
  3000. // ESC or O key
  3001. else if ( ( event.keyCode === 27 || event.keyCode === 79 ) && features.transforms3d ) {
  3002. if( dom.overlay ) {
  3003. closeOverlay();
  3004. }
  3005. else {
  3006. toggleOverview();
  3007. }
  3008. event.preventDefault && event.preventDefault();
  3009. }
  3010. // If auto-sliding is enabled we need to cue up
  3011. // another timeout
  3012. cueAutoSlide();
  3013. }
  3014. /**
  3015. * Handler for the 'touchstart' event, enables support for
  3016. * swipe and pinch gestures.
  3017. */
  3018. function onTouchStart( event ) {
  3019. touch.startX = event.touches[0].clientX;
  3020. touch.startY = event.touches[0].clientY;
  3021. touch.startCount = event.touches.length;
  3022. // If there's two touches we need to memorize the distance
  3023. // between those two points to detect pinching
  3024. if( event.touches.length === 2 && config.overview ) {
  3025. touch.startSpan = distanceBetween( {
  3026. x: event.touches[1].clientX,
  3027. y: event.touches[1].clientY
  3028. }, {
  3029. x: touch.startX,
  3030. y: touch.startY
  3031. } );
  3032. }
  3033. }
  3034. /**
  3035. * Handler for the 'touchmove' event.
  3036. */
  3037. function onTouchMove( event ) {
  3038. // Each touch should only trigger one action
  3039. if( !touch.captured ) {
  3040. onUserInput( event );
  3041. var currentX = event.touches[0].clientX;
  3042. var currentY = event.touches[0].clientY;
  3043. // If the touch started with two points and still has
  3044. // two active touches; test for the pinch gesture
  3045. if( event.touches.length === 2 && touch.startCount === 2 && config.overview ) {
  3046. // The current distance in pixels between the two touch points
  3047. var currentSpan = distanceBetween( {
  3048. x: event.touches[1].clientX,
  3049. y: event.touches[1].clientY
  3050. }, {
  3051. x: touch.startX,
  3052. y: touch.startY
  3053. } );
  3054. // If the span is larger than the desire amount we've got
  3055. // ourselves a pinch
  3056. if( Math.abs( touch.startSpan - currentSpan ) > touch.threshold ) {
  3057. touch.captured = true;
  3058. if( currentSpan < touch.startSpan ) {
  3059. activateOverview();
  3060. }
  3061. else {
  3062. deactivateOverview();
  3063. }
  3064. }
  3065. event.preventDefault();
  3066. }
  3067. // There was only one touch point, look for a swipe
  3068. else if( event.touches.length === 1 && touch.startCount !== 2 ) {
  3069. var deltaX = currentX - touch.startX,
  3070. deltaY = currentY - touch.startY;
  3071. if( deltaX > touch.threshold && Math.abs( deltaX ) > Math.abs( deltaY ) ) {
  3072. touch.captured = true;
  3073. navigateLeft();
  3074. }
  3075. else if( deltaX < -touch.threshold && Math.abs( deltaX ) > Math.abs( deltaY ) ) {
  3076. touch.captured = true;
  3077. navigateRight();
  3078. }
  3079. else if( deltaY > touch.threshold ) {
  3080. touch.captured = true;
  3081. navigateUp();
  3082. }
  3083. else if( deltaY < -touch.threshold ) {
  3084. touch.captured = true;
  3085. navigateDown();
  3086. }
  3087. // If we're embedded, only block touch events if they have
  3088. // triggered an action
  3089. if( config.embedded ) {
  3090. if( touch.captured || isVerticalSlide( currentSlide ) ) {
  3091. event.preventDefault();
  3092. }
  3093. }
  3094. // Not embedded? Block them all to avoid needless tossing
  3095. // around of the viewport in iOS
  3096. else {
  3097. event.preventDefault();
  3098. }
  3099. }
  3100. }
  3101. // There's a bug with swiping on some Android devices unless
  3102. // the default action is always prevented
  3103. else if( navigator.userAgent.match( /android/gi ) ) {
  3104. event.preventDefault();
  3105. }
  3106. }
  3107. /**
  3108. * Handler for the 'touchend' event.
  3109. */
  3110. function onTouchEnd( event ) {
  3111. touch.captured = false;
  3112. }
  3113. /**
  3114. * Convert pointer down to touch start.
  3115. */
  3116. function onPointerDown( event ) {
  3117. if( event.pointerType === event.MSPOINTER_TYPE_TOUCH || event.pointerType === "touch" ) {
  3118. event.touches = [{ clientX: event.clientX, clientY: event.clientY }];
  3119. onTouchStart( event );
  3120. }
  3121. }
  3122. /**
  3123. * Convert pointer move to touch move.
  3124. */
  3125. function onPointerMove( event ) {
  3126. if( event.pointerType === event.MSPOINTER_TYPE_TOUCH || event.pointerType === "touch" ) {
  3127. event.touches = [{ clientX: event.clientX, clientY: event.clientY }];
  3128. onTouchMove( event );
  3129. }
  3130. }
  3131. /**
  3132. * Convert pointer up to touch end.
  3133. */
  3134. function onPointerUp( event ) {
  3135. if( event.pointerType === event.MSPOINTER_TYPE_TOUCH || event.pointerType === "touch" ) {
  3136. event.touches = [{ clientX: event.clientX, clientY: event.clientY }];
  3137. onTouchEnd( event );
  3138. }
  3139. }
  3140. /**
  3141. * Handles mouse wheel scrolling, throttled to avoid skipping
  3142. * multiple slides.
  3143. */
  3144. function onDocumentMouseScroll( event ) {
  3145. if( Date.now() - lastMouseWheelStep > 600 ) {
  3146. lastMouseWheelStep = Date.now();
  3147. var delta = event.detail || -event.wheelDelta;
  3148. if( delta > 0 ) {
  3149. navigateNext();
  3150. }
  3151. else {
  3152. navigatePrev();
  3153. }
  3154. }
  3155. }
  3156. /**
  3157. * Clicking on the progress bar results in a navigation to the
  3158. * closest approximate horizontal slide using this equation:
  3159. *
  3160. * ( clickX / presentationWidth ) * numberOfSlides
  3161. */
  3162. function onProgressClicked( event ) {
  3163. onUserInput( event );
  3164. event.preventDefault();
  3165. var slidesTotal = toArray( dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) ).length;
  3166. var slideIndex = Math.floor( ( event.clientX / dom.wrapper.offsetWidth ) * slidesTotal );
  3167. if( config.rtl ) {
  3168. slideIndex = slidesTotal - slideIndex;
  3169. }
  3170. slide( slideIndex );
  3171. }
  3172. /**
  3173. * Event handler for navigation control buttons.
  3174. */
  3175. function onNavigateLeftClicked( event ) { event.preventDefault(); onUserInput(); navigateLeft(); }
  3176. function onNavigateRightClicked( event ) { event.preventDefault(); onUserInput(); navigateRight(); }
  3177. function onNavigateUpClicked( event ) { event.preventDefault(); onUserInput(); navigateUp(); }
  3178. function onNavigateDownClicked( event ) { event.preventDefault(); onUserInput(); navigateDown(); }
  3179. function onNavigatePrevClicked( event ) { event.preventDefault(); onUserInput(); navigatePrev(); }
  3180. function onNavigateNextClicked( event ) { event.preventDefault(); onUserInput(); navigateNext(); }
  3181. /**
  3182. * Handler for the window level 'hashchange' event.
  3183. */
  3184. function onWindowHashChange( event ) {
  3185. readURL();
  3186. }
  3187. /**
  3188. * Handler for the window level 'resize' event.
  3189. */
  3190. function onWindowResize( event ) {
  3191. layout();
  3192. }
  3193. /**
  3194. * Handle for the window level 'visibilitychange' event.
  3195. */
  3196. function onPageVisibilityChange( event ) {
  3197. var isHidden = document.webkitHidden ||
  3198. document.msHidden ||
  3199. document.hidden;
  3200. // If, after clicking a link or similar and we're coming back,
  3201. // focus the document.body to ensure we can use keyboard shortcuts
  3202. if( isHidden === false && document.activeElement !== document.body ) {
  3203. // Not all elements support .blur() - SVGs among them.
  3204. if( typeof document.activeElement.blur === 'function' ) {
  3205. document.activeElement.blur();
  3206. }
  3207. document.body.focus();
  3208. }
  3209. }
  3210. /**
  3211. * Invoked when a slide is and we're in the overview.
  3212. */
  3213. function onOverviewSlideClicked( event ) {
  3214. // TODO There's a bug here where the event listeners are not
  3215. // removed after deactivating the overview.
  3216. if( eventsAreBound && isOverview() ) {
  3217. event.preventDefault();
  3218. var element = event.target;
  3219. while( element && !element.nodeName.match( /section/gi ) ) {
  3220. element = element.parentNode;
  3221. }
  3222. if( element && !element.classList.contains( 'disabled' ) ) {
  3223. deactivateOverview();
  3224. if( element.nodeName.match( /section/gi ) ) {
  3225. var h = parseInt( element.getAttribute( 'data-index-h' ), 10 ),
  3226. v = parseInt( element.getAttribute( 'data-index-v' ), 10 );
  3227. slide( h, v );
  3228. }
  3229. }
  3230. }
  3231. }
  3232. /**
  3233. * Handles clicks on links that are set to preview in the
  3234. * iframe overlay.
  3235. */
  3236. function onPreviewLinkClicked( event ) {
  3237. if( event.currentTarget && event.currentTarget.hasAttribute( 'href' ) ) {
  3238. var url = event.currentTarget.getAttribute( 'href' );
  3239. if( url ) {
  3240. showPreview( url );
  3241. event.preventDefault();
  3242. }
  3243. }
  3244. }
  3245. /**
  3246. * Handles click on the auto-sliding controls element.
  3247. */
  3248. function onAutoSlidePlayerClick( event ) {
  3249. // Replay
  3250. if( Reveal.isLastSlide() && config.loop === false ) {
  3251. slide( 0, 0 );
  3252. resumeAutoSlide();
  3253. }
  3254. // Resume
  3255. else if( autoSlidePaused ) {
  3256. resumeAutoSlide();
  3257. }
  3258. // Pause
  3259. else {
  3260. pauseAutoSlide();
  3261. }
  3262. }
  3263. // --------------------------------------------------------------------//
  3264. // ------------------------ PLAYBACK COMPONENT ------------------------//
  3265. // --------------------------------------------------------------------//
  3266. /**
  3267. * Constructor for the playback component, which displays
  3268. * play/pause/progress controls.
  3269. *
  3270. * @param {HTMLElement} container The component will append
  3271. * itself to this
  3272. * @param {Function} progressCheck A method which will be
  3273. * called frequently to get the current progress on a range
  3274. * of 0-1
  3275. */
  3276. function Playback( container, progressCheck ) {
  3277. // Cosmetics
  3278. this.diameter = 50;
  3279. this.thickness = 3;
  3280. // Flags if we are currently playing
  3281. this.playing = false;
  3282. // Current progress on a 0-1 range
  3283. this.progress = 0;
  3284. // Used to loop the animation smoothly
  3285. this.progressOffset = 1;
  3286. this.container = container;
  3287. this.progressCheck = progressCheck;
  3288. this.canvas = document.createElement( 'canvas' );
  3289. this.canvas.className = 'playback';
  3290. this.canvas.width = this.diameter;
  3291. this.canvas.height = this.diameter;
  3292. this.context = this.canvas.getContext( '2d' );
  3293. this.container.appendChild( this.canvas );
  3294. this.render();
  3295. }
  3296. Playback.prototype.setPlaying = function( value ) {
  3297. var wasPlaying = this.playing;
  3298. this.playing = value;
  3299. // Start repainting if we weren't already
  3300. if( !wasPlaying && this.playing ) {
  3301. this.animate();
  3302. }
  3303. else {
  3304. this.render();
  3305. }
  3306. };
  3307. Playback.prototype.animate = function() {
  3308. var progressBefore = this.progress;
  3309. this.progress = this.progressCheck();
  3310. // When we loop, offset the progress so that it eases
  3311. // smoothly rather than immediately resetting
  3312. if( progressBefore > 0.8 && this.progress < 0.2 ) {
  3313. this.progressOffset = this.progress;
  3314. }
  3315. this.render();
  3316. if( this.playing ) {
  3317. features.requestAnimationFrameMethod.call( window, this.animate.bind( this ) );
  3318. }
  3319. };
  3320. /**
  3321. * Renders the current progress and playback state.
  3322. */
  3323. Playback.prototype.render = function() {
  3324. var progress = this.playing ? this.progress : 0,
  3325. radius = ( this.diameter / 2 ) - this.thickness,
  3326. x = this.diameter / 2,
  3327. y = this.diameter / 2,
  3328. iconSize = 14;
  3329. // Ease towards 1
  3330. this.progressOffset += ( 1 - this.progressOffset ) * 0.1;
  3331. var endAngle = ( - Math.PI / 2 ) + ( progress * ( Math.PI * 2 ) );
  3332. var startAngle = ( - Math.PI / 2 ) + ( this.progressOffset * ( Math.PI * 2 ) );
  3333. this.context.save();
  3334. this.context.clearRect( 0, 0, this.diameter, this.diameter );
  3335. // Solid background color
  3336. this.context.beginPath();
  3337. this.context.arc( x, y, radius + 2, 0, Math.PI * 2, false );
  3338. this.context.fillStyle = 'rgba( 0, 0, 0, 0.4 )';
  3339. this.context.fill();
  3340. // Draw progress track
  3341. this.context.beginPath();
  3342. this.context.arc( x, y, radius, 0, Math.PI * 2, false );
  3343. this.context.lineWidth = this.thickness;
  3344. this.context.strokeStyle = '#666';
  3345. this.context.stroke();
  3346. if( this.playing ) {
  3347. // Draw progress on top of track
  3348. this.context.beginPath();
  3349. this.context.arc( x, y, radius, startAngle, endAngle, false );
  3350. this.context.lineWidth = this.thickness;
  3351. this.context.strokeStyle = '#fff';
  3352. this.context.stroke();
  3353. }
  3354. this.context.translate( x - ( iconSize / 2 ), y - ( iconSize / 2 ) );
  3355. // Draw play/pause icons
  3356. if( this.playing ) {
  3357. this.context.fillStyle = '#fff';
  3358. this.context.fillRect( 0, 0, iconSize / 2 - 2, iconSize );
  3359. this.context.fillRect( iconSize / 2 + 2, 0, iconSize / 2 - 2, iconSize );
  3360. }
  3361. else {
  3362. this.context.beginPath();
  3363. this.context.translate( 2, 0 );
  3364. this.context.moveTo( 0, 0 );
  3365. this.context.lineTo( iconSize - 2, iconSize / 2 );
  3366. this.context.lineTo( 0, iconSize );
  3367. this.context.fillStyle = '#fff';
  3368. this.context.fill();
  3369. }
  3370. this.context.restore();
  3371. };
  3372. Playback.prototype.on = function( type, listener ) {
  3373. this.canvas.addEventListener( type, listener, false );
  3374. };
  3375. Playback.prototype.off = function( type, listener ) {
  3376. this.canvas.removeEventListener( type, listener, false );
  3377. };
  3378. Playback.prototype.destroy = function() {
  3379. this.playing = false;
  3380. if( this.canvas.parentNode ) {
  3381. this.container.removeChild( this.canvas );
  3382. }
  3383. };
  3384. // --------------------------------------------------------------------//
  3385. // ------------------------------- API --------------------------------//
  3386. // --------------------------------------------------------------------//
  3387. Reveal = {
  3388. initialize: initialize,
  3389. configure: configure,
  3390. sync: sync,
  3391. // Navigation methods
  3392. slide: slide,
  3393. left: navigateLeft,
  3394. right: navigateRight,
  3395. up: navigateUp,
  3396. down: navigateDown,
  3397. prev: navigatePrev,
  3398. next: navigateNext,
  3399. // Fragment methods
  3400. navigateFragment: navigateFragment,
  3401. prevFragment: previousFragment,
  3402. nextFragment: nextFragment,
  3403. // Deprecated aliases
  3404. navigateTo: slide,
  3405. navigateLeft: navigateLeft,
  3406. navigateRight: navigateRight,
  3407. navigateUp: navigateUp,
  3408. navigateDown: navigateDown,
  3409. navigatePrev: navigatePrev,
  3410. navigateNext: navigateNext,
  3411. // Forces an update in slide layout
  3412. layout: layout,
  3413. // Returns an object with the available routes as booleans (left/right/top/bottom)
  3414. availableRoutes: availableRoutes,
  3415. // Returns an object with the available fragments as booleans (prev/next)
  3416. availableFragments: availableFragments,
  3417. // Toggles the overview mode on/off
  3418. toggleOverview: toggleOverview,
  3419. // Toggles the "black screen" mode on/off
  3420. togglePause: togglePause,
  3421. // Toggles the auto slide mode on/off
  3422. toggleAutoSlide: toggleAutoSlide,
  3423. // State checks
  3424. isOverview: isOverview,
  3425. isPaused: isPaused,
  3426. isAutoSliding: isAutoSliding,
  3427. // Adds or removes all internal event listeners (such as keyboard)
  3428. addEventListeners: addEventListeners,
  3429. removeEventListeners: removeEventListeners,
  3430. // Facility for persisting and restoring the presentation state
  3431. getState: getState,
  3432. setState: setState,
  3433. // Presentation progress on range of 0-1
  3434. getProgress: getProgress,
  3435. // Returns the indices of the current, or specified, slide
  3436. getIndices: getIndices,
  3437. getTotalSlides: getTotalSlides,
  3438. // Returns the slide element at the specified index
  3439. getSlide: getSlide,
  3440. // Returns the slide background element at the specified index
  3441. getSlideBackground: getSlideBackground,
  3442. // Returns the previous slide element, may be null
  3443. getPreviousSlide: function() {
  3444. return previousSlide;
  3445. },
  3446. // Returns the current slide element
  3447. getCurrentSlide: function() {
  3448. return currentSlide;
  3449. },
  3450. // Returns the current scale of the presentation content
  3451. getScale: function() {
  3452. return scale;
  3453. },
  3454. // Returns the current configuration object
  3455. getConfig: function() {
  3456. return config;
  3457. },
  3458. // Helper method, retrieves query string as a key/value hash
  3459. getQueryHash: function() {
  3460. var query = {};
  3461. location.search.replace( /[A-Z0-9]+?=([\w\.%-]*)/gi, function(a) {
  3462. query[ a.split( '=' ).shift() ] = a.split( '=' ).pop();
  3463. } );
  3464. // Basic deserialization
  3465. for( var i in query ) {
  3466. var value = query[ i ];
  3467. query[ i ] = deserialize( unescape( value ) );
  3468. }
  3469. return query;
  3470. },
  3471. // Returns true if we're currently on the first slide
  3472. isFirstSlide: function() {
  3473. return ( indexh === 0 && indexv === 0 );
  3474. },
  3475. // Returns true if we're currently on the last slide
  3476. isLastSlide: function() {
  3477. if( currentSlide ) {
  3478. // Does this slide has next a sibling?
  3479. if( currentSlide.nextElementSibling ) return false;
  3480. // If it's vertical, does its parent have a next sibling?
  3481. if( isVerticalSlide( currentSlide ) && currentSlide.parentNode.nextElementSibling ) return false;
  3482. return true;
  3483. }
  3484. return false;
  3485. },
  3486. // Checks if reveal.js has been loaded and is ready for use
  3487. isReady: function() {
  3488. return loaded;
  3489. },
  3490. // Forward event binding to the reveal DOM element
  3491. addEventListener: function( type, listener, useCapture ) {
  3492. if( 'addEventListener' in window ) {
  3493. ( dom.wrapper || document.querySelector( '.reveal' ) ).addEventListener( type, listener, useCapture );
  3494. }
  3495. },
  3496. removeEventListener: function( type, listener, useCapture ) {
  3497. if( 'addEventListener' in window ) {
  3498. ( dom.wrapper || document.querySelector( '.reveal' ) ).removeEventListener( type, listener, useCapture );
  3499. }
  3500. },
  3501. // Programatically triggers a keyboard event
  3502. triggerKey: function( keyCode ) {
  3503. onDocumentKeyDown( { keyCode: keyCode } );
  3504. }
  3505. };
  3506. return Reveal;
  3507. }));