Christophe Dervieux | e1893ae | 2021-10-07 17:09:02 +0200 | [diff] [blame] | 1 | /** |
| 2 | * Handles hiding of the pointer/cursor when inactive. |
| 3 | */ |
| 4 | export default class Pointer { |
| 5 | |
| 6 | constructor( Reveal ) { |
| 7 | |
| 8 | this.Reveal = Reveal; |
| 9 | |
| 10 | // Throttles mouse wheel navigation |
| 11 | this.lastMouseWheelStep = 0; |
| 12 | |
| 13 | // Is the mouse pointer currently hidden from view |
| 14 | this.cursorHidden = false; |
| 15 | |
| 16 | // Timeout used to determine when the cursor is inactive |
| 17 | this.cursorInactiveTimeout = 0; |
| 18 | |
| 19 | this.onDocumentCursorActive = this.onDocumentCursorActive.bind( this ); |
| 20 | this.onDocumentMouseScroll = this.onDocumentMouseScroll.bind( this ); |
| 21 | |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Called when the reveal.js config is updated. |
| 26 | */ |
| 27 | configure( config, oldConfig ) { |
| 28 | |
| 29 | if( config.mouseWheel ) { |
Marc Kupietz | 9c036a4 | 2024-05-14 13:17:25 +0200 | [diff] [blame^] | 30 | document.addEventListener( 'wheel', this.onDocumentMouseScroll, false ); |
Christophe Dervieux | e1893ae | 2021-10-07 17:09:02 +0200 | [diff] [blame] | 31 | } |
| 32 | else { |
Marc Kupietz | 9c036a4 | 2024-05-14 13:17:25 +0200 | [diff] [blame^] | 33 | document.removeEventListener( 'wheel', this.onDocumentMouseScroll, false ); |
Christophe Dervieux | e1893ae | 2021-10-07 17:09:02 +0200 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | // Auto-hide the mouse pointer when its inactive |
| 37 | if( config.hideInactiveCursor ) { |
| 38 | document.addEventListener( 'mousemove', this.onDocumentCursorActive, false ); |
| 39 | document.addEventListener( 'mousedown', this.onDocumentCursorActive, false ); |
| 40 | } |
| 41 | else { |
| 42 | this.showCursor(); |
| 43 | |
| 44 | document.removeEventListener( 'mousemove', this.onDocumentCursorActive, false ); |
| 45 | document.removeEventListener( 'mousedown', this.onDocumentCursorActive, false ); |
| 46 | } |
| 47 | |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Shows the mouse pointer after it has been hidden with |
| 52 | * #hideCursor. |
| 53 | */ |
| 54 | showCursor() { |
| 55 | |
| 56 | if( this.cursorHidden ) { |
| 57 | this.cursorHidden = false; |
| 58 | this.Reveal.getRevealElement().style.cursor = ''; |
| 59 | } |
| 60 | |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Hides the mouse pointer when it's on top of the .reveal |
| 65 | * container. |
| 66 | */ |
| 67 | hideCursor() { |
| 68 | |
| 69 | if( this.cursorHidden === false ) { |
| 70 | this.cursorHidden = true; |
| 71 | this.Reveal.getRevealElement().style.cursor = 'none'; |
| 72 | } |
| 73 | |
| 74 | } |
| 75 | |
Marc Kupietz | 09b7575 | 2023-10-07 09:32:19 +0200 | [diff] [blame] | 76 | destroy() { |
| 77 | |
| 78 | this.showCursor(); |
| 79 | |
Marc Kupietz | 9c036a4 | 2024-05-14 13:17:25 +0200 | [diff] [blame^] | 80 | document.removeEventListener( 'wheel', this.onDocumentMouseScroll, false ); |
Marc Kupietz | 09b7575 | 2023-10-07 09:32:19 +0200 | [diff] [blame] | 81 | document.removeEventListener( 'mousemove', this.onDocumentCursorActive, false ); |
| 82 | document.removeEventListener( 'mousedown', this.onDocumentCursorActive, false ); |
| 83 | |
| 84 | } |
| 85 | |
Christophe Dervieux | e1893ae | 2021-10-07 17:09:02 +0200 | [diff] [blame] | 86 | /** |
| 87 | * Called whenever there is mouse input at the document level |
| 88 | * to determine if the cursor is active or not. |
| 89 | * |
| 90 | * @param {object} event |
| 91 | */ |
| 92 | onDocumentCursorActive( event ) { |
| 93 | |
| 94 | this.showCursor(); |
| 95 | |
| 96 | clearTimeout( this.cursorInactiveTimeout ); |
| 97 | |
| 98 | this.cursorInactiveTimeout = setTimeout( this.hideCursor.bind( this ), this.Reveal.getConfig().hideCursorTime ); |
| 99 | |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Handles mouse wheel scrolling, throttled to avoid skipping |
| 104 | * multiple slides. |
| 105 | * |
| 106 | * @param {object} event |
| 107 | */ |
| 108 | onDocumentMouseScroll( event ) { |
| 109 | |
| 110 | if( Date.now() - this.lastMouseWheelStep > 1000 ) { |
| 111 | |
| 112 | this.lastMouseWheelStep = Date.now(); |
| 113 | |
| 114 | let delta = event.detail || -event.wheelDelta; |
| 115 | if( delta > 0 ) { |
| 116 | this.Reveal.next(); |
| 117 | } |
| 118 | else if( delta < 0 ) { |
| 119 | this.Reveal.prev(); |
| 120 | } |
| 121 | |
| 122 | } |
| 123 | |
| 124 | } |
| 125 | |
Marc Kupietz | 9c036a4 | 2024-05-14 13:17:25 +0200 | [diff] [blame^] | 126 | } |