reveal.js 57 KB

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