reveal.js 50 KB

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