reveal.js 107 KB

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