slidenumber.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. // Offset the current slide number by 1 to make it 1-indexed
  58. let horizontalOffset = slide && slide.dataset.visibility === 'uncounted' ? 0 : 1;
  59. value = [];
  60. switch( format ) {
  61. case 'c':
  62. value.push( this.Reveal.getSlidePastCount( slide ) + horizontalOffset );
  63. break;
  64. case 'c/t':
  65. value.push( this.Reveal.getSlidePastCount( slide ) + horizontalOffset, '/', this.Reveal.getTotalSlides() );
  66. break;
  67. default:
  68. let indices = this.Reveal.getIndices( slide );
  69. value.push( indices.h + horizontalOffset );
  70. let sep = format === 'h/v' ? '/' : '.';
  71. if( this.Reveal.isVerticalSlide( slide ) ) value.push( sep, indices.v + 1 );
  72. }
  73. }
  74. let url = '#' + this.Reveal.location.getHash( slide );
  75. return this.formatNumber( value[0], value[1], value[2], url );
  76. }
  77. /**
  78. * Applies HTML formatting to a slide number before it's
  79. * written to the DOM.
  80. *
  81. * @param {number} a Current slide
  82. * @param {string} delimiter Character to separate slide numbers
  83. * @param {(number|*)} b Total slides
  84. * @param {HTMLElement} [url='#'+locationHash()] The url to link to
  85. * @return {string} HTML string fragment
  86. */
  87. formatNumber( a, delimiter, b, url = '#' + this.Reveal.location.getHash() ) {
  88. if( typeof b === 'number' && !isNaN( b ) ) {
  89. return `<a href="${url}">
  90. <span class="slide-number-a">${a}</span>
  91. <span class="slide-number-delimiter">${delimiter}</span>
  92. <span class="slide-number-b">${b}</span>
  93. </a>`;
  94. }
  95. else {
  96. return `<a href="${url}">
  97. <span class="slide-number-a">${a}</span>
  98. </a>`;
  99. }
  100. }
  101. }