reveal.js 108 KB

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