reveal.js 80 KB

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