reveal.js 56 KB

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