reveal.js 71 KB

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