reveal.js 49 KB

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