reveal.js 143 KB

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