reveal.js 105 KB

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