reveal.js 90 KB

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