reveal.js 57 KB

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