reveal.js 100 KB

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