reveal.js 90 KB

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