Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

pointer.js 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /**
  2. * Handles hiding of the pointer/cursor when inactive.
  3. */
  4. export default class Pointer {
  5. constructor( Reveal ) {
  6. this.Reveal = Reveal;
  7. // Throttles mouse wheel navigation
  8. this.lastMouseWheelStep = 0;
  9. // Is the mouse pointer currently hidden from view
  10. this.cursorHidden = false;
  11. // Timeout used to determine when the cursor is inactive
  12. this.cursorInactiveTimeout = 0;
  13. this.onDocumentCursorActive = this.onDocumentCursorActive.bind( this );
  14. this.onDocumentMouseScroll = this.onDocumentMouseScroll.bind( this );
  15. }
  16. /**
  17. * Called when the reveal.js config is updated.
  18. */
  19. configure( config, oldConfig ) {
  20. if( config.mouseWheel ) {
  21. document.addEventListener( 'DOMMouseScroll', this.onDocumentMouseScroll, false ); // FF
  22. document.addEventListener( 'mousewheel', this.onDocumentMouseScroll, false );
  23. }
  24. else {
  25. document.removeEventListener( 'DOMMouseScroll', this.onDocumentMouseScroll, false ); // FF
  26. document.removeEventListener( 'mousewheel', this.onDocumentMouseScroll, false );
  27. }
  28. // Auto-hide the mouse pointer when its inactive
  29. if( config.hideInactiveCursor ) {
  30. document.addEventListener( 'mousemove', this.onDocumentCursorActive, false );
  31. document.addEventListener( 'mousedown', this.onDocumentCursorActive, false );
  32. }
  33. else {
  34. this.showCursor();
  35. document.removeEventListener( 'mousemove', this.onDocumentCursorActive, false );
  36. document.removeEventListener( 'mousedown', this.onDocumentCursorActive, false );
  37. }
  38. }
  39. /**
  40. * Shows the mouse pointer after it has been hidden with
  41. * #hideCursor.
  42. */
  43. showCursor() {
  44. if( this.cursorHidden ) {
  45. this.cursorHidden = false;
  46. this.Reveal.getRevealElement().style.cursor = '';
  47. }
  48. }
  49. /**
  50. * Hides the mouse pointer when it's on top of the .reveal
  51. * container.
  52. */
  53. hideCursor() {
  54. if( this.cursorHidden === false ) {
  55. this.cursorHidden = true;
  56. this.Reveal.getRevealElement().style.cursor = 'none';
  57. }
  58. }
  59. destroy() {
  60. this.showCursor();
  61. document.removeEventListener( 'DOMMouseScroll', this.onDocumentMouseScroll, false );
  62. document.removeEventListener( 'mousewheel', this.onDocumentMouseScroll, false );
  63. document.removeEventListener( 'mousemove', this.onDocumentCursorActive, false );
  64. document.removeEventListener( 'mousedown', this.onDocumentCursorActive, false );
  65. }
  66. /**
  67. * Called whenever there is mouse input at the document level
  68. * to determine if the cursor is active or not.
  69. *
  70. * @param {object} event
  71. */
  72. onDocumentCursorActive( event ) {
  73. this.showCursor();
  74. clearTimeout( this.cursorInactiveTimeout );
  75. this.cursorInactiveTimeout = setTimeout( this.hideCursor.bind( this ), this.Reveal.getConfig().hideCursorTime );
  76. }
  77. /**
  78. * Handles mouse wheel scrolling, throttled to avoid skipping
  79. * multiple slides.
  80. *
  81. * @param {object} event
  82. */
  83. onDocumentMouseScroll( event ) {
  84. if( Date.now() - this.lastMouseWheelStep > 1000 ) {
  85. this.lastMouseWheelStep = Date.now();
  86. let delta = event.detail || -event.wheelDelta;
  87. if( delta > 0 ) {
  88. this.Reveal.next();
  89. }
  90. else if( delta < 0 ) {
  91. this.Reveal.prev();
  92. }
  93. }
  94. }
  95. }