reveal.js 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533
  1. /*!
  2. * reveal.js
  3. * http://lab.hakim.se/reveal-js
  4. * MIT licensed
  5. *
  6. * Copyright (C) 2013 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-child',
  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. // Push each slide change to the browser history
  30. history: false,
  31. // Enable keyboard shortcuts for navigation
  32. keyboard: true,
  33. // Enable the slide overview mode
  34. overview: true,
  35. // Vertical centring of slides
  36. center: true,
  37. // Enables touch navigation on devices with touch input
  38. touch: true,
  39. // Loop the presentation
  40. loop: false,
  41. // Change the presentation direction to be RTL
  42. rtl: false,
  43. // Turns fragments on and off globally
  44. fragments: true,
  45. // Number of milliseconds between automatically proceeding to the
  46. // next slide, disabled when set to 0, this value can be overwritten
  47. // by using a data-autoslide attribute on your slides
  48. autoSlide: 0,
  49. // Enable slide navigation via mouse wheel
  50. mouseWheel: false,
  51. // Opens links in an iframe preview overlay
  52. previewLinks: false,
  53. // Theme (see /css/theme)
  54. theme: null,
  55. // Transition style
  56. transition: 'default', // default/cube/page/concave/zoom/linear/fade/none
  57. // Transition speed
  58. transitionSpeed: 'default', // default/fast/slow
  59. // Transition style for full page slide backgrounds
  60. backgroundTransition: 'default', // default/linear/none
  61. // Script dependencies to load
  62. dependencies: []
  63. },
  64. // The current auto-slide duration
  65. autoSlide = 0,
  66. // The horizontal and vertical index of the currently active slide
  67. indexh,
  68. indexv,
  69. // The previous and current slide HTML elements
  70. previousSlide,
  71. currentSlide,
  72. // Slides may hold a data-state attribute which we pick up and apply
  73. // as a class to the body. This list contains the combined state of
  74. // all current slides.
  75. state = [],
  76. // The current scale of the presentation (see width/height config)
  77. scale = 1,
  78. // Cached references to DOM elements
  79. dom = {},
  80. // Detect support for CSS 3D transforms
  81. supports3DTransforms = 'WebkitPerspective' in document.body.style ||
  82. 'MozPerspective' in document.body.style ||
  83. 'msPerspective' in document.body.style ||
  84. 'OPerspective' in document.body.style ||
  85. 'perspective' in document.body.style,
  86. // Detect support for CSS 2D transforms
  87. supports2DTransforms = 'WebkitTransform' in document.body.style ||
  88. 'MozTransform' in document.body.style ||
  89. 'msTransform' in document.body.style ||
  90. 'OTransform' in document.body.style ||
  91. 'transform' in document.body.style,
  92. // Throttles mouse wheel navigation
  93. lastMouseWheelStep = 0,
  94. // An interval used to automatically move on to the next slide
  95. autoSlideTimeout = 0,
  96. // Delays updates to the URL due to a Chrome thumbnailer bug
  97. writeURLTimeout = 0,
  98. // A delay used to activate the overview mode
  99. activateOverviewTimeout = 0,
  100. // A delay used to deactivate the overview mode
  101. deactivateOverviewTimeout = 0,
  102. // Flags if the interaction event listeners are bound
  103. eventsAreBound = false,
  104. // Holds information about the currently ongoing touch input
  105. touch = {
  106. startX: 0,
  107. startY: 0,
  108. startSpan: 0,
  109. startCount: 0,
  110. handled: false,
  111. threshold: 80
  112. };
  113. /**
  114. * Starts up the presentation if the client is capable.
  115. */
  116. function initialize( options ) {
  117. if( !supports2DTransforms && !supports3DTransforms ) {
  118. document.body.setAttribute( 'class', 'no-transforms' );
  119. // If the browser doesn't support core features we won't be
  120. // using JavaScript to control the presentation
  121. return;
  122. }
  123. // Force a layout when the whole page, incl fonts, has loaded
  124. window.addEventListener( 'load', layout, false );
  125. // Copy options over to our config object
  126. extend( config, options );
  127. // Hide the address bar in mobile browsers
  128. hideAddressBar();
  129. // Loads the dependencies and continues to #start() once done
  130. load();
  131. }
  132. /**
  133. * Finds and stores references to DOM elements which are
  134. * required by the presentation. If a required element is
  135. * not found, it is created.
  136. */
  137. function setupDOM() {
  138. // Cache references to key DOM elements
  139. dom.theme = document.querySelector( '#theme' );
  140. dom.wrapper = document.querySelector( '.reveal' );
  141. dom.slides = document.querySelector( '.reveal .slides' );
  142. // Background element
  143. if( !document.querySelector( '.reveal .backgrounds' ) ) {
  144. dom.background = document.createElement( 'div' );
  145. dom.background.classList.add( 'backgrounds' );
  146. dom.wrapper.appendChild( dom.background );
  147. }
  148. // Progress bar
  149. if( !dom.wrapper.querySelector( '.progress' ) ) {
  150. var progressElement = document.createElement( 'div' );
  151. progressElement.classList.add( 'progress' );
  152. progressElement.innerHTML = '<span></span>';
  153. dom.wrapper.appendChild( progressElement );
  154. }
  155. // Arrow controls
  156. if( !dom.wrapper.querySelector( '.controls' ) ) {
  157. var controlsElement = document.createElement( 'aside' );
  158. controlsElement.classList.add( 'controls' );
  159. controlsElement.innerHTML = '<div class="navigate-left"></div>' +
  160. '<div class="navigate-right"></div>' +
  161. '<div class="navigate-up"></div>' +
  162. '<div class="navigate-down"></div>';
  163. dom.wrapper.appendChild( controlsElement );
  164. }
  165. // State background element [DEPRECATED]
  166. if( !dom.wrapper.querySelector( '.state-background' ) ) {
  167. var stateBackgroundElement = document.createElement( 'div' );
  168. stateBackgroundElement.classList.add( 'state-background' );
  169. dom.wrapper.appendChild( stateBackgroundElement );
  170. }
  171. // Overlay graphic which is displayed during the paused mode
  172. if( !dom.wrapper.querySelector( '.pause-overlay' ) ) {
  173. var pausedElement = document.createElement( 'div' );
  174. pausedElement.classList.add( 'pause-overlay' );
  175. dom.wrapper.appendChild( pausedElement );
  176. }
  177. // Cache references to elements
  178. dom.progress = document.querySelector( '.reveal .progress' );
  179. dom.progressbar = document.querySelector( '.reveal .progress span' );
  180. if ( config.controls ) {
  181. dom.controls = document.querySelector( '.reveal .controls' );
  182. // There can be multiple instances of controls throughout the page
  183. dom.controlsLeft = toArray( document.querySelectorAll( '.navigate-left' ) );
  184. dom.controlsRight = toArray( document.querySelectorAll( '.navigate-right' ) );
  185. dom.controlsUp = toArray( document.querySelectorAll( '.navigate-up' ) );
  186. dom.controlsDown = toArray( document.querySelectorAll( '.navigate-down' ) );
  187. dom.controlsPrev = toArray( document.querySelectorAll( '.navigate-prev' ) );
  188. dom.controlsNext = toArray( document.querySelectorAll( '.navigate-next' ) );
  189. }
  190. }
  191. /**
  192. * Creates the slide background elements and appends them
  193. * to the background container. One element is created per
  194. * slide no matter if the given slide has visible background.
  195. */
  196. function createBackgrounds() {
  197. if( isPrintingPDF() ) {
  198. document.body.classList.add( 'print-pdf' );
  199. }
  200. // Clear prior backgrounds
  201. dom.background.innerHTML = '';
  202. dom.background.classList.add( 'no-transition' );
  203. // Helper method for creating a background element for the
  204. // given slide
  205. function _createBackground( slide, container ) {
  206. var data = {
  207. background: slide.getAttribute( 'data-background' ),
  208. backgroundSize: slide.getAttribute( 'data-background-size' ),
  209. backgroundImage: slide.getAttribute( 'data-background-image' ),
  210. backgroundColor: slide.getAttribute( 'data-background-color' ),
  211. backgroundRepeat: slide.getAttribute( 'data-background-repeat' ),
  212. backgroundPosition: slide.getAttribute( 'data-background-position' ),
  213. backgroundTransition: slide.getAttribute( 'data-background-transition' )
  214. };
  215. var element = document.createElement( 'div' );
  216. element.className = 'slide-background';
  217. if( data.background ) {
  218. // Auto-wrap image urls in url(...)
  219. if( /^(http|file|\/\/)/gi.test( data.background ) || /\.(png|jpg|jpeg|gif|bmp)$/gi.test( data.background ) ) {
  220. element.style.backgroundImage = 'url('+ data.background +')';
  221. }
  222. else {
  223. element.style.background = data.background;
  224. }
  225. }
  226. // Additional and optional background properties
  227. if( data.backgroundSize ) element.style.backgroundSize = data.backgroundSize;
  228. if( data.backgroundImage ) element.style.backgroundImage = 'url("' + data.backgroundImage + '")';
  229. if( data.backgroundColor ) element.style.backgroundColor = data.backgroundColor;
  230. if( data.backgroundRepeat ) element.style.backgroundRepeat = data.backgroundRepeat;
  231. if( data.backgroundPosition ) element.style.backgroundPosition = data.backgroundPosition;
  232. if( data.backgroundTransition ) element.setAttribute( 'data-background-transition', data.backgroundTransition );
  233. container.appendChild( element );
  234. return element;
  235. }
  236. // Iterate over all horizontal slides
  237. toArray( document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) ).forEach( function( slideh ) {
  238. var backgroundStack;
  239. if( isPrintingPDF() ) {
  240. backgroundStack = _createBackground( slideh, slideh );
  241. }
  242. else {
  243. backgroundStack = _createBackground( slideh, dom.background );
  244. }
  245. // Iterate over all vertical slides
  246. toArray( slideh.querySelectorAll( 'section' ) ).forEach( function( slidev ) {
  247. if( isPrintingPDF() ) {
  248. _createBackground( slidev, slidev );
  249. }
  250. else {
  251. _createBackground( slidev, backgroundStack );
  252. }
  253. } );
  254. } );
  255. }
  256. /**
  257. * Hides the address bar if we're on a mobile device.
  258. */
  259. function hideAddressBar() {
  260. if( /iphone|ipod|android/gi.test( navigator.userAgent ) && !/crios/gi.test( navigator.userAgent ) ) {
  261. // Events that should trigger the address bar to hide
  262. window.addEventListener( 'load', removeAddressBar, false );
  263. window.addEventListener( 'orientationchange', removeAddressBar, false );
  264. }
  265. }
  266. /**
  267. * Loads the dependencies of reveal.js. Dependencies are
  268. * defined via the configuration option 'dependencies'
  269. * and will be loaded prior to starting/binding reveal.js.
  270. * Some dependencies may have an 'async' flag, if so they
  271. * will load after reveal.js has been started up.
  272. */
  273. function load() {
  274. var scripts = [],
  275. scriptsAsync = [];
  276. for( var i = 0, len = config.dependencies.length; i < len; i++ ) {
  277. var s = config.dependencies[i];
  278. // Load if there's no condition or the condition is truthy
  279. if( !s.condition || s.condition() ) {
  280. if( s.async ) {
  281. scriptsAsync.push( s.src );
  282. }
  283. else {
  284. scripts.push( s.src );
  285. }
  286. // Extension may contain callback functions
  287. if( typeof s.callback === 'function' ) {
  288. head.ready( s.src.match( /([\w\d_\-]*)\.?js$|[^\\\/]*$/i )[0], s.callback );
  289. }
  290. }
  291. }
  292. // Called once synchronous scripts finish loading
  293. function proceed() {
  294. if( scriptsAsync.length ) {
  295. // Load asynchronous scripts
  296. head.js.apply( null, scriptsAsync );
  297. }
  298. start();
  299. }
  300. if( scripts.length ) {
  301. head.ready( proceed );
  302. // Load synchronous scripts
  303. head.js.apply( null, scripts );
  304. }
  305. else {
  306. proceed();
  307. }
  308. }
  309. /**
  310. * Starts up reveal.js by binding input events and navigating
  311. * to the current URL deeplink if there is one.
  312. */
  313. function start() {
  314. // Make sure we've got all the DOM elements we need
  315. setupDOM();
  316. // Updates the presentation to match the current configuration values
  317. configure();
  318. // Read the initial hash
  319. readURL();
  320. // Notify listeners that the presentation is ready but use a 1ms
  321. // timeout to ensure it's not fired synchronously after #initialize()
  322. setTimeout( function() {
  323. dispatchEvent( 'ready', {
  324. 'indexh': indexh,
  325. 'indexv': indexv,
  326. 'currentSlide': currentSlide
  327. } );
  328. }, 1 );
  329. }
  330. /**
  331. * Applies the configuration settings from the config
  332. * object. May be called multiple times.
  333. */
  334. function configure( options ) {
  335. dom.wrapper.classList.remove( config.transition );
  336. // New config options may be passed when this method
  337. // is invoked through the API after initialization
  338. if( typeof options === 'object' ) extend( config, options );
  339. // Force linear transition based on browser capabilities
  340. if( supports3DTransforms === false ) config.transition = 'linear';
  341. dom.wrapper.classList.add( config.transition );
  342. dom.wrapper.setAttribute( 'data-transition-speed', config.transitionSpeed );
  343. dom.wrapper.setAttribute( 'data-background-transition', config.backgroundTransition );
  344. if( dom.controls ) {
  345. dom.controls.style.display = ( config.controls && dom.controls ) ? 'block' : 'none';
  346. }
  347. if( dom.progress ) {
  348. dom.progress.style.display = ( config.progress && dom.progress ) ? 'block' : 'none';
  349. }
  350. if( config.rtl ) {
  351. dom.wrapper.classList.add( 'rtl' );
  352. }
  353. else {
  354. dom.wrapper.classList.remove( 'rtl' );
  355. }
  356. if( config.center ) {
  357. dom.wrapper.classList.add( 'center' );
  358. }
  359. else {
  360. dom.wrapper.classList.remove( 'center' );
  361. }
  362. if( config.mouseWheel ) {
  363. document.addEventListener( 'DOMMouseScroll', onDocumentMouseScroll, false ); // FF
  364. document.addEventListener( 'mousewheel', onDocumentMouseScroll, false );
  365. }
  366. else {
  367. document.removeEventListener( 'DOMMouseScroll', onDocumentMouseScroll, false ); // FF
  368. document.removeEventListener( 'mousewheel', onDocumentMouseScroll, false );
  369. }
  370. // Iframe link previews
  371. if( config.previewLinks ) {
  372. enablePreviewLinks();
  373. }
  374. else {
  375. disablePreviewLinks();
  376. enablePreviewLinks( '[data-preview-link]' );
  377. }
  378. // Load the theme in the config, if it's not already loaded
  379. if( config.theme && dom.theme ) {
  380. var themeURL = dom.theme.getAttribute( 'href' );
  381. var themeFinder = /[^\/]*?(?=\.css)/;
  382. var themeName = themeURL.match(themeFinder)[0];
  383. if( config.theme !== themeName ) {
  384. themeURL = themeURL.replace(themeFinder, config.theme);
  385. dom.theme.setAttribute( 'href', themeURL );
  386. }
  387. }
  388. sync();
  389. }
  390. /**
  391. * Binds all event listeners.
  392. */
  393. function addEventListeners() {
  394. eventsAreBound = true;
  395. window.addEventListener( 'hashchange', onWindowHashChange, false );
  396. window.addEventListener( 'resize', onWindowResize, false );
  397. if( config.touch ) {
  398. dom.wrapper.addEventListener( 'touchstart', onTouchStart, false );
  399. dom.wrapper.addEventListener( 'touchmove', onTouchMove, false );
  400. dom.wrapper.addEventListener( 'touchend', onTouchEnd, false );
  401. // Support pointer-style touch interaction as well
  402. if( window.navigator.msPointerEnabled ) {
  403. dom.wrapper.addEventListener( 'MSPointerDown', onPointerDown, false );
  404. dom.wrapper.addEventListener( 'MSPointerMove', onPointerMove, false );
  405. dom.wrapper.addEventListener( 'MSPointerUp', onPointerUp, false );
  406. }
  407. }
  408. if( config.keyboard ) {
  409. document.addEventListener( 'keydown', onDocumentKeyDown, false );
  410. }
  411. if ( config.progress && dom.progress ) {
  412. dom.progress.addEventListener( 'click', onProgressClicked, false );
  413. }
  414. if ( config.controls && dom.controls ) {
  415. [ 'touchstart', 'click' ].forEach( function( eventName ) {
  416. dom.controlsLeft.forEach( function( el ) { el.addEventListener( eventName, onNavigateLeftClicked, false ); } );
  417. dom.controlsRight.forEach( function( el ) { el.addEventListener( eventName, onNavigateRightClicked, false ); } );
  418. dom.controlsUp.forEach( function( el ) { el.addEventListener( eventName, onNavigateUpClicked, false ); } );
  419. dom.controlsDown.forEach( function( el ) { el.addEventListener( eventName, onNavigateDownClicked, false ); } );
  420. dom.controlsPrev.forEach( function( el ) { el.addEventListener( eventName, onNavigatePrevClicked, false ); } );
  421. dom.controlsNext.forEach( function( el ) { el.addEventListener( eventName, onNavigateNextClicked, false ); } );
  422. } );
  423. }
  424. }
  425. /**
  426. * Unbinds all event listeners.
  427. */
  428. function removeEventListeners() {
  429. eventsAreBound = false;
  430. document.removeEventListener( 'keydown', onDocumentKeyDown, false );
  431. window.removeEventListener( 'hashchange', onWindowHashChange, false );
  432. window.removeEventListener( 'resize', onWindowResize, false );
  433. dom.wrapper.removeEventListener( 'touchstart', onTouchStart, false );
  434. dom.wrapper.removeEventListener( 'touchmove', onTouchMove, false );
  435. dom.wrapper.removeEventListener( 'touchend', onTouchEnd, false );
  436. if( window.navigator.msPointerEnabled ) {
  437. dom.wrapper.removeEventListener( 'MSPointerDown', onPointerDown, false );
  438. dom.wrapper.removeEventListener( 'MSPointerMove', onPointerMove, false );
  439. dom.wrapper.removeEventListener( 'MSPointerUp', onPointerUp, false );
  440. }
  441. if ( config.progress && dom.progress ) {
  442. dom.progress.removeEventListener( 'click', onProgressClicked, false );
  443. }
  444. if ( config.controls && dom.controls ) {
  445. [ 'touchstart', 'click' ].forEach( function( eventName ) {
  446. dom.controlsLeft.forEach( function( el ) { el.removeEventListener( eventName, onNavigateLeftClicked, false ); } );
  447. dom.controlsRight.forEach( function( el ) { el.removeEventListener( eventName, onNavigateRightClicked, false ); } );
  448. dom.controlsUp.forEach( function( el ) { el.removeEventListener( eventName, onNavigateUpClicked, false ); } );
  449. dom.controlsDown.forEach( function( el ) { el.removeEventListener( eventName, onNavigateDownClicked, false ); } );
  450. dom.controlsPrev.forEach( function( el ) { el.removeEventListener( eventName, onNavigatePrevClicked, false ); } );
  451. dom.controlsNext.forEach( function( el ) { el.removeEventListener( eventName, onNavigateNextClicked, false ); } );
  452. } );
  453. }
  454. }
  455. /**
  456. * Extend object a with the properties of object b.
  457. * If there's a conflict, object b takes precedence.
  458. */
  459. function extend( a, b ) {
  460. for( var i in b ) {
  461. a[ i ] = b[ i ];
  462. }
  463. }
  464. /**
  465. * Converts the target object to an array.
  466. */
  467. function toArray( o ) {
  468. return Array.prototype.slice.call( o );
  469. }
  470. /**
  471. * Measures the distance in pixels between point a
  472. * and point b.
  473. *
  474. * @param {Object} a point with x/y properties
  475. * @param {Object} b point with x/y properties
  476. */
  477. function distanceBetween( a, b ) {
  478. var dx = a.x - b.x,
  479. dy = a.y - b.y;
  480. return Math.sqrt( dx*dx + dy*dy );
  481. }
  482. /**
  483. * Retrieves the height of the given element by looking
  484. * at the position and height of its immediate children.
  485. */
  486. function getAbsoluteHeight( element ) {
  487. var height = 0;
  488. if( element ) {
  489. var absoluteChildren = 0;
  490. toArray( element.childNodes ).forEach( function( child ) {
  491. if( typeof child.offsetTop === 'number' && child.style ) {
  492. // Count # of abs children
  493. if( child.style.position === 'absolute' ) {
  494. absoluteChildren += 1;
  495. }
  496. height = Math.max( height, child.offsetTop + child.offsetHeight );
  497. }
  498. } );
  499. // If there are no absolute children, use offsetHeight
  500. if( absoluteChildren === 0 ) {
  501. height = element.offsetHeight;
  502. }
  503. }
  504. return height;
  505. }
  506. /**
  507. * Checks if this instance is being used to print a PDF.
  508. */
  509. function isPrintingPDF() {
  510. return ( /print-pdf/gi ).test( window.location.search );
  511. }
  512. /**
  513. * Causes the address bar to hide on mobile devices,
  514. * more vertical space ftw.
  515. */
  516. function removeAddressBar() {
  517. if( window.orientation === 0 ) {
  518. document.documentElement.style.overflow = 'scroll';
  519. document.body.style.height = '120%';
  520. }
  521. else {
  522. document.documentElement.style.overflow = '';
  523. document.body.style.height = '100%';
  524. }
  525. setTimeout( function() {
  526. window.scrollTo( 0, 1 );
  527. }, 10 );
  528. }
  529. /**
  530. * Dispatches an event of the specified type from the
  531. * reveal DOM element.
  532. */
  533. function dispatchEvent( type, properties ) {
  534. var event = document.createEvent( "HTMLEvents", 1, 2 );
  535. event.initEvent( type, true, true );
  536. extend( event, properties );
  537. dom.wrapper.dispatchEvent( event );
  538. }
  539. /**
  540. * Bind preview frame links.
  541. */
  542. function enablePreviewLinks( selector ) {
  543. var anchors = toArray( document.querySelectorAll( selector ? selector : 'a' ) );
  544. anchors.forEach( function( element ) {
  545. if( /^(http|www)/gi.test( element.getAttribute( 'href' ) ) ) {
  546. element.addEventListener( 'click', onPreviewLinkClicked, false );
  547. }
  548. } );
  549. }
  550. /**
  551. * Unbind preview frame links.
  552. */
  553. function disablePreviewLinks() {
  554. var anchors = toArray( document.querySelectorAll( 'a' ) );
  555. anchors.forEach( function( element ) {
  556. if( /^(http|www)/gi.test( element.getAttribute( 'href' ) ) ) {
  557. element.removeEventListener( 'click', onPreviewLinkClicked, false );
  558. }
  559. } );
  560. }
  561. /**
  562. * Opens a preview window for the target URL.
  563. */
  564. function openPreview( url ) {
  565. closePreview();
  566. dom.preview = document.createElement( 'div' );
  567. dom.preview.classList.add( 'preview-link-overlay' );
  568. dom.wrapper.appendChild( dom.preview );
  569. dom.preview.innerHTML = [
  570. '<header>',
  571. '<a class="close" href="#"><span class="icon"></span></a>',
  572. '<a class="external" href="'+ url +'" target="_blank"><span class="icon"></span></a>',
  573. '</header>',
  574. '<div class="spinner"></div>',
  575. '<div class="viewport">',
  576. '<iframe src="'+ url +'"></iframe>',
  577. '</div>'
  578. ].join('');
  579. dom.preview.querySelector( 'iframe' ).addEventListener( 'load', function( event ) {
  580. dom.preview.classList.add( 'loaded' );
  581. }, false );
  582. dom.preview.querySelector( '.close' ).addEventListener( 'click', function( event ) {
  583. closePreview();
  584. event.preventDefault();
  585. }, false );
  586. dom.preview.querySelector( '.external' ).addEventListener( 'click', function( event ) {
  587. closePreview();
  588. }, false );
  589. setTimeout( function() {
  590. dom.preview.classList.add( 'visible' );
  591. }, 1 );
  592. }
  593. /**
  594. * Closes the iframe preview window.
  595. */
  596. function closePreview() {
  597. if( dom.preview ) {
  598. dom.preview.setAttribute( 'src', '' );
  599. dom.preview.parentNode.removeChild( dom.preview );
  600. dom.preview = null;
  601. }
  602. }
  603. /**
  604. * Return a sorted fragments list, ordered by an increasing
  605. * "data-fragment-index" attribute.
  606. *
  607. * Fragments will be revealed in the order that they are returned by
  608. * this function, so you can use the index attributes to control the
  609. * order of fragment appearance.
  610. *
  611. * To maintain a sensible default fragment order, fragments are presumed
  612. * to be passed in document order. This function adds a "fragment-index"
  613. * attribute to each node if such an attribute is not already present,
  614. * and sets that attribute to an integer value which is the position of
  615. * the fragment within the fragments list.
  616. */
  617. function sortFragments( fragments ) {
  618. var a = toArray( fragments );
  619. a.forEach( function( el, idx ) {
  620. if( !el.hasAttribute( 'data-fragment-index' ) ) {
  621. el.setAttribute( 'data-fragment-index', idx );
  622. }
  623. } );
  624. a.sort( function( l, r ) {
  625. return l.getAttribute( 'data-fragment-index' ) - r.getAttribute( 'data-fragment-index');
  626. } );
  627. return a;
  628. }
  629. /**
  630. * Applies JavaScript-controlled layout rules to the
  631. * presentation.
  632. */
  633. function layout() {
  634. if( dom.wrapper && !isPrintingPDF() ) {
  635. // Available space to scale within
  636. var availableWidth = dom.wrapper.offsetWidth,
  637. availableHeight = dom.wrapper.offsetHeight;
  638. // Reduce available space by margin
  639. availableWidth -= ( availableHeight * config.margin );
  640. availableHeight -= ( availableHeight * config.margin );
  641. // Dimensions of the content
  642. var slideWidth = config.width,
  643. slideHeight = config.height;
  644. // Slide width may be a percentage of available width
  645. if( typeof slideWidth === 'string' && /%$/.test( slideWidth ) ) {
  646. slideWidth = parseInt( slideWidth, 10 ) / 100 * availableWidth;
  647. }
  648. // Slide height may be a percentage of available height
  649. if( typeof slideHeight === 'string' && /%$/.test( slideHeight ) ) {
  650. slideHeight = parseInt( slideHeight, 10 ) / 100 * availableHeight;
  651. }
  652. dom.slides.style.width = slideWidth + 'px';
  653. dom.slides.style.height = slideHeight + 'px';
  654. // Determine scale of content to fit within available space
  655. scale = Math.min( availableWidth / slideWidth, availableHeight / slideHeight );
  656. // Respect max/min scale settings
  657. scale = Math.max( scale, config.minScale );
  658. scale = Math.min( scale, config.maxScale );
  659. // Prefer applying scale via zoom since Chrome blurs scaled content
  660. // with nested transforms
  661. if( typeof dom.slides.style.zoom !== 'undefined' && !navigator.userAgent.match( /(iphone|ipod|ipad|android)/gi ) ) {
  662. dom.slides.style.zoom = scale;
  663. }
  664. // Apply scale transform as a fallback
  665. else {
  666. var transform = 'translate(-50%, -50%) scale('+ scale +') translate(50%, 50%)';
  667. dom.slides.style.WebkitTransform = transform;
  668. dom.slides.style.MozTransform = transform;
  669. dom.slides.style.msTransform = transform;
  670. dom.slides.style.OTransform = transform;
  671. dom.slides.style.transform = transform;
  672. }
  673. // Select all slides, vertical and horizontal
  674. var slides = toArray( document.querySelectorAll( SLIDES_SELECTOR ) );
  675. for( var i = 0, len = slides.length; i < len; i++ ) {
  676. var slide = slides[ i ];
  677. // Don't bother updating invisible slides
  678. if( slide.style.display === 'none' ) {
  679. continue;
  680. }
  681. if( config.center ) {
  682. // Vertical stacks are not centred since their section
  683. // children will be
  684. if( slide.classList.contains( 'stack' ) ) {
  685. slide.style.top = 0;
  686. }
  687. else {
  688. slide.style.top = Math.max( - ( getAbsoluteHeight( slide ) / 2 ) - 20, -slideHeight / 2 ) + 'px';
  689. }
  690. }
  691. else {
  692. slide.style.top = '';
  693. }
  694. }
  695. updateProgress();
  696. }
  697. }
  698. /**
  699. * Stores the vertical index of a stack so that the same
  700. * vertical slide can be selected when navigating to and
  701. * from the stack.
  702. *
  703. * @param {HTMLElement} stack The vertical stack element
  704. * @param {int} v Index to memorize
  705. */
  706. function setPreviousVerticalIndex( stack, v ) {
  707. if( typeof stack === 'object' && typeof stack.setAttribute === 'function' ) {
  708. stack.setAttribute( 'data-previous-indexv', v || 0 );
  709. }
  710. }
  711. /**
  712. * Retrieves the vertical index which was stored using
  713. * #setPreviousVerticalIndex() or 0 if no previous index
  714. * exists.
  715. *
  716. * @param {HTMLElement} stack The vertical stack element
  717. */
  718. function getPreviousVerticalIndex( stack ) {
  719. if( typeof stack === 'object' && typeof stack.setAttribute === 'function' && stack.classList.contains( 'stack' ) ) {
  720. // Prefer manually defined start-indexv
  721. var attributeName = stack.hasAttribute( 'data-start-indexv' ) ? 'data-start-indexv' : 'data-previous-indexv';
  722. return parseInt( stack.getAttribute( attributeName ) || 0, 10 );
  723. }
  724. return 0;
  725. }
  726. /**
  727. * Displays the overview of slides (quick nav) by
  728. * scaling down and arranging all slide elements.
  729. *
  730. * Experimental feature, might be dropped if perf
  731. * can't be improved.
  732. */
  733. function activateOverview() {
  734. // Only proceed if enabled in config
  735. if( config.overview ) {
  736. // Don't auto-slide while in overview mode
  737. cancelAutoSlide();
  738. var wasActive = dom.wrapper.classList.contains( 'overview' );
  739. dom.wrapper.classList.add( 'overview' );
  740. dom.wrapper.classList.remove( 'exit-overview' );
  741. clearTimeout( activateOverviewTimeout );
  742. clearTimeout( deactivateOverviewTimeout );
  743. // Not the pretties solution, but need to let the overview
  744. // class apply first so that slides are measured accurately
  745. // before we can position them
  746. activateOverviewTimeout = setTimeout( function(){
  747. var horizontalSlides = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR );
  748. for( var i = 0, len1 = horizontalSlides.length; i < len1; i++ ) {
  749. var hslide = horizontalSlides[i],
  750. hoffset = config.rtl ? -105 : 105,
  751. htransform = 'translateZ(-2500px) translate(' + ( ( i - indexh ) * hoffset ) + '%, 0%)';
  752. hslide.setAttribute( 'data-index-h', i );
  753. hslide.style.display = 'block';
  754. hslide.style.WebkitTransform = htransform;
  755. hslide.style.MozTransform = htransform;
  756. hslide.style.msTransform = htransform;
  757. hslide.style.OTransform = htransform;
  758. hslide.style.transform = htransform;
  759. if( hslide.classList.contains( 'stack' ) ) {
  760. var verticalSlides = hslide.querySelectorAll( 'section' );
  761. for( var j = 0, len2 = verticalSlides.length; j < len2; j++ ) {
  762. var verticalIndex = i === indexh ? indexv : getPreviousVerticalIndex( hslide );
  763. var vslide = verticalSlides[j],
  764. vtransform = 'translate(0%, ' + ( ( j - verticalIndex ) * 105 ) + '%)';
  765. vslide.setAttribute( 'data-index-h', i );
  766. vslide.setAttribute( 'data-index-v', j );
  767. vslide.style.display = 'block';
  768. vslide.style.WebkitTransform = vtransform;
  769. vslide.style.MozTransform = vtransform;
  770. vslide.style.msTransform = vtransform;
  771. vslide.style.OTransform = vtransform;
  772. vslide.style.transform = vtransform;
  773. // Navigate to this slide on click
  774. vslide.addEventListener( 'click', onOverviewSlideClicked, true );
  775. }
  776. }
  777. else {
  778. // Navigate to this slide on click
  779. hslide.addEventListener( 'click', onOverviewSlideClicked, true );
  780. }
  781. }
  782. layout();
  783. if( !wasActive ) {
  784. // Notify observers of the overview showing
  785. dispatchEvent( 'overviewshown', {
  786. 'indexh': indexh,
  787. 'indexv': indexv,
  788. 'currentSlide': currentSlide
  789. } );
  790. }
  791. }, 10 );
  792. }
  793. }
  794. /**
  795. * Exits the slide overview and enters the currently
  796. * active slide.
  797. */
  798. function deactivateOverview() {
  799. // Only proceed if enabled in config
  800. if( config.overview ) {
  801. clearTimeout( activateOverviewTimeout );
  802. clearTimeout( deactivateOverviewTimeout );
  803. dom.wrapper.classList.remove( 'overview' );
  804. // Temporarily add a class so that transitions can do different things
  805. // depending on whether they are exiting/entering overview, or just
  806. // moving from slide to slide
  807. dom.wrapper.classList.add( 'exit-overview' );
  808. deactivateOverviewTimeout = setTimeout( function () {
  809. dom.wrapper.classList.remove( 'exit-overview' );
  810. }, 10);
  811. // Select all slides
  812. var slides = toArray( document.querySelectorAll( SLIDES_SELECTOR ) );
  813. for( var i = 0, len = slides.length; i < len; i++ ) {
  814. var element = slides[i];
  815. element.style.display = '';
  816. // Resets all transforms to use the external styles
  817. element.style.WebkitTransform = '';
  818. element.style.MozTransform = '';
  819. element.style.msTransform = '';
  820. element.style.OTransform = '';
  821. element.style.transform = '';
  822. element.removeEventListener( 'click', onOverviewSlideClicked, true );
  823. }
  824. slide( indexh, indexv );
  825. cueAutoSlide();
  826. // Notify observers of the overview hiding
  827. dispatchEvent( 'overviewhidden', {
  828. 'indexh': indexh,
  829. 'indexv': indexv,
  830. 'currentSlide': currentSlide
  831. } );
  832. }
  833. }
  834. /**
  835. * Toggles the slide overview mode on and off.
  836. *
  837. * @param {Boolean} override Optional flag which overrides the
  838. * toggle logic and forcibly sets the desired state. True means
  839. * overview is open, false means it's closed.
  840. */
  841. function toggleOverview( override ) {
  842. if( typeof override === 'boolean' ) {
  843. override ? activateOverview() : deactivateOverview();
  844. }
  845. else {
  846. isOverview() ? deactivateOverview() : activateOverview();
  847. }
  848. }
  849. /**
  850. * Checks if the overview is currently active.
  851. *
  852. * @return {Boolean} true if the overview is active,
  853. * false otherwise
  854. */
  855. function isOverview() {
  856. return dom.wrapper.classList.contains( 'overview' );
  857. }
  858. /**
  859. * Checks if the current or specified slide is vertical
  860. * (nested within another slide).
  861. *
  862. * @param {HTMLElement} slide [optional] The slide to check
  863. * orientation of
  864. */
  865. function isVerticalSlide( slide ) {
  866. // Prefer slide argument, otherwise use current slide
  867. slide = slide ? slide : currentSlide;
  868. return slide && !!slide.parentNode.nodeName.match( /section/i );
  869. }
  870. /**
  871. * Handling the fullscreen functionality via the fullscreen API
  872. *
  873. * @see http://fullscreen.spec.whatwg.org/
  874. * @see https://developer.mozilla.org/en-US/docs/DOM/Using_fullscreen_mode
  875. */
  876. function enterFullscreen() {
  877. var element = document.body;
  878. // Check which implementation is available
  879. var requestMethod = element.requestFullScreen ||
  880. element.webkitRequestFullscreen ||
  881. element.webkitRequestFullScreen ||
  882. element.mozRequestFullScreen ||
  883. element.msRequestFullScreen;
  884. if( requestMethod ) {
  885. requestMethod.apply( element );
  886. }
  887. }
  888. /**
  889. * Enters the paused mode which fades everything on screen to
  890. * black.
  891. */
  892. function pause() {
  893. var wasPaused = dom.wrapper.classList.contains( 'paused' );
  894. cancelAutoSlide();
  895. dom.wrapper.classList.add( 'paused' );
  896. if( wasPaused === false ) {
  897. dispatchEvent( 'paused' );
  898. }
  899. }
  900. /**
  901. * Exits from the paused mode.
  902. */
  903. function resume() {
  904. var wasPaused = dom.wrapper.classList.contains( 'paused' );
  905. dom.wrapper.classList.remove( 'paused' );
  906. cueAutoSlide();
  907. if( wasPaused ) {
  908. dispatchEvent( 'resumed' );
  909. }
  910. }
  911. /**
  912. * Toggles the paused mode on and off.
  913. */
  914. function togglePause() {
  915. if( isPaused() ) {
  916. resume();
  917. }
  918. else {
  919. pause();
  920. }
  921. }
  922. /**
  923. * Checks if we are currently in the paused mode.
  924. */
  925. function isPaused() {
  926. return dom.wrapper.classList.contains( 'paused' );
  927. }
  928. /**
  929. * Steps from the current point in the presentation to the
  930. * slide which matches the specified horizontal and vertical
  931. * indices.
  932. *
  933. * @param {int} h Horizontal index of the target slide
  934. * @param {int} v Vertical index of the target slide
  935. * @param {int} f Optional index of a fragment within the
  936. * target slide to activate
  937. * @param {int} o Optional origin for use in multimaster environments
  938. */
  939. function slide( h, v, f, o ) {
  940. // Remember where we were at before
  941. previousSlide = currentSlide;
  942. // Query all horizontal slides in the deck
  943. var horizontalSlides = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR );
  944. // If no vertical index is specified and the upcoming slide is a
  945. // stack, resume at its previous vertical index
  946. if( v === undefined ) {
  947. v = getPreviousVerticalIndex( horizontalSlides[ h ] );
  948. }
  949. // If we were on a vertical stack, remember what vertical index
  950. // it was on so we can resume at the same position when returning
  951. if( previousSlide && previousSlide.parentNode && previousSlide.parentNode.classList.contains( 'stack' ) ) {
  952. setPreviousVerticalIndex( previousSlide.parentNode, indexv );
  953. }
  954. // Remember the state before this slide
  955. var stateBefore = state.concat();
  956. // Reset the state array
  957. state.length = 0;
  958. var indexhBefore = indexh,
  959. indexvBefore = indexv;
  960. // Activate and transition to the new slide
  961. indexh = updateSlides( HORIZONTAL_SLIDES_SELECTOR, h === undefined ? indexh : h );
  962. indexv = updateSlides( VERTICAL_SLIDES_SELECTOR, v === undefined ? indexv : v );
  963. layout();
  964. // Apply the new state
  965. stateLoop: for( var i = 0, len = state.length; i < len; i++ ) {
  966. // Check if this state existed on the previous slide. If it
  967. // did, we will avoid adding it repeatedly
  968. for( var j = 0; j < stateBefore.length; j++ ) {
  969. if( stateBefore[j] === state[i] ) {
  970. stateBefore.splice( j, 1 );
  971. continue stateLoop;
  972. }
  973. }
  974. document.documentElement.classList.add( state[i] );
  975. // Dispatch custom event matching the state's name
  976. dispatchEvent( state[i] );
  977. }
  978. // Clean up the remains of the previous state
  979. while( stateBefore.length ) {
  980. document.documentElement.classList.remove( stateBefore.pop() );
  981. }
  982. // If the overview is active, re-activate it to update positions
  983. if( isOverview() ) {
  984. activateOverview();
  985. }
  986. // Find the current horizontal slide and any possible vertical slides
  987. // within it
  988. var currentHorizontalSlide = horizontalSlides[ indexh ],
  989. currentVerticalSlides = currentHorizontalSlide.querySelectorAll( 'section' );
  990. // Store references to the previous and current slides
  991. currentSlide = currentVerticalSlides[ indexv ] || currentHorizontalSlide;
  992. // Show fragment, if specified
  993. if( typeof f !== 'undefined' ) {
  994. var fragments = sortFragments( currentSlide.querySelectorAll( '.fragment' ) );
  995. toArray( fragments ).forEach( function( fragment, indexf ) {
  996. if( indexf < f ) {
  997. fragment.classList.add( 'visible' );
  998. }
  999. else {
  1000. fragment.classList.remove( 'visible' );
  1001. }
  1002. } );
  1003. }
  1004. // Dispatch an event if the slide changed
  1005. var slideChanged = ( indexh !== indexhBefore || indexv !== indexvBefore );
  1006. if( slideChanged ) {
  1007. dispatchEvent( 'slidechanged', {
  1008. 'indexh': indexh,
  1009. 'indexv': indexv,
  1010. 'previousSlide': previousSlide,
  1011. 'currentSlide': currentSlide,
  1012. 'origin': o
  1013. } );
  1014. }
  1015. else {
  1016. // Ensure that the previous slide is never the same as the current
  1017. previousSlide = null;
  1018. }
  1019. // Solves an edge case where the previous slide maintains the
  1020. // 'present' class when navigating between adjacent vertical
  1021. // stacks
  1022. if( previousSlide ) {
  1023. previousSlide.classList.remove( 'present' );
  1024. // Reset all slides upon navigate to home
  1025. // Issue: #285
  1026. if ( document.querySelector( HOME_SLIDE_SELECTOR ).classList.contains( 'present' ) ) {
  1027. // Launch async task
  1028. setTimeout( function () {
  1029. var slides = toArray( document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR + '.stack') ), i;
  1030. for( i in slides ) {
  1031. if( slides[i] ) {
  1032. // Reset stack
  1033. setPreviousVerticalIndex( slides[i], 0 );
  1034. }
  1035. }
  1036. }, 0 );
  1037. }
  1038. }
  1039. // Handle embedded content
  1040. if( slideChanged ) {
  1041. stopEmbeddedContent( previousSlide );
  1042. startEmbeddedContent( currentSlide );
  1043. }
  1044. updateControls();
  1045. updateProgress();
  1046. updateBackground();
  1047. // Update the URL hash
  1048. writeURL();
  1049. }
  1050. /**
  1051. * Syncs the presentation with the current DOM. Useful
  1052. * when new slides or control elements are added or when
  1053. * the configuration has changed.
  1054. */
  1055. function sync() {
  1056. // Subscribe to input
  1057. removeEventListeners();
  1058. addEventListeners();
  1059. // Force a layout to make sure the current config is accounted for
  1060. layout();
  1061. // Reflect the current autoSlide value
  1062. autoSlide = config.autoSlide;
  1063. // Start auto-sliding if it's enabled
  1064. cueAutoSlide();
  1065. // Re-create the slide backgrounds
  1066. createBackgrounds();
  1067. updateControls();
  1068. updateProgress();
  1069. updateBackground();
  1070. }
  1071. /**
  1072. * Updates one dimension of slides by showing the slide
  1073. * with the specified index.
  1074. *
  1075. * @param {String} selector A CSS selector that will fetch
  1076. * the group of slides we are working with
  1077. * @param {Number} index The index of the slide that should be
  1078. * shown
  1079. *
  1080. * @return {Number} The index of the slide that is now shown,
  1081. * might differ from the passed in index if it was out of
  1082. * bounds.
  1083. */
  1084. function updateSlides( selector, index ) {
  1085. // Select all slides and convert the NodeList result to
  1086. // an array
  1087. var slides = toArray( document.querySelectorAll( selector ) ),
  1088. slidesLength = slides.length;
  1089. if( slidesLength ) {
  1090. // Should the index loop?
  1091. if( config.loop ) {
  1092. index %= slidesLength;
  1093. if( index < 0 ) {
  1094. index = slidesLength + index;
  1095. }
  1096. }
  1097. // Enforce max and minimum index bounds
  1098. index = Math.max( Math.min( index, slidesLength - 1 ), 0 );
  1099. for( var i = 0; i < slidesLength; i++ ) {
  1100. var element = slides[i];
  1101. // Optimization; hide all slides that are three or more steps
  1102. // away from the present slide
  1103. if( isOverview() === false ) {
  1104. // The distance loops so that it measures 1 between the first
  1105. // and last slides
  1106. var distance = Math.abs( ( index - i ) % ( slidesLength - 3 ) ) || 0;
  1107. element.style.display = distance > 3 ? 'none' : 'block';
  1108. }
  1109. var reverse = config.rtl && !isVerticalSlide( element );
  1110. element.classList.remove( 'past' );
  1111. element.classList.remove( 'present' );
  1112. element.classList.remove( 'future' );
  1113. // http://www.w3.org/html/wg/drafts/html/master/editing.html#the-hidden-attribute
  1114. element.setAttribute( 'hidden', '' );
  1115. if( i < index ) {
  1116. // Any element previous to index is given the 'past' class
  1117. element.classList.add( reverse ? 'future' : 'past' );
  1118. }
  1119. else if( i > index ) {
  1120. // Any element subsequent to index is given the 'future' class
  1121. element.classList.add( reverse ? 'past' : 'future' );
  1122. var fragments = toArray( element.querySelectorAll( '.fragment.visible' ) );
  1123. // No fragments in future slides should be visible ahead of time
  1124. while( fragments.length ) {
  1125. fragments.pop().classList.remove( 'visible' );
  1126. }
  1127. }
  1128. // If this element contains vertical slides
  1129. if( element.querySelector( 'section' ) ) {
  1130. element.classList.add( 'stack' );
  1131. }
  1132. }
  1133. // Mark the current slide as present
  1134. slides[index].classList.add( 'present' );
  1135. slides[index].removeAttribute( 'hidden' );
  1136. // If this slide has a state associated with it, add it
  1137. // onto the current state of the deck
  1138. var slideState = slides[index].getAttribute( 'data-state' );
  1139. if( slideState ) {
  1140. state = state.concat( slideState.split( ' ' ) );
  1141. }
  1142. // If this slide has a data-autoslide attribute associated use this as
  1143. // autoSlide value otherwise use the global configured time
  1144. var slideAutoSlide = slides[index].getAttribute( 'data-autoslide' );
  1145. if( slideAutoSlide ) {
  1146. autoSlide = parseInt( slideAutoSlide, 10 );
  1147. }
  1148. else {
  1149. autoSlide = config.autoSlide;
  1150. }
  1151. cueAutoSlide();
  1152. }
  1153. else {
  1154. // Since there are no slides we can't be anywhere beyond the
  1155. // zeroth index
  1156. index = 0;
  1157. }
  1158. return index;
  1159. }
  1160. /**
  1161. * Updates the progress bar to reflect the current slide.
  1162. */
  1163. function updateProgress() {
  1164. // Update progress if enabled
  1165. if( config.progress && dom.progress ) {
  1166. var horizontalSlides = toArray( document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) );
  1167. // The number of past and total slides
  1168. var totalCount = document.querySelectorAll( SLIDES_SELECTOR + ':not(.stack)' ).length;
  1169. var pastCount = 0;
  1170. // Step through all slides and count the past ones
  1171. mainLoop: for( var i = 0; i < horizontalSlides.length; i++ ) {
  1172. var horizontalSlide = horizontalSlides[i];
  1173. var verticalSlides = toArray( horizontalSlide.querySelectorAll( 'section' ) );
  1174. for( var j = 0; j < verticalSlides.length; j++ ) {
  1175. // Stop as soon as we arrive at the present
  1176. if( verticalSlides[j].classList.contains( 'present' ) ) {
  1177. break mainLoop;
  1178. }
  1179. pastCount++;
  1180. }
  1181. // Stop as soon as we arrive at the present
  1182. if( horizontalSlide.classList.contains( 'present' ) ) {
  1183. break;
  1184. }
  1185. // Don't count the wrapping section for vertical slides
  1186. if( horizontalSlide.classList.contains( 'stack' ) === false ) {
  1187. pastCount++;
  1188. }
  1189. }
  1190. dom.progressbar.style.width = ( pastCount / ( totalCount - 1 ) ) * window.innerWidth + 'px';
  1191. }
  1192. }
  1193. /**
  1194. * Updates the state of all control/navigation arrows.
  1195. */
  1196. function updateControls() {
  1197. if ( config.controls && dom.controls ) {
  1198. var routes = availableRoutes();
  1199. var fragments = availableFragments();
  1200. // Remove the 'enabled' class from all directions
  1201. dom.controlsLeft.concat( dom.controlsRight )
  1202. .concat( dom.controlsUp )
  1203. .concat( dom.controlsDown )
  1204. .concat( dom.controlsPrev )
  1205. .concat( dom.controlsNext ).forEach( function( node ) {
  1206. node.classList.remove( 'enabled' );
  1207. node.classList.remove( 'fragmented' );
  1208. } );
  1209. // Add the 'enabled' class to the available routes
  1210. if( routes.left ) dom.controlsLeft.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  1211. if( routes.right ) dom.controlsRight.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  1212. if( routes.up ) dom.controlsUp.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  1213. if( routes.down ) dom.controlsDown.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  1214. // Prev/next buttons
  1215. if( routes.left || routes.up ) dom.controlsPrev.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  1216. if( routes.right || routes.down ) dom.controlsNext.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  1217. // Highlight fragment directions
  1218. if( currentSlide ) {
  1219. // Always apply fragment decorator to prev/next buttons
  1220. if( fragments.prev ) dom.controlsPrev.forEach( function( el ) { el.classList.add( 'fragmented', 'enabled' ); } );
  1221. if( fragments.next ) dom.controlsNext.forEach( function( el ) { el.classList.add( 'fragmented', 'enabled' ); } );
  1222. // Apply fragment decorators to directional buttons based on
  1223. // what slide axis they are in
  1224. if( isVerticalSlide( currentSlide ) ) {
  1225. if( fragments.prev ) dom.controlsUp.forEach( function( el ) { el.classList.add( 'fragmented', 'enabled' ); } );
  1226. if( fragments.next ) dom.controlsDown.forEach( function( el ) { el.classList.add( 'fragmented', 'enabled' ); } );
  1227. }
  1228. else {
  1229. if( fragments.prev ) dom.controlsLeft.forEach( function( el ) { el.classList.add( 'fragmented', 'enabled' ); } );
  1230. if( fragments.next ) dom.controlsRight.forEach( function( el ) { el.classList.add( 'fragmented', 'enabled' ); } );
  1231. }
  1232. }
  1233. }
  1234. }
  1235. /**
  1236. * Updates the background elements to reflect the current
  1237. * slide.
  1238. */
  1239. function updateBackground() {
  1240. // Update the classes of all backgrounds to match the
  1241. // states of their slides (past/present/future)
  1242. toArray( dom.background.childNodes ).forEach( function( backgroundh, h ) {
  1243. // Reverse past/future classes when in RTL mode
  1244. var horizontalPast = config.rtl ? 'future' : 'past',
  1245. horizontalFuture = config.rtl ? 'past' : 'future';
  1246. backgroundh.className = 'slide-background ' + ( h < indexh ? horizontalPast : h > indexh ? horizontalFuture : 'present' );
  1247. toArray( backgroundh.childNodes ).forEach( function( backgroundv, v ) {
  1248. backgroundv.className = 'slide-background ' + ( v < indexv ? 'past' : v > indexv ? 'future' : 'present' );
  1249. } );
  1250. } );
  1251. // Allow the first background to apply without transition
  1252. setTimeout( function() {
  1253. dom.background.classList.remove( 'no-transition' );
  1254. }, 1 );
  1255. }
  1256. /**
  1257. * Determine what available routes there are for navigation.
  1258. *
  1259. * @return {Object} containing four booleans: left/right/up/down
  1260. */
  1261. function availableRoutes() {
  1262. var horizontalSlides = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ),
  1263. verticalSlides = document.querySelectorAll( VERTICAL_SLIDES_SELECTOR );
  1264. var routes = {
  1265. left: indexh > 0 || config.loop,
  1266. right: indexh < horizontalSlides.length - 1 || config.loop,
  1267. up: indexv > 0,
  1268. down: indexv < verticalSlides.length - 1
  1269. };
  1270. // reverse horizontal controls for rtl
  1271. if( config.rtl ) {
  1272. var left = routes.left;
  1273. routes.left = routes.right;
  1274. routes.right = left;
  1275. }
  1276. return routes;
  1277. }
  1278. /**
  1279. * Returns an object describing the available fragment
  1280. * directions.
  1281. *
  1282. * @return {Object} two boolean properties: prev/next
  1283. */
  1284. function availableFragments() {
  1285. if( currentSlide && config.fragments ) {
  1286. var fragments = currentSlide.querySelectorAll( '.fragment' );
  1287. var hiddenFragments = currentSlide.querySelectorAll( '.fragment:not(.visible)' );
  1288. return {
  1289. prev: fragments.length - hiddenFragments.length > 0,
  1290. next: !!hiddenFragments.length
  1291. };
  1292. }
  1293. else {
  1294. return { prev: false, next: false };
  1295. }
  1296. }
  1297. /**
  1298. * Start playback of any embedded content inside of
  1299. * the targeted slide.
  1300. */
  1301. function startEmbeddedContent( slide ) {
  1302. if( slide ) {
  1303. // HTML5 media elements
  1304. toArray( slide.querySelectorAll( 'video, audio' ) ).forEach( function( el ) {
  1305. if( el.hasAttribute( 'data-autoplay' ) ) {
  1306. el.play();
  1307. }
  1308. } );
  1309. // YouTube embeds
  1310. toArray( slide.querySelectorAll( 'iframe[src*="youtube.com/embed/"]' ) ).forEach( function( el ) {
  1311. if( el.hasAttribute( 'data-autoplay' ) ) {
  1312. el.contentWindow.postMessage('{"event":"command","func":"playVideo","args":""}', '*');
  1313. }
  1314. });
  1315. }
  1316. }
  1317. /**
  1318. * Stop playback of any embedded content inside of
  1319. * the targeted slide.
  1320. */
  1321. function stopEmbeddedContent( slide ) {
  1322. if( slide ) {
  1323. // HTML5 media elements
  1324. toArray( slide.querySelectorAll( 'video, audio' ) ).forEach( function( el ) {
  1325. if( !el.hasAttribute( 'data-ignore' ) ) {
  1326. el.pause();
  1327. }
  1328. } );
  1329. // YouTube embeds
  1330. toArray( slide.querySelectorAll( 'iframe[src*="youtube.com/embed/"]' ) ).forEach( function( el ) {
  1331. if( !el.hasAttribute( 'data-ignore' ) && typeof el.contentWindow.postMessage === 'function' ) {
  1332. el.contentWindow.postMessage('{"event":"command","func":"pauseVideo","args":""}', '*');
  1333. }
  1334. });
  1335. }
  1336. }
  1337. /**
  1338. * Reads the current URL (hash) and navigates accordingly.
  1339. */
  1340. function readURL() {
  1341. var hash = window.location.hash;
  1342. // Attempt to parse the hash as either an index or name
  1343. var bits = hash.slice( 2 ).split( '/' ),
  1344. name = hash.replace( /#|\//gi, '' );
  1345. // If the first bit is invalid and there is a name we can
  1346. // assume that this is a named link
  1347. if( isNaN( parseInt( bits[0], 10 ) ) && name.length ) {
  1348. // Find the slide with the specified name
  1349. var element = document.querySelector( '#' + name );
  1350. if( element ) {
  1351. // Find the position of the named slide and navigate to it
  1352. var indices = Reveal.getIndices( element );
  1353. slide( indices.h, indices.v );
  1354. }
  1355. // If the slide doesn't exist, navigate to the current slide
  1356. else {
  1357. slide( indexh, indexv );
  1358. }
  1359. }
  1360. else {
  1361. // Read the index components of the hash
  1362. var h = parseInt( bits[0], 10 ) || 0,
  1363. v = parseInt( bits[1], 10 ) || 0;
  1364. if( h !== indexh || v !== indexv ) {
  1365. slide( h, v );
  1366. }
  1367. }
  1368. }
  1369. /**
  1370. * Updates the page URL (hash) to reflect the current
  1371. * state.
  1372. *
  1373. * @param {Number} delay The time in ms to wait before
  1374. * writing the hash
  1375. */
  1376. function writeURL( delay ) {
  1377. if( config.history ) {
  1378. // Make sure there's never more than one timeout running
  1379. clearTimeout( writeURLTimeout );
  1380. // If a delay is specified, timeout this call
  1381. if( typeof delay === 'number' ) {
  1382. writeURLTimeout = setTimeout( writeURL, delay );
  1383. }
  1384. else {
  1385. var url = '/';
  1386. // If the current slide has an ID, use that as a named link
  1387. if( currentSlide && typeof currentSlide.getAttribute( 'id' ) === 'string' ) {
  1388. url = '/' + currentSlide.getAttribute( 'id' );
  1389. }
  1390. // Otherwise use the /h/v index
  1391. else {
  1392. if( indexh > 0 || indexv > 0 ) url += indexh;
  1393. if( indexv > 0 ) url += '/' + indexv;
  1394. }
  1395. window.location.hash = url;
  1396. }
  1397. }
  1398. }
  1399. /**
  1400. * Retrieves the h/v location of the current, or specified,
  1401. * slide.
  1402. *
  1403. * @param {HTMLElement} slide If specified, the returned
  1404. * index will be for this slide rather than the currently
  1405. * active one
  1406. *
  1407. * @return {Object} { h: <int>, v: <int>, f: <int> }
  1408. */
  1409. function getIndices( slide ) {
  1410. // By default, return the current indices
  1411. var h = indexh,
  1412. v = indexv,
  1413. f;
  1414. // If a slide is specified, return the indices of that slide
  1415. if( slide ) {
  1416. var isVertical = isVerticalSlide( slide );
  1417. var slideh = isVertical ? slide.parentNode : slide;
  1418. // Select all horizontal slides
  1419. var horizontalSlides = toArray( document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) );
  1420. // Now that we know which the horizontal slide is, get its index
  1421. h = Math.max( horizontalSlides.indexOf( slideh ), 0 );
  1422. // If this is a vertical slide, grab the vertical index
  1423. if( isVertical ) {
  1424. v = Math.max( toArray( slide.parentNode.querySelectorAll( 'section' ) ).indexOf( slide ), 0 );
  1425. }
  1426. }
  1427. if( !slide && currentSlide ) {
  1428. var visibleFragments = currentSlide.querySelectorAll( '.fragment.visible' );
  1429. if( visibleFragments.length ) {
  1430. f = visibleFragments.length;
  1431. }
  1432. }
  1433. return { h: h, v: v, f: f };
  1434. }
  1435. /**
  1436. * Navigate to the next slide fragment.
  1437. *
  1438. * @return {Boolean} true if there was a next fragment,
  1439. * false otherwise
  1440. */
  1441. function nextFragment() {
  1442. if( currentSlide && config.fragments ) {
  1443. var fragments = sortFragments( currentSlide.querySelectorAll( '.fragment:not(.visible)' ) );
  1444. if( fragments.length ) {
  1445. // Find the index of the next fragment
  1446. var index = fragments[0].getAttribute( 'data-fragment-index' );
  1447. // Find all fragments with the same index
  1448. fragments = currentSlide.querySelectorAll( '.fragment[data-fragment-index="'+ index +'"]' );
  1449. toArray( fragments ).forEach( function( element ) {
  1450. element.classList.add( 'visible' );
  1451. } );
  1452. // Notify subscribers of the change
  1453. dispatchEvent( 'fragmentshown', { fragment: fragments[0], fragments: fragments } );
  1454. updateControls();
  1455. return true;
  1456. }
  1457. }
  1458. return false;
  1459. }
  1460. /**
  1461. * Navigate to the previous slide fragment.
  1462. *
  1463. * @return {Boolean} true if there was a previous fragment,
  1464. * false otherwise
  1465. */
  1466. function previousFragment() {
  1467. if( currentSlide && config.fragments ) {
  1468. var fragments = sortFragments( currentSlide.querySelectorAll( '.fragment.visible' ) );
  1469. if( fragments.length ) {
  1470. // Find the index of the previous fragment
  1471. var index = fragments[ fragments.length - 1 ].getAttribute( 'data-fragment-index' );
  1472. // Find all fragments with the same index
  1473. fragments = currentSlide.querySelectorAll( '.fragment[data-fragment-index="'+ index +'"]' );
  1474. toArray( fragments ).forEach( function( f ) {
  1475. f.classList.remove( 'visible' );
  1476. } );
  1477. // Notify subscribers of the change
  1478. dispatchEvent( 'fragmenthidden', { fragment: fragments[0], fragments: fragments } );
  1479. updateControls();
  1480. return true;
  1481. }
  1482. }
  1483. return false;
  1484. }
  1485. /**
  1486. * Cues a new automated slide if enabled in the config.
  1487. */
  1488. function cueAutoSlide() {
  1489. clearTimeout( autoSlideTimeout );
  1490. // Cue the next auto-slide if enabled
  1491. if( autoSlide && !isPaused() && !isOverview() ) {
  1492. autoSlideTimeout = setTimeout( navigateNext, autoSlide );
  1493. }
  1494. }
  1495. /**
  1496. * Cancels any ongoing request to auto-slide.
  1497. */
  1498. function cancelAutoSlide() {
  1499. clearTimeout( autoSlideTimeout );
  1500. }
  1501. function navigateLeft() {
  1502. // Reverse for RTL
  1503. if( config.rtl ) {
  1504. if( ( isOverview() || nextFragment() === false ) && availableRoutes().left ) {
  1505. slide( indexh + 1 );
  1506. }
  1507. }
  1508. // Normal navigation
  1509. else if( ( isOverview() || previousFragment() === false ) && availableRoutes().left ) {
  1510. slide( indexh - 1 );
  1511. }
  1512. }
  1513. function navigateRight() {
  1514. // Reverse for RTL
  1515. if( config.rtl ) {
  1516. if( ( isOverview() || previousFragment() === false ) && availableRoutes().right ) {
  1517. slide( indexh - 1 );
  1518. }
  1519. }
  1520. // Normal navigation
  1521. else if( ( isOverview() || nextFragment() === false ) && availableRoutes().right ) {
  1522. slide( indexh + 1 );
  1523. }
  1524. }
  1525. function navigateUp() {
  1526. // Prioritize hiding fragments
  1527. if( ( isOverview() || previousFragment() === false ) && availableRoutes().up ) {
  1528. slide( indexh, indexv - 1 );
  1529. }
  1530. }
  1531. function navigateDown() {
  1532. // Prioritize revealing fragments
  1533. if( ( isOverview() || nextFragment() === false ) && availableRoutes().down ) {
  1534. slide( indexh, indexv + 1 );
  1535. }
  1536. }
  1537. /**
  1538. * Navigates backwards, prioritized in the following order:
  1539. * 1) Previous fragment
  1540. * 2) Previous vertical slide
  1541. * 3) Previous horizontal slide
  1542. */
  1543. function navigatePrev() {
  1544. // Prioritize revealing fragments
  1545. if( previousFragment() === false ) {
  1546. if( availableRoutes().up ) {
  1547. navigateUp();
  1548. }
  1549. else {
  1550. // Fetch the previous horizontal slide, if there is one
  1551. var previousSlide = document.querySelector( HORIZONTAL_SLIDES_SELECTOR + '.past:nth-child(' + indexh + ')' );
  1552. if( previousSlide ) {
  1553. var v = ( previousSlide.querySelectorAll( 'section' ).length - 1 ) || undefined;
  1554. var h = indexh - 1;
  1555. slide( h, v );
  1556. }
  1557. }
  1558. }
  1559. }
  1560. /**
  1561. * Same as #navigatePrev() but navigates forwards.
  1562. */
  1563. function navigateNext() {
  1564. // Prioritize revealing fragments
  1565. if( nextFragment() === false ) {
  1566. availableRoutes().down ? navigateDown() : navigateRight();
  1567. }
  1568. // If auto-sliding is enabled we need to cue up
  1569. // another timeout
  1570. cueAutoSlide();
  1571. }
  1572. // --------------------------------------------------------------------//
  1573. // ----------------------------- EVENTS -------------------------------//
  1574. // --------------------------------------------------------------------//
  1575. /**
  1576. * Handler for the document level 'keydown' event.
  1577. *
  1578. * @param {Object} event
  1579. */
  1580. function onDocumentKeyDown( event ) {
  1581. // Check if there's a focused element that could be using
  1582. // the keyboard
  1583. var activeElement = document.activeElement;
  1584. var hasFocus = !!( document.activeElement && ( document.activeElement.type || document.activeElement.href || document.activeElement.contentEditable !== 'inherit' ) );
  1585. // Disregard the event if there's a focused element or a
  1586. // keyboard modifier key is present
  1587. if( hasFocus || (event.shiftKey && event.keyCode !== 32) || event.altKey || event.ctrlKey || event.metaKey ) return;
  1588. // While paused only allow "unpausing" keyboard events (b and .)
  1589. if( isPaused() && [66,190,191].indexOf( event.keyCode ) === -1 ) {
  1590. return false;
  1591. }
  1592. var triggered = false;
  1593. // 1. User defined key bindings
  1594. if( typeof config.keyboard === 'object' ) {
  1595. for( var key in config.keyboard ) {
  1596. // Check if this binding matches the pressed key
  1597. if( parseInt( key, 10 ) === event.keyCode ) {
  1598. var value = config.keyboard[ key ];
  1599. // Callback function
  1600. if( typeof value === 'function' ) {
  1601. value.apply( null, [ event ] );
  1602. }
  1603. // String shortcuts to reveal.js API
  1604. else if( typeof value === 'string' && typeof Reveal[ value ] === 'function' ) {
  1605. Reveal[ value ].call();
  1606. }
  1607. triggered = true;
  1608. }
  1609. }
  1610. }
  1611. // 2. System defined key bindings
  1612. if( triggered === false ) {
  1613. // Assume true and try to prove false
  1614. triggered = true;
  1615. switch( event.keyCode ) {
  1616. // p, page up
  1617. case 80: case 33: navigatePrev(); break;
  1618. // n, page down
  1619. case 78: case 34: navigateNext(); break;
  1620. // h, left
  1621. case 72: case 37: navigateLeft(); break;
  1622. // l, right
  1623. case 76: case 39: navigateRight(); break;
  1624. // k, up
  1625. case 75: case 38: navigateUp(); break;
  1626. // j, down
  1627. case 74: case 40: navigateDown(); break;
  1628. // home
  1629. case 36: slide( 0 ); break;
  1630. // end
  1631. case 35: slide( Number.MAX_VALUE ); break;
  1632. // space
  1633. case 32: isOverview() ? deactivateOverview() : event.shiftKey ? navigatePrev() : navigateNext(); break;
  1634. // return
  1635. case 13: isOverview() ? deactivateOverview() : triggered = false; break;
  1636. // b, period, Logitech presenter tools "black screen" button
  1637. case 66: case 190: case 191: togglePause(); break;
  1638. // f
  1639. case 70: enterFullscreen(); break;
  1640. default:
  1641. triggered = false;
  1642. }
  1643. }
  1644. // If the input resulted in a triggered action we should prevent
  1645. // the browsers default behavior
  1646. if( triggered ) {
  1647. event.preventDefault();
  1648. }
  1649. // ESC or O key
  1650. else if ( ( event.keyCode === 27 || event.keyCode === 79 ) && supports3DTransforms ) {
  1651. toggleOverview();
  1652. event.preventDefault();
  1653. }
  1654. // If auto-sliding is enabled we need to cue up
  1655. // another timeout
  1656. cueAutoSlide();
  1657. }
  1658. /**
  1659. * Handler for the 'touchstart' event, enables support for
  1660. * swipe and pinch gestures.
  1661. */
  1662. function onTouchStart( event ) {
  1663. touch.startX = event.touches[0].clientX;
  1664. touch.startY = event.touches[0].clientY;
  1665. touch.startCount = event.touches.length;
  1666. // If there's two touches we need to memorize the distance
  1667. // between those two points to detect pinching
  1668. if( event.touches.length === 2 && config.overview ) {
  1669. touch.startSpan = distanceBetween( {
  1670. x: event.touches[1].clientX,
  1671. y: event.touches[1].clientY
  1672. }, {
  1673. x: touch.startX,
  1674. y: touch.startY
  1675. } );
  1676. }
  1677. }
  1678. /**
  1679. * Handler for the 'touchmove' event.
  1680. */
  1681. function onTouchMove( event ) {
  1682. // Each touch should only trigger one action
  1683. if( !touch.handled ) {
  1684. var currentX = event.touches[0].clientX;
  1685. var currentY = event.touches[0].clientY;
  1686. // If the touch started off with two points and still has
  1687. // two active touches; test for the pinch gesture
  1688. if( event.touches.length === 2 && touch.startCount === 2 && config.overview ) {
  1689. // The current distance in pixels between the two touch points
  1690. var currentSpan = distanceBetween( {
  1691. x: event.touches[1].clientX,
  1692. y: event.touches[1].clientY
  1693. }, {
  1694. x: touch.startX,
  1695. y: touch.startY
  1696. } );
  1697. // If the span is larger than the desire amount we've got
  1698. // ourselves a pinch
  1699. if( Math.abs( touch.startSpan - currentSpan ) > touch.threshold ) {
  1700. touch.handled = true;
  1701. if( currentSpan < touch.startSpan ) {
  1702. activateOverview();
  1703. }
  1704. else {
  1705. deactivateOverview();
  1706. }
  1707. }
  1708. event.preventDefault();
  1709. }
  1710. // There was only one touch point, look for a swipe
  1711. else if( event.touches.length === 1 && touch.startCount !== 2 ) {
  1712. var deltaX = currentX - touch.startX,
  1713. deltaY = currentY - touch.startY;
  1714. if( deltaX > touch.threshold && Math.abs( deltaX ) > Math.abs( deltaY ) ) {
  1715. touch.handled = true;
  1716. navigateLeft();
  1717. }
  1718. else if( deltaX < -touch.threshold && Math.abs( deltaX ) > Math.abs( deltaY ) ) {
  1719. touch.handled = true;
  1720. navigateRight();
  1721. }
  1722. else if( deltaY > touch.threshold ) {
  1723. touch.handled = true;
  1724. navigateUp();
  1725. }
  1726. else if( deltaY < -touch.threshold ) {
  1727. touch.handled = true;
  1728. navigateDown();
  1729. }
  1730. event.preventDefault();
  1731. }
  1732. }
  1733. // There's a bug with swiping on some Android devices unless
  1734. // the default action is always prevented
  1735. else if( navigator.userAgent.match( /android/gi ) ) {
  1736. event.preventDefault();
  1737. }
  1738. }
  1739. /**
  1740. * Handler for the 'touchend' event.
  1741. */
  1742. function onTouchEnd( event ) {
  1743. touch.handled = false;
  1744. }
  1745. /**
  1746. * Convert pointer down to touch start.
  1747. */
  1748. function onPointerDown( event ) {
  1749. if( event.pointerType === event.MSPOINTER_TYPE_TOUCH ) {
  1750. event.touches = [{ clientX: event.clientX, clientY: event.clientY }];
  1751. onTouchStart( event );
  1752. }
  1753. }
  1754. /**
  1755. * Convert pointer move to touch move.
  1756. */
  1757. function onPointerMove( event ) {
  1758. if( event.pointerType === event.MSPOINTER_TYPE_TOUCH ) {
  1759. event.touches = [{ clientX: event.clientX, clientY: event.clientY }];
  1760. onTouchMove( event );
  1761. }
  1762. }
  1763. /**
  1764. * Convert pointer up to touch end.
  1765. */
  1766. function onPointerUp( event ) {
  1767. if( event.pointerType === event.MSPOINTER_TYPE_TOUCH ) {
  1768. event.touches = [{ clientX: event.clientX, clientY: event.clientY }];
  1769. onTouchEnd( event );
  1770. }
  1771. }
  1772. /**
  1773. * Handles mouse wheel scrolling, throttled to avoid skipping
  1774. * multiple slides.
  1775. */
  1776. function onDocumentMouseScroll( event ) {
  1777. if( Date.now() - lastMouseWheelStep > 600 ) {
  1778. lastMouseWheelStep = Date.now();
  1779. var delta = event.detail || -event.wheelDelta;
  1780. if( delta > 0 ) {
  1781. navigateNext();
  1782. }
  1783. else {
  1784. navigatePrev();
  1785. }
  1786. }
  1787. }
  1788. /**
  1789. * Clicking on the progress bar results in a navigation to the
  1790. * closest approximate horizontal slide using this equation:
  1791. *
  1792. * ( clickX / presentationWidth ) * numberOfSlides
  1793. */
  1794. function onProgressClicked( event ) {
  1795. event.preventDefault();
  1796. var slidesTotal = toArray( document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) ).length;
  1797. var slideIndex = Math.floor( ( event.clientX / dom.wrapper.offsetWidth ) * slidesTotal );
  1798. slide( slideIndex );
  1799. }
  1800. /**
  1801. * Event handler for navigation control buttons.
  1802. */
  1803. function onNavigateLeftClicked( event ) { event.preventDefault(); navigateLeft(); }
  1804. function onNavigateRightClicked( event ) { event.preventDefault(); navigateRight(); }
  1805. function onNavigateUpClicked( event ) { event.preventDefault(); navigateUp(); }
  1806. function onNavigateDownClicked( event ) { event.preventDefault(); navigateDown(); }
  1807. function onNavigatePrevClicked( event ) { event.preventDefault(); navigatePrev(); }
  1808. function onNavigateNextClicked( event ) { event.preventDefault(); navigateNext(); }
  1809. /**
  1810. * Handler for the window level 'hashchange' event.
  1811. */
  1812. function onWindowHashChange( event ) {
  1813. readURL();
  1814. }
  1815. /**
  1816. * Handler for the window level 'resize' event.
  1817. */
  1818. function onWindowResize( event ) {
  1819. layout();
  1820. }
  1821. /**
  1822. * Invoked when a slide is and we're in the overview.
  1823. */
  1824. function onOverviewSlideClicked( event ) {
  1825. // TODO There's a bug here where the event listeners are not
  1826. // removed after deactivating the overview.
  1827. if( eventsAreBound && isOverview() ) {
  1828. event.preventDefault();
  1829. var element = event.target;
  1830. while( element && !element.nodeName.match( /section/gi ) ) {
  1831. element = element.parentNode;
  1832. }
  1833. if( element && !element.classList.contains( 'disabled' ) ) {
  1834. deactivateOverview();
  1835. if( element.nodeName.match( /section/gi ) ) {
  1836. var h = parseInt( element.getAttribute( 'data-index-h' ), 10 ),
  1837. v = parseInt( element.getAttribute( 'data-index-v' ), 10 );
  1838. slide( h, v );
  1839. }
  1840. }
  1841. }
  1842. }
  1843. /**
  1844. * Handles clicks on links that are set to preview in the
  1845. * iframe overlay.
  1846. */
  1847. function onPreviewLinkClicked( event ) {
  1848. var url = event.target.getAttribute( 'href' );
  1849. if( url ) {
  1850. openPreview( url );
  1851. event.preventDefault();
  1852. }
  1853. }
  1854. // --------------------------------------------------------------------//
  1855. // ------------------------------- API --------------------------------//
  1856. // --------------------------------------------------------------------//
  1857. return {
  1858. initialize: initialize,
  1859. configure: configure,
  1860. sync: sync,
  1861. // Navigation methods
  1862. slide: slide,
  1863. left: navigateLeft,
  1864. right: navigateRight,
  1865. up: navigateUp,
  1866. down: navigateDown,
  1867. prev: navigatePrev,
  1868. next: navigateNext,
  1869. prevFragment: previousFragment,
  1870. nextFragment: nextFragment,
  1871. // Deprecated aliases
  1872. navigateTo: slide,
  1873. navigateLeft: navigateLeft,
  1874. navigateRight: navigateRight,
  1875. navigateUp: navigateUp,
  1876. navigateDown: navigateDown,
  1877. navigatePrev: navigatePrev,
  1878. navigateNext: navigateNext,
  1879. // Forces an update in slide layout
  1880. layout: layout,
  1881. // Returns an object with the available routes as booleans (left/right/top/bottom)
  1882. availableRoutes: availableRoutes,
  1883. // Returns an object with the available fragments as booleans (prev/next)
  1884. availableFragments: availableFragments,
  1885. // Toggles the overview mode on/off
  1886. toggleOverview: toggleOverview,
  1887. // Toggles the "black screen" mode on/off
  1888. togglePause: togglePause,
  1889. // State checks
  1890. isOverview: isOverview,
  1891. isPaused: isPaused,
  1892. // Adds or removes all internal event listeners (such as keyboard)
  1893. addEventListeners: addEventListeners,
  1894. removeEventListeners: removeEventListeners,
  1895. // Returns the indices of the current, or specified, slide
  1896. getIndices: getIndices,
  1897. // Returns the slide at the specified index, y is optional
  1898. getSlide: function( x, y ) {
  1899. var horizontalSlide = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR )[ x ];
  1900. var verticalSlides = horizontalSlide && horizontalSlide.querySelectorAll( 'section' );
  1901. if( typeof y !== 'undefined' ) {
  1902. return verticalSlides ? verticalSlides[ y ] : undefined;
  1903. }
  1904. return horizontalSlide;
  1905. },
  1906. // Returns the previous slide element, may be null
  1907. getPreviousSlide: function() {
  1908. return previousSlide;
  1909. },
  1910. // Returns the current slide element
  1911. getCurrentSlide: function() {
  1912. return currentSlide;
  1913. },
  1914. // Returns the current scale of the presentation content
  1915. getScale: function() {
  1916. return scale;
  1917. },
  1918. // Returns the current configuration object
  1919. getConfig: function() {
  1920. return config;
  1921. },
  1922. // Helper method, retrieves query string as a key/value hash
  1923. getQueryHash: function() {
  1924. var query = {};
  1925. location.search.replace( /[A-Z0-9]+?=(\w*)/gi, function(a) {
  1926. query[ a.split( '=' ).shift() ] = a.split( '=' ).pop();
  1927. } );
  1928. return query;
  1929. },
  1930. // Returns true if we're currently on the first slide
  1931. isFirstSlide: function() {
  1932. return document.querySelector( SLIDES_SELECTOR + '.past' ) == null ? true : false;
  1933. },
  1934. // Returns true if we're currently on the last slide
  1935. isLastSlide: function() {
  1936. if( currentSlide && currentSlide.classList.contains( '.stack' ) ) {
  1937. return currentSlide.querySelector( SLIDES_SELECTOR + '.future' ) == null ? true : false;
  1938. }
  1939. else {
  1940. return document.querySelector( SLIDES_SELECTOR + '.future' ) == null ? true : false;
  1941. }
  1942. },
  1943. // Forward event binding to the reveal DOM element
  1944. addEventListener: function( type, listener, useCapture ) {
  1945. if( 'addEventListener' in window ) {
  1946. ( dom.wrapper || document.querySelector( '.reveal' ) ).addEventListener( type, listener, useCapture );
  1947. }
  1948. },
  1949. removeEventListener: function( type, listener, useCapture ) {
  1950. if( 'addEventListener' in window ) {
  1951. ( dom.wrapper || document.querySelector( '.reveal' ) ).removeEventListener( type, listener, useCapture );
  1952. }
  1953. }
  1954. };
  1955. })();