reveal.js 137 KB

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