reveal.js 56 KB

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