reveal.js 98 KB

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