reveal.js 143 KB

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