reveal.js 57 KB

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