reveal.js 117 KB

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