reveal.js 145 KB

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