reveal.js 52 KB

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