reveal.js 55 KB

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