reveal.js 51 KB

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