jumptoslide.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /**
  2. * Makes it possble to jump to a slide by entering its
  3. * slide number or id.
  4. */
  5. export default class JumpToSlide {
  6. constructor( Reveal ) {
  7. this.Reveal = Reveal;
  8. this.onInput = this.onInput.bind( this );
  9. this.onBlur = this.onBlur.bind( this );
  10. this.onKeyDown = this.onKeyDown.bind( this );
  11. }
  12. render() {
  13. this.element = document.createElement( 'div' );
  14. this.element.className = 'jump-to-slide';
  15. this.jumpInput = document.createElement( 'input' );
  16. this.jumpInput.type = 'text';
  17. this.jumpInput.className = 'jump-to-slide-input';
  18. this.jumpInput.placeholder = 'Jump to slide';
  19. this.jumpInput.addEventListener( 'input', this.onInput );
  20. this.jumpInput.addEventListener( 'keydown', this.onKeyDown );
  21. this.jumpInput.addEventListener( 'blur', this.onBlur );
  22. this.element.appendChild( this.jumpInput );
  23. }
  24. show() {
  25. this.indicesOnShow = this.Reveal.getIndices();
  26. this.Reveal.getRevealElement().appendChild( this.element );
  27. this.jumpInput.focus();
  28. }
  29. hide() {
  30. if( this.isVisible() ) {
  31. this.element.remove();
  32. this.jumpInput.value = '';
  33. clearTimeout( this.jumpTimeout );
  34. delete this.jumpTimeout;
  35. }
  36. }
  37. isVisible() {
  38. return !!this.element.parentNode;
  39. }
  40. /**
  41. * Parses the current input and jumps to the given slide.
  42. */
  43. jump() {
  44. clearTimeout( this.jumpTimeout );
  45. delete this.jumpTimeout;
  46. const value = this.jumpInput.value.trim( '' );
  47. const indices = this.Reveal.location.getIndicesFromHash( value );
  48. if( indices && value !== '' ) {
  49. this.Reveal.slide( indices.h, indices.v, indices.f );
  50. return true;
  51. }
  52. else {
  53. this.Reveal.slide( this.indicesOnShow.h, this.indicesOnShow.v, this.indicesOnShow.f );
  54. return false;
  55. }
  56. }
  57. jumpAfter( delay ) {
  58. clearTimeout( this.jumpTimeout );
  59. this.jumpTimeout = setTimeout( () => this.jump(), delay );
  60. }
  61. /**
  62. * Reverts back to the slide we were on when jump to slide was
  63. * invoked.
  64. */
  65. cancel() {
  66. this.Reveal.slide( this.indicesOnShow.h, this.indicesOnShow.v, this.indicesOnShow.f );
  67. this.hide();
  68. }
  69. confirm() {
  70. this.hide();
  71. }
  72. destroy() {
  73. this.jumpInput.removeEventListener( 'input', this.onInput );
  74. this.jumpInput.removeEventListener( 'keydown', this.onKeyDown );
  75. this.jumpInput.removeEventListener( 'blur', this.onBlur );
  76. this.element.remove();
  77. }
  78. onKeyDown( event ) {
  79. if( event.keyCode === 13 ) {
  80. this.confirm();
  81. }
  82. else if( event.keyCode === 27 ) {
  83. this.cancel();
  84. event.stopImmediatePropagation();
  85. }
  86. }
  87. onInput( event ) {
  88. this.jumpAfter( 200 );
  89. }
  90. onBlur() {
  91. setTimeout( () => this.hide(), 1 );
  92. }
  93. }