reveal.js 127 KB

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