reveal.js 74 KB

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