blob: 1a02762d217106f726f2605416236cc630bd68e5 [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 ) {
Marc Kupietz9c036a42024-05-14 13:17:25 +020030 document.addEventListener( 'wheel', this.onDocumentMouseScroll, false );
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +020031 }
32 else {
Marc Kupietz9c036a42024-05-14 13:17:25 +020033 document.removeEventListener( 'wheel', this.onDocumentMouseScroll, false );
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +020034 }
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 Kupietz09b75752023-10-07 09:32:19 +020076 destroy() {
77
78 this.showCursor();
79
Marc Kupietz9c036a42024-05-14 13:17:25 +020080 document.removeEventListener( 'wheel', this.onDocumentMouseScroll, false );
Marc Kupietz09b75752023-10-07 09:32:19 +020081 document.removeEventListener( 'mousemove', this.onDocumentCursorActive, false );
82 document.removeEventListener( 'mousedown', this.onDocumentCursorActive, false );
83
84 }
85
Christophe Dervieuxe1893ae2021-10-07 17:09:02 +020086 /**
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 Kupietz9c036a42024-05-14 13:17:25 +0200126}