reveal.js 108 KB

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