reveal.js 148 KB

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