reveal.js 56 KB

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