reveal.js 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075
  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. * @param {int} o Optional origin for use in multimaster environments
  788. */
  789. function slide( h, v, f, o ) {
  790. // Remember where we were at before
  791. previousSlide = currentSlide;
  792. // Query all horizontal slides in the deck
  793. var horizontalSlides = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR );
  794. // If no vertical index is specified and the upcoming slide is a
  795. // stack, resume at its previous vertical index
  796. if( v === undefined ) {
  797. v = getPreviousVerticalIndex( horizontalSlides[ h ] );
  798. }
  799. // If we were on a vertical stack, remember what vertical index
  800. // it was on so we can resume at the same position when returning
  801. if( previousSlide && previousSlide.parentNode && previousSlide.parentNode.classList.contains( 'stack' ) ) {
  802. setPreviousVerticalIndex( previousSlide.parentNode, indexv );
  803. }
  804. // Remember the state before this slide
  805. var stateBefore = state.concat();
  806. // Reset the state array
  807. state.length = 0;
  808. var indexhBefore = indexh,
  809. indexvBefore = indexv;
  810. // Activate and transition to the new slide
  811. indexh = updateSlides( HORIZONTAL_SLIDES_SELECTOR, h === undefined ? indexh : h );
  812. indexv = updateSlides( VERTICAL_SLIDES_SELECTOR, v === undefined ? indexv : v );
  813. layout();
  814. // Apply the new state
  815. stateLoop: for( var i = 0, len = state.length; i < len; i++ ) {
  816. // Check if this state existed on the previous slide. If it
  817. // did, we will avoid adding it repeatedly
  818. for( var j = 0; j < stateBefore.length; j++ ) {
  819. if( stateBefore[j] === state[i] ) {
  820. stateBefore.splice( j, 1 );
  821. continue stateLoop;
  822. }
  823. }
  824. document.documentElement.classList.add( state[i] );
  825. // Dispatch custom event matching the state's name
  826. dispatchEvent( state[i] );
  827. }
  828. // Clean up the remains of the previous state
  829. while( stateBefore.length ) {
  830. document.documentElement.classList.remove( stateBefore.pop() );
  831. }
  832. // If the overview is active, re-activate it to update positions
  833. if( isOverview() ) {
  834. activateOverview();
  835. }
  836. // Update the URL hash after a delay since updating it mid-transition
  837. // is likely to cause visual lag
  838. writeURL( 1500 );
  839. // Find the current horizontal slide and any possible vertical slides
  840. // within it
  841. var currentHorizontalSlide = horizontalSlides[ indexh ],
  842. currentVerticalSlides = currentHorizontalSlide.querySelectorAll( 'section' );
  843. // Store references to the previous and current slides
  844. currentSlide = currentVerticalSlides[ indexv ] || currentHorizontalSlide;
  845. // Show fragment, if specified
  846. if( typeof f !== 'undefined' ) {
  847. var fragments = sortFragments( currentSlide.querySelectorAll( '.fragment' ) );
  848. toArray( fragments ).forEach( function( fragment, indexf ) {
  849. if( indexf < f ) {
  850. fragment.classList.add( 'visible' );
  851. }
  852. else {
  853. fragment.classList.remove( 'visible' );
  854. }
  855. } );
  856. }
  857. // Dispatch an event if the slide changed
  858. if( indexh !== indexhBefore || indexv !== indexvBefore ) {
  859. dispatchEvent( 'slidechanged', {
  860. 'indexh': indexh,
  861. 'indexv': indexv,
  862. 'previousSlide': previousSlide,
  863. 'currentSlide': currentSlide,
  864. 'origin': o
  865. } );
  866. }
  867. else {
  868. // Ensure that the previous slide is never the same as the current
  869. previousSlide = null;
  870. }
  871. // Solves an edge case where the previous slide maintains the
  872. // 'present' class when navigating between adjacent vertical
  873. // stacks
  874. if( previousSlide ) {
  875. previousSlide.classList.remove( 'present' );
  876. // Reset all slides upon navigate to home
  877. // Issue: #285
  878. if ( document.querySelector( HOME_SLIDE_SELECTOR ).classList.contains( 'present' ) ) {
  879. // Launch async task
  880. setTimeout( function () {
  881. var slides = toArray( document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR + '.stack') ), i;
  882. for( i in slides ) {
  883. if( slides[i] ) {
  884. // Reset stack
  885. setPreviousVerticalIndex( slides[i], 0 );
  886. }
  887. }
  888. }, 0 );
  889. }
  890. }
  891. updateControls();
  892. updateProgress();
  893. }
  894. /**
  895. * Updates one dimension of slides by showing the slide
  896. * with the specified index.
  897. *
  898. * @param {String} selector A CSS selector that will fetch
  899. * the group of slides we are working with
  900. * @param {Number} index The index of the slide that should be
  901. * shown
  902. *
  903. * @return {Number} The index of the slide that is now shown,
  904. * might differ from the passed in index if it was out of
  905. * bounds.
  906. */
  907. function updateSlides( selector, index ) {
  908. // Select all slides and convert the NodeList result to
  909. // an array
  910. var slides = toArray( document.querySelectorAll( selector ) ),
  911. slidesLength = slides.length;
  912. if( slidesLength ) {
  913. // Should the index loop?
  914. if( config.loop ) {
  915. index %= slidesLength;
  916. if( index < 0 ) {
  917. index = slidesLength + index;
  918. }
  919. }
  920. // Enforce max and minimum index bounds
  921. index = Math.max( Math.min( index, slidesLength - 1 ), 0 );
  922. for( var i = 0; i < slidesLength; i++ ) {
  923. var element = slides[i];
  924. // Optimization; hide all slides that are three or more steps
  925. // away from the present slide
  926. if( isOverview() === false ) {
  927. // The distance loops so that it measures 1 between the first
  928. // and last slides
  929. var distance = Math.abs( ( index - i ) % ( slidesLength - 3 ) ) || 0;
  930. element.style.display = distance > 3 ? 'none' : 'block';
  931. }
  932. slides[i].classList.remove( 'past' );
  933. slides[i].classList.remove( 'present' );
  934. slides[i].classList.remove( 'future' );
  935. if( i < index ) {
  936. // Any element previous to index is given the 'past' class
  937. slides[i].classList.add( 'past' );
  938. }
  939. else if( i > index ) {
  940. // Any element subsequent to index is given the 'future' class
  941. slides[i].classList.add( 'future' );
  942. }
  943. // If this element contains vertical slides
  944. if( element.querySelector( 'section' ) ) {
  945. slides[i].classList.add( 'stack' );
  946. }
  947. }
  948. // Mark the current slide as present
  949. slides[index].classList.add( 'present' );
  950. // If this slide has a state associated with it, add it
  951. // onto the current state of the deck
  952. var slideState = slides[index].getAttribute( 'data-state' );
  953. if( slideState ) {
  954. state = state.concat( slideState.split( ' ' ) );
  955. }
  956. // If this slide has a data-autoslide attribtue associated use this as
  957. // autoSlide value otherwise use the global configured time
  958. var slideAutoSlide = slides[index].getAttribute( 'data-autoslide' );
  959. if( slideAutoSlide ) {
  960. autoSlide = parseInt( slideAutoSlide, 10 );
  961. }
  962. else {
  963. autoSlide = config.autoSlide;
  964. }
  965. }
  966. else {
  967. // Since there are no slides we can't be anywhere beyond the
  968. // zeroth index
  969. index = 0;
  970. }
  971. return index;
  972. }
  973. /**
  974. * Updates the progress bar to reflect the current slide.
  975. */
  976. function updateProgress() {
  977. // Update progress if enabled
  978. if( config.progress && dom.progress ) {
  979. var horizontalSlides = toArray( document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) );
  980. // The number of past and total slides
  981. var totalCount = document.querySelectorAll( SLIDES_SELECTOR + ':not(.stack)' ).length;
  982. var pastCount = 0;
  983. // Step through all slides and count the past ones
  984. mainLoop: for( var i = 0; i < horizontalSlides.length; i++ ) {
  985. var horizontalSlide = horizontalSlides[i];
  986. var verticalSlides = toArray( horizontalSlide.querySelectorAll( 'section' ) );
  987. for( var j = 0; j < verticalSlides.length; j++ ) {
  988. // Stop as soon as we arrive at the present
  989. if( verticalSlides[j].classList.contains( 'present' ) ) {
  990. break mainLoop;
  991. }
  992. pastCount++;
  993. }
  994. // Stop as soon as we arrive at the present
  995. if( horizontalSlide.classList.contains( 'present' ) ) {
  996. break;
  997. }
  998. // Don't count the wrapping section for vertical slides
  999. if( horizontalSlide.classList.contains( 'stack' ) === false ) {
  1000. pastCount++;
  1001. }
  1002. }
  1003. dom.progressbar.style.width = ( pastCount / ( totalCount - 1 ) ) * window.innerWidth + 'px';
  1004. }
  1005. }
  1006. /**
  1007. * Updates the state of all control/navigation arrows.
  1008. */
  1009. function updateControls() {
  1010. if ( config.controls && dom.controls ) {
  1011. var routes = availableRoutes();
  1012. // Remove the 'enabled' class from all directions
  1013. dom.controlsLeft.concat( dom.controlsRight )
  1014. .concat( dom.controlsUp )
  1015. .concat( dom.controlsDown )
  1016. .concat( dom.controlsPrev )
  1017. .concat( dom.controlsNext ).forEach( function( node ) {
  1018. node.classList.remove( 'enabled' );
  1019. } );
  1020. // Add the 'enabled' class to the available routes
  1021. if( routes.left ) dom.controlsLeft.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  1022. if( routes.right ) dom.controlsRight.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  1023. if( routes.up ) dom.controlsUp.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  1024. if( routes.down ) dom.controlsDown.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  1025. // Prev/next buttons
  1026. if( routes.left || routes.up ) dom.controlsPrev.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  1027. if( routes.right || routes.down ) dom.controlsNext.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  1028. }
  1029. }
  1030. /**
  1031. * Determine what available routes there are for navigation.
  1032. *
  1033. * @return {Object} containing four booleans: left/right/up/down
  1034. */
  1035. function availableRoutes() {
  1036. var horizontalSlides = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ),
  1037. verticalSlides = document.querySelectorAll( VERTICAL_SLIDES_SELECTOR );
  1038. return {
  1039. left: indexh > 0 || config.loop,
  1040. right: indexh < horizontalSlides.length - 1 || config.loop,
  1041. up: indexv > 0,
  1042. down: indexv < verticalSlides.length - 1
  1043. };
  1044. }
  1045. /**
  1046. * Reads the current URL (hash) and navigates accordingly.
  1047. */
  1048. function readURL() {
  1049. var hash = window.location.hash;
  1050. // Attempt to parse the hash as either an index or name
  1051. var bits = hash.slice( 2 ).split( '/' ),
  1052. name = hash.replace( /#|\//gi, '' );
  1053. // If the first bit is invalid and there is a name we can
  1054. // assume that this is a named link
  1055. if( isNaN( parseInt( bits[0], 10 ) ) && name.length ) {
  1056. // Find the slide with the specified name
  1057. var element = document.querySelector( '#' + name );
  1058. if( element ) {
  1059. // Find the position of the named slide and navigate to it
  1060. var indices = Reveal.getIndices( element );
  1061. slide( indices.h, indices.v );
  1062. }
  1063. // If the slide doesn't exist, navigate to the current slide
  1064. else {
  1065. slide( indexh, indexv );
  1066. }
  1067. }
  1068. else {
  1069. // Read the index components of the hash
  1070. var h = parseInt( bits[0], 10 ) || 0,
  1071. v = parseInt( bits[1], 10 ) || 0;
  1072. slide( h, v );
  1073. }
  1074. }
  1075. /**
  1076. * Updates the page URL (hash) to reflect the current
  1077. * state.
  1078. *
  1079. * @param {Number} delay The time in ms to wait before
  1080. * writing the hash
  1081. */
  1082. function writeURL( delay ) {
  1083. if( config.history ) {
  1084. // Make sure there's never more than one timeout running
  1085. clearTimeout( writeURLTimeout );
  1086. // If a delay is specified, timeout this call
  1087. if( typeof delay === 'number' ) {
  1088. writeURLTimeout = setTimeout( writeURL, delay );
  1089. }
  1090. else {
  1091. var url = '/';
  1092. // If the current slide has an ID, use that as a named link
  1093. if( currentSlide && typeof currentSlide.getAttribute( 'id' ) === 'string' ) {
  1094. url = '/' + currentSlide.getAttribute( 'id' );
  1095. }
  1096. // Otherwise use the /h/v index
  1097. else {
  1098. if( indexh > 0 || indexv > 0 ) url += indexh;
  1099. if( indexv > 0 ) url += '/' + indexv;
  1100. }
  1101. window.location.hash = url;
  1102. }
  1103. }
  1104. }
  1105. /**
  1106. * Retrieves the h/v location of the current, or specified,
  1107. * slide.
  1108. *
  1109. * @param {HTMLElement} slide If specified, the returned
  1110. * index will be for this slide rather than the currently
  1111. * active one
  1112. *
  1113. * @return {Object} { h: <int>, v: <int> }
  1114. */
  1115. function getIndices( slide ) {
  1116. // By default, return the current indices
  1117. var h = indexh,
  1118. v = indexv;
  1119. // If a slide is specified, return the indices of that slide
  1120. if( slide ) {
  1121. var isVertical = !!slide.parentNode.nodeName.match( /section/gi );
  1122. var slideh = isVertical ? slide.parentNode : slide;
  1123. // Select all horizontal slides
  1124. var horizontalSlides = toArray( document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) );
  1125. // Now that we know which the horizontal slide is, get its index
  1126. h = Math.max( horizontalSlides.indexOf( slideh ), 0 );
  1127. // If this is a vertical slide, grab the vertical index
  1128. if( isVertical ) {
  1129. v = Math.max( toArray( slide.parentNode.querySelectorAll( 'section' ) ).indexOf( slide ), 0 );
  1130. }
  1131. }
  1132. return { h: h, v: v };
  1133. }
  1134. /**
  1135. * Navigate to the next slide fragment.
  1136. *
  1137. * @return {Boolean} true if there was a next fragment,
  1138. * false otherwise
  1139. */
  1140. function nextFragment() {
  1141. // Vertical slides:
  1142. if( document.querySelector( VERTICAL_SLIDES_SELECTOR + '.present' ) ) {
  1143. var verticalFragments = sortFragments( document.querySelectorAll( VERTICAL_SLIDES_SELECTOR + '.present .fragment:not(.visible)' ) );
  1144. if( verticalFragments.length ) {
  1145. verticalFragments[0].classList.add( 'visible' );
  1146. // Notify subscribers of the change
  1147. dispatchEvent( 'fragmentshown', { fragment: verticalFragments[0] } );
  1148. return true;
  1149. }
  1150. }
  1151. // Horizontal slides:
  1152. else {
  1153. var horizontalFragments = sortFragments( document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR + '.present .fragment:not(.visible)' ) );
  1154. if( horizontalFragments.length ) {
  1155. horizontalFragments[0].classList.add( 'visible' );
  1156. // Notify subscribers of the change
  1157. dispatchEvent( 'fragmentshown', { fragment: horizontalFragments[0] } );
  1158. return true;
  1159. }
  1160. }
  1161. return false;
  1162. }
  1163. /**
  1164. * Navigate to the previous slide fragment.
  1165. *
  1166. * @return {Boolean} true if there was a previous fragment,
  1167. * false otherwise
  1168. */
  1169. function previousFragment() {
  1170. // Vertical slides:
  1171. if( document.querySelector( VERTICAL_SLIDES_SELECTOR + '.present' ) ) {
  1172. var verticalFragments = sortFragments( document.querySelectorAll( VERTICAL_SLIDES_SELECTOR + '.present .fragment.visible' ) );
  1173. if( verticalFragments.length ) {
  1174. verticalFragments[ verticalFragments.length - 1 ].classList.remove( 'visible' );
  1175. // Notify subscribers of the change
  1176. dispatchEvent( 'fragmenthidden', { fragment: verticalFragments[ verticalFragments.length - 1 ] } );
  1177. return true;
  1178. }
  1179. }
  1180. // Horizontal slides:
  1181. else {
  1182. var horizontalFragments = sortFragments( document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR + '.present .fragment.visible' ) );
  1183. if( horizontalFragments.length ) {
  1184. horizontalFragments[ horizontalFragments.length - 1 ].classList.remove( 'visible' );
  1185. // Notify subscribers of the change
  1186. dispatchEvent( 'fragmenthidden', { fragment: horizontalFragments[ horizontalFragments.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( availableRoutes().left && ( isOverview() || previousFragment() === false ) ) {
  1211. slide( indexh - 1 );
  1212. }
  1213. }
  1214. function navigateRight() {
  1215. // Prioritize revealing fragments
  1216. if( availableRoutes().right && ( isOverview() || nextFragment() === false ) ) {
  1217. slide( indexh + 1 );
  1218. }
  1219. }
  1220. function navigateUp() {
  1221. // Prioritize hiding fragments
  1222. if( availableRoutes().up && isOverview() || previousFragment() === false ) {
  1223. slide( indexh, indexv - 1 );
  1224. }
  1225. }
  1226. function navigateDown() {
  1227. // Prioritize revealing fragments
  1228. if( availableRoutes().down && isOverview() || nextFragment() === false ) {
  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();
  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. // Navigation methods
  1521. slide: slide,
  1522. left: navigateLeft,
  1523. right: navigateRight,
  1524. up: navigateUp,
  1525. down: navigateDown,
  1526. prev: navigatePrev,
  1527. next: navigateNext,
  1528. prevFragment: previousFragment,
  1529. nextFragment: nextFragment,
  1530. // Deprecated aliases
  1531. navigateTo: slide,
  1532. navigateLeft: navigateLeft,
  1533. navigateRight: navigateRight,
  1534. navigateUp: navigateUp,
  1535. navigateDown: navigateDown,
  1536. navigatePrev: navigatePrev,
  1537. navigateNext: navigateNext,
  1538. // Forces an update in slide layout
  1539. layout: layout,
  1540. // Toggles the overview mode on/off
  1541. toggleOverview: toggleOverview,
  1542. // Toggles the "black screen" mode on/off
  1543. togglePause: togglePause,
  1544. // State checks
  1545. isOverview: isOverview,
  1546. isPaused: isPaused,
  1547. // Adds or removes all internal event listeners (such as keyboard)
  1548. addEventListeners: addEventListeners,
  1549. removeEventListeners: removeEventListeners,
  1550. // Returns the indices of the current, or specified, slide
  1551. getIndices: getIndices,
  1552. // Returns the slide at the specified index, y is optional
  1553. getSlide: function( x, y ) {
  1554. var horizontalSlide = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR )[ x ];
  1555. var verticalSlides = horizontalSlide && horizontalSlide.querySelectorAll( 'section' );
  1556. if( typeof y !== 'undefined' ) {
  1557. return verticalSlides ? verticalSlides[ y ] : undefined;
  1558. }
  1559. return horizontalSlide;
  1560. },
  1561. // Returns the previous slide element, may be null
  1562. getPreviousSlide: function() {
  1563. return previousSlide;
  1564. },
  1565. // Returns the current slide element
  1566. getCurrentSlide: function() {
  1567. return currentSlide;
  1568. },
  1569. // Returns the current scale of the presentation content
  1570. getScale: function() {
  1571. return scale;
  1572. },
  1573. // Returns the current configuration object
  1574. getConfig: function() {
  1575. return config;
  1576. },
  1577. // Helper method, retrieves query string as a key/value hash
  1578. getQueryHash: function() {
  1579. var query = {};
  1580. location.search.replace( /[A-Z0-9]+?=(\w*)/gi, function(a) {
  1581. query[ a.split( '=' ).shift() ] = a.split( '=' ).pop();
  1582. } );
  1583. return query;
  1584. },
  1585. // Returns true if we're currently on the first slide
  1586. isFirstSlide: function() {
  1587. return document.querySelector( SLIDES_SELECTOR + '.past' ) == null ? true : false;
  1588. },
  1589. // Returns true if we're currently on the last slide
  1590. isLastSlide: function() {
  1591. if( currentSlide && currentSlide.classList.contains( '.stack' ) ) {
  1592. return currentSlide.querySelector( SLIDES_SELECTOR + '.future' ) == null ? true : false;
  1593. }
  1594. else {
  1595. return document.querySelector( SLIDES_SELECTOR + '.future' ) == null ? true : false;
  1596. }
  1597. },
  1598. // Forward event binding to the reveal DOM element
  1599. addEventListener: function( type, listener, useCapture ) {
  1600. if( 'addEventListener' in window ) {
  1601. ( dom.wrapper || document.querySelector( '.reveal' ) ).addEventListener( type, listener, useCapture );
  1602. }
  1603. },
  1604. removeEventListener: function( type, listener, useCapture ) {
  1605. if( 'addEventListener' in window ) {
  1606. ( dom.wrapper || document.querySelector( '.reveal' ) ).removeEventListener( type, listener, useCapture );
  1607. }
  1608. }
  1609. };
  1610. })();