reveal.js 115 KB

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