reveal.js 54 KB

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