reveal.js 54 KB

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