reveal.js 102 KB

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