reveal.js 129 KB

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