reveal.js 54 KB

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