reveal.js 93 KB

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