reveal.js 83 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149
  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 centering 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: true,
  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 horizontal and vertical index of the currently active slide
  84. indexh,
  85. indexv,
  86. // The previous and current slide HTML elements
  87. previousSlide,
  88. currentSlide,
  89. // Slides may hold a data-state attribute which we pick up and apply
  90. // as a class to the body. This list contains the combined state of
  91. // all current slides.
  92. state = [],
  93. // The current scale of the presentation (see width/height config)
  94. scale = 1,
  95. // Cached references to DOM elements
  96. dom = {},
  97. // Features supported by the browser, see #checkCapabilities()
  98. features = {},
  99. // Client is a mobile device, see #checkCapabilities()
  100. isMobileDevice,
  101. // Throttles mouse wheel navigation
  102. lastMouseWheelStep = 0,
  103. // Delays updates to the URL due to a Chrome thumbnailer bug
  104. writeURLTimeout = 0,
  105. // A delay used to activate the overview mode
  106. activateOverviewTimeout = 0,
  107. // A delay used to deactivate the overview mode
  108. deactivateOverviewTimeout = 0,
  109. // Flags if the interaction event listeners are bound
  110. eventsAreBound = false,
  111. // The current auto-slide duration
  112. autoSlide = 0,
  113. // Auto slide properties
  114. autoSlidePlayer,
  115. autoSlideTimeout = 0,
  116. autoSlideStartTime = -1,
  117. autoSlidePaused = false,
  118. // Holds information about the currently ongoing touch input
  119. touch = {
  120. startX: 0,
  121. startY: 0,
  122. startSpan: 0,
  123. startCount: 0,
  124. captured: false,
  125. threshold: 40
  126. };
  127. /**
  128. * Starts up the presentation if the client is capable.
  129. */
  130. function initialize( options ) {
  131. checkCapabilities();
  132. if( !features.transforms2d && !features.transforms3d ) {
  133. document.body.setAttribute( 'class', 'no-transforms' );
  134. // If the browser doesn't support core features we won't be
  135. // using JavaScript to control the presentation
  136. return;
  137. }
  138. // Force a layout when the whole page, incl fonts, has loaded
  139. window.addEventListener( 'load', layout, false );
  140. var query = Reveal.getQueryHash();
  141. // Do not accept new dependencies via query config to avoid
  142. // the potential of malicious script injection
  143. if( typeof query['dependencies'] !== 'undefined' ) delete query['dependencies'];
  144. // Copy options over to our config object
  145. extend( config, options );
  146. extend( config, query );
  147. // Hide the address bar in mobile browsers
  148. hideAddressBar();
  149. // Loads the dependencies and continues to #start() once done
  150. load();
  151. }
  152. /**
  153. * Inspect the client to see what it's capable of, this
  154. * should only happens once per runtime.
  155. */
  156. function checkCapabilities() {
  157. features.transforms3d = 'WebkitPerspective' in document.body.style ||
  158. 'MozPerspective' in document.body.style ||
  159. 'msPerspective' in document.body.style ||
  160. 'OPerspective' in document.body.style ||
  161. 'perspective' in document.body.style;
  162. features.transforms2d = 'WebkitTransform' in document.body.style ||
  163. 'MozTransform' in document.body.style ||
  164. 'msTransform' in document.body.style ||
  165. 'OTransform' in document.body.style ||
  166. 'transform' in document.body.style;
  167. features.requestAnimationFrameMethod = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame;
  168. features.requestAnimationFrame = typeof features.requestAnimationFrameMethod === 'function';
  169. features.canvas = !!document.createElement( 'canvas' ).getContext;
  170. isMobileDevice = navigator.userAgent.match( /(iphone|ipod|android)/gi );
  171. }
  172. /**
  173. * Loads the dependencies of reveal.js. Dependencies are
  174. * defined via the configuration option 'dependencies'
  175. * and will be loaded prior to starting/binding reveal.js.
  176. * Some dependencies may have an 'async' flag, if so they
  177. * will load after reveal.js has been started up.
  178. */
  179. function load() {
  180. var scripts = [],
  181. scriptsAsync = [];
  182. for( var i = 0, len = config.dependencies.length; i < len; i++ ) {
  183. var s = config.dependencies[i];
  184. // Load if there's no condition or the condition is truthy
  185. if( !s.condition || s.condition() ) {
  186. if( s.async ) {
  187. scriptsAsync.push( s.src );
  188. }
  189. else {
  190. scripts.push( s.src );
  191. }
  192. // Extension may contain callback functions
  193. if( typeof s.callback === 'function' ) {
  194. head.ready( s.src.match( /([\w\d_\-]*)\.?js$|[^\\\/]*$/i )[0], s.callback );
  195. }
  196. }
  197. }
  198. // Called once synchronous scripts finish loading
  199. function proceed() {
  200. if( scriptsAsync.length ) {
  201. // Load asynchronous scripts
  202. head.js.apply( null, scriptsAsync );
  203. }
  204. start();
  205. }
  206. if( scripts.length ) {
  207. head.ready( proceed );
  208. // Load synchronous scripts
  209. head.js.apply( null, scripts );
  210. }
  211. else {
  212. proceed();
  213. }
  214. }
  215. /**
  216. * Starts up reveal.js by binding input events and navigating
  217. * to the current URL deeplink if there is one.
  218. */
  219. function start() {
  220. // Make sure we've got all the DOM elements we need
  221. setupDOM();
  222. // Decorate the slide DOM elements with state classes (past/future)
  223. setupSlides();
  224. // Updates the presentation to match the current configuration values
  225. configure();
  226. // Read the initial hash
  227. readURL();
  228. // Notify listeners that the presentation is ready but use a 1ms
  229. // timeout to ensure it's not fired synchronously after #initialize()
  230. setTimeout( function() {
  231. // Enable transitions now that we're loaded
  232. dom.slides.classList.remove( 'no-transition' );
  233. loaded = true;
  234. dispatchEvent( 'ready', {
  235. 'indexh': indexh,
  236. 'indexv': indexv,
  237. 'currentSlide': currentSlide
  238. } );
  239. }, 1 );
  240. }
  241. /**
  242. * Iterates through and decorates slides DOM elements with
  243. * appropriate classes.
  244. */
  245. function setupSlides() {
  246. var horizontalSlides = toArray( document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) );
  247. horizontalSlides.forEach( function( horizontalSlide ) {
  248. var verticalSlides = toArray( horizontalSlide.querySelectorAll( 'section' ) );
  249. verticalSlides.forEach( function( verticalSlide, y ) {
  250. if( y > 0 ) verticalSlide.classList.add( 'future' );
  251. } );
  252. } );
  253. }
  254. /**
  255. * Finds and stores references to DOM elements which are
  256. * required by the presentation. If a required element is
  257. * not found, it is created.
  258. */
  259. function setupDOM() {
  260. // Cache references to key DOM elements
  261. dom.theme = document.querySelector( '#theme' );
  262. dom.wrapper = document.querySelector( '.reveal' );
  263. dom.slides = document.querySelector( '.reveal .slides' );
  264. // Prevent transitions while we're loading
  265. dom.slides.classList.add( 'no-transition' );
  266. // Background element
  267. dom.background = createSingletonNode( dom.wrapper, 'div', 'backgrounds', null );
  268. // Progress bar
  269. dom.progress = createSingletonNode( dom.wrapper, 'div', 'progress', '<span></span>' );
  270. dom.progressbar = dom.progress.querySelector( 'span' );
  271. // Arrow controls
  272. createSingletonNode( dom.wrapper, 'aside', 'controls',
  273. '<div class="navigate-left"></div>' +
  274. '<div class="navigate-right"></div>' +
  275. '<div class="navigate-up"></div>' +
  276. '<div class="navigate-down"></div>' );
  277. // State background element [DEPRECATED]
  278. createSingletonNode( dom.wrapper, 'div', 'state-background', null );
  279. // Overlay graphic which is displayed during the paused mode
  280. createSingletonNode( dom.wrapper, 'div', 'pause-overlay', null );
  281. // Cache references to elements
  282. dom.controls = document.querySelector( '.reveal .controls' );
  283. // There can be multiple instances of controls throughout the page
  284. dom.controlsLeft = toArray( document.querySelectorAll( '.navigate-left' ) );
  285. dom.controlsRight = toArray( document.querySelectorAll( '.navigate-right' ) );
  286. dom.controlsUp = toArray( document.querySelectorAll( '.navigate-up' ) );
  287. dom.controlsDown = toArray( document.querySelectorAll( '.navigate-down' ) );
  288. dom.controlsPrev = toArray( document.querySelectorAll( '.navigate-prev' ) );
  289. dom.controlsNext = toArray( document.querySelectorAll( '.navigate-next' ) );
  290. }
  291. /**
  292. * Creates an HTML element and returns a reference to it.
  293. * If the element already exists the existing instance will
  294. * be returned.
  295. */
  296. function createSingletonNode( container, tagname, classname, innerHTML ) {
  297. var node = container.querySelector( '.' + classname );
  298. if( !node ) {
  299. node = document.createElement( tagname );
  300. node.classList.add( classname );
  301. if( innerHTML !== null ) {
  302. node.innerHTML = innerHTML;
  303. }
  304. container.appendChild( node );
  305. }
  306. return node;
  307. }
  308. /**
  309. * Creates the slide background elements and appends them
  310. * to the background container. One element is created per
  311. * slide no matter if the given slide has visible background.
  312. */
  313. function createBackgrounds() {
  314. if( isPrintingPDF() ) {
  315. document.body.classList.add( 'print-pdf' );
  316. }
  317. // Clear prior backgrounds
  318. dom.background.innerHTML = '';
  319. dom.background.classList.add( 'no-transition' );
  320. // Helper method for creating a background element for the
  321. // given slide
  322. function _createBackground( slide, container ) {
  323. var data = {
  324. background: slide.getAttribute( 'data-background' ),
  325. backgroundSize: slide.getAttribute( 'data-background-size' ),
  326. backgroundImage: slide.getAttribute( 'data-background-image' ),
  327. backgroundColor: slide.getAttribute( 'data-background-color' ),
  328. backgroundRepeat: slide.getAttribute( 'data-background-repeat' ),
  329. backgroundPosition: slide.getAttribute( 'data-background-position' ),
  330. backgroundTransition: slide.getAttribute( 'data-background-transition' )
  331. };
  332. var element = document.createElement( 'div' );
  333. element.className = 'slide-background';
  334. if( data.background ) {
  335. // Auto-wrap image urls in url(...)
  336. if( /^(http|file|\/\/)/gi.test( data.background ) || /\.(svg|png|jpg|jpeg|gif|bmp)$/gi.test( data.background ) ) {
  337. element.style.backgroundImage = 'url('+ data.background +')';
  338. }
  339. else {
  340. element.style.background = data.background;
  341. }
  342. }
  343. // Additional and optional background properties
  344. if( data.backgroundSize ) element.style.backgroundSize = data.backgroundSize;
  345. if( data.backgroundImage ) element.style.backgroundImage = 'url("' + data.backgroundImage + '")';
  346. if( data.backgroundColor ) element.style.backgroundColor = data.backgroundColor;
  347. if( data.backgroundRepeat ) element.style.backgroundRepeat = data.backgroundRepeat;
  348. if( data.backgroundPosition ) element.style.backgroundPosition = data.backgroundPosition;
  349. if( data.backgroundTransition ) element.setAttribute( 'data-background-transition', data.backgroundTransition );
  350. container.appendChild( element );
  351. return element;
  352. }
  353. // Iterate over all horizontal slides
  354. toArray( document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) ).forEach( function( slideh ) {
  355. var backgroundStack;
  356. if( isPrintingPDF() ) {
  357. backgroundStack = _createBackground( slideh, slideh );
  358. }
  359. else {
  360. backgroundStack = _createBackground( slideh, dom.background );
  361. }
  362. // Iterate over all vertical slides
  363. toArray( slideh.querySelectorAll( 'section' ) ).forEach( function( slidev ) {
  364. if( isPrintingPDF() ) {
  365. _createBackground( slidev, slidev );
  366. }
  367. else {
  368. _createBackground( slidev, backgroundStack );
  369. }
  370. } );
  371. } );
  372. // Add parallax background if specified
  373. if( config.parallaxBackgroundImage ) {
  374. dom.background.style.backgroundImage = 'url("' + config.parallaxBackgroundImage + '")';
  375. dom.background.style.backgroundSize = config.parallaxBackgroundSize;
  376. // Make sure the below properties are set on the element - these properties are
  377. // needed for proper transitions to be set on the element via CSS. To remove
  378. // annoying background slide-in effect when the presentation starts, apply
  379. // these properties after short time delay
  380. setTimeout( function() {
  381. dom.wrapper.classList.add( 'has-parallax-background' );
  382. }, 1 );
  383. }
  384. else {
  385. dom.background.style.backgroundImage = '';
  386. dom.wrapper.classList.remove( 'has-parallax-background' );
  387. }
  388. }
  389. /**
  390. * Applies the configuration settings from the config
  391. * object. May be called multiple times.
  392. */
  393. function configure( options ) {
  394. var numberOfSlides = document.querySelectorAll( SLIDES_SELECTOR ).length;
  395. dom.wrapper.classList.remove( config.transition );
  396. // New config options may be passed when this method
  397. // is invoked through the API after initialization
  398. if( typeof options === 'object' ) extend( config, options );
  399. // Force linear transition based on browser capabilities
  400. if( features.transforms3d === false ) config.transition = 'linear';
  401. dom.wrapper.classList.add( config.transition );
  402. dom.wrapper.setAttribute( 'data-transition-speed', config.transitionSpeed );
  403. dom.wrapper.setAttribute( 'data-background-transition', config.backgroundTransition );
  404. dom.controls.style.display = config.controls ? 'block' : 'none';
  405. dom.progress.style.display = config.progress ? 'block' : 'none';
  406. if( config.rtl ) {
  407. dom.wrapper.classList.add( 'rtl' );
  408. }
  409. else {
  410. dom.wrapper.classList.remove( 'rtl' );
  411. }
  412. if( config.center ) {
  413. dom.wrapper.classList.add( 'center' );
  414. }
  415. else {
  416. dom.wrapper.classList.remove( 'center' );
  417. }
  418. if( config.mouseWheel ) {
  419. document.addEventListener( 'DOMMouseScroll', onDocumentMouseScroll, false ); // FF
  420. document.addEventListener( 'mousewheel', onDocumentMouseScroll, false );
  421. }
  422. else {
  423. document.removeEventListener( 'DOMMouseScroll', onDocumentMouseScroll, false ); // FF
  424. document.removeEventListener( 'mousewheel', onDocumentMouseScroll, false );
  425. }
  426. // Rolling 3D links
  427. if( config.rollingLinks ) {
  428. enableRollingLinks();
  429. }
  430. else {
  431. disableRollingLinks();
  432. }
  433. // Iframe link previews
  434. if( config.previewLinks ) {
  435. enablePreviewLinks();
  436. }
  437. else {
  438. disablePreviewLinks();
  439. enablePreviewLinks( '[data-preview-link]' );
  440. }
  441. // Auto-slide playback controls
  442. if( numberOfSlides > 1 && config.autoSlide && config.autoSlideStoppable && features.canvas && features.requestAnimationFrame ) {
  443. autoSlidePlayer = new Playback( dom.wrapper, function() {
  444. return Math.min( Math.max( ( Date.now() - autoSlideStartTime ) / autoSlide, 0 ), 1 );
  445. } );
  446. autoSlidePlayer.on( 'click', onAutoSlidePlayerClick );
  447. autoSlidePaused = false;
  448. }
  449. else if( autoSlidePlayer ) {
  450. autoSlidePlayer.destroy();
  451. autoSlidePlayer = null;
  452. }
  453. // Load the theme in the config, if it's not already loaded
  454. if( config.theme && dom.theme ) {
  455. var themeURL = dom.theme.getAttribute( 'href' );
  456. var themeFinder = /[^\/]*?(?=\.css)/;
  457. var themeName = themeURL.match(themeFinder)[0];
  458. if( config.theme !== themeName ) {
  459. themeURL = themeURL.replace(themeFinder, config.theme);
  460. dom.theme.setAttribute( 'href', themeURL );
  461. }
  462. }
  463. sync();
  464. }
  465. /**
  466. * Binds all event listeners.
  467. */
  468. function addEventListeners() {
  469. eventsAreBound = true;
  470. window.addEventListener( 'hashchange', onWindowHashChange, false );
  471. window.addEventListener( 'resize', onWindowResize, false );
  472. if( config.touch ) {
  473. dom.wrapper.addEventListener( 'touchstart', onTouchStart, false );
  474. dom.wrapper.addEventListener( 'touchmove', onTouchMove, false );
  475. dom.wrapper.addEventListener( 'touchend', onTouchEnd, false );
  476. // Support pointer-style touch interaction as well
  477. if( window.navigator.msPointerEnabled ) {
  478. dom.wrapper.addEventListener( 'MSPointerDown', onPointerDown, false );
  479. dom.wrapper.addEventListener( 'MSPointerMove', onPointerMove, false );
  480. dom.wrapper.addEventListener( 'MSPointerUp', onPointerUp, false );
  481. }
  482. }
  483. if( config.keyboard ) {
  484. document.addEventListener( 'keydown', onDocumentKeyDown, false );
  485. }
  486. if( config.progress && dom.progress ) {
  487. dom.progress.addEventListener( 'click', onProgressClicked, false );
  488. }
  489. if( config.focusBodyOnPageVisiblityChange ) {
  490. var visibilityChange;
  491. if( 'hidden' in document ) {
  492. visibilityChange = 'visibilitychange';
  493. }
  494. else if( 'msHidden' in document ) {
  495. visibilityChange = 'msvisibilitychange';
  496. }
  497. else if( 'webkitHidden' in document ) {
  498. visibilityChange = 'webkitvisibilitychange';
  499. }
  500. if( visibilityChange ) {
  501. document.addEventListener( visibilityChange, onPageVisibilityChange, false );
  502. }
  503. }
  504. [ 'touchstart', 'click' ].forEach( function( eventName ) {
  505. dom.controlsLeft.forEach( function( el ) { el.addEventListener( eventName, onNavigateLeftClicked, false ); } );
  506. dom.controlsRight.forEach( function( el ) { el.addEventListener( eventName, onNavigateRightClicked, false ); } );
  507. dom.controlsUp.forEach( function( el ) { el.addEventListener( eventName, onNavigateUpClicked, false ); } );
  508. dom.controlsDown.forEach( function( el ) { el.addEventListener( eventName, onNavigateDownClicked, false ); } );
  509. dom.controlsPrev.forEach( function( el ) { el.addEventListener( eventName, onNavigatePrevClicked, false ); } );
  510. dom.controlsNext.forEach( function( el ) { el.addEventListener( eventName, onNavigateNextClicked, false ); } );
  511. } );
  512. }
  513. /**
  514. * Unbinds all event listeners.
  515. */
  516. function removeEventListeners() {
  517. eventsAreBound = false;
  518. document.removeEventListener( 'keydown', onDocumentKeyDown, false );
  519. window.removeEventListener( 'hashchange', onWindowHashChange, false );
  520. window.removeEventListener( 'resize', onWindowResize, false );
  521. dom.wrapper.removeEventListener( 'touchstart', onTouchStart, false );
  522. dom.wrapper.removeEventListener( 'touchmove', onTouchMove, false );
  523. dom.wrapper.removeEventListener( 'touchend', onTouchEnd, false );
  524. if( window.navigator.msPointerEnabled ) {
  525. dom.wrapper.removeEventListener( 'MSPointerDown', onPointerDown, false );
  526. dom.wrapper.removeEventListener( 'MSPointerMove', onPointerMove, false );
  527. dom.wrapper.removeEventListener( 'MSPointerUp', onPointerUp, false );
  528. }
  529. if ( config.progress && dom.progress ) {
  530. dom.progress.removeEventListener( 'click', onProgressClicked, false );
  531. }
  532. [ 'touchstart', 'click' ].forEach( function( eventName ) {
  533. dom.controlsLeft.forEach( function( el ) { el.removeEventListener( eventName, onNavigateLeftClicked, false ); } );
  534. dom.controlsRight.forEach( function( el ) { el.removeEventListener( eventName, onNavigateRightClicked, false ); } );
  535. dom.controlsUp.forEach( function( el ) { el.removeEventListener( eventName, onNavigateUpClicked, false ); } );
  536. dom.controlsDown.forEach( function( el ) { el.removeEventListener( eventName, onNavigateDownClicked, false ); } );
  537. dom.controlsPrev.forEach( function( el ) { el.removeEventListener( eventName, onNavigatePrevClicked, false ); } );
  538. dom.controlsNext.forEach( function( el ) { el.removeEventListener( eventName, onNavigateNextClicked, false ); } );
  539. } );
  540. }
  541. /**
  542. * Extend object a with the properties of object b.
  543. * If there's a conflict, object b takes precedence.
  544. */
  545. function extend( a, b ) {
  546. for( var i in b ) {
  547. a[ i ] = b[ i ];
  548. }
  549. }
  550. /**
  551. * Converts the target object to an array.
  552. */
  553. function toArray( o ) {
  554. return Array.prototype.slice.call( o );
  555. }
  556. /**
  557. * Measures the distance in pixels between point a
  558. * and point b.
  559. *
  560. * @param {Object} a point with x/y properties
  561. * @param {Object} b point with x/y properties
  562. */
  563. function distanceBetween( a, b ) {
  564. var dx = a.x - b.x,
  565. dy = a.y - b.y;
  566. return Math.sqrt( dx*dx + dy*dy );
  567. }
  568. /**
  569. * Applies a CSS transform to the target element.
  570. */
  571. function transformElement( element, transform ) {
  572. element.style.WebkitTransform = transform;
  573. element.style.MozTransform = transform;
  574. element.style.msTransform = transform;
  575. element.style.OTransform = transform;
  576. element.style.transform = transform;
  577. }
  578. /**
  579. * Retrieves the height of the given element by looking
  580. * at the position and height of its immediate children.
  581. */
  582. function getAbsoluteHeight( element ) {
  583. var height = 0;
  584. if( element ) {
  585. var absoluteChildren = 0;
  586. toArray( element.childNodes ).forEach( function( child ) {
  587. if( typeof child.offsetTop === 'number' && child.style ) {
  588. // Count # of abs children
  589. if( child.style.position === 'absolute' ) {
  590. absoluteChildren += 1;
  591. }
  592. height = Math.max( height, child.offsetTop + child.offsetHeight );
  593. }
  594. } );
  595. // If there are no absolute children, use offsetHeight
  596. if( absoluteChildren === 0 ) {
  597. height = element.offsetHeight;
  598. }
  599. }
  600. return height;
  601. }
  602. /**
  603. * Returns the remaining height within the parent of the
  604. * target element after subtracting the height of all
  605. * siblings.
  606. *
  607. * remaining height = [parent height] - [ siblings height]
  608. */
  609. function getRemainingHeight( element, height ) {
  610. height = height || 0;
  611. if( element ) {
  612. var parent = element.parentNode;
  613. var siblings = parent.childNodes;
  614. // Subtract the height of each sibling
  615. toArray( siblings ).forEach( function( sibling ) {
  616. if( typeof sibling.offsetHeight === 'number' && sibling !== element ) {
  617. var styles = window.getComputedStyle( sibling ),
  618. marginTop = parseInt( styles.marginTop, 10 ),
  619. marginBottom = parseInt( styles.marginBottom, 10 );
  620. height -= sibling.offsetHeight + marginTop + marginBottom;
  621. }
  622. } );
  623. var elementStyles = window.getComputedStyle( element );
  624. // Subtract the margins of the target element
  625. height -= parseInt( elementStyles.marginTop, 10 ) +
  626. parseInt( elementStyles.marginBottom, 10 );
  627. }
  628. return height;
  629. }
  630. /**
  631. * Checks if this instance is being used to print a PDF.
  632. */
  633. function isPrintingPDF() {
  634. return ( /print-pdf/gi ).test( window.location.search );
  635. }
  636. /**
  637. * Hides the address bar if we're on a mobile device.
  638. */
  639. function hideAddressBar() {
  640. if( config.hideAddressBar && isMobileDevice ) {
  641. // Events that should trigger the address bar to hide
  642. window.addEventListener( 'load', removeAddressBar, false );
  643. window.addEventListener( 'orientationchange', removeAddressBar, false );
  644. }
  645. }
  646. /**
  647. * Causes the address bar to hide on mobile devices,
  648. * more vertical space ftw.
  649. */
  650. function removeAddressBar() {
  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. cueAutoSlide();
  1220. }
  1221. /**
  1222. * Syncs the presentation with the current DOM. Useful
  1223. * when new slides or control elements are added or when
  1224. * the configuration has changed.
  1225. */
  1226. function sync() {
  1227. // Subscribe to input
  1228. removeEventListeners();
  1229. addEventListeners();
  1230. // Force a layout to make sure the current config is accounted for
  1231. layout();
  1232. // Reflect the current autoSlide value
  1233. autoSlide = config.autoSlide;
  1234. // Start auto-sliding if it's enabled
  1235. cueAutoSlide();
  1236. // Re-create the slide backgrounds
  1237. createBackgrounds();
  1238. updateControls();
  1239. updateProgress();
  1240. updateBackground();
  1241. }
  1242. /**
  1243. * Updates one dimension of slides by showing the slide
  1244. * with the specified index.
  1245. *
  1246. * @param {String} selector A CSS selector that will fetch
  1247. * the group of slides we are working with
  1248. * @param {Number} index The index of the slide that should be
  1249. * shown
  1250. *
  1251. * @return {Number} The index of the slide that is now shown,
  1252. * might differ from the passed in index if it was out of
  1253. * bounds.
  1254. */
  1255. function updateSlides( selector, index ) {
  1256. // Select all slides and convert the NodeList result to
  1257. // an array
  1258. var slides = toArray( document.querySelectorAll( selector ) ),
  1259. slidesLength = slides.length;
  1260. if( slidesLength ) {
  1261. // Should the index loop?
  1262. if( config.loop ) {
  1263. index %= slidesLength;
  1264. if( index < 0 ) {
  1265. index = slidesLength + index;
  1266. }
  1267. }
  1268. // Enforce max and minimum index bounds
  1269. index = Math.max( Math.min( index, slidesLength - 1 ), 0 );
  1270. for( var i = 0; i < slidesLength; i++ ) {
  1271. var element = slides[i];
  1272. var reverse = config.rtl && !isVerticalSlide( element );
  1273. element.classList.remove( 'past' );
  1274. element.classList.remove( 'present' );
  1275. element.classList.remove( 'future' );
  1276. // http://www.w3.org/html/wg/drafts/html/master/editing.html#the-hidden-attribute
  1277. element.setAttribute( 'hidden', '' );
  1278. if( i < index ) {
  1279. // Any element previous to index is given the 'past' class
  1280. element.classList.add( reverse ? 'future' : 'past' );
  1281. }
  1282. else if( i > index ) {
  1283. // Any element subsequent to index is given the 'future' class
  1284. element.classList.add( reverse ? 'past' : 'future' );
  1285. var fragments = toArray( element.querySelectorAll( '.fragment.visible' ) );
  1286. // No fragments in future slides should be visible ahead of time
  1287. while( fragments.length ) {
  1288. fragments.pop().classList.remove( 'visible' );
  1289. }
  1290. }
  1291. // If this element contains vertical slides
  1292. if( element.querySelector( 'section' ) ) {
  1293. element.classList.add( 'stack' );
  1294. }
  1295. }
  1296. // Mark the current slide as present
  1297. slides[index].classList.add( 'present' );
  1298. slides[index].removeAttribute( 'hidden' );
  1299. // If this slide has a state associated with it, add it
  1300. // onto the current state of the deck
  1301. var slideState = slides[index].getAttribute( 'data-state' );
  1302. if( slideState ) {
  1303. state = state.concat( slideState.split( ' ' ) );
  1304. }
  1305. }
  1306. else {
  1307. // Since there are no slides we can't be anywhere beyond the
  1308. // zeroth index
  1309. index = 0;
  1310. }
  1311. return index;
  1312. }
  1313. /**
  1314. * Optimization method; hide all slides that are far away
  1315. * from the present slide.
  1316. */
  1317. function updateSlidesVisibility() {
  1318. // Select all slides and convert the NodeList result to
  1319. // an array
  1320. var horizontalSlides = toArray( document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) ),
  1321. horizontalSlidesLength = horizontalSlides.length,
  1322. distanceX,
  1323. distanceY;
  1324. if( horizontalSlidesLength ) {
  1325. // The number of steps away from the present slide that will
  1326. // be visible
  1327. var viewDistance = isOverview() ? 10 : config.viewDistance;
  1328. // Limit view distance on weaker devices
  1329. if( isMobileDevice ) {
  1330. viewDistance = isOverview() ? 6 : 1;
  1331. }
  1332. for( var x = 0; x < horizontalSlidesLength; x++ ) {
  1333. var horizontalSlide = horizontalSlides[x];
  1334. var verticalSlides = toArray( horizontalSlide.querySelectorAll( 'section' ) ),
  1335. verticalSlidesLength = verticalSlides.length;
  1336. // Loops so that it measures 1 between the first and last slides
  1337. distanceX = Math.abs( ( indexh - x ) % ( horizontalSlidesLength - viewDistance ) ) || 0;
  1338. // Show the horizontal slide if it's within the view distance
  1339. horizontalSlide.style.display = distanceX > viewDistance ? 'none' : 'block';
  1340. if( verticalSlidesLength ) {
  1341. var oy = getPreviousVerticalIndex( horizontalSlide );
  1342. for( var y = 0; y < verticalSlidesLength; y++ ) {
  1343. var verticalSlide = verticalSlides[y];
  1344. distanceY = x === indexh ? Math.abs( indexv - y ) : Math.abs( y - oy );
  1345. verticalSlide.style.display = ( distanceX + distanceY ) > viewDistance ? 'none' : 'block';
  1346. }
  1347. }
  1348. }
  1349. }
  1350. }
  1351. /**
  1352. * Updates the progress bar to reflect the current slide.
  1353. */
  1354. function updateProgress() {
  1355. // Update progress if enabled
  1356. if( config.progress && dom.progress ) {
  1357. var horizontalSlides = toArray( document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) );
  1358. // The number of past and total slides
  1359. var totalCount = document.querySelectorAll( SLIDES_SELECTOR + ':not(.stack)' ).length;
  1360. var pastCount = 0;
  1361. // Step through all slides and count the past ones
  1362. mainLoop: for( var i = 0; i < horizontalSlides.length; i++ ) {
  1363. var horizontalSlide = horizontalSlides[i];
  1364. var verticalSlides = toArray( horizontalSlide.querySelectorAll( 'section' ) );
  1365. for( var j = 0; j < verticalSlides.length; j++ ) {
  1366. // Stop as soon as we arrive at the present
  1367. if( verticalSlides[j].classList.contains( 'present' ) ) {
  1368. break mainLoop;
  1369. }
  1370. pastCount++;
  1371. }
  1372. // Stop as soon as we arrive at the present
  1373. if( horizontalSlide.classList.contains( 'present' ) ) {
  1374. break;
  1375. }
  1376. // Don't count the wrapping section for vertical slides
  1377. if( horizontalSlide.classList.contains( 'stack' ) === false ) {
  1378. pastCount++;
  1379. }
  1380. }
  1381. dom.progressbar.style.width = ( pastCount / ( totalCount - 1 ) ) * window.innerWidth + 'px';
  1382. }
  1383. }
  1384. /**
  1385. * Updates the state of all control/navigation arrows.
  1386. */
  1387. function updateControls() {
  1388. var routes = availableRoutes();
  1389. var fragments = availableFragments();
  1390. // Remove the 'enabled' class from all directions
  1391. dom.controlsLeft.concat( dom.controlsRight )
  1392. .concat( dom.controlsUp )
  1393. .concat( dom.controlsDown )
  1394. .concat( dom.controlsPrev )
  1395. .concat( dom.controlsNext ).forEach( function( node ) {
  1396. node.classList.remove( 'enabled' );
  1397. node.classList.remove( 'fragmented' );
  1398. } );
  1399. // Add the 'enabled' class to the available routes
  1400. if( routes.left ) dom.controlsLeft.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  1401. if( routes.right ) dom.controlsRight.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  1402. if( routes.up ) dom.controlsUp.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  1403. if( routes.down ) dom.controlsDown.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  1404. // Prev/next buttons
  1405. if( routes.left || routes.up ) dom.controlsPrev.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  1406. if( routes.right || routes.down ) dom.controlsNext.forEach( function( el ) { el.classList.add( 'enabled' ); } );
  1407. // Highlight fragment directions
  1408. if( currentSlide ) {
  1409. // Always apply fragment decorator to prev/next buttons
  1410. if( fragments.prev ) dom.controlsPrev.forEach( function( el ) { el.classList.add( 'fragmented', 'enabled' ); } );
  1411. if( fragments.next ) dom.controlsNext.forEach( function( el ) { el.classList.add( 'fragmented', 'enabled' ); } );
  1412. // Apply fragment decorators to directional buttons based on
  1413. // what slide axis they are in
  1414. if( isVerticalSlide( currentSlide ) ) {
  1415. if( fragments.prev ) dom.controlsUp.forEach( function( el ) { el.classList.add( 'fragmented', 'enabled' ); } );
  1416. if( fragments.next ) dom.controlsDown.forEach( function( el ) { el.classList.add( 'fragmented', 'enabled' ); } );
  1417. }
  1418. else {
  1419. if( fragments.prev ) dom.controlsLeft.forEach( function( el ) { el.classList.add( 'fragmented', 'enabled' ); } );
  1420. if( fragments.next ) dom.controlsRight.forEach( function( el ) { el.classList.add( 'fragmented', 'enabled' ); } );
  1421. }
  1422. }
  1423. }
  1424. /**
  1425. * Updates the background elements to reflect the current
  1426. * slide.
  1427. */
  1428. function updateBackground() {
  1429. // Update the classes of all backgrounds to match the
  1430. // states of their slides (past/present/future)
  1431. toArray( dom.background.childNodes ).forEach( function( backgroundh, h ) {
  1432. // Reverse past/future classes when in RTL mode
  1433. var horizontalPast = config.rtl ? 'future' : 'past',
  1434. horizontalFuture = config.rtl ? 'past' : 'future';
  1435. backgroundh.className = 'slide-background ' + ( h < indexh ? horizontalPast : h > indexh ? horizontalFuture : 'present' );
  1436. toArray( backgroundh.childNodes ).forEach( function( backgroundv, v ) {
  1437. backgroundv.className = 'slide-background ' + ( v < indexv ? 'past' : v > indexv ? 'future' : 'present' );
  1438. } );
  1439. } );
  1440. // Allow the first background to apply without transition
  1441. setTimeout( function() {
  1442. dom.background.classList.remove( 'no-transition' );
  1443. }, 1 );
  1444. }
  1445. /**
  1446. * Updates the position of the parallax background based
  1447. * on the current slide index.
  1448. */
  1449. function updateParallax() {
  1450. if( config.parallaxBackgroundImage ) {
  1451. var horizontalSlides = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ),
  1452. verticalSlides = document.querySelectorAll( VERTICAL_SLIDES_SELECTOR );
  1453. var backgroundSize = dom.background.style.backgroundSize.split( ' ' ),
  1454. backgroundWidth, backgroundHeight;
  1455. if( backgroundSize.length === 1 ) {
  1456. backgroundWidth = backgroundHeight = parseInt( backgroundSize[0], 10 );
  1457. }
  1458. else {
  1459. backgroundWidth = parseInt( backgroundSize[0], 10 );
  1460. backgroundHeight = parseInt( backgroundSize[1], 10 );
  1461. }
  1462. var slideWidth = dom.background.offsetWidth;
  1463. var horizontalSlideCount = horizontalSlides.length;
  1464. var horizontalOffset = -( backgroundWidth - slideWidth ) / ( horizontalSlideCount-1 ) * indexh;
  1465. var slideHeight = dom.background.offsetHeight;
  1466. var verticalSlideCount = verticalSlides.length;
  1467. var verticalOffset = verticalSlideCount > 0 ? -( backgroundHeight - slideHeight ) / ( verticalSlideCount-1 ) * indexv : 0;
  1468. dom.background.style.backgroundPosition = horizontalOffset + 'px ' + verticalOffset + 'px';
  1469. }
  1470. }
  1471. /**
  1472. * Determine what available routes there are for navigation.
  1473. *
  1474. * @return {Object} containing four booleans: left/right/up/down
  1475. */
  1476. function availableRoutes() {
  1477. var horizontalSlides = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ),
  1478. verticalSlides = document.querySelectorAll( VERTICAL_SLIDES_SELECTOR );
  1479. var routes = {
  1480. left: indexh > 0 || config.loop,
  1481. right: indexh < horizontalSlides.length - 1 || config.loop,
  1482. up: indexv > 0,
  1483. down: indexv < verticalSlides.length - 1
  1484. };
  1485. // reverse horizontal controls for rtl
  1486. if( config.rtl ) {
  1487. var left = routes.left;
  1488. routes.left = routes.right;
  1489. routes.right = left;
  1490. }
  1491. return routes;
  1492. }
  1493. /**
  1494. * Returns an object describing the available fragment
  1495. * directions.
  1496. *
  1497. * @return {Object} two boolean properties: prev/next
  1498. */
  1499. function availableFragments() {
  1500. if( currentSlide && config.fragments ) {
  1501. var fragments = currentSlide.querySelectorAll( '.fragment' );
  1502. var hiddenFragments = currentSlide.querySelectorAll( '.fragment:not(.visible)' );
  1503. return {
  1504. prev: fragments.length - hiddenFragments.length > 0,
  1505. next: !!hiddenFragments.length
  1506. };
  1507. }
  1508. else {
  1509. return { prev: false, next: false };
  1510. }
  1511. }
  1512. /**
  1513. * Start playback of any embedded content inside of
  1514. * the targeted slide.
  1515. */
  1516. function startEmbeddedContent( slide ) {
  1517. if( slide ) {
  1518. // HTML5 media elements
  1519. toArray( slide.querySelectorAll( 'video, audio' ) ).forEach( function( el ) {
  1520. if( el.hasAttribute( 'data-autoplay' ) ) {
  1521. el.play();
  1522. }
  1523. } );
  1524. // YouTube embeds
  1525. toArray( slide.querySelectorAll( 'iframe[src*="youtube.com/embed/"]' ) ).forEach( function( el ) {
  1526. if( el.hasAttribute( 'data-autoplay' ) ) {
  1527. el.contentWindow.postMessage('{"event":"command","func":"playVideo","args":""}', '*');
  1528. }
  1529. });
  1530. }
  1531. }
  1532. /**
  1533. * Stop playback of any embedded content inside of
  1534. * the targeted slide.
  1535. */
  1536. function stopEmbeddedContent( slide ) {
  1537. if( slide ) {
  1538. // HTML5 media elements
  1539. toArray( slide.querySelectorAll( 'video, audio' ) ).forEach( function( el ) {
  1540. if( !el.hasAttribute( 'data-ignore' ) ) {
  1541. el.pause();
  1542. }
  1543. } );
  1544. // YouTube embeds
  1545. toArray( slide.querySelectorAll( 'iframe[src*="youtube.com/embed/"]' ) ).forEach( function( el ) {
  1546. if( !el.hasAttribute( 'data-ignore' ) && typeof el.contentWindow.postMessage === 'function' ) {
  1547. el.contentWindow.postMessage('{"event":"command","func":"pauseVideo","args":""}', '*');
  1548. }
  1549. });
  1550. }
  1551. }
  1552. /**
  1553. * Reads the current URL (hash) and navigates accordingly.
  1554. */
  1555. function readURL() {
  1556. var hash = window.location.hash;
  1557. // Attempt to parse the hash as either an index or name
  1558. var bits = hash.slice( 2 ).split( '/' ),
  1559. name = hash.replace( /#|\//gi, '' );
  1560. // If the first bit is invalid and there is a name we can
  1561. // assume that this is a named link
  1562. if( isNaN( parseInt( bits[0], 10 ) ) && name.length ) {
  1563. // Find the slide with the specified name
  1564. var element = document.querySelector( '#' + name );
  1565. if( element ) {
  1566. // Find the position of the named slide and navigate to it
  1567. var indices = Reveal.getIndices( element );
  1568. slide( indices.h, indices.v );
  1569. }
  1570. // If the slide doesn't exist, navigate to the current slide
  1571. else {
  1572. slide( indexh || 0, indexv || 0 );
  1573. }
  1574. }
  1575. else {
  1576. // Read the index components of the hash
  1577. var h = parseInt( bits[0], 10 ) || 0,
  1578. v = parseInt( bits[1], 10 ) || 0;
  1579. if( h !== indexh || v !== indexv ) {
  1580. slide( h, v );
  1581. }
  1582. }
  1583. }
  1584. /**
  1585. * Updates the page URL (hash) to reflect the current
  1586. * state.
  1587. *
  1588. * @param {Number} delay The time in ms to wait before
  1589. * writing the hash
  1590. */
  1591. function writeURL( delay ) {
  1592. if( config.history ) {
  1593. // Make sure there's never more than one timeout running
  1594. clearTimeout( writeURLTimeout );
  1595. // If a delay is specified, timeout this call
  1596. if( typeof delay === 'number' ) {
  1597. writeURLTimeout = setTimeout( writeURL, delay );
  1598. }
  1599. else {
  1600. var url = '/';
  1601. // If the current slide has an ID, use that as a named link
  1602. if( currentSlide && typeof currentSlide.getAttribute( 'id' ) === 'string' ) {
  1603. url = '/' + currentSlide.getAttribute( 'id' );
  1604. }
  1605. // Otherwise use the /h/v index
  1606. else {
  1607. if( indexh > 0 || indexv > 0 ) url += indexh;
  1608. if( indexv > 0 ) url += '/' + indexv;
  1609. }
  1610. window.location.hash = url;
  1611. }
  1612. }
  1613. }
  1614. /**
  1615. * Retrieves the h/v location of the current, or specified,
  1616. * slide.
  1617. *
  1618. * @param {HTMLElement} slide If specified, the returned
  1619. * index will be for this slide rather than the currently
  1620. * active one
  1621. *
  1622. * @return {Object} { h: <int>, v: <int>, f: <int> }
  1623. */
  1624. function getIndices( slide ) {
  1625. // By default, return the current indices
  1626. var h = indexh,
  1627. v = indexv,
  1628. f;
  1629. // If a slide is specified, return the indices of that slide
  1630. if( slide ) {
  1631. var isVertical = isVerticalSlide( slide );
  1632. var slideh = isVertical ? slide.parentNode : slide;
  1633. // Select all horizontal slides
  1634. var horizontalSlides = toArray( document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) );
  1635. // Now that we know which the horizontal slide is, get its index
  1636. h = Math.max( horizontalSlides.indexOf( slideh ), 0 );
  1637. // If this is a vertical slide, grab the vertical index
  1638. if( isVertical ) {
  1639. v = Math.max( toArray( slide.parentNode.querySelectorAll( 'section' ) ).indexOf( slide ), 0 );
  1640. }
  1641. }
  1642. if( !slide && currentSlide ) {
  1643. var hasFragments = currentSlide.querySelectorAll( '.fragment' ).length > 0;
  1644. if( hasFragments ) {
  1645. var visibleFragments = currentSlide.querySelectorAll( '.fragment.visible' );
  1646. f = visibleFragments.length;
  1647. }
  1648. }
  1649. return { h: h, v: v, f: f };
  1650. }
  1651. /**
  1652. * Navigate to the next slide fragment.
  1653. *
  1654. * @return {Boolean} true if there was a next fragment,
  1655. * false otherwise
  1656. */
  1657. function nextFragment() {
  1658. if( currentSlide && config.fragments ) {
  1659. var fragments = sortFragments( currentSlide.querySelectorAll( '.fragment:not(.visible)' ) );
  1660. if( fragments.length ) {
  1661. // Find the index of the next fragment
  1662. var index = fragments[0].getAttribute( 'data-fragment-index' );
  1663. // Find all fragments with the same index
  1664. fragments = currentSlide.querySelectorAll( '.fragment[data-fragment-index="'+ index +'"]' );
  1665. toArray( fragments ).forEach( function( element ) {
  1666. element.classList.add( 'visible' );
  1667. } );
  1668. // Notify subscribers of the change
  1669. dispatchEvent( 'fragmentshown', { fragment: fragments[0], fragments: fragments } );
  1670. updateControls();
  1671. return true;
  1672. }
  1673. }
  1674. return false;
  1675. }
  1676. /**
  1677. * Navigate to the previous slide fragment.
  1678. *
  1679. * @return {Boolean} true if there was a previous fragment,
  1680. * false otherwise
  1681. */
  1682. function previousFragment() {
  1683. if( currentSlide && config.fragments ) {
  1684. var fragments = sortFragments( currentSlide.querySelectorAll( '.fragment.visible' ) );
  1685. if( fragments.length ) {
  1686. // Find the index of the previous fragment
  1687. var index = fragments[ fragments.length - 1 ].getAttribute( 'data-fragment-index' );
  1688. // Find all fragments with the same index
  1689. fragments = currentSlide.querySelectorAll( '.fragment[data-fragment-index="'+ index +'"]' );
  1690. toArray( fragments ).forEach( function( f ) {
  1691. f.classList.remove( 'visible' );
  1692. } );
  1693. // Notify subscribers of the change
  1694. dispatchEvent( 'fragmenthidden', { fragment: fragments[0], fragments: fragments } );
  1695. updateControls();
  1696. return true;
  1697. }
  1698. }
  1699. return false;
  1700. }
  1701. /**
  1702. * Cues a new automated slide if enabled in the config.
  1703. */
  1704. function cueAutoSlide() {
  1705. cancelAutoSlide();
  1706. if( currentSlide ) {
  1707. // If the current slide has a data-autoslide use that,
  1708. // otherwise use the config.autoSlide value
  1709. var slideAutoSlide = currentSlide.getAttribute( 'data-autoslide' );
  1710. if( slideAutoSlide ) {
  1711. autoSlide = parseInt( slideAutoSlide, 10 );
  1712. }
  1713. else {
  1714. autoSlide = config.autoSlide;
  1715. }
  1716. // Cue the next auto-slide if:
  1717. // - There is an autoSlide value
  1718. // - Auto-sliding isn't paused by the user
  1719. // - The presentation isn't paused
  1720. // - The overview isn't active
  1721. // - The presentation isn't over
  1722. if( autoSlide && !autoSlidePaused && !isPaused() && !isOverview() && ( !Reveal.isLastSlide() || config.loop === true ) ) {
  1723. autoSlideTimeout = setTimeout( navigateNext, autoSlide );
  1724. autoSlideStartTime = Date.now();
  1725. }
  1726. if( autoSlidePlayer ) {
  1727. autoSlidePlayer.setPlaying( autoSlideTimeout !== -1 );
  1728. }
  1729. }
  1730. }
  1731. /**
  1732. * Cancels any ongoing request to auto-slide.
  1733. */
  1734. function cancelAutoSlide() {
  1735. clearTimeout( autoSlideTimeout );
  1736. autoSlideTimeout = -1;
  1737. }
  1738. function pauseAutoSlide() {
  1739. autoSlidePaused = true;
  1740. clearTimeout( autoSlideTimeout );
  1741. if( autoSlidePlayer ) {
  1742. autoSlidePlayer.setPlaying( false );
  1743. }
  1744. }
  1745. function resumeAutoSlide() {
  1746. autoSlidePaused = false;
  1747. cueAutoSlide();
  1748. }
  1749. function navigateLeft() {
  1750. // Reverse for RTL
  1751. if( config.rtl ) {
  1752. if( ( isOverview() || nextFragment() === false ) && availableRoutes().left ) {
  1753. slide( indexh + 1 );
  1754. }
  1755. }
  1756. // Normal navigation
  1757. else if( ( isOverview() || previousFragment() === false ) && availableRoutes().left ) {
  1758. slide( indexh - 1 );
  1759. }
  1760. }
  1761. function navigateRight() {
  1762. // Reverse for RTL
  1763. if( config.rtl ) {
  1764. if( ( isOverview() || previousFragment() === false ) && availableRoutes().right ) {
  1765. slide( indexh - 1 );
  1766. }
  1767. }
  1768. // Normal navigation
  1769. else if( ( isOverview() || nextFragment() === false ) && availableRoutes().right ) {
  1770. slide( indexh + 1 );
  1771. }
  1772. }
  1773. function navigateUp() {
  1774. // Prioritize hiding fragments
  1775. if( ( isOverview() || previousFragment() === false ) && availableRoutes().up ) {
  1776. slide( indexh, indexv - 1 );
  1777. }
  1778. }
  1779. function navigateDown() {
  1780. // Prioritize revealing fragments
  1781. if( ( isOverview() || nextFragment() === false ) && availableRoutes().down ) {
  1782. slide( indexh, indexv + 1 );
  1783. }
  1784. }
  1785. /**
  1786. * Navigates backwards, prioritized in the following order:
  1787. * 1) Previous fragment
  1788. * 2) Previous vertical slide
  1789. * 3) Previous horizontal slide
  1790. */
  1791. function navigatePrev() {
  1792. // Prioritize revealing fragments
  1793. if( previousFragment() === false ) {
  1794. if( availableRoutes().up ) {
  1795. navigateUp();
  1796. }
  1797. else {
  1798. // Fetch the previous horizontal slide, if there is one
  1799. var previousSlide = document.querySelector( HORIZONTAL_SLIDES_SELECTOR + '.past:nth-child(' + indexh + ')' );
  1800. if( previousSlide ) {
  1801. var v = ( previousSlide.querySelectorAll( 'section' ).length - 1 ) || undefined;
  1802. var h = indexh - 1;
  1803. slide( h, v );
  1804. }
  1805. }
  1806. }
  1807. }
  1808. /**
  1809. * Same as #navigatePrev() but navigates forwards.
  1810. */
  1811. function navigateNext() {
  1812. // Prioritize revealing fragments
  1813. if( nextFragment() === false ) {
  1814. availableRoutes().down ? navigateDown() : navigateRight();
  1815. }
  1816. // If auto-sliding is enabled we need to cue up
  1817. // another timeout
  1818. cueAutoSlide();
  1819. }
  1820. // --------------------------------------------------------------------//
  1821. // ----------------------------- EVENTS -------------------------------//
  1822. // --------------------------------------------------------------------//
  1823. /**
  1824. * Called by all event handlers that are based on user
  1825. * input.
  1826. */
  1827. function onUserInput( event ) {
  1828. if( config.autoSlideStoppable ) {
  1829. pauseAutoSlide();
  1830. }
  1831. }
  1832. /**
  1833. * Handler for the document level 'keydown' event.
  1834. */
  1835. function onDocumentKeyDown( event ) {
  1836. onUserInput( event );
  1837. // Check if there's a focused element that could be using
  1838. // the keyboard
  1839. var activeElement = document.activeElement;
  1840. var hasFocus = !!( document.activeElement && ( document.activeElement.type || document.activeElement.href || document.activeElement.contentEditable !== 'inherit' ) );
  1841. // Disregard the event if there's a focused element or a
  1842. // keyboard modifier key is present
  1843. if( hasFocus || (event.shiftKey && event.keyCode !== 32) || event.altKey || event.ctrlKey || event.metaKey ) return;
  1844. // While paused only allow "unpausing" keyboard events (b and .)
  1845. if( isPaused() && [66,190,191].indexOf( event.keyCode ) === -1 ) {
  1846. return false;
  1847. }
  1848. var triggered = false;
  1849. // 1. User defined key bindings
  1850. if( typeof config.keyboard === 'object' ) {
  1851. for( var key in config.keyboard ) {
  1852. // Check if this binding matches the pressed key
  1853. if( parseInt( key, 10 ) === event.keyCode ) {
  1854. var value = config.keyboard[ key ];
  1855. // Callback function
  1856. if( typeof value === 'function' ) {
  1857. value.apply( null, [ event ] );
  1858. }
  1859. // String shortcuts to reveal.js API
  1860. else if( typeof value === 'string' && typeof Reveal[ value ] === 'function' ) {
  1861. Reveal[ value ].call();
  1862. }
  1863. triggered = true;
  1864. }
  1865. }
  1866. }
  1867. // 2. System defined key bindings
  1868. if( triggered === false ) {
  1869. // Assume true and try to prove false
  1870. triggered = true;
  1871. switch( event.keyCode ) {
  1872. // p, page up
  1873. case 80: case 33: navigatePrev(); break;
  1874. // n, page down
  1875. case 78: case 34: navigateNext(); break;
  1876. // h, left
  1877. case 72: case 37: navigateLeft(); break;
  1878. // l, right
  1879. case 76: case 39: navigateRight(); break;
  1880. // k, up
  1881. case 75: case 38: navigateUp(); break;
  1882. // j, down
  1883. case 74: case 40: navigateDown(); break;
  1884. // home
  1885. case 36: slide( 0 ); break;
  1886. // end
  1887. case 35: slide( Number.MAX_VALUE ); break;
  1888. // space
  1889. case 32: isOverview() ? deactivateOverview() : event.shiftKey ? navigatePrev() : navigateNext(); break;
  1890. // return
  1891. case 13: isOverview() ? deactivateOverview() : triggered = false; break;
  1892. // b, period, Logitech presenter tools "black screen" button
  1893. case 66: case 190: case 191: togglePause(); break;
  1894. // f
  1895. case 70: enterFullscreen(); break;
  1896. default:
  1897. triggered = false;
  1898. }
  1899. }
  1900. // If the input resulted in a triggered action we should prevent
  1901. // the browsers default behavior
  1902. if( triggered ) {
  1903. event.preventDefault();
  1904. }
  1905. // ESC or O key
  1906. else if ( ( event.keyCode === 27 || event.keyCode === 79 ) && features.transforms3d ) {
  1907. if( dom.preview ) {
  1908. closePreview();
  1909. }
  1910. else {
  1911. toggleOverview();
  1912. }
  1913. event.preventDefault();
  1914. }
  1915. // If auto-sliding is enabled we need to cue up
  1916. // another timeout
  1917. cueAutoSlide();
  1918. }
  1919. /**
  1920. * Handler for the 'touchstart' event, enables support for
  1921. * swipe and pinch gestures.
  1922. */
  1923. function onTouchStart( event ) {
  1924. touch.startX = event.touches[0].clientX;
  1925. touch.startY = event.touches[0].clientY;
  1926. touch.startCount = event.touches.length;
  1927. // If there's two touches we need to memorize the distance
  1928. // between those two points to detect pinching
  1929. if( event.touches.length === 2 && config.overview ) {
  1930. touch.startSpan = distanceBetween( {
  1931. x: event.touches[1].clientX,
  1932. y: event.touches[1].clientY
  1933. }, {
  1934. x: touch.startX,
  1935. y: touch.startY
  1936. } );
  1937. }
  1938. }
  1939. /**
  1940. * Handler for the 'touchmove' event.
  1941. */
  1942. function onTouchMove( event ) {
  1943. // Each touch should only trigger one action
  1944. if( !touch.captured ) {
  1945. onUserInput( event );
  1946. var currentX = event.touches[0].clientX;
  1947. var currentY = event.touches[0].clientY;
  1948. // If the touch started with two points and still has
  1949. // two active touches; test for the pinch gesture
  1950. if( event.touches.length === 2 && touch.startCount === 2 && config.overview ) {
  1951. // The current distance in pixels between the two touch points
  1952. var currentSpan = distanceBetween( {
  1953. x: event.touches[1].clientX,
  1954. y: event.touches[1].clientY
  1955. }, {
  1956. x: touch.startX,
  1957. y: touch.startY
  1958. } );
  1959. // If the span is larger than the desire amount we've got
  1960. // ourselves a pinch
  1961. if( Math.abs( touch.startSpan - currentSpan ) > touch.threshold ) {
  1962. touch.captured = true;
  1963. if( currentSpan < touch.startSpan ) {
  1964. activateOverview();
  1965. }
  1966. else {
  1967. deactivateOverview();
  1968. }
  1969. }
  1970. event.preventDefault();
  1971. }
  1972. // There was only one touch point, look for a swipe
  1973. else if( event.touches.length === 1 && touch.startCount !== 2 ) {
  1974. var deltaX = currentX - touch.startX,
  1975. deltaY = currentY - touch.startY;
  1976. if( deltaX > touch.threshold && Math.abs( deltaX ) > Math.abs( deltaY ) ) {
  1977. touch.captured = true;
  1978. navigateLeft();
  1979. }
  1980. else if( deltaX < -touch.threshold && Math.abs( deltaX ) > Math.abs( deltaY ) ) {
  1981. touch.captured = true;
  1982. navigateRight();
  1983. }
  1984. else if( deltaY > touch.threshold ) {
  1985. touch.captured = true;
  1986. navigateUp();
  1987. }
  1988. else if( deltaY < -touch.threshold ) {
  1989. touch.captured = true;
  1990. navigateDown();
  1991. }
  1992. // If we're embedded, only block touch events if they have
  1993. // triggered an action
  1994. if( config.embedded ) {
  1995. if( touch.captured || isVerticalSlide( currentSlide ) ) {
  1996. event.preventDefault();
  1997. }
  1998. }
  1999. // Not embedded? Block them all to avoid needless tossing
  2000. // around of the viewport in iOS
  2001. else {
  2002. event.preventDefault();
  2003. }
  2004. }
  2005. }
  2006. // There's a bug with swiping on some Android devices unless
  2007. // the default action is always prevented
  2008. else if( navigator.userAgent.match( /android/gi ) ) {
  2009. event.preventDefault();
  2010. }
  2011. }
  2012. /**
  2013. * Handler for the 'touchend' event.
  2014. */
  2015. function onTouchEnd( event ) {
  2016. touch.captured = false;
  2017. }
  2018. /**
  2019. * Convert pointer down to touch start.
  2020. */
  2021. function onPointerDown( event ) {
  2022. if( event.pointerType === event.MSPOINTER_TYPE_TOUCH ) {
  2023. event.touches = [{ clientX: event.clientX, clientY: event.clientY }];
  2024. onTouchStart( event );
  2025. }
  2026. }
  2027. /**
  2028. * Convert pointer move to touch move.
  2029. */
  2030. function onPointerMove( event ) {
  2031. if( event.pointerType === event.MSPOINTER_TYPE_TOUCH ) {
  2032. event.touches = [{ clientX: event.clientX, clientY: event.clientY }];
  2033. onTouchMove( event );
  2034. }
  2035. }
  2036. /**
  2037. * Convert pointer up to touch end.
  2038. */
  2039. function onPointerUp( event ) {
  2040. if( event.pointerType === event.MSPOINTER_TYPE_TOUCH ) {
  2041. event.touches = [{ clientX: event.clientX, clientY: event.clientY }];
  2042. onTouchEnd( event );
  2043. }
  2044. }
  2045. /**
  2046. * Handles mouse wheel scrolling, throttled to avoid skipping
  2047. * multiple slides.
  2048. */
  2049. function onDocumentMouseScroll( event ) {
  2050. if( Date.now() - lastMouseWheelStep > 600 ) {
  2051. lastMouseWheelStep = Date.now();
  2052. var delta = event.detail || -event.wheelDelta;
  2053. if( delta > 0 ) {
  2054. navigateNext();
  2055. }
  2056. else {
  2057. navigatePrev();
  2058. }
  2059. }
  2060. }
  2061. /**
  2062. * Clicking on the progress bar results in a navigation to the
  2063. * closest approximate horizontal slide using this equation:
  2064. *
  2065. * ( clickX / presentationWidth ) * numberOfSlides
  2066. */
  2067. function onProgressClicked( event ) {
  2068. onUserInput( event );
  2069. event.preventDefault();
  2070. var slidesTotal = toArray( document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ) ).length;
  2071. var slideIndex = Math.floor( ( event.clientX / dom.wrapper.offsetWidth ) * slidesTotal );
  2072. slide( slideIndex );
  2073. }
  2074. /**
  2075. * Event handler for navigation control buttons.
  2076. */
  2077. function onNavigateLeftClicked( event ) { event.preventDefault(); onUserInput(); navigateLeft(); }
  2078. function onNavigateRightClicked( event ) { event.preventDefault(); onUserInput(); navigateRight(); }
  2079. function onNavigateUpClicked( event ) { event.preventDefault(); onUserInput(); navigateUp(); }
  2080. function onNavigateDownClicked( event ) { event.preventDefault(); onUserInput(); navigateDown(); }
  2081. function onNavigatePrevClicked( event ) { event.preventDefault(); onUserInput(); navigatePrev(); }
  2082. function onNavigateNextClicked( event ) { event.preventDefault(); onUserInput(); navigateNext(); }
  2083. /**
  2084. * Handler for the window level 'hashchange' event.
  2085. */
  2086. function onWindowHashChange( event ) {
  2087. readURL();
  2088. }
  2089. /**
  2090. * Handler for the window level 'resize' event.
  2091. */
  2092. function onWindowResize( event ) {
  2093. layout();
  2094. }
  2095. /**
  2096. * Handle for the window level 'visibilitychange' event.
  2097. */
  2098. function onPageVisibilityChange( event ) {
  2099. var isHidden = document.webkitHidden ||
  2100. document.msHidden ||
  2101. document.hidden;
  2102. // If, after clicking a link or similar and we're coming back,
  2103. // focus the document.body to ensure we can use keyboard shortcuts
  2104. if( isHidden === false && document.activeElement !== document.body ) {
  2105. document.activeElement.blur();
  2106. document.body.focus();
  2107. }
  2108. }
  2109. /**
  2110. * Invoked when a slide is and we're in the overview.
  2111. */
  2112. function onOverviewSlideClicked( event ) {
  2113. // TODO There's a bug here where the event listeners are not
  2114. // removed after deactivating the overview.
  2115. if( eventsAreBound && isOverview() ) {
  2116. event.preventDefault();
  2117. var element = event.target;
  2118. while( element && !element.nodeName.match( /section/gi ) ) {
  2119. element = element.parentNode;
  2120. }
  2121. if( element && !element.classList.contains( 'disabled' ) ) {
  2122. deactivateOverview();
  2123. if( element.nodeName.match( /section/gi ) ) {
  2124. var h = parseInt( element.getAttribute( 'data-index-h' ), 10 ),
  2125. v = parseInt( element.getAttribute( 'data-index-v' ), 10 );
  2126. slide( h, v );
  2127. }
  2128. }
  2129. }
  2130. }
  2131. /**
  2132. * Handles clicks on links that are set to preview in the
  2133. * iframe overlay.
  2134. */
  2135. function onPreviewLinkClicked( event ) {
  2136. var url = event.target.getAttribute( 'href' );
  2137. if( url ) {
  2138. openPreview( url );
  2139. event.preventDefault();
  2140. }
  2141. }
  2142. /**
  2143. * Handles click on the auto-sliding controls element.
  2144. */
  2145. function onAutoSlidePlayerClick( event ) {
  2146. // Replay
  2147. if( Reveal.isLastSlide() && config.loop === false ) {
  2148. slide( 0, 0 );
  2149. resumeAutoSlide();
  2150. }
  2151. // Resume
  2152. else if( autoSlidePaused ) {
  2153. resumeAutoSlide();
  2154. }
  2155. // Pause
  2156. else {
  2157. pauseAutoSlide();
  2158. }
  2159. }
  2160. // --------------------------------------------------------------------//
  2161. // ------------------------ PLAYBACK COMPONENT ------------------------//
  2162. // --------------------------------------------------------------------//
  2163. /**
  2164. * Constructor for the playback component, which displays
  2165. * play/pause/progress controls.
  2166. *
  2167. * @param {HTMLElement} container The component will append
  2168. * itself to this
  2169. * @param {Function} progressCheck A method which will be
  2170. * called frequently to get the current progress on a range
  2171. * of 0-1
  2172. */
  2173. function Playback( container, progressCheck ) {
  2174. // Cosmetics
  2175. this.diameter = 50;
  2176. this.thickness = 3;
  2177. // Flags if we are currently playing
  2178. this.playing = false;
  2179. // Current progress on a 0-1 range
  2180. this.progress = 0;
  2181. // Used to loop the animation smoothly
  2182. this.progressOffset = 1;
  2183. this.container = container;
  2184. this.progressCheck = progressCheck;
  2185. this.canvas = document.createElement( 'canvas' );
  2186. this.canvas.className = 'playback';
  2187. this.canvas.width = this.diameter;
  2188. this.canvas.height = this.diameter;
  2189. this.context = this.canvas.getContext( '2d' );
  2190. this.container.appendChild( this.canvas );
  2191. this.render();
  2192. }
  2193. Playback.prototype.setPlaying = function( value ) {
  2194. var wasPlaying = this.playing;
  2195. this.playing = value;
  2196. // Start repainting if we weren't already
  2197. if( !wasPlaying && this.playing ) {
  2198. this.animate();
  2199. }
  2200. else {
  2201. this.render();
  2202. }
  2203. };
  2204. Playback.prototype.animate = function() {
  2205. var progressBefore = this.progress;
  2206. this.progress = this.progressCheck();
  2207. // When we loop, offset the progress so that it eases
  2208. // smoothly rather than immediately resetting
  2209. if( progressBefore > 0.8 && this.progress < 0.2 ) {
  2210. this.progressOffset = this.progress;
  2211. }
  2212. this.render();
  2213. if( this.playing ) {
  2214. features.requestAnimationFrameMethod.call( window, this.animate.bind( this ) );
  2215. }
  2216. };
  2217. /**
  2218. * Renders the current progress and playback state.
  2219. */
  2220. Playback.prototype.render = function() {
  2221. var progress = this.playing ? this.progress : 0,
  2222. radius = ( this.diameter / 2 ) - this.thickness,
  2223. x = this.diameter / 2,
  2224. y = this.diameter / 2,
  2225. iconSize = 14;
  2226. // Ease towards 1
  2227. this.progressOffset += ( 1 - this.progressOffset ) * 0.1;
  2228. var endAngle = ( - Math.PI / 2 ) + ( progress * ( Math.PI * 2 ) );
  2229. var startAngle = ( - Math.PI / 2 ) + ( this.progressOffset * ( Math.PI * 2 ) );
  2230. this.context.save();
  2231. this.context.clearRect( 0, 0, this.diameter, this.diameter );
  2232. // Solid background color
  2233. this.context.beginPath();
  2234. this.context.arc( x, y, radius + 2, 0, Math.PI * 2, false );
  2235. this.context.fillStyle = 'rgba( 0, 0, 0, 0.4 )';
  2236. this.context.fill();
  2237. // Draw progress track
  2238. this.context.beginPath();
  2239. this.context.arc( x, y, radius, 0, Math.PI * 2, false );
  2240. this.context.lineWidth = this.thickness;
  2241. this.context.strokeStyle = '#666';
  2242. this.context.stroke();
  2243. if( this.playing ) {
  2244. // Draw progress on top of track
  2245. this.context.beginPath();
  2246. this.context.arc( x, y, radius, startAngle, endAngle, false );
  2247. this.context.lineWidth = this.thickness;
  2248. this.context.strokeStyle = '#fff';
  2249. this.context.stroke();
  2250. }
  2251. this.context.translate( x - ( iconSize / 2 ), y - ( iconSize / 2 ) );
  2252. // Draw play/pause icons
  2253. if( this.playing ) {
  2254. this.context.fillStyle = '#fff';
  2255. this.context.fillRect( 0, 0, iconSize / 2 - 2, iconSize );
  2256. this.context.fillRect( iconSize / 2 + 2, 0, iconSize / 2 - 2, iconSize );
  2257. }
  2258. else {
  2259. this.context.beginPath();
  2260. this.context.translate( 2, 0 );
  2261. this.context.moveTo( 0, 0 );
  2262. this.context.lineTo( iconSize - 2, iconSize / 2 );
  2263. this.context.lineTo( 0, iconSize );
  2264. this.context.fillStyle = '#fff';
  2265. this.context.fill();
  2266. }
  2267. this.context.restore();
  2268. };
  2269. Playback.prototype.on = function( type, listener ) {
  2270. this.canvas.addEventListener( type, listener, false );
  2271. };
  2272. Playback.prototype.off = function( type, listener ) {
  2273. this.canvas.removeEventListener( type, listener, false );
  2274. };
  2275. Playback.prototype.destroy = function() {
  2276. this.playing = false;
  2277. if( this.canvas.parentNode ) {
  2278. this.container.removeChild( this.canvas );
  2279. }
  2280. };
  2281. // --------------------------------------------------------------------//
  2282. // ------------------------------- API --------------------------------//
  2283. // --------------------------------------------------------------------//
  2284. return {
  2285. initialize: initialize,
  2286. configure: configure,
  2287. sync: sync,
  2288. // Navigation methods
  2289. slide: slide,
  2290. left: navigateLeft,
  2291. right: navigateRight,
  2292. up: navigateUp,
  2293. down: navigateDown,
  2294. prev: navigatePrev,
  2295. next: navigateNext,
  2296. prevFragment: previousFragment,
  2297. nextFragment: nextFragment,
  2298. // Deprecated aliases
  2299. navigateTo: slide,
  2300. navigateLeft: navigateLeft,
  2301. navigateRight: navigateRight,
  2302. navigateUp: navigateUp,
  2303. navigateDown: navigateDown,
  2304. navigatePrev: navigatePrev,
  2305. navigateNext: navigateNext,
  2306. // Forces an update in slide layout
  2307. layout: layout,
  2308. // Returns an object with the available routes as booleans (left/right/top/bottom)
  2309. availableRoutes: availableRoutes,
  2310. // Returns an object with the available fragments as booleans (prev/next)
  2311. availableFragments: availableFragments,
  2312. // Toggles the overview mode on/off
  2313. toggleOverview: toggleOverview,
  2314. // Toggles the "black screen" mode on/off
  2315. togglePause: togglePause,
  2316. // State checks
  2317. isOverview: isOverview,
  2318. isPaused: isPaused,
  2319. // Adds or removes all internal event listeners (such as keyboard)
  2320. addEventListeners: addEventListeners,
  2321. removeEventListeners: removeEventListeners,
  2322. // Returns the indices of the current, or specified, slide
  2323. getIndices: getIndices,
  2324. // Returns the slide at the specified index, y is optional
  2325. getSlide: function( x, y ) {
  2326. var horizontalSlide = document.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR )[ x ];
  2327. var verticalSlides = horizontalSlide && horizontalSlide.querySelectorAll( 'section' );
  2328. if( typeof y !== 'undefined' ) {
  2329. return verticalSlides ? verticalSlides[ y ] : undefined;
  2330. }
  2331. return horizontalSlide;
  2332. },
  2333. // Returns the previous slide element, may be null
  2334. getPreviousSlide: function() {
  2335. return previousSlide;
  2336. },
  2337. // Returns the current slide element
  2338. getCurrentSlide: function() {
  2339. return currentSlide;
  2340. },
  2341. // Returns the current scale of the presentation content
  2342. getScale: function() {
  2343. return scale;
  2344. },
  2345. // Returns the current configuration object
  2346. getConfig: function() {
  2347. return config;
  2348. },
  2349. // Helper method, retrieves query string as a key/value hash
  2350. getQueryHash: function() {
  2351. var query = {};
  2352. location.search.replace( /[A-Z0-9]+?=(\w*)/gi, function(a) {
  2353. query[ a.split( '=' ).shift() ] = a.split( '=' ).pop();
  2354. } );
  2355. // Basic deserialization
  2356. for( var i in query ) {
  2357. var value = query[ i ];
  2358. if( value === 'null' ) query[ i ] = null;
  2359. else if( value === 'true' ) query[ i ] = true;
  2360. else if( value === 'false' ) query[ i ] = false;
  2361. else if( !isNaN( parseFloat( value ) ) ) query[ i ] = parseFloat( value );
  2362. }
  2363. return query;
  2364. },
  2365. // Returns true if we're currently on the first slide
  2366. isFirstSlide: function() {
  2367. return document.querySelector( SLIDES_SELECTOR + '.past' ) == null ? true : false;
  2368. },
  2369. // Returns true if we're currently on the last slide
  2370. isLastSlide: function() {
  2371. if( currentSlide ) {
  2372. // Does this slide has next a sibling?
  2373. if( currentSlide.nextElementSibling ) return false;
  2374. // If it's vertical, does its parent have a next sibling?
  2375. if( isVerticalSlide( currentSlide ) && currentSlide.parentNode.nextElementSibling ) return false;
  2376. return true;
  2377. }
  2378. return false;
  2379. },
  2380. // Checks if reveal.js has been loaded and is ready for use
  2381. isReady: function() {
  2382. return loaded;
  2383. },
  2384. // Forward event binding to the reveal DOM element
  2385. addEventListener: function( type, listener, useCapture ) {
  2386. if( 'addEventListener' in window ) {
  2387. ( dom.wrapper || document.querySelector( '.reveal' ) ).addEventListener( type, listener, useCapture );
  2388. }
  2389. },
  2390. removeEventListener: function( type, listener, useCapture ) {
  2391. if( 'addEventListener' in window ) {
  2392. ( dom.wrapper || document.querySelector( '.reveal' ) ).removeEventListener( type, listener, useCapture );
  2393. }
  2394. }
  2395. };
  2396. })();