reveal.js 86 KB

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