reveal.js 50 KB

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