reveal.js 139 KB

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