reveal.js 87 KB

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