reveal.js 53 KB

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