reveal.js 74 KB

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