reveal.js 65 KB

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