reveal.js 104 KB

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