reveal.js 54 KB

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