reveal.js 138 KB

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