reveal.js 60 KB

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