reveal.js 90 KB

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