blob: f632fcecb09e656c4768573b1fb9b7d051a48e34 [file] [log] [blame]
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +02001/**
2 * Handles hiding of the pointer/cursor when inactive.
3 */
4export 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
Marc Kupietz09b75752023-10-07 09:32:19 +020078 destroy() {
79
80 this.showCursor();
81
82 document.removeEventListener( 'DOMMouseScroll', this.onDocumentMouseScroll, false );
83 document.removeEventListener( 'mousewheel', this.onDocumentMouseScroll, false );
84 document.removeEventListener( 'mousemove', this.onDocumentCursorActive, false );
85 document.removeEventListener( 'mousedown', this.onDocumentCursorActive, false );
86
87 }
88
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +020089 /**
90 * Called whenever there is mouse input at the document level
91 * to determine if the cursor is active or not.
92 *
93 * @param {object} event
94 */
95 onDocumentCursorActive( event ) {
96
97 this.showCursor();
98
99 clearTimeout( this.cursorInactiveTimeout );
100
101 this.cursorInactiveTimeout = setTimeout( this.hideCursor.bind( this ), this.Reveal.getConfig().hideCursorTime );
102
103 }
104
105 /**
106 * Handles mouse wheel scrolling, throttled to avoid skipping
107 * multiple slides.
108 *
109 * @param {object} event
110 */
111 onDocumentMouseScroll( event ) {
112
113 if( Date.now() - this.lastMouseWheelStep > 1000 ) {
114
115 this.lastMouseWheelStep = Date.now();
116
117 let delta = event.detail || -event.wheelDelta;
118 if( delta > 0 ) {
119 this.Reveal.next();
120 }
121 else if( delta < 0 ) {
122 this.Reveal.prev();
123 }
124
125 }
126
127 }
128
129}