reveal.js 98 KB

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