reveal.js 72 KB

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