reveal.js 56 KB

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