|
1 | 1 | import { escapeClientId } from './cui_utilities.js';
|
2 | 2 |
|
3 |
| -// Module-scoped variables to hold state |
4 |
| -let _timeout = null; |
5 |
| -let _interval = 0; |
6 |
| -let _linkId = ''; |
7 |
| -let _storedCallback = null; |
| 3 | +// Assuming jQuery ($) is available globally |
8 | 4 |
|
9 |
| -/** |
10 |
| - * Internal function to set the actual timeout. |
11 |
| - * Uses module-scoped variables. |
12 |
| - */ |
13 |
| -function _setLogoutTimeout() { |
14 |
| - if (_timeout) { |
15 |
| - clearTimeout(_timeout); |
| 5 | +export class Session { |
| 6 | + constructor(linkId, intervalSec, callback) { |
| 7 | + this.linkId = linkId; |
| 8 | + this.interval = (intervalSec === undefined || intervalSec <= 0 ? 1 : intervalSec) * 1000; // Default to 1 sec if invalid |
| 9 | + this.callback = callback; |
| 10 | + this.timeoutId = null; |
16 | 11 | }
|
17 | 12 |
|
18 |
| - if (_storedCallback) { |
19 |
| - if (_interval > 0) { |
20 |
| - _timeout = setTimeout(_storedCallback, _interval); |
| 13 | + /** |
| 14 | + * Internal method to set the actual timeout. |
| 15 | + * Uses instance properties. |
| 16 | + */ |
| 17 | + _setTimer() { |
| 18 | + if (this.timeoutId) { |
| 19 | + clearTimeout(this.timeoutId); |
21 | 20 | }
|
22 |
| - } else { |
23 |
| - if (_interval > 0) { |
24 |
| - _timeout = setTimeout(() => { |
25 |
| - // Assuming jQuery ($) and escapeClientId are available |
26 |
| - // escapeClientId is imported, $ is assumed global for now |
27 |
| - const elementToClick = $(escapeClientId(_linkId)); |
28 |
| - if (elementToClick.length > 0 && typeof elementToClick[0].click === 'function') { |
29 |
| - elementToClick[0].click(); |
30 |
| - } else if (elementToClick.length > 0 && typeof elementToClick.click === 'function') { |
31 |
| - elementToClick.click(); |
| 21 | + |
| 22 | + if (this.interval <= 0) { // Do not set timer if interval is not positive |
| 23 | + this.timeoutId = null; |
| 24 | + return; |
| 25 | + } |
| 26 | + |
| 27 | + if (this.callback) { |
| 28 | + this.timeoutId = setTimeout(this.callback, this.interval); |
| 29 | + } else if (this.linkId) { // Only set timeout if linkId is provided and no callback |
| 30 | + this.timeoutId = setTimeout(() => { |
| 31 | + const elementToClick = $(escapeClientId(this.linkId)); |
| 32 | + if (elementToClick.length > 0) { |
| 33 | + if (typeof elementToClick[0].click === 'function') { |
| 34 | + elementToClick[0].click(); |
| 35 | + } else if (typeof elementToClick.click === 'function') { |
| 36 | + elementToClick.click(); |
| 37 | + } |
32 | 38 | }
|
33 |
| - }, _interval); |
| 39 | + }, this.interval); |
| 40 | + } else { |
| 41 | + this.timeoutId = null; // No action possible |
34 | 42 | }
|
35 | 43 | }
|
36 |
| -} |
37 |
| - |
38 |
| -/** |
39 |
| -* Initialize logout timeout. |
40 |
| -* @param {number} intervalSec - Interval in seconds. |
41 |
| -* @param {string} linkId - The ID of the link to click if no callback. |
42 |
| -* @param {function} [callback] - Function to execute after timeout. |
43 |
| -*/ |
44 |
| -export function startLogoutTimeout(intervalSec, linkId, callback) { |
45 |
| - _interval = (intervalSec === undefined ? 1 : intervalSec) * 1000; |
46 |
| - _linkId = linkId; |
47 |
| - _storedCallback = callback; |
48 |
| - _setLogoutTimeout(); |
49 |
| -} |
50 | 44 |
|
51 |
| -/** |
52 |
| - * Reset logout timeout using previously stored settings. |
53 |
| - */ |
54 |
| -export function resetLogoutTimeout() { |
55 |
| - if (_timeout) { |
56 |
| - clearTimeout(_timeout); |
57 |
| - _timeout = null; |
| 45 | + /** |
| 46 | + * Starts the logout timeout. |
| 47 | + */ |
| 48 | + start() { |
| 49 | + this._setTimer(); |
58 | 50 | }
|
59 |
| - // Re-apply the timeout with the stored interval, linkId, and callback |
60 |
| - _setLogoutTimeout(); |
61 |
| -} |
62 | 51 |
|
63 |
| -/** |
64 |
| - * Stop the logout timeout. |
65 |
| - */ |
66 |
| -export function stopLogoutTimeout() { |
67 |
| - if (_timeout) { |
68 |
| - clearTimeout(_timeout); |
69 |
| - _timeout = null; |
| 52 | + /** |
| 53 | + * Resets the logout timeout using previously stored settings. |
| 54 | + */ |
| 55 | + reset() { |
| 56 | + // Stop any existing timer |
| 57 | + if (this.timeoutId) { |
| 58 | + clearTimeout(this.timeoutId); |
| 59 | + this.timeoutId = null; |
| 60 | + } |
| 61 | + // Re-apply the timer with the current instance settings |
| 62 | + this._setTimer(); |
70 | 63 | }
|
71 |
| -} |
72 | 64 |
|
73 |
| -/** |
74 |
| - * FOR TESTING PURPOSES ONLY. |
75 |
| - * Resets the internal state of the session module. |
76 |
| - */ |
77 |
| -export function _resetSessionState() { |
78 |
| - if (_timeout) { |
79 |
| - clearTimeout(_timeout); |
| 65 | + /** |
| 66 | + * Stops the logout timeout. |
| 67 | + */ |
| 68 | + stop() { |
| 69 | + if (this.timeoutId) { |
| 70 | + clearTimeout(this.timeoutId); |
| 71 | + this.timeoutId = null; |
| 72 | + } |
80 | 73 | }
|
81 |
| - _timeout = null; |
82 |
| - _interval = 0; |
83 |
| - _linkId = ''; |
84 |
| - _storedCallback = null; |
85 |
| -} |
86 | 74 |
|
87 |
| -/** |
88 |
| - * FOR TESTING PURPOSES ONLY. |
89 |
| - * Gets the current internal state. |
90 |
| - * @returns {object} The internal state. |
91 |
| - */ |
92 |
| -export function _getSessionState() { |
93 |
| - return { |
94 |
| - timeout: _timeout, |
95 |
| - interval: _interval, |
96 |
| - linkId: _linkId, |
97 |
| - storedCallback: _storedCallback |
98 |
| - }; |
| 75 | + /** |
| 76 | + * FOR TESTING PURPOSES ONLY. |
| 77 | + * Gets the current timeout ID. |
| 78 | + * @returns {object} The internal timeout ID. |
| 79 | + */ |
| 80 | + _getTimeoutId() { |
| 81 | + return this.timeoutId; |
| 82 | + } |
99 | 83 | }
|
0 commit comments