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.

96 lines
1.8KB

  1. /**
  2. * Creates a visual progress bar for the presentation.
  3. */
  4. export default class Progress {
  5. constructor( Reveal ) {
  6. this.Reveal = Reveal;
  7. this.onProgressClicked = this.onProgressClicked.bind( this );
  8. }
  9. render() {
  10. this.element = document.createElement( 'div' );
  11. this.element.className = 'progress';
  12. this.Reveal.getRevealElement().appendChild( this.element );
  13. this.bar = document.createElement( 'span' );
  14. this.element.appendChild( this.bar );
  15. }
  16. /**
  17. * Called when the reveal.js config is updated.
  18. */
  19. configure( config, oldConfig ) {
  20. this.element.style.display = config.progress ? 'block' : 'none';
  21. }
  22. bind() {
  23. if( this.Reveal.getConfig().progress && this.element ) {
  24. this.element.addEventListener( 'click', this.onProgressClicked, false );
  25. }
  26. }
  27. unbind() {
  28. if ( this.Reveal.getConfig().progress && this.element ) {
  29. this.element.removeEventListener( 'click', this.onProgressClicked, false );
  30. }
  31. }
  32. /**
  33. * Updates the progress bar to reflect the current slide.
  34. */
  35. update() {
  36. // Update progress if enabled
  37. if( this.Reveal.getConfig().progress && this.bar ) {
  38. this.bar.style.transform = 'scaleX('+ this.Reveal.getProgress() +')';
  39. }
  40. }
  41. getMaxWidth() {
  42. return this.Reveal.getRevealElement().offsetWidth;
  43. }
  44. /**
  45. * Clicking on the progress bar results in a navigation to the
  46. * closest approximate horizontal slide using this equation:
  47. *
  48. * ( clickX / presentationWidth ) * numberOfSlides
  49. *
  50. * @param {object} event
  51. */
  52. onProgressClicked( event ) {
  53. this.Reveal.onUserInput( event );
  54. event.preventDefault();
  55. let slidesTotal = this.Reveal.getHorizontalSlides().length;
  56. let slideIndex = Math.floor( ( event.clientX / this.getMaxWidth() ) * slidesTotal );
  57. if( this.Reveal.getConfig().rtl ) {
  58. slideIndex = slidesTotal - slideIndex;
  59. }
  60. this.Reveal.slide( slideIndex );
  61. }
  62. }