reveal.js 115 KB

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