reveal.js 92 KB

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