reveal.js 121 KB

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