reveal.js 137 KB

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