reveal.js 52 KB

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