reveal.js 56 KB

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