reveal.js 85 KB

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