reveal.js 139 KB

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