You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2590 lines
66KB

  1. import SlideContent from './controllers/slidecontent.js'
  2. import SlideNumber from './controllers/slidenumber.js'
  3. import Backgrounds from './controllers/backgrounds.js'
  4. import AutoAnimate from './controllers/autoanimate.js'
  5. import Fragments from './controllers/fragments.js'
  6. import Overview from './controllers/overview.js'
  7. import Keyboard from './controllers/keyboard.js'
  8. import Location from './controllers/location.js'
  9. import Controls from './controllers/controls.js'
  10. import Progress from './controllers/progress.js'
  11. import Pointer from './controllers/pointer.js'
  12. import Plugins from './controllers/plugins.js'
  13. import Print from './controllers/print.js'
  14. import Touch from './controllers/touch.js'
  15. import Focus from './controllers/focus.js'
  16. import Notes from './controllers/notes.js'
  17. import Playback from './components/playback.js'
  18. import defaultConfig from './config.js'
  19. import * as Util from './utils/util.js'
  20. import * as Device from './utils/device.js'
  21. import {
  22. SLIDES_SELECTOR,
  23. HORIZONTAL_SLIDES_SELECTOR,
  24. VERTICAL_SLIDES_SELECTOR,
  25. POST_MESSAGE_METHOD_BLACKLIST
  26. } from './utils/constants.js'
  27. // The reveal.js version
  28. export const VERSION = '4.0.2';
  29. /**
  30. * reveal.js
  31. * https://revealjs.com
  32. * MIT licensed
  33. *
  34. * Copyright (C) 2020 Hakim El Hattab, https://hakim.se
  35. */
  36. export default function( revealElement, options ) {
  37. // Support initialization with no args, one arg
  38. // [options] or two args [revealElement, options]
  39. if( arguments.length < 2 ) {
  40. options = arguments[0];
  41. revealElement = document.querySelector( '.reveal' );
  42. }
  43. const Reveal = {};
  44. // Configuration defaults, can be overridden at initialization time
  45. let config = {},
  46. // Flags if reveal.js is loaded (has dispatched the 'ready' event)
  47. ready = false,
  48. // The horizontal and vertical index of the currently active slide
  49. indexh,
  50. indexv,
  51. // The previous and current slide HTML elements
  52. previousSlide,
  53. currentSlide,
  54. // Remember which directions that the user has navigated towards
  55. navigationHistory = {
  56. hasNavigatedHorizontally: false,
  57. hasNavigatedVertically: false
  58. },
  59. // Slides may have a data-state attribute which we pick up and apply
  60. // as a class to the body. This list contains the combined state of
  61. // all current slides.
  62. state = [],
  63. // The current scale of the presentation (see width/height config)
  64. scale = 1,
  65. // CSS transform that is currently applied to the slides container,
  66. // split into two groups
  67. slidesTransform = { layout: '', overview: '' },
  68. // Cached references to DOM elements
  69. dom = {},
  70. // Flags if the interaction event listeners are bound
  71. eventsAreBound = false,
  72. // The current slide transition state; idle or running
  73. transition = 'idle',
  74. // The current auto-slide duration
  75. autoSlide = 0,
  76. // Auto slide properties
  77. autoSlidePlayer,
  78. autoSlideTimeout = 0,
  79. autoSlideStartTime = -1,
  80. autoSlidePaused = false,
  81. // Controllers for different aspects of our presentation. They're
  82. // all given direct references to this Reveal instance since there
  83. // may be multiple presentations running in parallel.
  84. slideContent = new SlideContent( Reveal ),
  85. slideNumber = new SlideNumber( Reveal ),
  86. autoAnimate = new AutoAnimate( Reveal ),
  87. backgrounds = new Backgrounds( Reveal ),
  88. fragments = new Fragments( Reveal ),
  89. overview = new Overview( Reveal ),
  90. keyboard = new Keyboard( Reveal ),
  91. location = new Location( Reveal ),
  92. controls = new Controls( Reveal ),
  93. progress = new Progress( Reveal ),
  94. pointer = new Pointer( Reveal ),
  95. plugins = new Plugins( Reveal ),
  96. print = new Print( Reveal ),
  97. focus = new Focus( Reveal ),
  98. touch = new Touch( Reveal ),
  99. notes = new Notes( Reveal );
  100. /**
  101. * Starts up the presentation.
  102. */
  103. function initialize( initOptions ) {
  104. // Cache references to key DOM elements
  105. dom.wrapper = revealElement;
  106. dom.slides = revealElement.querySelector( '.slides' );
  107. // Compose our config object in order of increasing precedence:
  108. // 1. Default reveal.js options
  109. // 2. Options provided via Reveal.configure() prior to
  110. // initialization
  111. // 3. Options passed to the Reveal constructor
  112. // 4. Options passed to Reveal.initialize
  113. // 5. Query params
  114. config = { ...defaultConfig, ...config, ...options, ...initOptions, ...Util.getQueryHash() };
  115. setViewport();
  116. // Force a layout when the whole page, incl fonts, has loaded
  117. window.addEventListener( 'load', layout, false );
  118. // Register plugins and load dependencies, then move on to #start()
  119. plugins.load( config.plugins, config.dependencies ).then( start );
  120. return new Promise( resolve => Reveal.on( 'ready', resolve ) );
  121. }
  122. /**
  123. * Encase the presentation in a reveal.js viewport. The
  124. * extent of the viewport differs based on configuration.
  125. */
  126. function setViewport() {
  127. // Embedded decks use the reveal element as their viewport
  128. if( config.embedded === true ) {
  129. dom.viewport = Util.closest( revealElement, '.reveal-viewport' ) || revealElement;
  130. }
  131. // Full-page decks use the body as their viewport
  132. else {
  133. dom.viewport = document.body;
  134. document.documentElement.classList.add( 'reveal-full-page' );
  135. }
  136. dom.viewport.classList.add( 'reveal-viewport' );
  137. }
  138. /**
  139. * Starts up reveal.js by binding input events and navigating
  140. * to the current URL deeplink if there is one.
  141. */
  142. function start() {
  143. ready = true;
  144. // Make sure we've got all the DOM elements we need
  145. setupDOM();
  146. // Listen to messages posted to this window
  147. setupPostMessage();
  148. // Prevent the slides from being scrolled out of view
  149. setupScrollPrevention();
  150. // Resets all vertical slides so that only the first is visible
  151. resetVerticalSlides();
  152. // Updates the presentation to match the current configuration values
  153. configure();
  154. // Read the initial hash
  155. location.readURL();
  156. // Create slide backgrounds
  157. backgrounds.update( true );
  158. // Notify listeners that the presentation is ready but use a 1ms
  159. // timeout to ensure it's not fired synchronously after #initialize()
  160. setTimeout( () => {
  161. // Enable transitions now that we're loaded
  162. dom.slides.classList.remove( 'no-transition' );
  163. dom.wrapper.classList.add( 'ready' );
  164. dispatchEvent({
  165. type: 'ready',
  166. data: {
  167. indexh,
  168. indexv,
  169. currentSlide
  170. }
  171. });
  172. }, 1 );
  173. // Special setup and config is required when printing to PDF
  174. if( print.isPrintingPDF() ) {
  175. removeEventListeners();
  176. // The document needs to have loaded for the PDF layout
  177. // measurements to be accurate
  178. if( document.readyState === 'complete' ) {
  179. print.setupPDF();
  180. }
  181. else {
  182. window.addEventListener( 'load', () => {
  183. print.setupPDF();
  184. } );
  185. }
  186. }
  187. }
  188. /**
  189. * Finds and stores references to DOM elements which are
  190. * required by the presentation. If a required element is
  191. * not found, it is created.
  192. */
  193. function setupDOM() {
  194. // Prevent transitions while we're loading
  195. dom.slides.classList.add( 'no-transition' );
  196. if( Device.isMobile ) {
  197. dom.wrapper.classList.add( 'no-hover' );
  198. }
  199. else {
  200. dom.wrapper.classList.remove( 'no-hover' );
  201. }
  202. backgrounds.render();
  203. slideNumber.render();
  204. controls.render();
  205. progress.render();
  206. notes.render();
  207. // Overlay graphic which is displayed during the paused mode
  208. dom.pauseOverlay = Util.createSingletonNode( dom.wrapper, 'div', 'pause-overlay', config.controls ? '<button class="resume-button">Resume presentation</button>' : null );
  209. dom.statusElement = createStatusElement();
  210. dom.wrapper.setAttribute( 'role', 'application' );
  211. }
  212. /**
  213. * Creates a hidden div with role aria-live to announce the
  214. * current slide content. Hide the div off-screen to make it
  215. * available only to Assistive Technologies.
  216. *
  217. * @return {HTMLElement}
  218. */
  219. function createStatusElement() {
  220. let statusElement = dom.wrapper.querySelector( '.aria-status' );
  221. if( !statusElement ) {
  222. statusElement = document.createElement( 'div' );
  223. statusElement.style.position = 'absolute';
  224. statusElement.style.height = '1px';
  225. statusElement.style.width = '1px';
  226. statusElement.style.overflow = 'hidden';
  227. statusElement.style.clip = 'rect( 1px, 1px, 1px, 1px )';
  228. statusElement.classList.add( 'aria-status' );
  229. statusElement.setAttribute( 'aria-live', 'polite' );
  230. statusElement.setAttribute( 'aria-atomic','true' );
  231. dom.wrapper.appendChild( statusElement );
  232. }
  233. return statusElement;
  234. }
  235. /**
  236. * Announces the given text to screen readers.
  237. */
  238. function announceStatus( value ) {
  239. dom.statusElement.textContent = value;
  240. }
  241. /**
  242. * Converts the given HTML element into a string of text
  243. * that can be announced to a screen reader. Hidden
  244. * elements are excluded.
  245. */
  246. function getStatusText( node ) {
  247. let text = '';
  248. // Text node
  249. if( node.nodeType === 3 ) {
  250. text += node.textContent;
  251. }
  252. // Element node
  253. else if( node.nodeType === 1 ) {
  254. let isAriaHidden = node.getAttribute( 'aria-hidden' );
  255. let isDisplayHidden = window.getComputedStyle( node )['display'] === 'none';
  256. if( isAriaHidden !== 'true' && !isDisplayHidden ) {
  257. Array.from( node.childNodes ).forEach( child => {
  258. text += getStatusText( child );
  259. } );
  260. }
  261. }
  262. text = text.trim();
  263. return text === '' ? '' : text + ' ';
  264. }
  265. /**
  266. * This is an unfortunate necessity. Some actions – such as
  267. * an input field being focused in an iframe or using the
  268. * keyboard to expand text selection beyond the bounds of
  269. * a slide – can trigger our content to be pushed out of view.
  270. * This scrolling can not be prevented by hiding overflow in
  271. * CSS (we already do) so we have to resort to repeatedly
  272. * checking if the slides have been offset :(
  273. */
  274. function setupScrollPrevention() {
  275. setInterval( () => {
  276. if( dom.wrapper.scrollTop !== 0 || dom.wrapper.scrollLeft !== 0 ) {
  277. dom.wrapper.scrollTop = 0;
  278. dom.wrapper.scrollLeft = 0;
  279. }
  280. }, 1000 );
  281. }
  282. /**
  283. * Registers a listener to postMessage events, this makes it
  284. * possible to call all reveal.js API methods from another
  285. * window. For example:
  286. *
  287. * revealWindow.postMessage( JSON.stringify({
  288. * method: 'slide',
  289. * args: [ 2 ]
  290. * }), '*' );
  291. */
  292. function setupPostMessage() {
  293. if( config.postMessage ) {
  294. window.addEventListener( 'message', event => {
  295. let data = event.data;
  296. // Make sure we're dealing with JSON
  297. if( typeof data === 'string' && data.charAt( 0 ) === '{' && data.charAt( data.length - 1 ) === '}' ) {
  298. data = JSON.parse( data );
  299. // Check if the requested method can be found
  300. if( data.method && typeof Reveal[data.method] === 'function' ) {
  301. if( POST_MESSAGE_METHOD_BLACKLIST.test( data.method ) === false ) {
  302. const result = Reveal[data.method].apply( Reveal, data.args );
  303. // Dispatch a postMessage event with the returned value from
  304. // our method invocation for getter functions
  305. dispatchPostMessage( 'callback', { method: data.method, result: result } );
  306. }
  307. else {
  308. console.warn( 'reveal.js: "'+ data.method +'" is is blacklisted from the postMessage API' );
  309. }
  310. }
  311. }
  312. }, false );
  313. }
  314. }
  315. /**
  316. * Applies the configuration settings from the config
  317. * object. May be called multiple times.
  318. *
  319. * @param {object} options
  320. */
  321. function configure( options ) {
  322. const oldConfig = { ...config }
  323. // New config options may be passed when this method
  324. // is invoked through the API after initialization
  325. if( typeof options === 'object' ) Util.extend( config, options );
  326. // Abort if reveal.js hasn't finished loading, config
  327. // changes will be applied automatically once ready
  328. if( Reveal.isReady() === false ) return;
  329. const numberOfSlides = dom.wrapper.querySelectorAll( SLIDES_SELECTOR ).length;
  330. // The transition is added as a class on the .reveal element
  331. dom.wrapper.classList.remove( oldConfig.transition );
  332. dom.wrapper.classList.add( config.transition );
  333. dom.wrapper.setAttribute( 'data-transition-speed', config.transitionSpeed );
  334. dom.wrapper.setAttribute( 'data-background-transition', config.backgroundTransition );
  335. if( config.shuffle ) {
  336. shuffle();
  337. }
  338. Util.toggleClass( dom.wrapper, 'embedded', config.embedded );
  339. Util.toggleClass( dom.wrapper, 'rtl', config.rtl );
  340. Util.toggleClass( dom.wrapper, 'center', config.center );
  341. // Exit the paused mode if it was configured off
  342. if( config.pause === false ) {
  343. resume();
  344. }
  345. // Iframe link previews
  346. if( config.previewLinks ) {
  347. enablePreviewLinks();
  348. disablePreviewLinks( '[data-preview-link=false]' );
  349. }
  350. else {
  351. disablePreviewLinks();
  352. enablePreviewLinks( '[data-preview-link]:not([data-preview-link=false])' );
  353. }
  354. // Reset all changes made by auto-animations
  355. autoAnimate.reset();
  356. // Remove existing auto-slide controls
  357. if( autoSlidePlayer ) {
  358. autoSlidePlayer.destroy();
  359. autoSlidePlayer = null;
  360. }
  361. // Generate auto-slide controls if needed
  362. if( numberOfSlides > 1 && config.autoSlide && config.autoSlideStoppable ) {
  363. autoSlidePlayer = new Playback( dom.wrapper, () => {
  364. return Math.min( Math.max( ( Date.now() - autoSlideStartTime ) / autoSlide, 0 ), 1 );
  365. } );
  366. autoSlidePlayer.on( 'click', onAutoSlidePlayerClick );
  367. autoSlidePaused = false;
  368. }
  369. // Add the navigation mode to the DOM so we can adjust styling
  370. if( config.navigationMode !== 'default' ) {
  371. dom.wrapper.setAttribute( 'data-navigation-mode', config.navigationMode );
  372. }
  373. else {
  374. dom.wrapper.removeAttribute( 'data-navigation-mode' );
  375. }
  376. notes.configure( config, oldConfig );
  377. focus.configure( config, oldConfig );
  378. pointer.configure( config, oldConfig );
  379. controls.configure( config, oldConfig );
  380. progress.configure( config, oldConfig );
  381. keyboard.configure( config, oldConfig );
  382. fragments.configure( config, oldConfig );
  383. slideNumber.configure( config, oldConfig );
  384. sync();
  385. }
  386. /**
  387. * Binds all event listeners.
  388. */
  389. function addEventListeners() {
  390. eventsAreBound = true;
  391. window.addEventListener( 'resize', onWindowResize, false );
  392. if( config.touch ) touch.bind();
  393. if( config.keyboard ) keyboard.bind();
  394. if( config.progress ) progress.bind();
  395. if( config.respondToHashChanges ) location.bind();
  396. controls.bind();
  397. focus.bind();
  398. dom.slides.addEventListener( 'transitionend', onTransitionEnd, false );
  399. dom.pauseOverlay.addEventListener( 'click', resume, false );
  400. if( config.focusBodyOnPageVisibilityChange ) {
  401. document.addEventListener( 'visibilitychange', onPageVisibilityChange, false );
  402. }
  403. }
  404. /**
  405. * Unbinds all event listeners.
  406. */
  407. function removeEventListeners() {
  408. eventsAreBound = false;
  409. touch.unbind();
  410. focus.unbind();
  411. keyboard.unbind();
  412. controls.unbind();
  413. progress.unbind();
  414. location.unbind();
  415. window.removeEventListener( 'resize', onWindowResize, false );
  416. dom.slides.removeEventListener( 'transitionend', onTransitionEnd, false );
  417. dom.pauseOverlay.removeEventListener( 'click', resume, false );
  418. }
  419. /**
  420. * Adds a listener to one of our custom reveal.js events,
  421. * like slidechanged.
  422. */
  423. function on( type, listener, useCapture ) {
  424. revealElement.addEventListener( type, listener, useCapture );
  425. }
  426. /**
  427. * Unsubscribes from a reveal.js event.
  428. */
  429. function off( type, listener, useCapture ) {
  430. revealElement.removeEventListener( type, listener, useCapture );
  431. }
  432. /**
  433. * Applies CSS transforms to the slides container. The container
  434. * is transformed from two separate sources: layout and the overview
  435. * mode.
  436. *
  437. * @param {object} transforms
  438. */
  439. function transformSlides( transforms ) {
  440. // Pick up new transforms from arguments
  441. if( typeof transforms.layout === 'string' ) slidesTransform.layout = transforms.layout;
  442. if( typeof transforms.overview === 'string' ) slidesTransform.overview = transforms.overview;
  443. // Apply the transforms to the slides container
  444. if( slidesTransform.layout ) {
  445. Util.transformElement( dom.slides, slidesTransform.layout + ' ' + slidesTransform.overview );
  446. }
  447. else {
  448. Util.transformElement( dom.slides, slidesTransform.overview );
  449. }
  450. }
  451. /**
  452. * Dispatches an event of the specified type from the
  453. * reveal DOM element.
  454. */
  455. function dispatchEvent({ target=dom.wrapper, type, data, bubbles=true }) {
  456. let event = document.createEvent( 'HTMLEvents', 1, 2 );
  457. event.initEvent( type, bubbles, true );
  458. Util.extend( event, data );
  459. target.dispatchEvent( event );
  460. if( target === dom.wrapper ) {
  461. // If we're in an iframe, post each reveal.js event to the
  462. // parent window. Used by the notes plugin
  463. dispatchPostMessage( type );
  464. }
  465. }
  466. /**
  467. * Dispatched a postMessage of the given type from our window.
  468. */
  469. function dispatchPostMessage( type, data ) {
  470. if( config.postMessageEvents && window.parent !== window.self ) {
  471. let message = {
  472. namespace: 'reveal',
  473. eventName: type,
  474. state: getState()
  475. };
  476. Util.extend( message, data );
  477. window.parent.postMessage( JSON.stringify( message ), '*' );
  478. }
  479. }
  480. /**
  481. * Bind preview frame links.
  482. *
  483. * @param {string} [selector=a] - selector for anchors
  484. */
  485. function enablePreviewLinks( selector = 'a' ) {
  486. Array.from( dom.wrapper.querySelectorAll( selector ) ).forEach( element => {
  487. if( /^(http|www)/gi.test( element.getAttribute( 'href' ) ) ) {
  488. element.addEventListener( 'click', onPreviewLinkClicked, false );
  489. }
  490. } );
  491. }
  492. /**
  493. * Unbind preview frame links.
  494. */
  495. function disablePreviewLinks( selector = 'a' ) {
  496. Array.from( dom.wrapper.querySelectorAll( selector ) ).forEach( element => {
  497. if( /^(http|www)/gi.test( element.getAttribute( 'href' ) ) ) {
  498. element.removeEventListener( 'click', onPreviewLinkClicked, false );
  499. }
  500. } );
  501. }
  502. /**
  503. * Opens a preview window for the target URL.
  504. *
  505. * @param {string} url - url for preview iframe src
  506. */
  507. function showPreview( url ) {
  508. closeOverlay();
  509. dom.overlay = document.createElement( 'div' );
  510. dom.overlay.classList.add( 'overlay' );
  511. dom.overlay.classList.add( 'overlay-preview' );
  512. dom.wrapper.appendChild( dom.overlay );
  513. dom.overlay.innerHTML =
  514. `<header>
  515. <a class="close" href="#"><span class="icon"></span></a>
  516. <a class="external" href="${url}" target="_blank"><span class="icon"></span></a>
  517. </header>
  518. <div class="spinner"></div>
  519. <div class="viewport">
  520. <iframe src="${url}"></iframe>
  521. <small class="viewport-inner">
  522. <span class="x-frame-error">Unable to load iframe. This is likely due to the site's policy (x-frame-options).</span>
  523. </small>
  524. </div>`;
  525. dom.overlay.querySelector( 'iframe' ).addEventListener( 'load', event => {
  526. dom.overlay.classList.add( 'loaded' );
  527. }, false );
  528. dom.overlay.querySelector( '.close' ).addEventListener( 'click', event => {
  529. closeOverlay();
  530. event.preventDefault();
  531. }, false );
  532. dom.overlay.querySelector( '.external' ).addEventListener( 'click', event => {
  533. closeOverlay();
  534. }, false );
  535. }
  536. /**
  537. * Open or close help overlay window.
  538. *
  539. * @param {Boolean} [override] Flag which overrides the
  540. * toggle logic and forcibly sets the desired state. True means
  541. * help is open, false means it's closed.
  542. */
  543. function toggleHelp( override ){
  544. if( typeof override === 'boolean' ) {
  545. override ? showHelp() : closeOverlay();
  546. }
  547. else {
  548. if( dom.overlay ) {
  549. closeOverlay();
  550. }
  551. else {
  552. showHelp();
  553. }
  554. }
  555. }
  556. /**
  557. * Opens an overlay window with help material.
  558. */
  559. function showHelp() {
  560. if( config.help ) {
  561. closeOverlay();
  562. dom.overlay = document.createElement( 'div' );
  563. dom.overlay.classList.add( 'overlay' );
  564. dom.overlay.classList.add( 'overlay-help' );
  565. dom.wrapper.appendChild( dom.overlay );
  566. let html = '<p class="title">Keyboard Shortcuts</p><br/>';
  567. let shortcuts = keyboard.getShortcuts(),
  568. bindings = keyboard.getBindings();
  569. html += '<table><th>KEY</th><th>ACTION</th>';
  570. for( let key in shortcuts ) {
  571. html += `<tr><td>${key}</td><td>${shortcuts[ key ]}</td></tr>`;
  572. }
  573. // Add custom key bindings that have associated descriptions
  574. for( let binding in bindings ) {
  575. if( bindings[binding].key && bindings[binding].description ) {
  576. html += `<tr><td>${bindings[binding].key}</td><td>${bindings[binding].description}</td></tr>`;
  577. }
  578. }
  579. html += '</table>';
  580. dom.overlay.innerHTML = `
  581. <header>
  582. <a class="close" href="#"><span class="icon"></span></a>
  583. </header>
  584. <div class="viewport">
  585. <div class="viewport-inner">${html}</div>
  586. </div>
  587. `;
  588. dom.overlay.querySelector( '.close' ).addEventListener( 'click', event => {
  589. closeOverlay();
  590. event.preventDefault();
  591. }, false );
  592. }
  593. }
  594. /**
  595. * Closes any currently open overlay.
  596. */
  597. function closeOverlay() {
  598. if( dom.overlay ) {
  599. dom.overlay.parentNode.removeChild( dom.overlay );
  600. dom.overlay = null;
  601. return true;
  602. }
  603. return false;
  604. }
  605. /**
  606. * Applies JavaScript-controlled layout rules to the
  607. * presentation.
  608. */
  609. function layout() {
  610. if( dom.wrapper && !print.isPrintingPDF() ) {
  611. if( !config.disableLayout ) {
  612. // On some mobile devices '100vh' is taller than the visible
  613. // viewport which leads to part of the presentation being
  614. // cut off. To work around this we define our own '--vh' custom
  615. // property where 100x adds up to the correct height.
  616. //
  617. // https://css-tricks.com/the-trick-to-viewport-units-on-mobile/
  618. if( Device.isMobile && !config.embedded ) {
  619. document.documentElement.style.setProperty( '--vh', ( window.innerHeight * 0.01 ) + 'px' );
  620. }
  621. const size = getComputedSlideSize();
  622. const oldScale = scale;
  623. // Layout the contents of the slides
  624. layoutSlideContents( config.width, config.height );
  625. dom.slides.style.width = size.width + 'px';
  626. dom.slides.style.height = size.height + 'px';
  627. // Determine scale of content to fit within available space
  628. scale = Math.min( size.presentationWidth / size.width, size.presentationHeight / size.height );
  629. // Respect max/min scale settings
  630. scale = Math.max( scale, config.minScale );
  631. scale = Math.min( scale, config.maxScale );
  632. // Don't apply any scaling styles if scale is 1
  633. if( scale === 1 ) {
  634. dom.slides.style.zoom = '';
  635. dom.slides.style.left = '';
  636. dom.slides.style.top = '';
  637. dom.slides.style.bottom = '';
  638. dom.slides.style.right = '';
  639. transformSlides( { layout: '' } );
  640. }
  641. else {
  642. // Zoom Scaling
  643. // Content remains crisp no matter how much we scale. Side
  644. // effects are minor differences in text layout and iframe
  645. // viewports changing size. A 200x200 iframe viewport in a
  646. // 2x zoomed presentation ends up having a 400x400 viewport.
  647. if( scale > 1 && Device.supportsZoom && window.devicePixelRatio < 2 ) {
  648. dom.slides.style.zoom = scale;
  649. dom.slides.style.left = '';
  650. dom.slides.style.top = '';
  651. dom.slides.style.bottom = '';
  652. dom.slides.style.right = '';
  653. transformSlides( { layout: '' } );
  654. }
  655. // Transform Scaling
  656. // Content layout remains the exact same when scaled up.
  657. // Side effect is content becoming blurred, especially with
  658. // high scale values on ldpi screens.
  659. else {
  660. dom.slides.style.zoom = '';
  661. dom.slides.style.left = '50%';
  662. dom.slides.style.top = '50%';
  663. dom.slides.style.bottom = 'auto';
  664. dom.slides.style.right = 'auto';
  665. transformSlides( { layout: 'translate(-50%, -50%) scale('+ scale +')' } );
  666. }
  667. }
  668. // Select all slides, vertical and horizontal
  669. const slides = Array.from( dom.wrapper.querySelectorAll( SLIDES_SELECTOR ) );
  670. for( let i = 0, len = slides.length; i < len; i++ ) {
  671. const slide = slides[ i ];
  672. // Don't bother updating invisible slides
  673. if( slide.style.display === 'none' ) {
  674. continue;
  675. }
  676. if( config.center || slide.classList.contains( 'center' ) ) {
  677. // Vertical stacks are not centred since their section
  678. // children will be
  679. if( slide.classList.contains( 'stack' ) ) {
  680. slide.style.top = 0;
  681. }
  682. else {
  683. slide.style.top = Math.max( ( size.height - slide.scrollHeight ) / 2, 0 ) + 'px';
  684. }
  685. }
  686. else {
  687. slide.style.top = '';
  688. }
  689. }
  690. if( oldScale !== scale ) {
  691. dispatchEvent({
  692. type: 'resize',
  693. data: {
  694. oldScale,
  695. scale,
  696. size
  697. }
  698. });
  699. }
  700. }
  701. progress.update();
  702. backgrounds.updateParallax();
  703. if( overview.isActive() ) {
  704. overview.update();
  705. }
  706. }
  707. }
  708. /**
  709. * Applies layout logic to the contents of all slides in
  710. * the presentation.
  711. *
  712. * @param {string|number} width
  713. * @param {string|number} height
  714. */
  715. function layoutSlideContents( width, height ) {
  716. // Handle sizing of elements with the 'r-stretch' class
  717. Util.queryAll( dom.slides, 'section > .stretch, section > .r-stretch' ).forEach( element => {
  718. // Determine how much vertical space we can use
  719. let remainingHeight = Util.getRemainingHeight( element, height );
  720. // Consider the aspect ratio of media elements
  721. if( /(img|video)/gi.test( element.nodeName ) ) {
  722. const nw = element.naturalWidth || element.videoWidth,
  723. nh = element.naturalHeight || element.videoHeight;
  724. const es = Math.min( width / nw, remainingHeight / nh );
  725. element.style.width = ( nw * es ) + 'px';
  726. element.style.height = ( nh * es ) + 'px';
  727. }
  728. else {
  729. element.style.width = width + 'px';
  730. element.style.height = remainingHeight + 'px';
  731. }
  732. } );
  733. }
  734. /**
  735. * Calculates the computed pixel size of our slides. These
  736. * values are based on the width and height configuration
  737. * options.
  738. *
  739. * @param {number} [presentationWidth=dom.wrapper.offsetWidth]
  740. * @param {number} [presentationHeight=dom.wrapper.offsetHeight]
  741. */
  742. function getComputedSlideSize( presentationWidth, presentationHeight ) {
  743. const size = {
  744. // Slide size
  745. width: config.width,
  746. height: config.height,
  747. // Presentation size
  748. presentationWidth: presentationWidth || dom.wrapper.offsetWidth,
  749. presentationHeight: presentationHeight || dom.wrapper.offsetHeight
  750. };
  751. // Reduce available space by margin
  752. size.presentationWidth -= ( size.presentationWidth * config.margin );
  753. size.presentationHeight -= ( size.presentationHeight * config.margin );
  754. // Slide width may be a percentage of available width
  755. if( typeof size.width === 'string' && /%$/.test( size.width ) ) {
  756. size.width = parseInt( size.width, 10 ) / 100 * size.presentationWidth;
  757. }
  758. // Slide height may be a percentage of available height
  759. if( typeof size.height === 'string' && /%$/.test( size.height ) ) {
  760. size.height = parseInt( size.height, 10 ) / 100 * size.presentationHeight;
  761. }
  762. return size;
  763. }
  764. /**
  765. * Stores the vertical index of a stack so that the same
  766. * vertical slide can be selected when navigating to and
  767. * from the stack.
  768. *
  769. * @param {HTMLElement} stack The vertical stack element
  770. * @param {string|number} [v=0] Index to memorize
  771. */
  772. function setPreviousVerticalIndex( stack, v ) {
  773. if( typeof stack === 'object' && typeof stack.setAttribute === 'function' ) {
  774. stack.setAttribute( 'data-previous-indexv', v || 0 );
  775. }
  776. }
  777. /**
  778. * Retrieves the vertical index which was stored using
  779. * #setPreviousVerticalIndex() or 0 if no previous index
  780. * exists.
  781. *
  782. * @param {HTMLElement} stack The vertical stack element
  783. */
  784. function getPreviousVerticalIndex( stack ) {
  785. if( typeof stack === 'object' && typeof stack.setAttribute === 'function' && stack.classList.contains( 'stack' ) ) {
  786. // Prefer manually defined start-indexv
  787. const attributeName = stack.hasAttribute( 'data-start-indexv' ) ? 'data-start-indexv' : 'data-previous-indexv';
  788. return parseInt( stack.getAttribute( attributeName ) || 0, 10 );
  789. }
  790. return 0;
  791. }
  792. /**
  793. * Checks if the current or specified slide is vertical
  794. * (nested within another slide).
  795. *
  796. * @param {HTMLElement} [slide=currentSlide] The slide to check
  797. * orientation of
  798. * @return {Boolean}
  799. */
  800. function isVerticalSlide( slide = currentSlide ) {
  801. return slide && slide.parentNode && !!slide.parentNode.nodeName.match( /section/i );
  802. }
  803. /**
  804. * Returns true if we're on the last slide in the current
  805. * vertical stack.
  806. */
  807. function isLastVerticalSlide() {
  808. if( currentSlide && isVerticalSlide( currentSlide ) ) {
  809. // Does this slide have a next sibling?
  810. if( currentSlide.nextElementSibling ) return false;
  811. return true;
  812. }
  813. return false;
  814. }
  815. /**
  816. * Returns true if we're currently on the first slide in
  817. * the presentation.
  818. */
  819. function isFirstSlide() {
  820. return indexh === 0 && indexv === 0;
  821. }
  822. /**
  823. * Returns true if we're currently on the last slide in
  824. * the presenation. If the last slide is a stack, we only
  825. * consider this the last slide if it's at the end of the
  826. * stack.
  827. */
  828. function isLastSlide() {
  829. if( currentSlide ) {
  830. // Does this slide have a next sibling?
  831. if( currentSlide.nextElementSibling ) return false;
  832. // If it's vertical, does its parent have a next sibling?
  833. if( isVerticalSlide( currentSlide ) && currentSlide.parentNode.nextElementSibling ) return false;
  834. return true;
  835. }
  836. return false;
  837. }
  838. /**
  839. * Enters the paused mode which fades everything on screen to
  840. * black.
  841. */
  842. function pause() {
  843. if( config.pause ) {
  844. const wasPaused = dom.wrapper.classList.contains( 'paused' );
  845. cancelAutoSlide();
  846. dom.wrapper.classList.add( 'paused' );
  847. if( wasPaused === false ) {
  848. dispatchEvent({ type: 'paused' });
  849. }
  850. }
  851. }
  852. /**
  853. * Exits from the paused mode.
  854. */
  855. function resume() {
  856. const wasPaused = dom.wrapper.classList.contains( 'paused' );
  857. dom.wrapper.classList.remove( 'paused' );
  858. cueAutoSlide();
  859. if( wasPaused ) {
  860. dispatchEvent({ type: 'resumed' });
  861. }
  862. }
  863. /**
  864. * Toggles the paused mode on and off.
  865. */
  866. function togglePause( override ) {
  867. if( typeof override === 'boolean' ) {
  868. override ? pause() : resume();
  869. }
  870. else {
  871. isPaused() ? resume() : pause();
  872. }
  873. }
  874. /**
  875. * Checks if we are currently in the paused mode.
  876. *
  877. * @return {Boolean}
  878. */
  879. function isPaused() {
  880. return dom.wrapper.classList.contains( 'paused' );
  881. }
  882. /**
  883. * Toggles the auto slide mode on and off.
  884. *
  885. * @param {Boolean} [override] Flag which sets the desired state.
  886. * True means autoplay starts, false means it stops.
  887. */
  888. function toggleAutoSlide( override ) {
  889. if( typeof override === 'boolean' ) {
  890. override ? resumeAutoSlide() : pauseAutoSlide();
  891. }
  892. else {
  893. autoSlidePaused ? resumeAutoSlide() : pauseAutoSlide();
  894. }
  895. }
  896. /**
  897. * Checks if the auto slide mode is currently on.
  898. *
  899. * @return {Boolean}
  900. */
  901. function isAutoSliding() {
  902. return !!( autoSlide && !autoSlidePaused );
  903. }
  904. /**
  905. * Steps from the current point in the presentation to the
  906. * slide which matches the specified horizontal and vertical
  907. * indices.
  908. *
  909. * @param {number} [h=indexh] Horizontal index of the target slide
  910. * @param {number} [v=indexv] Vertical index of the target slide
  911. * @param {number} [f] Index of a fragment within the
  912. * target slide to activate
  913. * @param {number} [o] Origin for use in multimaster environments
  914. */
  915. function slide( h, v, f, o ) {
  916. // Remember where we were at before
  917. previousSlide = currentSlide;
  918. // Query all horizontal slides in the deck
  919. const horizontalSlides = dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR );
  920. // Abort if there are no slides
  921. if( horizontalSlides.length === 0 ) return;
  922. // If no vertical index is specified and the upcoming slide is a
  923. // stack, resume at its previous vertical index
  924. if( v === undefined && !overview.isActive() ) {
  925. v = getPreviousVerticalIndex( horizontalSlides[ h ] );
  926. }
  927. // If we were on a vertical stack, remember what vertical index
  928. // it was on so we can resume at the same position when returning
  929. if( previousSlide && previousSlide.parentNode && previousSlide.parentNode.classList.contains( 'stack' ) ) {
  930. setPreviousVerticalIndex( previousSlide.parentNode, indexv );
  931. }
  932. // Remember the state before this slide
  933. const stateBefore = state.concat();
  934. // Reset the state array
  935. state.length = 0;
  936. let indexhBefore = indexh || 0,
  937. indexvBefore = indexv || 0;
  938. // Activate and transition to the new slide
  939. indexh = updateSlides( HORIZONTAL_SLIDES_SELECTOR, h === undefined ? indexh : h );
  940. indexv = updateSlides( VERTICAL_SLIDES_SELECTOR, v === undefined ? indexv : v );
  941. // Dispatch an event if the slide changed
  942. let slideChanged = ( indexh !== indexhBefore || indexv !== indexvBefore );
  943. // Ensure that the previous slide is never the same as the current
  944. if( !slideChanged ) previousSlide = null;
  945. // Find the current horizontal slide and any possible vertical slides
  946. // within it
  947. let currentHorizontalSlide = horizontalSlides[ indexh ],
  948. currentVerticalSlides = currentHorizontalSlide.querySelectorAll( 'section' );
  949. // Store references to the previous and current slides
  950. currentSlide = currentVerticalSlides[ indexv ] || currentHorizontalSlide;
  951. let autoAnimateTransition = false;
  952. // Detect if we're moving between two auto-animated slides
  953. if( slideChanged && previousSlide && currentSlide && !overview.isActive() ) {
  954. // If this is an auto-animated transition, we disable the
  955. // regular slide transition
  956. //
  957. // Note 20-03-2020:
  958. // This needs to happen before we update slide visibility,
  959. // otherwise transitions will still run in Safari.
  960. if( previousSlide.hasAttribute( 'data-auto-animate' ) && currentSlide.hasAttribute( 'data-auto-animate' ) ) {
  961. autoAnimateTransition = true;
  962. dom.slides.classList.add( 'disable-slide-transitions' );
  963. }
  964. transition = 'running';
  965. }
  966. // Update the visibility of slides now that the indices have changed
  967. updateSlidesVisibility();
  968. layout();
  969. // Update the overview if it's currently active
  970. if( overview.isActive() ) {
  971. overview.update();
  972. }
  973. // Show fragment, if specified
  974. if( typeof f !== 'undefined' ) {
  975. fragments.goto( f );
  976. }
  977. // Solves an edge case where the previous slide maintains the
  978. // 'present' class when navigating between adjacent vertical
  979. // stacks
  980. if( previousSlide && previousSlide !== currentSlide ) {
  981. previousSlide.classList.remove( 'present' );
  982. previousSlide.setAttribute( 'aria-hidden', 'true' );
  983. // Reset all slides upon navigate to home
  984. if( isFirstSlide() ) {
  985. // Launch async task
  986. setTimeout( () => {
  987. getVerticalStacks().forEach( slide => {
  988. setPreviousVerticalIndex( slide, 0 );
  989. } );
  990. }, 0 );
  991. }
  992. }
  993. // Apply the new state
  994. stateLoop: for( let i = 0, len = state.length; i < len; i++ ) {
  995. // Check if this state existed on the previous slide. If it
  996. // did, we will avoid adding it repeatedly
  997. for( let j = 0; j < stateBefore.length; j++ ) {
  998. if( stateBefore[j] === state[i] ) {
  999. stateBefore.splice( j, 1 );
  1000. continue stateLoop;
  1001. }
  1002. }
  1003. dom.viewport.classList.add( state[i] );
  1004. // Dispatch custom event matching the state's name
  1005. dispatchEvent({ type: state[i] });
  1006. }
  1007. // Clean up the remains of the previous state
  1008. while( stateBefore.length ) {
  1009. dom.viewport.classList.remove( stateBefore.pop() );
  1010. }
  1011. if( slideChanged ) {
  1012. dispatchEvent({
  1013. type: 'slidechanged',
  1014. data: {
  1015. indexh,
  1016. indexv,
  1017. previousSlide,
  1018. currentSlide,
  1019. origin: o
  1020. }
  1021. });
  1022. }
  1023. // Handle embedded content
  1024. if( slideChanged || !previousSlide ) {
  1025. slideContent.stopEmbeddedContent( previousSlide );
  1026. slideContent.startEmbeddedContent( currentSlide );
  1027. }
  1028. // Announce the current slide contents to screen readers
  1029. announceStatus( getStatusText( currentSlide ) );
  1030. progress.update();
  1031. controls.update();
  1032. notes.update();
  1033. backgrounds.update();
  1034. backgrounds.updateParallax();
  1035. slideNumber.update();
  1036. fragments.update();
  1037. // Update the URL hash
  1038. location.writeURL();
  1039. cueAutoSlide();
  1040. // Auto-animation
  1041. if( autoAnimateTransition ) {
  1042. setTimeout( () => {
  1043. dom.slides.classList.remove( 'disable-slide-transitions' );
  1044. }, 0 );
  1045. if( config.autoAnimate ) {
  1046. // Run the auto-animation between our slides
  1047. autoAnimate.run( previousSlide, currentSlide );
  1048. }
  1049. }
  1050. }
  1051. /**
  1052. * Syncs the presentation with the current DOM. Useful
  1053. * when new slides or control elements are added or when
  1054. * the configuration has changed.
  1055. */
  1056. function sync() {
  1057. // Subscribe to input
  1058. removeEventListeners();
  1059. addEventListeners();
  1060. // Force a layout to make sure the current config is accounted for
  1061. layout();
  1062. // Reflect the current autoSlide value
  1063. autoSlide = config.autoSlide;
  1064. // Start auto-sliding if it's enabled
  1065. cueAutoSlide();
  1066. // Re-create all slide backgrounds
  1067. backgrounds.create();
  1068. // Write the current hash to the URL
  1069. location.writeURL();
  1070. fragments.sortAll();
  1071. controls.update();
  1072. progress.update();
  1073. updateSlidesVisibility();
  1074. notes.update();
  1075. notes.updateVisibility();
  1076. backgrounds.update( true );
  1077. slideNumber.update();
  1078. slideContent.formatEmbeddedContent();
  1079. // Start or stop embedded content depending on global config
  1080. if( config.autoPlayMedia === false ) {
  1081. slideContent.stopEmbeddedContent( currentSlide, { unloadIframes: false } );
  1082. }
  1083. else {
  1084. slideContent.startEmbeddedContent( currentSlide );
  1085. }
  1086. if( overview.isActive() ) {
  1087. overview.layout();
  1088. }
  1089. }
  1090. /**
  1091. * Updates reveal.js to keep in sync with new slide attributes. For
  1092. * example, if you add a new `data-background-image` you can call
  1093. * this to have reveal.js render the new background image.
  1094. *
  1095. * Similar to #sync() but more efficient when you only need to
  1096. * refresh a specific slide.
  1097. *
  1098. * @param {HTMLElement} slide
  1099. */
  1100. function syncSlide( slide = currentSlide ) {
  1101. backgrounds.sync( slide );
  1102. fragments.sync( slide );
  1103. slideContent.load( slide );
  1104. backgrounds.update();
  1105. notes.update();
  1106. }
  1107. /**
  1108. * Resets all vertical slides so that only the first
  1109. * is visible.
  1110. */
  1111. function resetVerticalSlides() {
  1112. getHorizontalSlides().forEach( horizontalSlide => {
  1113. Util.queryAll( horizontalSlide, 'section' ).forEach( ( verticalSlide, y ) => {
  1114. if( y > 0 ) {
  1115. verticalSlide.classList.remove( 'present' );
  1116. verticalSlide.classList.remove( 'past' );
  1117. verticalSlide.classList.add( 'future' );
  1118. verticalSlide.setAttribute( 'aria-hidden', 'true' );
  1119. }
  1120. } );
  1121. } );
  1122. }
  1123. /**
  1124. * Randomly shuffles all slides in the deck.
  1125. */
  1126. function shuffle() {
  1127. getHorizontalSlides().forEach( ( slide, i, slides ) => {
  1128. // Insert this slide next to another random slide. This may
  1129. // cause the slide to insert before itself but that's fine.
  1130. dom.slides.insertBefore( slide, slides[ Math.floor( Math.random() * slides.length ) ] );
  1131. } );
  1132. }
  1133. /**
  1134. * Updates one dimension of slides by showing the slide
  1135. * with the specified index.
  1136. *
  1137. * @param {string} selector A CSS selector that will fetch
  1138. * the group of slides we are working with
  1139. * @param {number} index The index of the slide that should be
  1140. * shown
  1141. *
  1142. * @return {number} The index of the slide that is now shown,
  1143. * might differ from the passed in index if it was out of
  1144. * bounds.
  1145. */
  1146. function updateSlides( selector, index ) {
  1147. // Select all slides and convert the NodeList result to
  1148. // an array
  1149. let slides = Util.queryAll( dom.wrapper, selector ),
  1150. slidesLength = slides.length;
  1151. let printMode = print.isPrintingPDF();
  1152. if( slidesLength ) {
  1153. // Should the index loop?
  1154. if( config.loop ) {
  1155. index %= slidesLength;
  1156. if( index < 0 ) {
  1157. index = slidesLength + index;
  1158. }
  1159. }
  1160. // Enforce max and minimum index bounds
  1161. index = Math.max( Math.min( index, slidesLength - 1 ), 0 );
  1162. for( let i = 0; i < slidesLength; i++ ) {
  1163. let element = slides[i];
  1164. let reverse = config.rtl && !isVerticalSlide( element );
  1165. // Avoid .remove() with multiple args for IE11 support
  1166. element.classList.remove( 'past' );
  1167. element.classList.remove( 'present' );
  1168. element.classList.remove( 'future' );
  1169. // http://www.w3.org/html/wg/drafts/html/master/editing.html#the-hidden-attribute
  1170. element.setAttribute( 'hidden', '' );
  1171. element.setAttribute( 'aria-hidden', 'true' );
  1172. // If this element contains vertical slides
  1173. if( element.querySelector( 'section' ) ) {
  1174. element.classList.add( 'stack' );
  1175. }
  1176. // If we're printing static slides, all slides are "present"
  1177. if( printMode ) {
  1178. element.classList.add( 'present' );
  1179. continue;
  1180. }
  1181. if( i < index ) {
  1182. // Any element previous to index is given the 'past' class
  1183. element.classList.add( reverse ? 'future' : 'past' );
  1184. if( config.fragments ) {
  1185. // Show all fragments in prior slides
  1186. Util.queryAll( element, '.fragment' ).forEach( fragment => {
  1187. fragment.classList.add( 'visible' );
  1188. fragment.classList.remove( 'current-fragment' );
  1189. } );
  1190. }
  1191. }
  1192. else if( i > index ) {
  1193. // Any element subsequent to index is given the 'future' class
  1194. element.classList.add( reverse ? 'past' : 'future' );
  1195. if( config.fragments ) {
  1196. // Hide all fragments in future slides
  1197. Util.queryAll( element, '.fragment.visible' ).forEach( fragment => {
  1198. fragment.classList.remove( 'visible', 'current-fragment' );
  1199. } );
  1200. }
  1201. }
  1202. }
  1203. let slide = slides[index];
  1204. let wasPresent = slide.classList.contains( 'present' );
  1205. // Mark the current slide as present
  1206. slide.classList.add( 'present' );
  1207. slide.removeAttribute( 'hidden' );
  1208. slide.removeAttribute( 'aria-hidden' );
  1209. if( !wasPresent ) {
  1210. // Dispatch an event indicating the slide is now visible
  1211. dispatchEvent({
  1212. target: slide,
  1213. type: 'visible',
  1214. bubbles: false
  1215. });
  1216. }
  1217. // If this slide has a state associated with it, add it
  1218. // onto the current state of the deck
  1219. let slideState = slide.getAttribute( 'data-state' );
  1220. if( slideState ) {
  1221. state = state.concat( slideState.split( ' ' ) );
  1222. }
  1223. }
  1224. else {
  1225. // Since there are no slides we can't be anywhere beyond the
  1226. // zeroth index
  1227. index = 0;
  1228. }
  1229. return index;
  1230. }
  1231. /**
  1232. * Optimization method; hide all slides that are far away
  1233. * from the present slide.
  1234. */
  1235. function updateSlidesVisibility() {
  1236. // Select all slides and convert the NodeList result to
  1237. // an array
  1238. let horizontalSlides = getHorizontalSlides(),
  1239. horizontalSlidesLength = horizontalSlides.length,
  1240. distanceX,
  1241. distanceY;
  1242. if( horizontalSlidesLength && typeof indexh !== 'undefined' ) {
  1243. // The number of steps away from the present slide that will
  1244. // be visible
  1245. let viewDistance = overview.isActive() ? 10 : config.viewDistance;
  1246. // Shorten the view distance on devices that typically have
  1247. // less resources
  1248. if( Device.isMobile ) {
  1249. viewDistance = overview.isActive() ? 6 : config.mobileViewDistance;
  1250. }
  1251. // All slides need to be visible when exporting to PDF
  1252. if( print.isPrintingPDF() ) {
  1253. viewDistance = Number.MAX_VALUE;
  1254. }
  1255. for( let x = 0; x < horizontalSlidesLength; x++ ) {
  1256. let horizontalSlide = horizontalSlides[x];
  1257. let verticalSlides = Util.queryAll( horizontalSlide, 'section' ),
  1258. verticalSlidesLength = verticalSlides.length;
  1259. // Determine how far away this slide is from the present
  1260. distanceX = Math.abs( ( indexh || 0 ) - x ) || 0;
  1261. // If the presentation is looped, distance should measure
  1262. // 1 between the first and last slides
  1263. if( config.loop ) {
  1264. distanceX = Math.abs( ( ( indexh || 0 ) - x ) % ( horizontalSlidesLength - viewDistance ) ) || 0;
  1265. }
  1266. // Show the horizontal slide if it's within the view distance
  1267. if( distanceX < viewDistance ) {
  1268. slideContent.load( horizontalSlide );
  1269. }
  1270. else {
  1271. slideContent.unload( horizontalSlide );
  1272. }
  1273. if( verticalSlidesLength ) {
  1274. let oy = getPreviousVerticalIndex( horizontalSlide );
  1275. for( let y = 0; y < verticalSlidesLength; y++ ) {
  1276. let verticalSlide = verticalSlides[y];
  1277. distanceY = x === ( indexh || 0 ) ? Math.abs( ( indexv || 0 ) - y ) : Math.abs( y - oy );
  1278. if( distanceX + distanceY < viewDistance ) {
  1279. slideContent.load( verticalSlide );
  1280. }
  1281. else {
  1282. slideContent.unload( verticalSlide );
  1283. }
  1284. }
  1285. }
  1286. }
  1287. // Flag if there are ANY vertical slides, anywhere in the deck
  1288. if( hasVerticalSlides() ) {
  1289. dom.wrapper.classList.add( 'has-vertical-slides' );
  1290. }
  1291. else {
  1292. dom.wrapper.classList.remove( 'has-vertical-slides' );
  1293. }
  1294. // Flag if there are ANY horizontal slides, anywhere in the deck
  1295. if( hasHorizontalSlides() ) {
  1296. dom.wrapper.classList.add( 'has-horizontal-slides' );
  1297. }
  1298. else {
  1299. dom.wrapper.classList.remove( 'has-horizontal-slides' );
  1300. }
  1301. }
  1302. }
  1303. /**
  1304. * Determine what available routes there are for navigation.
  1305. *
  1306. * @return {{left: boolean, right: boolean, up: boolean, down: boolean}}
  1307. */
  1308. function availableRoutes({ includeFragments = false } = {}) {
  1309. let horizontalSlides = dom.wrapper.querySelectorAll( HORIZONTAL_SLIDES_SELECTOR ),
  1310. verticalSlides = dom.wrapper.querySelectorAll( VERTICAL_SLIDES_SELECTOR );
  1311. let routes = {
  1312. left: indexh > 0,
  1313. right: indexh < horizontalSlides.length - 1,
  1314. up: indexv > 0,
  1315. down: indexv < verticalSlides.length - 1
  1316. };
  1317. // Looped presentations can always be navigated as long as
  1318. // there are slides available
  1319. if( config.loop ) {
  1320. if( horizontalSlides.length > 1 ) {
  1321. routes.left = true;
  1322. routes.right = true;
  1323. }
  1324. if( verticalSlides.length > 1 ) {
  1325. routes.up = true;
  1326. routes.down = true;
  1327. }
  1328. }
  1329. if ( horizontalSlides.length > 1 && config.navigationMode === 'linear' ) {
  1330. routes.right = routes.right || routes.down;
  1331. routes.left = routes.left || routes.up;
  1332. }
  1333. // If includeFragments is set, a route will be considered
  1334. // availalbe if either a slid OR fragment is available in
  1335. // the given direction
  1336. if( includeFragments === true ) {
  1337. let fragmentRoutes = fragments.availableRoutes();
  1338. routes.left = routes.left || fragmentRoutes.prev;
  1339. routes.up = routes.up || fragmentRoutes.prev;
  1340. routes.down = routes.down || fragmentRoutes.next;
  1341. routes.right = routes.right || fragmentRoutes.next;
  1342. }
  1343. // Reverse horizontal controls for rtl
  1344. if( config.rtl ) {
  1345. let left = routes.left;
  1346. routes.left = routes.right;
  1347. routes.right = left;
  1348. }
  1349. return routes;
  1350. }
  1351. /**
  1352. * Returns the number of past slides. This can be used as a global
  1353. * flattened index for slides.
  1354. *
  1355. * @param {HTMLElement} [slide=currentSlide] The slide we're counting before
  1356. *
  1357. * @return {number} Past slide count
  1358. */
  1359. function getSlidePastCount( slide = currentSlide ) {
  1360. let horizontalSlides = getHorizontalSlides();
  1361. // The number of past slides
  1362. let pastCount = 0;
  1363. // Step through all slides and count the past ones
  1364. mainLoop: for( let i = 0; i < horizontalSlides.length; i++ ) {
  1365. let horizontalSlide = horizontalSlides[i];
  1366. let verticalSlides = horizontalSlide.querySelectorAll( 'section' );
  1367. for( let j = 0; j < verticalSlides.length; j++ ) {
  1368. // Stop as soon as we arrive at the present
  1369. if( verticalSlides[j] === slide ) {
  1370. break mainLoop;
  1371. }
  1372. // Don't count slides with the "uncounted" class
  1373. if( verticalSlides[j].dataset.visibility !== 'uncounted' ) {
  1374. pastCount++;
  1375. }
  1376. }
  1377. // Stop as soon as we arrive at the present
  1378. if( horizontalSlide === slide ) {
  1379. break;
  1380. }
  1381. // Don't count the wrapping section for vertical slides and
  1382. // slides marked as uncounted
  1383. if( horizontalSlide.classList.contains( 'stack' ) === false && !horizontalSlide.dataset.visibility !== 'uncounted' ) {
  1384. pastCount++;
  1385. }
  1386. }
  1387. return pastCount;
  1388. }
  1389. /**
  1390. * Returns a value ranging from 0-1 that represents
  1391. * how far into the presentation we have navigated.
  1392. *
  1393. * @return {number}
  1394. */
  1395. function getProgress() {
  1396. // The number of past and total slides
  1397. let totalCount = getTotalSlides();
  1398. let pastCount = getSlidePastCount();
  1399. if( currentSlide ) {
  1400. let allFragments = currentSlide.querySelectorAll( '.fragment' );
  1401. // If there are fragments in the current slide those should be
  1402. // accounted for in the progress.
  1403. if( allFragments.length > 0 ) {
  1404. let visibleFragments = currentSlide.querySelectorAll( '.fragment.visible' );
  1405. // This value represents how big a portion of the slide progress
  1406. // that is made up by its fragments (0-1)
  1407. let fragmentWeight = 0.9;
  1408. // Add fragment progress to the past slide count
  1409. pastCount += ( visibleFragments.length / allFragments.length ) * fragmentWeight;
  1410. }
  1411. }
  1412. return Math.min( pastCount / ( totalCount - 1 ), 1 );
  1413. }
  1414. /**
  1415. * Retrieves the h/v location and fragment of the current,
  1416. * or specified, slide.
  1417. *
  1418. * @param {HTMLElement} [slide] If specified, the returned
  1419. * index will be for this slide rather than the currently
  1420. * active one
  1421. *
  1422. * @return {{h: number, v: number, f: number}}
  1423. */
  1424. function getIndices( slide ) {
  1425. // By default, return the current indices
  1426. let h = indexh,
  1427. v = indexv,
  1428. f;
  1429. // If a slide is specified, return the indices of that slide
  1430. if( slide ) {
  1431. let isVertical = isVerticalSlide( slide );
  1432. let slideh = isVertical ? slide.parentNode : slide;
  1433. // Select all horizontal slides
  1434. let horizontalSlides = getHorizontalSlides();
  1435. // Now that we know which the horizontal slide is, get its index
  1436. h = Math.max( horizontalSlides.indexOf( slideh ), 0 );
  1437. // Assume we're not vertical
  1438. v = undefined;
  1439. // If this is a vertical slide, grab the vertical index
  1440. if( isVertical ) {
  1441. v = Math.max( Util.queryAll( slide.parentNode, 'section' ).indexOf( slide ), 0 );
  1442. }
  1443. }
  1444. if( !slide && currentSlide ) {
  1445. let hasFragments = currentSlide.querySelectorAll( '.fragment' ).length > 0;
  1446. if( hasFragments ) {
  1447. let currentFragment = currentSlide.querySelector( '.current-fragment' );
  1448. if( currentFragment && currentFragment.hasAttribute( 'data-fragment-index' ) ) {
  1449. f = parseInt( currentFragment.getAttribute( 'data-fragment-index' ), 10 );
  1450. }
  1451. else {
  1452. f = currentSlide.querySelectorAll( '.fragment.visible' ).length - 1;
  1453. }
  1454. }
  1455. }
  1456. return { h, v, f };
  1457. }
  1458. /**
  1459. * Retrieves all slides in this presentation.
  1460. */
  1461. function getSlides() {
  1462. return Util.queryAll( dom.wrapper, SLIDES_SELECTOR + ':not(.stack):not([data-visibility="uncounted"])' );
  1463. }
  1464. /**
  1465. * Returns a list of all horizontal slides in the deck. Each
  1466. * vertical stack is included as one horizontal slide in the
  1467. * resulting array.
  1468. */
  1469. function getHorizontalSlides() {
  1470. return Util.queryAll( dom.wrapper, HORIZONTAL_SLIDES_SELECTOR );
  1471. }
  1472. /**
  1473. * Returns all vertical slides that exist within this deck.
  1474. */
  1475. function getVerticalSlides() {
  1476. return Util.queryAll( dom.wrapper, '.slides>section>section' );
  1477. }
  1478. /**
  1479. * Returns all vertical stacks (each stack can contain multiple slides).
  1480. */
  1481. function getVerticalStacks() {
  1482. return Util.queryAll( dom.wrapper, HORIZONTAL_SLIDES_SELECTOR + '.stack');
  1483. }
  1484. /**
  1485. * Returns true if there are at least two horizontal slides.
  1486. */
  1487. function hasHorizontalSlides() {
  1488. return getHorizontalSlides().length > 1;
  1489. }
  1490. /**
  1491. * Returns true if there are at least two vertical slides.
  1492. */
  1493. function hasVerticalSlides() {
  1494. return getVerticalSlides().length > 1;
  1495. }
  1496. /**
  1497. * Returns an array of objects where each object represents the
  1498. * attributes on its respective slide.
  1499. */
  1500. function getSlidesAttributes() {
  1501. return getSlides().map( slide => {
  1502. let attributes = {};
  1503. for( let i = 0; i < slide.attributes.length; i++ ) {
  1504. let attribute = slide.attributes[ i ];
  1505. attributes[ attribute.name ] = attribute.value;
  1506. }
  1507. return attributes;
  1508. } );
  1509. }
  1510. /**
  1511. * Retrieves the total number of slides in this presentation.
  1512. *
  1513. * @return {number}
  1514. */
  1515. function getTotalSlides() {
  1516. return getSlides().length;
  1517. }
  1518. /**
  1519. * Returns the slide element matching the specified index.
  1520. *
  1521. * @return {HTMLElement}
  1522. */
  1523. function getSlide( x, y ) {
  1524. let horizontalSlide = getHorizontalSlides()[ x ];
  1525. let verticalSlides = horizontalSlide && horizontalSlide.querySelectorAll( 'section' );
  1526. if( verticalSlides && verticalSlides.length && typeof y === 'number' ) {
  1527. return verticalSlides ? verticalSlides[ y ] : undefined;
  1528. }
  1529. return horizontalSlide;
  1530. }
  1531. /**
  1532. * Returns the background element for the given slide.
  1533. * All slides, even the ones with no background properties
  1534. * defined, have a background element so as long as the
  1535. * index is valid an element will be returned.
  1536. *
  1537. * @param {mixed} x Horizontal background index OR a slide
  1538. * HTML element
  1539. * @param {number} y Vertical background index
  1540. * @return {(HTMLElement[]|*)}
  1541. */
  1542. function getSlideBackground( x, y ) {
  1543. let slide = typeof x === 'number' ? getSlide( x, y ) : x;
  1544. if( slide ) {
  1545. return slide.slideBackgroundElement;
  1546. }
  1547. return undefined;
  1548. }
  1549. /**
  1550. * Retrieves the current state of the presentation as
  1551. * an object. This state can then be restored at any
  1552. * time.
  1553. *
  1554. * @return {{indexh: number, indexv: number, indexf: number, paused: boolean, overview: boolean}}
  1555. */
  1556. function getState() {
  1557. let indices = getIndices();
  1558. return {
  1559. indexh: indices.h,
  1560. indexv: indices.v,
  1561. indexf: indices.f,
  1562. paused: isPaused(),
  1563. overview: overview.isActive()
  1564. };
  1565. }
  1566. /**
  1567. * Restores the presentation to the given state.
  1568. *
  1569. * @param {object} state As generated by getState()
  1570. * @see {@link getState} generates the parameter `state`
  1571. */
  1572. function setState( state ) {
  1573. if( typeof state === 'object' ) {
  1574. slide( Util.deserialize( state.indexh ), Util.deserialize( state.indexv ), Util.deserialize( state.indexf ) );
  1575. let pausedFlag = Util.deserialize( state.paused ),
  1576. overviewFlag = Util.deserialize( state.overview );
  1577. if( typeof pausedFlag === 'boolean' && pausedFlag !== isPaused() ) {
  1578. togglePause( pausedFlag );
  1579. }
  1580. if( typeof overviewFlag === 'boolean' && overviewFlag !== overview.isActive() ) {
  1581. overview.toggle( overviewFlag );
  1582. }
  1583. }
  1584. }
  1585. /**
  1586. * Cues a new automated slide if enabled in the config.
  1587. */
  1588. function cueAutoSlide() {
  1589. cancelAutoSlide();
  1590. if( currentSlide && config.autoSlide !== false ) {
  1591. let fragment = currentSlide.querySelector( '.current-fragment' );
  1592. // When the slide first appears there is no "current" fragment so
  1593. // we look for a data-autoslide timing on the first fragment
  1594. if( !fragment ) fragment = currentSlide.querySelector( '.fragment' );
  1595. let fragmentAutoSlide = fragment ? fragment.getAttribute( 'data-autoslide' ) : null;
  1596. let parentAutoSlide = currentSlide.parentNode ? currentSlide.parentNode.getAttribute( 'data-autoslide' ) : null;
  1597. let slideAutoSlide = currentSlide.getAttribute( 'data-autoslide' );
  1598. // Pick value in the following priority order:
  1599. // 1. Current fragment's data-autoslide
  1600. // 2. Current slide's data-autoslide
  1601. // 3. Parent slide's data-autoslide
  1602. // 4. Global autoSlide setting
  1603. if( fragmentAutoSlide ) {
  1604. autoSlide = parseInt( fragmentAutoSlide, 10 );
  1605. }
  1606. else if( slideAutoSlide ) {
  1607. autoSlide = parseInt( slideAutoSlide, 10 );
  1608. }
  1609. else if( parentAutoSlide ) {
  1610. autoSlide = parseInt( parentAutoSlide, 10 );
  1611. }
  1612. else {
  1613. autoSlide = config.autoSlide;
  1614. // If there are media elements with data-autoplay,
  1615. // automatically set the autoSlide duration to the
  1616. // length of that media. Not applicable if the slide
  1617. // is divided up into fragments.
  1618. // playbackRate is accounted for in the duration.
  1619. if( currentSlide.querySelectorAll( '.fragment' ).length === 0 ) {
  1620. Util.queryAll( currentSlide, 'video, audio' ).forEach( el => {
  1621. if( el.hasAttribute( 'data-autoplay' ) ) {
  1622. if( autoSlide && (el.duration * 1000 / el.playbackRate ) > autoSlide ) {
  1623. autoSlide = ( el.duration * 1000 / el.playbackRate ) + 1000;
  1624. }
  1625. }
  1626. } );
  1627. }
  1628. }
  1629. // Cue the next auto-slide if:
  1630. // - There is an autoSlide value
  1631. // - Auto-sliding isn't paused by the user
  1632. // - The presentation isn't paused
  1633. // - The overview isn't active
  1634. // - The presentation isn't over
  1635. if( autoSlide && !autoSlidePaused && !isPaused() && !overview.isActive() && ( !isLastSlide() || fragments.availableRoutes().next || config.loop === true ) ) {
  1636. autoSlideTimeout = setTimeout( () => {
  1637. if( typeof config.autoSlideMethod === 'function' ) {
  1638. config.autoSlideMethod()
  1639. }
  1640. else {
  1641. navigateNext();
  1642. }
  1643. cueAutoSlide();
  1644. }, autoSlide );
  1645. autoSlideStartTime = Date.now();
  1646. }
  1647. if( autoSlidePlayer ) {
  1648. autoSlidePlayer.setPlaying( autoSlideTimeout !== -1 );
  1649. }
  1650. }
  1651. }
  1652. /**
  1653. * Cancels any ongoing request to auto-slide.
  1654. */
  1655. function cancelAutoSlide() {
  1656. clearTimeout( autoSlideTimeout );
  1657. autoSlideTimeout = -1;
  1658. }
  1659. function pauseAutoSlide() {
  1660. if( autoSlide && !autoSlidePaused ) {
  1661. autoSlidePaused = true;
  1662. dispatchEvent({ type: 'autoslidepaused' });
  1663. clearTimeout( autoSlideTimeout );
  1664. if( autoSlidePlayer ) {
  1665. autoSlidePlayer.setPlaying( false );
  1666. }
  1667. }
  1668. }
  1669. function resumeAutoSlide() {
  1670. if( autoSlide && autoSlidePaused ) {
  1671. autoSlidePaused = false;
  1672. dispatchEvent({ type: 'autoslideresumed' });
  1673. cueAutoSlide();
  1674. }
  1675. }
  1676. function navigateLeft() {
  1677. navigationHistory.hasNavigatedHorizontally = true;
  1678. // Reverse for RTL
  1679. if( config.rtl ) {
  1680. if( ( overview.isActive() || fragments.next() === false ) && availableRoutes().left ) {
  1681. slide( indexh + 1, config.navigationMode === 'grid' ? indexv : undefined );
  1682. }
  1683. }
  1684. // Normal navigation
  1685. else if( ( overview.isActive() || fragments.prev() === false ) && availableRoutes().left ) {
  1686. slide( indexh - 1, config.navigationMode === 'grid' ? indexv : undefined );
  1687. }
  1688. }
  1689. function navigateRight() {
  1690. navigationHistory.hasNavigatedHorizontally = true;
  1691. // Reverse for RTL
  1692. if( config.rtl ) {
  1693. if( ( overview.isActive() || fragments.prev() === false ) && availableRoutes().right ) {
  1694. slide( indexh - 1, config.navigationMode === 'grid' ? indexv : undefined );
  1695. }
  1696. }
  1697. // Normal navigation
  1698. else if( ( overview.isActive() || fragments.next() === false ) && availableRoutes().right ) {
  1699. slide( indexh + 1, config.navigationMode === 'grid' ? indexv : undefined );
  1700. }
  1701. }
  1702. function navigateUp() {
  1703. // Prioritize hiding fragments
  1704. if( ( overview.isActive() || fragments.prev() === false ) && availableRoutes().up ) {
  1705. slide( indexh, indexv - 1 );
  1706. }
  1707. }
  1708. function navigateDown() {
  1709. navigationHistory.hasNavigatedVertically = true;
  1710. // Prioritize revealing fragments
  1711. if( ( overview.isActive() || fragments.next() === false ) && availableRoutes().down ) {
  1712. slide( indexh, indexv + 1 );
  1713. }
  1714. }
  1715. /**
  1716. * Navigates backwards, prioritized in the following order:
  1717. * 1) Previous fragment
  1718. * 2) Previous vertical slide
  1719. * 3) Previous horizontal slide
  1720. */
  1721. function navigatePrev() {
  1722. // Prioritize revealing fragments
  1723. if( fragments.prev() === false ) {
  1724. if( availableRoutes().up ) {
  1725. navigateUp();
  1726. }
  1727. else {
  1728. // Fetch the previous horizontal slide, if there is one
  1729. let previousSlide;
  1730. if( config.rtl ) {
  1731. previousSlide = Util.queryAll( dom.wrapper, HORIZONTAL_SLIDES_SELECTOR + '.future' ).pop();
  1732. }
  1733. else {
  1734. previousSlide = Util.queryAll( dom.wrapper, HORIZONTAL_SLIDES_SELECTOR + '.past' ).pop();
  1735. }
  1736. if( previousSlide ) {
  1737. let v = ( previousSlide.querySelectorAll( 'section' ).length - 1 ) || undefined;
  1738. let h = indexh - 1;
  1739. slide( h, v );
  1740. }
  1741. }
  1742. }
  1743. }
  1744. /**
  1745. * The reverse of #navigatePrev().
  1746. */
  1747. function navigateNext() {
  1748. navigationHistory.hasNavigatedHorizontally = true;
  1749. navigationHistory.hasNavigatedVertically = true;
  1750. // Prioritize revealing fragments
  1751. if( fragments.next() === false ) {
  1752. let routes = availableRoutes();
  1753. // When looping is enabled `routes.down` is always available
  1754. // so we need a separate check for when we've reached the
  1755. // end of a stack and should move horizontally
  1756. if( routes.down && routes.right && config.loop && isLastVerticalSlide( currentSlide ) ) {
  1757. routes.down = false;
  1758. }
  1759. if( routes.down ) {
  1760. navigateDown();
  1761. }
  1762. else if( config.rtl ) {
  1763. navigateLeft();
  1764. }
  1765. else {
  1766. navigateRight();
  1767. }
  1768. }
  1769. }
  1770. // --------------------------------------------------------------------//
  1771. // ----------------------------- EVENTS -------------------------------//
  1772. // --------------------------------------------------------------------//
  1773. /**
  1774. * Called by all event handlers that are based on user
  1775. * input.
  1776. *
  1777. * @param {object} [event]
  1778. */
  1779. function onUserInput( event ) {
  1780. if( config.autoSlideStoppable ) {
  1781. pauseAutoSlide();
  1782. }
  1783. }
  1784. /**
  1785. * Event listener for transition end on the current slide.
  1786. *
  1787. * @param {object} [event]
  1788. */
  1789. function onTransitionEnd( event ) {
  1790. if( transition === 'running' && /section/gi.test( event.target.nodeName ) ) {
  1791. transition = 'idle';
  1792. dispatchEvent({
  1793. type: 'slidetransitionend',
  1794. data: { indexh, indexv, previousSlide, currentSlide }
  1795. });
  1796. }
  1797. }
  1798. /**
  1799. * Handler for the window level 'resize' event.
  1800. *
  1801. * @param {object} [event]
  1802. */
  1803. function onWindowResize( event ) {
  1804. layout();
  1805. }
  1806. /**
  1807. * Handle for the window level 'visibilitychange' event.
  1808. *
  1809. * @param {object} [event]
  1810. */
  1811. function onPageVisibilityChange( event ) {
  1812. // If, after clicking a link or similar and we're coming back,
  1813. // focus the document.body to ensure we can use keyboard shortcuts
  1814. if( document.hidden === false && document.activeElement !== document.body ) {
  1815. // Not all elements support .blur() - SVGs among them.
  1816. if( typeof document.activeElement.blur === 'function' ) {
  1817. document.activeElement.blur();
  1818. }
  1819. document.body.focus();
  1820. }
  1821. }
  1822. /**
  1823. * Handles clicks on links that are set to preview in the
  1824. * iframe overlay.
  1825. *
  1826. * @param {object} event
  1827. */
  1828. function onPreviewLinkClicked( event ) {
  1829. if( event.currentTarget && event.currentTarget.hasAttribute( 'href' ) ) {
  1830. let url = event.currentTarget.getAttribute( 'href' );
  1831. if( url ) {
  1832. showPreview( url );
  1833. event.preventDefault();
  1834. }
  1835. }
  1836. }
  1837. /**
  1838. * Handles click on the auto-sliding controls element.
  1839. *
  1840. * @param {object} [event]
  1841. */
  1842. function onAutoSlidePlayerClick( event ) {
  1843. // Replay
  1844. if( isLastSlide() && config.loop === false ) {
  1845. slide( 0, 0 );
  1846. resumeAutoSlide();
  1847. }
  1848. // Resume
  1849. else if( autoSlidePaused ) {
  1850. resumeAutoSlide();
  1851. }
  1852. // Pause
  1853. else {
  1854. pauseAutoSlide();
  1855. }
  1856. }
  1857. // --------------------------------------------------------------------//
  1858. // ------------------------------- API --------------------------------//
  1859. // --------------------------------------------------------------------//
  1860. // The public reveal.js API
  1861. const API = {
  1862. VERSION,
  1863. initialize,
  1864. configure,
  1865. sync,
  1866. syncSlide,
  1867. syncFragments: fragments.sync.bind( fragments ),
  1868. // Navigation methods
  1869. slide,
  1870. left: navigateLeft,
  1871. right: navigateRight,
  1872. up: navigateUp,
  1873. down: navigateDown,
  1874. prev: navigatePrev,
  1875. next: navigateNext,
  1876. // Navigation aliases
  1877. navigateLeft, navigateRight, navigateUp, navigateDown, navigatePrev, navigateNext,
  1878. // Fragment methods
  1879. navigateFragment: fragments.goto.bind( fragments ),
  1880. prevFragment: fragments.prev.bind( fragments ),
  1881. nextFragment: fragments.next.bind( fragments ),
  1882. // Event binding
  1883. on,
  1884. off,
  1885. // Legacy event binding methods left in for backwards compatibility
  1886. addEventListener: on,
  1887. removeEventListener: off,
  1888. // Forces an update in slide layout
  1889. layout,
  1890. // Randomizes the order of slides
  1891. shuffle,
  1892. // Returns an object with the available routes as booleans (left/right/top/bottom)
  1893. availableRoutes,
  1894. // Returns an object with the available fragments as booleans (prev/next)
  1895. availableFragments: fragments.availableRoutes.bind( fragments ),
  1896. // Toggles a help overlay with keyboard shortcuts
  1897. toggleHelp,
  1898. // Toggles the overview mode on/off
  1899. toggleOverview: overview.toggle.bind( overview ),
  1900. // Toggles the "black screen" mode on/off
  1901. togglePause,
  1902. // Toggles the auto slide mode on/off
  1903. toggleAutoSlide,
  1904. // Slide navigation checks
  1905. isFirstSlide,
  1906. isLastSlide,
  1907. isLastVerticalSlide,
  1908. isVerticalSlide,
  1909. // State checks
  1910. isPaused,
  1911. isAutoSliding,
  1912. isSpeakerNotes: notes.isSpeakerNotesWindow.bind( notes ),
  1913. isOverview: overview.isActive.bind( overview ),
  1914. isFocused: focus.isFocused.bind( focus ),
  1915. isPrintingPDF: print.isPrintingPDF.bind( print ),
  1916. // Checks if reveal.js has been loaded and is ready for use
  1917. isReady: () => ready,
  1918. // Slide preloading
  1919. loadSlide: slideContent.load.bind( slideContent ),
  1920. unloadSlide: slideContent.unload.bind( slideContent ),
  1921. // Adds or removes all internal event listeners
  1922. addEventListeners,
  1923. removeEventListeners,
  1924. dispatchEvent,
  1925. // Facility for persisting and restoring the presentation state
  1926. getState,
  1927. setState,
  1928. // Presentation progress on range of 0-1
  1929. getProgress,
  1930. // Returns the indices of the current, or specified, slide
  1931. getIndices,
  1932. // Returns an Array of key:value maps of the attributes of each
  1933. // slide in the deck
  1934. getSlidesAttributes,
  1935. // Returns the number of slides that we have passed
  1936. getSlidePastCount,
  1937. // Returns the total number of slides
  1938. getTotalSlides,
  1939. // Returns the slide element at the specified index
  1940. getSlide,
  1941. // Returns the previous slide element, may be null
  1942. getPreviousSlide: () => previousSlide,
  1943. // Returns the current slide element
  1944. getCurrentSlide: () => currentSlide,
  1945. // Returns the slide background element at the specified index
  1946. getSlideBackground,
  1947. // Returns the speaker notes string for a slide, or null
  1948. getSlideNotes: notes.getSlideNotes.bind( notes ),
  1949. // Returns an Array of all slides
  1950. getSlides,
  1951. // Returns an array with all horizontal/vertical slides in the deck
  1952. getHorizontalSlides,
  1953. getVerticalSlides,
  1954. // Checks if the presentation contains two or more horizontal
  1955. // and vertical slides
  1956. hasHorizontalSlides,
  1957. hasVerticalSlides,
  1958. // Checks if the deck has navigated on either axis at least once
  1959. hasNavigatedHorizontally: () => navigationHistory.hasNavigatedHorizontally,
  1960. hasNavigatedVertically: () => navigationHistory.hasNavigatedVertically,
  1961. // Adds/removes a custom key binding
  1962. addKeyBinding: keyboard.addKeyBinding.bind( keyboard ),
  1963. removeKeyBinding: keyboard.removeKeyBinding.bind( keyboard ),
  1964. // Programmatically triggers a keyboard event
  1965. triggerKey: keyboard.triggerKey.bind( keyboard ),
  1966. // Registers a new shortcut to include in the help overlay
  1967. registerKeyboardShortcut: keyboard.registerKeyboardShortcut.bind( keyboard ),
  1968. getComputedSlideSize,
  1969. // Returns the current scale of the presentation content
  1970. getScale: () => scale,
  1971. // Returns the current configuration object
  1972. getConfig: () => config,
  1973. // Helper method, retrieves query string as a key:value map
  1974. getQueryHash: Util.getQueryHash,
  1975. // Returns reveal.js DOM elements
  1976. getRevealElement: () => revealElement,
  1977. getSlidesElement: () => dom.slides,
  1978. getViewportElement: () => dom.viewport,
  1979. getBackgroundsElement: () => backgrounds.element,
  1980. // API for registering and retrieving plugins
  1981. registerPlugin: plugins.registerPlugin.bind( plugins ),
  1982. hasPlugin: plugins.hasPlugin.bind( plugins ),
  1983. getPlugin: plugins.getPlugin.bind( plugins ),
  1984. getPlugins: plugins.getRegisteredPlugins.bind( plugins )
  1985. };
  1986. // Our internal API which controllers have access to
  1987. Util.extend( Reveal, {
  1988. ...API,
  1989. // Methods for announcing content to screen readers
  1990. announceStatus,
  1991. getStatusText,
  1992. // Controllers
  1993. print,
  1994. focus,
  1995. progress,
  1996. controls,
  1997. location,
  1998. overview,
  1999. fragments,
  2000. slideContent,
  2001. slideNumber,
  2002. onUserInput,
  2003. closeOverlay,
  2004. updateSlidesVisibility,
  2005. layoutSlideContents,
  2006. transformSlides,
  2007. cueAutoSlide,
  2008. cancelAutoSlide
  2009. } );
  2010. return API;
  2011. };