reveal.js 91 KB

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