reveal.js 157 KB

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