reveal.js 48 KB

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