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.

123 lines
3.0KB

  1. /**
  2. * Handles the display of reveal.js' optional slide number.
  3. */
  4. export default class SlideNumber {
  5. constructor( Reveal ) {
  6. this.Reveal = Reveal;
  7. }
  8. render() {
  9. this.element = document.createElement( 'div' );
  10. this.element.className = 'slide-number';
  11. this.Reveal.getRevealElement().appendChild( this.element );
  12. }
  13. /**
  14. * Called when the reveal.js config is updated.
  15. */
  16. configure( config, oldConfig ) {
  17. let slideNumberDisplay = 'none';
  18. if( config.slideNumber && !this.Reveal.isPrintingPDF() ) {
  19. if( config.showSlideNumber === 'all' ) {
  20. slideNumberDisplay = 'block';
  21. }
  22. else if( config.showSlideNumber === 'speaker' && this.Reveal.isSpeakerNotes() ) {
  23. slideNumberDisplay = 'block';
  24. }
  25. }
  26. this.element.style.display = slideNumberDisplay;
  27. }
  28. /**
  29. * Updates the slide number to match the current slide.
  30. */
  31. update() {
  32. // Update slide number if enabled
  33. if( this.Reveal.getConfig().slideNumber && this.element ) {
  34. this.element.innerHTML = this.getSlideNumber();
  35. }
  36. }
  37. /**
  38. * Returns the HTML string corresponding to the current slide
  39. * number, including formatting.
  40. */
  41. getSlideNumber( slide = this.Reveal.getCurrentSlide() ) {
  42. let config = this.Reveal.getConfig();
  43. let value;
  44. let format = 'h.v';
  45. if ( typeof config.slideNumber === 'function' ) {
  46. value = config.slideNumber( slide );
  47. } else {
  48. // Check if a custom number format is available
  49. if( typeof config.slideNumber === 'string' ) {
  50. format = config.slideNumber;
  51. }
  52. // If there are ONLY vertical slides in this deck, always use
  53. // a flattened slide number
  54. if( !/c/.test( format ) && this.Reveal.getHorizontalSlides().length === 1 ) {
  55. format = 'c';
  56. }
  57. value = [];
  58. switch( format ) {
  59. case 'c':
  60. value.push( this.Reveal.getSlidePastCount( slide ) + 1 );
  61. break;
  62. case 'c/t':
  63. value.push( this.Reveal.getSlidePastCount( slide ) + 1, '/', this.Reveal.getTotalSlides() );
  64. break;
  65. default:
  66. let indices = this.Reveal.getIndices( slide );
  67. value.push( indices.h + 1 );
  68. let sep = format === 'h/v' ? '/' : '.';
  69. if( this.Reveal.isVerticalSlide( slide ) ) value.push( sep, indices.v + 1 );
  70. }
  71. }
  72. let url = '#' + this.Reveal.location.getHash( slide );
  73. return this.formatNumber( value[0], value[1], value[2], url );
  74. }
  75. /**
  76. * Applies HTML formatting to a slide number before it's
  77. * written to the DOM.
  78. *
  79. * @param {number} a Current slide
  80. * @param {string} delimiter Character to separate slide numbers
  81. * @param {(number|*)} b Total slides
  82. * @param {HTMLElement} [url='#'+locationHash()] The url to link to
  83. * @return {string} HTML string fragment
  84. */
  85. formatNumber( a, delimiter, b, url = '#' + this.Reveal.location.getHash() ) {
  86. if( typeof b === 'number' && !isNaN( b ) ) {
  87. return `<a href="${url}">
  88. <span class="slide-number-a">${a}</span>
  89. <span class="slide-number-delimiter">${delimiter}</span>
  90. <span class="slide-number-b">${b}</span>
  91. </a>`;
  92. }
  93. else {
  94. return `<a href="${url}">
  95. <span class="slide-number-a">${a}</span>
  96. </a>`;
  97. }
  98. }
  99. }