reveal.js 54 KB

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