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 ) { |
| 30 | document.addEventListener( 'DOMMouseScroll', this.onDocumentMouseScroll, false ); // FF |
| 31 | document.addEventListener( 'mousewheel', this.onDocumentMouseScroll, false ); |
| 32 | } |
| 33 | else { |
| 34 | document.removeEventListener( 'DOMMouseScroll', this.onDocumentMouseScroll, false ); // FF |
| 35 | document.removeEventListener( 'mousewheel', this.onDocumentMouseScroll, false ); |
| 36 | } |
| 37 | |
| 38 | // Auto-hide the mouse pointer when its inactive |
| 39 | if( config.hideInactiveCursor ) { |
| 40 | document.addEventListener( 'mousemove', this.onDocumentCursorActive, false ); |
| 41 | document.addEventListener( 'mousedown', this.onDocumentCursorActive, false ); |
| 42 | } |
| 43 | else { |
| 44 | this.showCursor(); |
| 45 | |
| 46 | document.removeEventListener( 'mousemove', this.onDocumentCursorActive, false ); |
| 47 | document.removeEventListener( 'mousedown', this.onDocumentCursorActive, false ); |
| 48 | } |
| 49 | |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Shows the mouse pointer after it has been hidden with |
| 54 | * #hideCursor. |
| 55 | */ |
| 56 | showCursor() { |
| 57 | |
| 58 | if( this.cursorHidden ) { |
| 59 | this.cursorHidden = false; |
| 60 | this.Reveal.getRevealElement().style.cursor = ''; |
| 61 | } |
| 62 | |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Hides the mouse pointer when it's on top of the .reveal |
| 67 | * container. |
| 68 | */ |
| 69 | hideCursor() { |
| 70 | |
| 71 | if( this.cursorHidden === false ) { |
| 72 | this.cursorHidden = true; |
| 73 | this.Reveal.getRevealElement().style.cursor = 'none'; |
| 74 | } |
| 75 | |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Called whenever there is mouse input at the document level |
| 80 | * to determine if the cursor is active or not. |
| 81 | * |
| 82 | * @param {object} event |
| 83 | */ |
| 84 | onDocumentCursorActive( event ) { |
| 85 | |
| 86 | this.showCursor(); |
| 87 | |
| 88 | clearTimeout( this.cursorInactiveTimeout ); |
| 89 | |
| 90 | this.cursorInactiveTimeout = setTimeout( this.hideCursor.bind( this ), this.Reveal.getConfig().hideCursorTime ); |
| 91 | |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Handles mouse wheel scrolling, throttled to avoid skipping |
| 96 | * multiple slides. |
| 97 | * |
| 98 | * @param {object} event |
| 99 | */ |
| 100 | onDocumentMouseScroll( event ) { |
| 101 | |
| 102 | if( Date.now() - this.lastMouseWheelStep > 1000 ) { |
| 103 | |
| 104 | this.lastMouseWheelStep = Date.now(); |
| 105 | |
| 106 | let delta = event.detail || -event.wheelDelta; |
| 107 | if( delta > 0 ) { |
| 108 | this.Reveal.next(); |
| 109 | } |
| 110 | else if( delta < 0 ) { |
| 111 | this.Reveal.prev(); |
| 112 | } |
| 113 | |
| 114 | } |
| 115 | |
| 116 | } |
| 117 | |
| 118 | } |