reveal.js 128 KB

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