reveal.js 93 KB

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