reveal.js 69 KB

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