reveal.js 124 KB

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