reveal.js 129 KB

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