reveal.js 110 KB

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