reveal.js 107 KB

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