reveal.js 98 KB

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