reveal.js 147 KB

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