reveal.js 87 KB

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