reveal.js 54 KB

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