reveal.js 69 KB

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