reveal.js 50 KB

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