reveal.js 125 KB

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