reveal.js 83 KB

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