reveal.js 142 KB

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