reveal.js 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903
  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 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( /iphone|ipod|android/gi.test( navigator.userAgent ) && !/crios/gi.test( navigator.userAgent ) ) {
  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', onProgressClicked, 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, onNavigateLeftClicked, false ); } );
  320. dom.controlsRight.forEach( function( el ) { el.addEventListener( actionEvent, onNavigateRightClicked, false ); } );
  321. dom.controlsUp.forEach( function( el ) { el.addEventListener( actionEvent, onNavigateUpClicked, false ); } );
  322. dom.controlsDown.forEach( function( el ) { el.addEventListener( actionEvent, onNavigateDownClicked, false ); } );
  323. dom.controlsPrev.forEach( function( el ) { el.addEventListener( actionEvent, onNavigatePrevClicked, false ); } );
  324. dom.controlsNext.forEach( function( el ) { el.addEventListener( actionEvent, onNavigateNextClicked, 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', onProgressClicked, 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, onNavigateLeftClicked, false ); } );
  345. dom.controlsRight.forEach( function( el ) { el.removeEventListener( actionEvent, onNavigateRightClicked, false ); } );
  346. dom.controlsUp.forEach( function( el ) { el.removeEventListener( actionEvent, onNavigateUpClicked, false ); } );
  347. dom.controlsDown.forEach( function( el ) { el.removeEventListener( actionEvent, onNavigateDownClicked, false ); } );
  348. dom.controlsPrev.forEach( function( el ) { el.removeEventListener( actionEvent, onNavigatePrevClicked, false ); } );
  349. dom.controlsNext.forEach( function( el ) { el.removeEventListener( actionEvent, onNavigateNextClicked, 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. * Causes the address bar to hide on mobile devices,
  381. * more vertical space ftw.
  382. */
  383. function removeAddressBar() {
  384. if( window.orientation === 0 ) {
  385. document.documentElement.style.overflow = 'scroll';
  386. document.body.style.height = '120%';
  387. }
  388. else {
  389. document.documentElement.style.overflow = '';
  390. document.body.style.height = '100%';
  391. }
  392. setTimeout( function() {
  393. window.scrollTo( 0, 1 );
  394. }, 10 );
  395. }
  396. /**
  397. * Dispatches an event of the specified type from the
  398. * reveal DOM element.
  399. */
  400. function dispatchEvent( type, properties ) {
  401. var event = document.createEvent( "HTMLEvents", 1, 2 );
  402. event.initEvent( type, true, true );
  403. extend( event, properties );
  404. dom.wrapper.dispatchEvent( event );
  405. }
  406. /**
  407. * Wrap all links in 3D goodness.
  408. */
  409. function linkify() {
  410. if( supports3DTransforms && !( 'msPerspective' in document.body.style ) ) {
  411. var nodes = document.querySelectorAll( SLIDES_SELECTOR + ' a:not(.image)' );
  412. for( var i = 0, len = nodes.length; i < len; i++ ) {
  413. var node = nodes[i];
  414. if( node.textContent && !node.querySelector( '*' ) && ( !node.className || !node.classList.contains( node, 'roll' ) ) ) {
  415. var span = document.createElement('span');
  416. span.setAttribute('data-title', node.text);
  417. span.innerHTML = node.innerHTML;
  418. node.classList.add( 'roll' );
  419. node.innerHTML = '';
  420. node.appendChild(span);
  421. }
  422. }
  423. }
  424. }
  425. /**
  426. * Applies JavaScript-controlled layout rules to the
  427. * presentation.
  428. */
  429. function layout() {
  430. // Available space to scale within
  431. var availableWidth = dom.wrapper.offsetWidth,
  432. availableHeight = dom.wrapper.offsetHeight;
  433. // Reduce availabe space by margin
  434. availableWidth -= ( availableHeight * config.margin );
  435. availableHeight -= ( availableHeight * config.margin );
  436. // Dimensions of the content
  437. var slideWidth = config.width,
  438. slideHeight = config.height;
  439. // Slide width may be a percentage of available width
  440. if( typeof slideWidth === 'string' && /%$/.test( slideWidth ) ) {
  441. slideWidth = parseInt( slideWidth, 10 ) / 100 * availableWidth;
  442. }
  443. // Slide height may be a percentage of available height
  444. if( typeof slideHeight === 'string' && /%$/.test( slideHeight ) ) {
  445. slideHeight = parseInt( slideHeight, 10 ) / 100 * availableHeight;
  446. }
  447. dom.slides.style.width = slideWidth + 'px';
  448. dom.slides.style.height = slideHeight + 'px';
  449. // Determine scale of content to fit within available space
  450. scale = Math.min( availableWidth / slideWidth, availableHeight / slideHeight );
  451. // Respect max/min scale settings
  452. scale = Math.max( scale, config.minScale );
  453. scale = Math.min( scale, config.maxScale );
  454. // Prefer applying scale via zoom since Chrome blurs scaled content
  455. // with nested transforms
  456. if( typeof dom.slides.style.zoom !== 'undefined' && !navigator.userAgent.match( /(iphone|ipod|android)/gi ) ) {
  457. dom.slides.style.zoom = scale;
  458. }
  459. // Apply scale transform as a fallback
  460. else {
  461. var transform = 'translate(-50%, -50%) scale('+ scale +') translate(50%, 50%)';
  462. dom.slides.style.WebkitTransform = transform;
  463. dom.slides.style.MozTransform = transform;
  464. dom.slides.style.msTransform = transform;
  465. dom.slides.style.OTransform = transform;
  466. dom.slides.style.transform = transform;
  467. }
  468. if( config.center ) {
  469. // Select all slides, vertical and horizontal
  470. var slides = toArray( document.querySelectorAll( SLIDES_SELECTOR ) );
  471. // Determine the minimum top offset for slides
  472. var minTop = -slideHeight / 2;
  473. for( var i = 0, len = slides.length; i < len; i++ ) {
  474. var slide = slides[ i ];
  475. // Don't bother updating invisible slides
  476. if( slide.style.display === 'none' ) {
  477. continue;
  478. }
  479. // Vertical stacks are not centered since their section
  480. // children will be
  481. if( slide.classList.contains( 'stack' ) ) {
  482. slide.style.top = 0;
  483. }
  484. else {
  485. slide.style.top = Math.max( - ( slide.offsetHeight / 2 ) - 20, minTop ) + 'px';
  486. }
  487. }
  488. }
  489. }
  490. /**
  491. * Stores the vertical index of a stack so that the same
  492. * vertical slide can be selected when navigating to and
  493. * from the stack.
  494. *
  495. * @param {HTMLElement} stack The vertical stack element
  496. * @param {int} v Index to memorize
  497. */
  498. function setPreviousVerticalIndex( stack, v ) {
  499. if( stack ) {
  500. stack.setAttribute( 'data-previous-indexv', v || 0 );
  501. }
  502. }
  503. /**
  504. * Retrieves the vertical index which was stored using
  505. * #setPreviousVerticalIndex() or 0 if no previous index
  506. * exists.
  507. *
  508. * @param {HTMLElement} stack The vertical stack element
  509. */
  510. function getPreviousVerticalIndex( stack ) {
  511. if( stack && stack.classList.contains( 'stack' ) ) {
  512. return parseInt( stack.getAttribute( 'data-previous-indexv' ) || 0, 10 );
  513. }
  514. return 0;
  515. }
  516. /**
  517. * Displays the overview of slides (quick nav) by
  518. * scaling down and arranging all slide elements.
  519. *
  520. * Experimental feature, might be dropped if perf
  521. * can't be improved.
  522. */
  523. function activateOverview() {
  524. // Only proceed if enabled in config
  525. if( config.overview ) {
  526. // Don't auto-slide while in overview mode
  527. cancelAutoSlide();
  528. var wasActive = dom.wrapper.classList.contains( 'overview' );
  529. dom.wrapper.classList.add( 'overview' );
  530. dom.wrapper.classList.remove( 'exit-overview' );
  531. clearTimeout( activateOverviewTimeout );
  532. clearTimeout( deactivateOverviewTimeout );
  533. // Not the pretties solution, but need to let the overview
  534. // class apply first so that slides are measured accurately
  535. // before we can position them
  536. activateOverviewTimeout = setTimeout( function(){
  537. var horizontalSlides = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR );
  538. for( var i = 0, len1 = horizontalSlides.length; i < len1; i++ ) {
  539. var hslide = horizontalSlides[i],
  540. htransform = 'translateZ(-2500px) translate(' + ( ( i - indexh ) * 105 ) + '%, 0%)';
  541. hslide.setAttribute( 'data-index-h', i );
  542. hslide.style.display = 'block';
  543. hslide.style.WebkitTransform = htransform;
  544. hslide.style.MozTransform = htransform;
  545. hslide.style.msTransform = htransform;
  546. hslide.style.OTransform = htransform;
  547. hslide.style.transform = htransform;
  548. if( hslide.classList.contains( 'stack' ) ) {
  549. var verticalSlides = hslide.querySelectorAll( 'section' );
  550. for( var j = 0, len2 = verticalSlides.length; j < len2; j++ ) {
  551. var verticalIndex = i === indexh ? indexv : getPreviousVerticalIndex( hslide );
  552. var vslide = verticalSlides[j],
  553. vtransform = 'translate(0%, ' + ( ( j - verticalIndex ) * 105 ) + '%)';
  554. vslide.setAttribute( 'data-index-h', i );
  555. vslide.setAttribute( 'data-index-v', j );
  556. vslide.style.display = 'block';
  557. vslide.style.WebkitTransform = vtransform;
  558. vslide.style.MozTransform = vtransform;
  559. vslide.style.msTransform = vtransform;
  560. vslide.style.OTransform = vtransform;
  561. vslide.style.transform = vtransform;
  562. // Navigate to this slide on click
  563. vslide.addEventListener( 'click', onOverviewSlideClicked, true );
  564. }
  565. }
  566. else {
  567. // Navigate to this slide on click
  568. hslide.addEventListener( 'click', onOverviewSlideClicked, true );
  569. }
  570. }
  571. layout();
  572. if( !wasActive ) {
  573. // Notify observers of the overview showing
  574. dispatchEvent( 'overviewshown', {
  575. 'indexh': indexh,
  576. 'indexv': indexv,
  577. 'currentSlide': currentSlide
  578. } );
  579. }
  580. }, 10 );
  581. }
  582. }
  583. /**
  584. * Exits the slide overview and enters the currently
  585. * active slide.
  586. */
  587. function deactivateOverview() {
  588. // Only proceed if enabled in config
  589. if( config.overview ) {
  590. clearTimeout( activateOverviewTimeout );
  591. clearTimeout( deactivateOverviewTimeout );
  592. dom.wrapper.classList.remove( 'overview' );
  593. // Temporarily add a class so that transitions can do different things
  594. // depending on whether they are exiting/entering overview, or just
  595. // moving from slide to slide
  596. dom.wrapper.classList.add( 'exit-overview' );
  597. deactivateOverviewTimeout = setTimeout( function () {
  598. dom.wrapper.classList.remove( 'exit-overview' );
  599. }, 10);
  600. // Select all slides
  601. var slides = toArray( document.querySelectorAll( SLIDES_SELECTOR ) );
  602. for( var i = 0, len = slides.length; i < len; i++ ) {
  603. var element = slides[i];
  604. element.style.display = '';
  605. // Resets all transforms to use the external styles
  606. element.style.WebkitTransform = '';
  607. element.style.MozTransform = '';
  608. element.style.msTransform = '';
  609. element.style.OTransform = '';
  610. element.style.transform = '';
  611. element.removeEventListener( 'click', onOverviewSlideClicked, true );
  612. }
  613. slide( indexh, indexv );
  614. cueAutoSlide();
  615. // Notify observers of the overview hiding
  616. dispatchEvent( 'overviewhidden', {
  617. 'indexh': indexh,
  618. 'indexv': indexv,
  619. 'currentSlide': currentSlide
  620. } );
  621. }
  622. }
  623. /**
  624. * Toggles the slide overview mode on and off.
  625. *
  626. * @param {Boolean} override Optional flag which overrides the
  627. * toggle logic and forcibly sets the desired state. True means
  628. * overview is open, false means it's closed.
  629. */
  630. function toggleOverview( override ) {
  631. if( typeof override === 'boolean' ) {
  632. override ? activateOverview() : deactivateOverview();
  633. }
  634. else {
  635. isOverview() ? deactivateOverview() : activateOverview();
  636. }
  637. }
  638. /**
  639. * Checks if the overview is currently active.
  640. *
  641. * @return {Boolean} true if the overview is active,
  642. * false otherwise
  643. */
  644. function isOverview() {
  645. return dom.wrapper.classList.contains( 'overview' );
  646. }
  647. /**
  648. * Handling the fullscreen functionality via the fullscreen API
  649. *
  650. * @see http://fullscreen.spec.whatwg.org/
  651. * @see https://developer.mozilla.org/en-US/docs/DOM/Using_fullscreen_mode
  652. */
  653. function enterFullscreen() {
  654. var element = document.body;
  655. // Check which implementation is available
  656. var requestMethod = element.requestFullScreen ||
  657. element.webkitRequestFullScreen ||
  658. element.mozRequestFullScreen ||
  659. element.msRequestFullScreen;
  660. if( requestMethod ) {
  661. requestMethod.apply( element );
  662. }
  663. }
  664. /**
  665. * Enters the paused mode which fades everything on screen to
  666. * black.
  667. */
  668. function pause() {
  669. cancelAutoSlide();
  670. dom.wrapper.classList.add( 'paused' );
  671. }
  672. /**
  673. * Exits from the paused mode.
  674. */
  675. function resume() {
  676. cueAutoSlide();
  677. dom.wrapper.classList.remove( 'paused' );
  678. }
  679. /**
  680. * Toggles the paused mode on and off.
  681. */
  682. function togglePause() {
  683. if( isPaused() ) {
  684. resume();
  685. }
  686. else {
  687. pause();
  688. }
  689. }
  690. /**
  691. * Checks if we are currently in the paused mode.
  692. */
  693. function isPaused() {
  694. return dom.wrapper.classList.contains( 'paused' );
  695. }
  696. /**
  697. * Steps from the current point in the presentation to the
  698. * slide which matches the specified horizontal and vertical
  699. * indices.
  700. *
  701. * @param {int} h Horizontal index of the target slide
  702. * @param {int} v Vertical index of the target slide
  703. * @param {int} f Optional index of a fragment within the
  704. * target slide to activate
  705. */
  706. function slide( h, v, f ) {
  707. // Remember where we were at before
  708. previousSlide = currentSlide;
  709. // Query all horizontal slides in the deck
  710. var horizontalSlides = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR );
  711. // If no vertical index is specified and the upcoming slide is a
  712. // stack, resume at its previous vertical index
  713. if( v === undefined ) {
  714. v = getPreviousVerticalIndex( horizontalSlides[ h ] );
  715. }
  716. // If we were on a vertical stack, remember what vertical index
  717. // it was on so we can resume at the same position when returning
  718. if( previousSlide && previousSlide.parentNode && previousSlide.parentNode.classList.contains( 'stack' ) ) {
  719. setPreviousVerticalIndex( previousSlide.parentNode, indexv );
  720. }
  721. // Remember the state before this slide
  722. var stateBefore = state.concat();
  723. // Reset the state array
  724. state.length = 0;
  725. var indexhBefore = indexh,
  726. indexvBefore = indexv;
  727. // Activate and transition to the new slide
  728. indexh = updateSlides( HORIZONTAL_SLIDES_SELECTOR, h === undefined ? indexh : h );
  729. indexv = updateSlides( VERTICAL_SLIDES_SELECTOR, v === undefined ? indexv : v );
  730. layout();
  731. // Apply the new state
  732. stateLoop: for( var i = 0, len = state.length; i < len; i++ ) {
  733. // Check if this state existed on the previous slide. If it
  734. // did, we will avoid adding it repeatedly
  735. for( var j = 0; j < stateBefore.length; j++ ) {
  736. if( stateBefore[j] === state[i] ) {
  737. stateBefore.splice( j, 1 );
  738. continue stateLoop;
  739. }
  740. }
  741. document.documentElement.classList.add( state[i] );
  742. // Dispatch custom event matching the state's name
  743. dispatchEvent( state[i] );
  744. }
  745. // Clean up the remains of the previous state
  746. while( stateBefore.length ) {
  747. document.documentElement.classList.remove( stateBefore.pop() );
  748. }
  749. // If the overview is active, re-activate it to update positions
  750. if( isOverview() ) {
  751. activateOverview();
  752. }
  753. // Update the URL hash after a delay since updating it mid-transition
  754. // is likely to cause visual lag
  755. writeURL( 1500 );
  756. // Find the current horizontal slide and any possible vertical slides
  757. // within it
  758. var currentHorizontalSlide = horizontalSlides[ indexh ],
  759. currentVerticalSlides = currentHorizontalSlide.querySelectorAll( 'section' );
  760. // Store references to the previous and current slides
  761. currentSlide = currentVerticalSlides[ indexv ] || currentHorizontalSlide;
  762. // Show fragment, if specified
  763. if( typeof f !== 'undefined' ) {
  764. var fragments = currentSlide.querySelectorAll( '.fragment' );
  765. toArray( fragments ).forEach( function( fragment, indexf ) {
  766. if( indexf < f ) {
  767. fragment.classList.add( 'visible' );
  768. }
  769. else {
  770. fragment.classList.remove( 'visible' );
  771. }
  772. } );
  773. }
  774. // Dispatch an event if the slide changed
  775. if( indexh !== indexhBefore || indexv !== indexvBefore ) {
  776. dispatchEvent( 'slidechanged', {
  777. 'indexh': indexh,
  778. 'indexv': indexv,
  779. 'previousSlide': previousSlide,
  780. 'currentSlide': currentSlide
  781. } );
  782. }
  783. else {
  784. // Ensure that the previous slide is never the same as the current
  785. previousSlide = null;
  786. }
  787. // Solves an edge case where the previous slide maintains the
  788. // 'present' class when navigating between adjacent vertical
  789. // stacks
  790. if( previousSlide ) {
  791. previousSlide.classList.remove( 'present' );
  792. // Reset all slides upon navigate to home
  793. // Issue: #285
  794. if ( document.querySelector( HOME_SLIDE_SELECTOR ).classList.contains( 'present' ) ) {
  795. // Launch async task
  796. setTimeout( function () {
  797. var slides = toArray( document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR + '.stack') ), i;
  798. for( i in slides ) {
  799. if( slides[i] ) {
  800. // Reset stack
  801. setPreviousVerticalIndex( slides[i], 0 );
  802. }
  803. }
  804. }, 0 );
  805. }
  806. }
  807. updateControls();
  808. updateProgress();
  809. }
  810. /**
  811. * Updates one dimension of slides by showing the slide
  812. * with the specified index.
  813. *
  814. * @param {String} selector A CSS selector that will fetch
  815. * the group of slides we are working with
  816. * @param {Number} index The index of the slide that should be
  817. * shown
  818. *
  819. * @return {Number} The index of the slide that is now shown,
  820. * might differ from the passed in index if it was out of
  821. * bounds.
  822. */
  823. function updateSlides( selector, index ) {
  824. // Select all slides and convert the NodeList result to
  825. // an array
  826. var slides = toArray( document.querySelectorAll( selector ) ),
  827. slidesLength = slides.length;
  828. if( slidesLength ) {
  829. // Should the index loop?
  830. if( config.loop ) {
  831. index %= slidesLength;
  832. if( index < 0 ) {
  833. index = slidesLength + index;
  834. }
  835. }
  836. // Enforce max and minimum index bounds
  837. index = Math.max( Math.min( index, slidesLength - 1 ), 0 );
  838. for( var i = 0; i < slidesLength; i++ ) {
  839. var element = slides[i];
  840. // Optimization; hide all slides that are three or more steps
  841. // away from the present slide
  842. if( isOverview() === false ) {
  843. // The distance loops so that it measures 1 between the first
  844. // and last slides
  845. var distance = Math.abs( ( index - i ) % ( slidesLength - 3 ) ) || 0;
  846. element.style.display = distance > 3 ? 'none' : 'block';
  847. }
  848. slides[i].classList.remove( 'past' );
  849. slides[i].classList.remove( 'present' );
  850. slides[i].classList.remove( 'future' );
  851. if( i < index ) {
  852. // Any element previous to index is given the 'past' class
  853. slides[i].classList.add( 'past' );
  854. }
  855. else if( i > index ) {
  856. // Any element subsequent to index is given the 'future' class
  857. slides[i].classList.add( 'future' );
  858. }
  859. // If this element contains vertical slides
  860. if( element.querySelector( 'section' ) ) {
  861. slides[i].classList.add( 'stack' );
  862. }
  863. }
  864. // Mark the current slide as present
  865. slides[index].classList.add( 'present' );
  866. // If this slide has a state associated with it, add it
  867. // onto the current state of the deck
  868. var slideState = slides[index].getAttribute( 'data-state' );
  869. if( slideState ) {
  870. state = state.concat( slideState.split( ' ' ) );
  871. }
  872. // If this slide has a data-autoslide attribtue associated use this as
  873. // autoSlide value otherwise use the global configured time
  874. var slideAutoSlide = slides[index].getAttribute( 'data-autoslide' );
  875. if( slideAutoSlide ) {
  876. autoSlide = parseInt( slideAutoSlide, 10 );
  877. }
  878. else {
  879. autoSlide = config.autoSlide;
  880. }
  881. }
  882. else {
  883. // Since there are no slides we can't be anywhere beyond the
  884. // zeroth index
  885. index = 0;
  886. }
  887. return index;
  888. }
  889. /**
  890. * Updates the progress bar to reflect the current slide.
  891. */
  892. function updateProgress() {
  893. // Update progress if enabled
  894. if( config.progress && dom.progress ) {
  895. var horizontalSlides = toArray( document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) );
  896. // The number of past and total slides
  897. var totalCount = document.querySelectorAll( SLIDES_SELECTOR + ':not(.stack)' ).length;
  898. var pastCount = 0;
  899. // Step through all slides and count the past ones
  900. mainLoop: for( var i = 0; i < horizontalSlides.length; i++ ) {
  901. var horizontalSlide = horizontalSlides[i];
  902. var verticalSlides = toArray( horizontalSlide.querySelectorAll( 'section' ) );
  903. for( var j = 0; j < verticalSlides.length; j++ ) {
  904. // Stop as soon as we arrive at the present
  905. if( verticalSlides[j].classList.contains( 'present' ) ) {
  906. break mainLoop;
  907. }
  908. pastCount++;
  909. }
  910. // Stop as soon as we arrive at the present
  911. if( horizontalSlide.classList.contains( 'present' ) ) {
  912. break;
  913. }
  914. // Don't count the wrapping section for vertical slides
  915. if( horizontalSlide.classList.contains( 'stack' ) === false ) {
  916. pastCount++;
  917. }
  918. }
  919. dom.progressbar.style.width = ( pastCount / ( totalCount - 1 ) ) * window.innerWidth + 'px';
  920. }
  921. }
  922. /**
  923. * Updates the state of all control/navigation arrows.
  924. */
  925. function updateControls() {
  926. if ( config.controls && dom.controls ) {
  927. var routes = availableRoutes();
  928. // Remove the 'enabled' class from all directions
  929. dom.controlsLeft.concat( dom.controlsRight )
  930. .concat( dom.controlsUp )
  931. .concat( dom.controlsDown )
  932. .concat( dom.controlsPrev )
  933. .concat( dom.controlsNext ).forEach( function( node ) {
  934. node.classList.remove( 'enabled' );
  935. } );
  936. // Add the 'enabled' class to the available routes
  937. if( routes.left ) dom.controlsLeft.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  938. if( routes.right ) dom.controlsRight.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  939. if( routes.up ) dom.controlsUp.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  940. if( routes.down ) dom.controlsDown.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  941. // Prev/next buttons
  942. if( routes.left || routes.up ) dom.controlsPrev.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  943. if( routes.right || routes.down ) dom.controlsNext.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  944. }
  945. }
  946. /**
  947. * Determine what available routes there are for navigation.
  948. *
  949. * @return {Object} containing four booleans: left/right/up/down
  950. */
  951. function availableRoutes() {
  952. var horizontalSlides = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ),
  953. verticalSlides = document.querySelectorAll( VERTICAL_SLIDES_SELECTOR );
  954. return {
  955. left: indexh > 0,
  956. right: indexh < horizontalSlides.length - 1,
  957. up: indexv > 0,
  958. down: indexv < verticalSlides.length - 1
  959. };
  960. }
  961. /**
  962. * Reads the current URL (hash) and navigates accordingly.
  963. */
  964. function readURL() {
  965. var hash = window.location.hash;
  966. // Attempt to parse the hash as either an index or name
  967. var bits = hash.slice( 2 ).split( '/' ),
  968. name = hash.replace( /#|\//gi, '' );
  969. // If the first bit is invalid and there is a name we can
  970. // assume that this is a named link
  971. if( isNaN( parseInt( bits[0], 10 ) ) && name.length ) {
  972. // Find the slide with the specified name
  973. var element = document.querySelector( '#' + name );
  974. if( element ) {
  975. // Find the position of the named slide and navigate to it
  976. var indices = Reveal.getIndices( element );
  977. slide( indices.h, indices.v );
  978. }
  979. // If the slide doesn't exist, navigate to the current slide
  980. else {
  981. slide( indexh, indexv );
  982. }
  983. }
  984. else {
  985. // Read the index components of the hash
  986. var h = parseInt( bits[0], 10 ) || 0,
  987. v = parseInt( bits[1], 10 ) || 0;
  988. slide( h, v );
  989. }
  990. }
  991. /**
  992. * Updates the page URL (hash) to reflect the current
  993. * state.
  994. *
  995. * @param {Number} delay The time in ms to wait before
  996. * writing the hash
  997. */
  998. function writeURL( delay ) {
  999. if( config.history ) {
  1000. // Make sure there's never more than one timeout running
  1001. clearTimeout( writeURLTimeout );
  1002. // If a delay is specified, timeout this call
  1003. if( typeof delay === 'number' ) {
  1004. writeURLTimeout = setTimeout( writeURL, delay );
  1005. }
  1006. else {
  1007. var url = '/';
  1008. // If the current slide has an ID, use that as a named link
  1009. if( currentSlide && typeof currentSlide.getAttribute( 'id' ) === 'string' ) {
  1010. url = '/' + currentSlide.getAttribute( 'id' );
  1011. }
  1012. // Otherwise use the /h/v index
  1013. else {
  1014. if( indexh > 0 || indexv > 0 ) url += indexh;
  1015. if( indexv > 0 ) url += '/' + indexv;
  1016. }
  1017. window.location.hash = url;
  1018. }
  1019. }
  1020. }
  1021. /**
  1022. * Retrieves the h/v location of the current, or specified,
  1023. * slide.
  1024. *
  1025. * @param {HTMLElement} slide If specified, the returned
  1026. * index will be for this slide rather than the currently
  1027. * active one
  1028. *
  1029. * @return {Object} { h: <int>, v: <int> }
  1030. */
  1031. function getIndices( slide ) {
  1032. // By default, return the current indices
  1033. var h = indexh,
  1034. v = indexv;
  1035. // If a slide is specified, return the indices of that slide
  1036. if( slide ) {
  1037. var isVertical = !!slide.parentNode.nodeName.match( /section/gi );
  1038. var slideh = isVertical ? slide.parentNode : slide;
  1039. // Select all horizontal slides
  1040. var horizontalSlides = toArray( document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) );
  1041. // Now that we know which the horizontal slide is, get its index
  1042. h = Math.max( horizontalSlides.indexOf( slideh ), 0 );
  1043. // If this is a vertical slide, grab the vertical index
  1044. if( isVertical ) {
  1045. v = Math.max( toArray( slide.parentNode.querySelectorAll( 'section' ) ).indexOf( slide ), 0 );
  1046. }
  1047. }
  1048. return { h: h, v: v };
  1049. }
  1050. /**
  1051. * Navigate to the next slide fragment.
  1052. *
  1053. * @return {Boolean} true if there was a next fragment,
  1054. * false otherwise
  1055. */
  1056. function nextFragment() {
  1057. // Vertical slides:
  1058. if( document.querySelector( VERTICAL_SLIDES_SELECTOR + '.present' ) ) {
  1059. var verticalFragments = document.querySelectorAll( VERTICAL_SLIDES_SELECTOR + '.present .fragment:not(.visible)' );
  1060. if( verticalFragments.length ) {
  1061. verticalFragments[0].classList.add( 'visible' );
  1062. // Notify subscribers of the change
  1063. dispatchEvent( 'fragmentshown', { fragment: verticalFragments[0] } );
  1064. return true;
  1065. }
  1066. }
  1067. // Horizontal slides:
  1068. else {
  1069. var horizontalFragments = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR + '.present .fragment:not(.visible)' );
  1070. if( horizontalFragments.length ) {
  1071. horizontalFragments[0].classList.add( 'visible' );
  1072. // Notify subscribers of the change
  1073. dispatchEvent( 'fragmentshown', { fragment: horizontalFragments[0] } );
  1074. return true;
  1075. }
  1076. }
  1077. return false;
  1078. }
  1079. /**
  1080. * Navigate to the previous slide fragment.
  1081. *
  1082. * @return {Boolean} true if there was a previous fragment,
  1083. * false otherwise
  1084. */
  1085. function previousFragment() {
  1086. // Vertical slides:
  1087. if( document.querySelector( VERTICAL_SLIDES_SELECTOR + '.present' ) ) {
  1088. var verticalFragments = document.querySelectorAll( VERTICAL_SLIDES_SELECTOR + '.present .fragment.visible' );
  1089. if( verticalFragments.length ) {
  1090. verticalFragments[ verticalFragments.length - 1 ].classList.remove( 'visible' );
  1091. // Notify subscribers of the change
  1092. dispatchEvent( 'fragmenthidden', { fragment: verticalFragments[ verticalFragments.length - 1 ] } );
  1093. return true;
  1094. }
  1095. }
  1096. // Horizontal slides:
  1097. else {
  1098. var horizontalFragments = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR + '.present .fragment.visible' );
  1099. if( horizontalFragments.length ) {
  1100. horizontalFragments[ horizontalFragments.length - 1 ].classList.remove( 'visible' );
  1101. // Notify subscribers of the change
  1102. dispatchEvent( 'fragmenthidden', { fragment: horizontalFragments[ horizontalFragments.length - 1 ] } );
  1103. return true;
  1104. }
  1105. }
  1106. return false;
  1107. }
  1108. /**
  1109. * Cues a new automated slide if enabled in the config.
  1110. */
  1111. function cueAutoSlide() {
  1112. clearTimeout( autoSlideTimeout );
  1113. // Cue the next auto-slide if enabled
  1114. if( autoSlide && !isPaused() && !isOverview() ) {
  1115. autoSlideTimeout = setTimeout( navigateNext, autoSlide );
  1116. }
  1117. }
  1118. /**
  1119. * Cancels any ongoing request to auto-slide.
  1120. */
  1121. function cancelAutoSlide() {
  1122. clearTimeout( autoSlideTimeout );
  1123. }
  1124. function navigateLeft() {
  1125. // Prioritize hiding fragments
  1126. if( availableRoutes().left && isOverview() || previousFragment() === false ) {
  1127. slide( indexh - 1 );
  1128. }
  1129. }
  1130. function navigateRight() {
  1131. // Prioritize revealing fragments
  1132. if( availableRoutes().right && isOverview() || nextFragment() === false ) {
  1133. slide( indexh + 1 );
  1134. }
  1135. }
  1136. function navigateUp() {
  1137. // Prioritize hiding fragments
  1138. if( availableRoutes().up && isOverview() || previousFragment() === false ) {
  1139. slide( indexh, indexv - 1 );
  1140. }
  1141. }
  1142. function navigateDown() {
  1143. // Prioritize revealing fragments
  1144. if( availableRoutes().down && isOverview() || nextFragment() === false ) {
  1145. slide( indexh, indexv + 1 );
  1146. }
  1147. }
  1148. /**
  1149. * Navigates backwards, prioritized in the following order:
  1150. * 1) Previous fragment
  1151. * 2) Previous vertical slide
  1152. * 3) Previous horizontal slide
  1153. */
  1154. function navigatePrev() {
  1155. // Prioritize revealing fragments
  1156. if( previousFragment() === false ) {
  1157. if( availableRoutes().up ) {
  1158. navigateUp();
  1159. }
  1160. else {
  1161. // Fetch the previous horizontal slide, if there is one
  1162. var previousSlide = document.querySelector( HORIZONTAL_SLIDES_SELECTOR + '.past:nth-child(' + indexh + ')' );
  1163. if( previousSlide ) {
  1164. indexv = ( previousSlide.querySelectorAll( 'section' ).length + 1 ) || undefined;
  1165. indexh --;
  1166. slide();
  1167. }
  1168. }
  1169. }
  1170. }
  1171. /**
  1172. * Same as #navigatePrev() but navigates forwards.
  1173. */
  1174. function navigateNext() {
  1175. // Prioritize revealing fragments
  1176. if( nextFragment() === false ) {
  1177. availableRoutes().down ? navigateDown() : navigateRight();
  1178. }
  1179. // If auto-sliding is enabled we need to cue up
  1180. // another timeout
  1181. cueAutoSlide();
  1182. }
  1183. // --------------------------------------------------------------------//
  1184. // ----------------------------- EVENTS -------------------------------//
  1185. // --------------------------------------------------------------------//
  1186. /**
  1187. * Handler for the document level 'keydown' event.
  1188. *
  1189. * @param {Object} event
  1190. */
  1191. function onDocumentKeyDown( event ) {
  1192. // Check if there's a focused element that could be using
  1193. // the keyboard
  1194. var activeElement = document.activeElement;
  1195. var hasFocus = !!( document.activeElement && ( document.activeElement.type || document.activeElement.href || document.activeElement.contentEditable !== 'inherit' ) );
  1196. // Disregard the event if there's a focused element or a
  1197. // keyboard modifier key is present
  1198. if( hasFocus || event.shiftKey || event.altKey || event.ctrlKey || event.metaKey ) return;
  1199. var triggered = true;
  1200. // while paused only allow "unpausing" keyboard events (b and .)
  1201. if( isPaused() && [66,190,191].indexOf( event.keyCode ) === -1 ) {
  1202. return false;
  1203. }
  1204. switch( event.keyCode ) {
  1205. // p, page up
  1206. case 80: case 33: navigatePrev(); break;
  1207. // n, page down
  1208. case 78: case 34: navigateNext(); break;
  1209. // h, left
  1210. case 72: case 37: navigateLeft(); break;
  1211. // l, right
  1212. case 76: case 39: navigateRight(); break;
  1213. // k, up
  1214. case 75: case 38: navigateUp(); break;
  1215. // j, down
  1216. case 74: case 40: navigateDown(); break;
  1217. // home
  1218. case 36: slide( 0 ); break;
  1219. // end
  1220. case 35: slide( Number.MAX_VALUE ); break;
  1221. // space
  1222. case 32: isOverview() ? deactivateOverview() : navigateNext(); break;
  1223. // return
  1224. case 13: isOverview() ? deactivateOverview() : triggered = false; break;
  1225. // b, period, Logitech presenter tools "black screen" button
  1226. case 66: case 190: case 191: togglePause(); break;
  1227. // f
  1228. case 70: enterFullscreen(); break;
  1229. default:
  1230. triggered = false;
  1231. }
  1232. // If the input resulted in a triggered action we should prevent
  1233. // the browsers default behavior
  1234. if( triggered ) {
  1235. event.preventDefault();
  1236. }
  1237. else if ( event.keyCode === 27 && supports3DTransforms ) {
  1238. toggleOverview();
  1239. event.preventDefault();
  1240. }
  1241. // If auto-sliding is enabled we need to cue up
  1242. // another timeout
  1243. cueAutoSlide();
  1244. }
  1245. /**
  1246. * Handler for the document level 'touchstart' event,
  1247. * enables support for swipe and pinch gestures.
  1248. */
  1249. function onDocumentTouchStart( event ) {
  1250. touch.startX = event.touches[0].clientX;
  1251. touch.startY = event.touches[0].clientY;
  1252. touch.startCount = event.touches.length;
  1253. // If there's two touches we need to memorize the distance
  1254. // between those two points to detect pinching
  1255. if( event.touches.length === 2 && config.overview ) {
  1256. touch.startSpan = distanceBetween( {
  1257. x: event.touches[1].clientX,
  1258. y: event.touches[1].clientY
  1259. }, {
  1260. x: touch.startX,
  1261. y: touch.startY
  1262. } );
  1263. }
  1264. }
  1265. /**
  1266. * Handler for the document level 'touchmove' event.
  1267. */
  1268. function onDocumentTouchMove( event ) {
  1269. // Each touch should only trigger one action
  1270. if( !touch.handled ) {
  1271. var currentX = event.touches[0].clientX;
  1272. var currentY = event.touches[0].clientY;
  1273. // If the touch started off with two points and still has
  1274. // two active touches; test for the pinch gesture
  1275. if( event.touches.length === 2 && touch.startCount === 2 && config.overview ) {
  1276. // The current distance in pixels between the two touch points
  1277. var currentSpan = distanceBetween( {
  1278. x: event.touches[1].clientX,
  1279. y: event.touches[1].clientY
  1280. }, {
  1281. x: touch.startX,
  1282. y: touch.startY
  1283. } );
  1284. // If the span is larger than the desire amount we've got
  1285. // ourselves a pinch
  1286. if( Math.abs( touch.startSpan - currentSpan ) > touch.threshold ) {
  1287. touch.handled = true;
  1288. if( currentSpan < touch.startSpan ) {
  1289. activateOverview();
  1290. }
  1291. else {
  1292. deactivateOverview();
  1293. }
  1294. }
  1295. event.preventDefault();
  1296. }
  1297. // There was only one touch point, look for a swipe
  1298. else if( event.touches.length === 1 && touch.startCount !== 2 ) {
  1299. var deltaX = currentX - touch.startX,
  1300. deltaY = currentY - touch.startY;
  1301. if( deltaX > touch.threshold && Math.abs( deltaX ) > Math.abs( deltaY ) ) {
  1302. touch.handled = true;
  1303. navigateLeft();
  1304. }
  1305. else if( deltaX < -touch.threshold && Math.abs( deltaX ) > Math.abs( deltaY ) ) {
  1306. touch.handled = true;
  1307. navigateRight();
  1308. }
  1309. else if( deltaY > touch.threshold ) {
  1310. touch.handled = true;
  1311. navigateUp();
  1312. }
  1313. else if( deltaY < -touch.threshold ) {
  1314. touch.handled = true;
  1315. navigateDown();
  1316. }
  1317. event.preventDefault();
  1318. }
  1319. }
  1320. // There's a bug with swiping on some Android devices unless
  1321. // the default action is always prevented
  1322. else if( navigator.userAgent.match( /android/gi ) ) {
  1323. event.preventDefault();
  1324. }
  1325. }
  1326. /**
  1327. * Handler for the document level 'touchend' event.
  1328. */
  1329. function onDocumentTouchEnd( event ) {
  1330. touch.handled = false;
  1331. }
  1332. /**
  1333. * Handles mouse wheel scrolling, throttled to avoid skipping
  1334. * multiple slides.
  1335. */
  1336. function onDocumentMouseScroll( event ) {
  1337. clearTimeout( mouseWheelTimeout );
  1338. mouseWheelTimeout = setTimeout( function() {
  1339. var delta = event.detail || -event.wheelDelta;
  1340. if( delta > 0 ) {
  1341. navigateNext();
  1342. }
  1343. else {
  1344. navigatePrev();
  1345. }
  1346. }, 100 );
  1347. }
  1348. /**
  1349. * Clicking on the progress bar results in a navigation to the
  1350. * closest approximate horizontal slide using this equation:
  1351. *
  1352. * ( clickX / presentationWidth ) * numberOfSlides
  1353. */
  1354. function onProgressClicked( event ) {
  1355. event.preventDefault();
  1356. var slidesTotal = toArray( document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) ).length;
  1357. var slideIndex = Math.floor( ( event.clientX / dom.wrapper.offsetWidth ) * slidesTotal );
  1358. slide( slideIndex );
  1359. }
  1360. /**
  1361. * Event handler for navigation control buttons.
  1362. */
  1363. function onNavigateLeftClicked( event ) { event.preventDefault(); navigateLeft(); }
  1364. function onNavigateRightClicked( event ) { event.preventDefault(); navigateRight(); }
  1365. function onNavigateUpClicked( event ) { event.preventDefault(); navigateUp(); }
  1366. function onNavigateDownClicked( event ) { event.preventDefault(); navigateDown(); }
  1367. function onNavigatePrevClicked( event ) { event.preventDefault(); navigatePrev(); }
  1368. function onNavigateNextClicked( event ) { event.preventDefault(); navigateNext(); }
  1369. /**
  1370. * Handler for the window level 'hashchange' event.
  1371. */
  1372. function onWindowHashChange( event ) {
  1373. readURL();
  1374. }
  1375. /**
  1376. * Handler for the window level 'resize' event.
  1377. */
  1378. function onWindowResize( event ) {
  1379. layout();
  1380. }
  1381. /**
  1382. * Invoked when a slide is and we're in the overview.
  1383. */
  1384. function onOverviewSlideClicked( event ) {
  1385. // TODO There's a bug here where the event listeners are not
  1386. // removed after deactivating the overview.
  1387. if( isOverview() ) {
  1388. event.preventDefault();
  1389. deactivateOverview();
  1390. var element = event.target;
  1391. while( element && !element.nodeName.match( /section/gi ) ) {
  1392. element = element.parentNode;
  1393. }
  1394. if( element.nodeName.match( /section/gi ) ) {
  1395. var h = parseInt( element.getAttribute( 'data-index-h' ), 10 ),
  1396. v = parseInt( element.getAttribute( 'data-index-v' ), 10 );
  1397. slide( h, v );
  1398. }
  1399. }
  1400. }
  1401. // --------------------------------------------------------------------//
  1402. // ------------------------------- API --------------------------------//
  1403. // --------------------------------------------------------------------//
  1404. return {
  1405. initialize: initialize,
  1406. // Navigation methods
  1407. slide: slide,
  1408. left: navigateLeft,
  1409. right: navigateRight,
  1410. up: navigateUp,
  1411. down: navigateDown,
  1412. prev: navigatePrev,
  1413. next: navigateNext,
  1414. prevFragment: previousFragment,
  1415. nextFragment: nextFragment,
  1416. // Deprecated aliases
  1417. navigateTo: slide,
  1418. navigateLeft: navigateLeft,
  1419. navigateRight: navigateRight,
  1420. navigateUp: navigateUp,
  1421. navigateDown: navigateDown,
  1422. navigatePrev: navigatePrev,
  1423. navigateNext: navigateNext,
  1424. // Forces an update in slide layout
  1425. layout: layout,
  1426. // Toggles the overview mode on/off
  1427. toggleOverview: toggleOverview,
  1428. // Toggles the "black screen" mode on/off
  1429. togglePause: togglePause,
  1430. // State checks
  1431. isOverview: isOverview,
  1432. isPaused: isPaused,
  1433. // Adds or removes all internal event listeners (such as keyboard)
  1434. addEventListeners: addEventListeners,
  1435. removeEventListeners: removeEventListeners,
  1436. // Returns the indices of the current, or specified, slide
  1437. getIndices: getIndices,
  1438. // Returns the slide at the specified index, y is optional
  1439. getSlide: function( x, y ) {
  1440. var horizontalSlide = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR )[ x ];
  1441. var verticalSlides = horizontalSlide && horizontalSlide.querySelectorAll( 'section' );
  1442. if( typeof y !== 'undefined' ) {
  1443. return verticalSlides ? verticalSlides[ y ] : undefined;
  1444. }
  1445. return horizontalSlide;
  1446. },
  1447. // Returns the previous slide element, may be null
  1448. getPreviousSlide: function() {
  1449. return previousSlide;
  1450. },
  1451. // Returns the current slide element
  1452. getCurrentSlide: function() {
  1453. return currentSlide;
  1454. },
  1455. // Returns the current scale of the presentation content
  1456. getScale: function() {
  1457. return scale;
  1458. },
  1459. // Helper method, retrieves query string as a key/value hash
  1460. getQueryHash: function() {
  1461. var query = {};
  1462. location.search.replace( /[A-Z0-9]+?=(\w*)/gi, function(a) {
  1463. query[ a.split( '=' ).shift() ] = a.split( '=' ).pop();
  1464. } );
  1465. return query;
  1466. },
  1467. // Forward event binding to the reveal DOM element
  1468. addEventListener: function( type, listener, useCapture ) {
  1469. if( 'addEventListener' in window ) {
  1470. ( dom.wrapper || document.querySelector( '.reveal' ) ).addEventListener( type, listener, useCapture );
  1471. }
  1472. },
  1473. removeEventListener: function( type, listener, useCapture ) {
  1474. if( 'addEventListener' in window ) {
  1475. ( dom.wrapper || document.querySelector( '.reveal' ) ).removeEventListener( type, listener, useCapture );
  1476. }
  1477. }
  1478. };
  1479. })();