reveal.js 54 KB

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