@genesislcap/foundation-utils 14.314.2 → 14.314.3-alpha-c442ac5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/custom-elements.json +633 -15
- package/dist/dts/inactivity/inactivity-dialog.d.ts +23 -0
- package/dist/dts/inactivity/inactivity-dialog.d.ts.map +1 -0
- package/dist/dts/inactivity/inactivity-manager.d.ts +24 -0
- package/dist/dts/inactivity/inactivity-manager.d.ts.map +1 -0
- package/dist/dts/inactivity/inactivity-service.d.ts +34 -0
- package/dist/dts/inactivity/inactivity-service.d.ts.map +1 -0
- package/dist/dts/inactivity/index.d.ts +4 -0
- package/dist/dts/inactivity/index.d.ts.map +1 -0
- package/dist/dts/index.d.ts +1 -0
- package/dist/dts/index.d.ts.map +1 -1
- package/dist/esm/inactivity/inactivity-dialog.js +213 -0
- package/dist/esm/inactivity/inactivity-manager.js +87 -0
- package/dist/esm/inactivity/inactivity-service.js +128 -0
- package/dist/esm/inactivity/index.js +3 -0
- package/dist/esm/index.js +1 -0
- package/dist/foundation-utils.api.json +1217 -0
- package/dist/foundation-utils.d.ts +83 -0
- package/docs/api/foundation-utils.inactivityconfig.md +71 -0
- package/docs/api/foundation-utils.inactivityconfig.timeoutminutes.md +11 -0
- package/docs/api/foundation-utils.inactivityconfig.warningminutes.md +11 -0
- package/docs/api/foundation-utils.inactivitydialog.disconnectedcallback.md +15 -0
- package/docs/api/foundation-utils.inactivitydialog.handlecontinue.md +15 -0
- package/docs/api/foundation-utils.inactivitydialog.handlelogout.md +15 -0
- package/docs/api/foundation-utils.inactivitydialog.hide.md +15 -0
- package/docs/api/foundation-utils.inactivitydialog.isvisible.md +11 -0
- package/docs/api/foundation-utils.inactivitydialog.md +152 -0
- package/docs/api/foundation-utils.inactivitydialog.remainingseconds.md +11 -0
- package/docs/api/foundation-utils.inactivitydialog.show.md +50 -0
- package/docs/api/foundation-utils.inactivitydialogoptions.md +88 -0
- package/docs/api/foundation-utils.inactivitydialogoptions.oncontinue.md +11 -0
- package/docs/api/foundation-utils.inactivitydialogoptions.onlogout.md +11 -0
- package/docs/api/foundation-utils.inactivitydialogoptions.remainingseconds.md +11 -0
- package/docs/api/foundation-utils.inactivityevents._inactivity-reset_.md +11 -0
- package/docs/api/foundation-utils.inactivityevents._inactivity-timeout_.md +11 -0
- package/docs/api/foundation-utils.inactivityevents._inactivity-warning_.md +13 -0
- package/docs/api/foundation-utils.inactivityevents.md +88 -0
- package/docs/api/foundation-utils.inactivitymanager._constructor_.md +48 -0
- package/docs/api/foundation-utils.inactivitymanager.destroy.md +15 -0
- package/docs/api/foundation-utils.inactivitymanager.getservice.md +15 -0
- package/docs/api/foundation-utils.inactivitymanager.iswarningvisible.md +15 -0
- package/docs/api/foundation-utils.inactivitymanager.md +138 -0
- package/docs/api/foundation-utils.inactivitymanager.reset.md +15 -0
- package/docs/api/foundation-utils.inactivitymanager.start.md +15 -0
- package/docs/api/foundation-utils.inactivitymanager.stop.md +15 -0
- package/docs/api/foundation-utils.inactivitymanagerconfig.md +55 -0
- package/docs/api/foundation-utils.inactivitymanagerconfig.onlogout.md +11 -0
- package/docs/api/foundation-utils.inactivityservice._constructor_.md +48 -0
- package/docs/api/foundation-utils.inactivityservice.destroy.md +15 -0
- package/docs/api/foundation-utils.inactivityservice.md +138 -0
- package/docs/api/foundation-utils.inactivityservice.off.md +64 -0
- package/docs/api/foundation-utils.inactivityservice.on.md +64 -0
- package/docs/api/foundation-utils.inactivityservice.resettimer.md +15 -0
- package/docs/api/foundation-utils.inactivityservice.start.md +15 -0
- package/docs/api/foundation-utils.inactivityservice.stop.md +15 -0
- package/docs/api/foundation-utils.md +63 -0
- package/docs/api-report.md.api.md +90 -0
- package/package.json +11 -11
@@ -0,0 +1,24 @@
|
|
1
|
+
import { InactivityService, InactivityConfig } from './inactivity-service';
|
2
|
+
export interface InactivityManagerConfig extends InactivityConfig {
|
3
|
+
onLogout: () => void;
|
4
|
+
}
|
5
|
+
export declare class InactivityManager {
|
6
|
+
private service;
|
7
|
+
private dialog;
|
8
|
+
private config;
|
9
|
+
private isDialogVisible;
|
10
|
+
constructor(config: InactivityManagerConfig);
|
11
|
+
private setupEventListeners;
|
12
|
+
private showWarningDialog;
|
13
|
+
private hideWarningDialog;
|
14
|
+
private handleContinue;
|
15
|
+
private handleLogout;
|
16
|
+
private handleTimeout;
|
17
|
+
start(): void;
|
18
|
+
stop(): void;
|
19
|
+
reset(): void;
|
20
|
+
destroy(): void;
|
21
|
+
getService(): InactivityService;
|
22
|
+
isWarningVisible(): boolean;
|
23
|
+
}
|
24
|
+
//# sourceMappingURL=inactivity-manager.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"inactivity-manager.d.ts","sourceRoot":"","sources":["../../../src/inactivity/inactivity-manager.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AAE3E,MAAM,WAAW,uBAAwB,SAAQ,gBAAgB;IAC/D,QAAQ,EAAE,MAAM,IAAI,CAAC;CACtB;AAED,qBAAa,iBAAiB;IAC5B,OAAO,CAAC,OAAO,CAAoB;IACnC,OAAO,CAAC,MAAM,CAAiC;IAC/C,OAAO,CAAC,MAAM,CAA0B;IACxC,OAAO,CAAC,eAAe,CAAkB;gBAE7B,MAAM,EAAE,uBAAuB;IAU3C,OAAO,CAAC,mBAAmB;IAU3B,OAAO,CAAC,iBAAiB;IAoBzB,OAAO,CAAC,iBAAiB;IAYzB,OAAO,CAAC,cAAc;IAKtB,OAAO,CAAC,YAAY;IAMpB,OAAO,CAAC,aAAa;IAQd,KAAK,IAAI,IAAI;IAIb,IAAI,IAAI,IAAI;IAKZ,KAAK,IAAI,IAAI;IAKb,OAAO,IAAI,IAAI;IAKf,UAAU,IAAI,iBAAiB;IAI/B,gBAAgB,IAAI,OAAO;CAGnC"}
|
@@ -0,0 +1,34 @@
|
|
1
|
+
export interface InactivityConfig {
|
2
|
+
timeoutMinutes: number;
|
3
|
+
warningMinutes: number;
|
4
|
+
}
|
5
|
+
export interface InactivityEvents {
|
6
|
+
'inactivity-warning': {
|
7
|
+
remainingSeconds: number;
|
8
|
+
};
|
9
|
+
'inactivity-timeout': void;
|
10
|
+
'inactivity-reset': void;
|
11
|
+
}
|
12
|
+
type EventListener<T = any> = (data: T) => void;
|
13
|
+
export declare class InactivityService {
|
14
|
+
private config;
|
15
|
+
private warningId;
|
16
|
+
private timeoutId;
|
17
|
+
private lastActivity;
|
18
|
+
private isActive;
|
19
|
+
private eventListeners;
|
20
|
+
constructor(config: InactivityConfig);
|
21
|
+
private setupActivityListeners;
|
22
|
+
private checkTimeout;
|
23
|
+
on<K extends keyof InactivityEvents>(event: K, listener: EventListener<InactivityEvents[K]>): void;
|
24
|
+
off<K extends keyof InactivityEvents>(event: K, listener: EventListener<InactivityEvents[K]>): void;
|
25
|
+
private emit;
|
26
|
+
start(): void;
|
27
|
+
stop(): void;
|
28
|
+
resetTimer(): void;
|
29
|
+
private scheduleTimers;
|
30
|
+
private clearTimers;
|
31
|
+
destroy(): void;
|
32
|
+
}
|
33
|
+
export {};
|
34
|
+
//# sourceMappingURL=inactivity-service.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"inactivity-service.d.ts","sourceRoot":"","sources":["../../../src/inactivity/inactivity-service.ts"],"names":[],"mappings":"AAKA,MAAM,WAAW,gBAAgB;IAC/B,cAAc,EAAE,MAAM,CAAC;IACvB,cAAc,EAAE,MAAM,CAAC;CACxB;AAED,MAAM,WAAW,gBAAgB;IAC/B,oBAAoB,EAAE;QAAE,gBAAgB,EAAE,MAAM,CAAA;KAAE,CAAC;IACnD,oBAAoB,EAAE,IAAI,CAAC;IAC3B,kBAAkB,EAAE,IAAI,CAAC;CAC1B;AAED,KAAK,aAAa,CAAC,CAAC,GAAG,GAAG,IAAI,CAAC,IAAI,EAAE,CAAC,KAAK,IAAI,CAAC;AAEhD,qBAAa,iBAAiB;IAC5B,OAAO,CAAC,MAAM,CAAmB;IACjC,OAAO,CAAC,SAAS,CAAuB;IACxC,OAAO,CAAC,SAAS,CAAuB;IACxC,OAAO,CAAC,YAAY,CAAsB;IAC1C,OAAO,CAAC,QAAQ,CAAkB;IAClC,OAAO,CAAC,cAAc,CAA2D;gBAErE,MAAM,EAAE,gBAAgB;IAKpC,OAAO,CAAC,sBAAsB;IA6B9B,OAAO,CAAC,YAAY;IAkBb,EAAE,CAAC,CAAC,SAAS,MAAM,gBAAgB,EACxC,KAAK,EAAE,CAAC,EACR,QAAQ,EAAE,aAAa,CAAC,gBAAgB,CAAC,CAAC,CAAC,CAAC,GAC3C,IAAI;IAKA,GAAG,CAAC,CAAC,SAAS,MAAM,gBAAgB,EACzC,KAAK,EAAE,CAAC,EACR,QAAQ,EAAE,aAAa,CAAC,gBAAgB,CAAC,CAAC,CAAC,CAAC,GAC3C,IAAI;IAOP,OAAO,CAAC,IAAI;IAYL,KAAK,IAAI,IAAI;IAMb,IAAI,IAAI,IAAI;IAKZ,UAAU,IAAI,IAAI;IAQzB,OAAO,CAAC,cAAc;IAkBtB,OAAO,CAAC,WAAW;IAWZ,OAAO,IAAI,IAAI;CAIvB"}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/inactivity/index.ts"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAC;AACpC,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC"}
|
package/dist/dts/index.d.ts
CHANGED
package/dist/dts/index.d.ts.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,QAAQ,CAAC;AACvB,cAAc,cAAc,CAAC;AAC7B,cAAc,iBAAiB,CAAC;AAChC,cAAc,cAAc,CAAC;AAC7B,cAAc,YAAY,CAAC;AAC3B,cAAc,OAAO,CAAC;AACtB,cAAc,SAAS,CAAC;AACxB,cAAc,cAAc,CAAC;AAC7B,cAAc,UAAU,CAAC;AACzB,cAAc,WAAW,CAAC;AAC1B,cAAc,UAAU,CAAC;AACzB,cAAc,YAAY,CAAC;AAC3B,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAC3B,cAAc,eAAe,CAAC;AAC9B,cAAc,oBAAoB,CAAC;AACnC,cAAc,SAAS,CAAC;AACxB,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC;AACxB,cAAc,QAAQ,CAAC;AACvB,cAAc,UAAU,CAAC"}
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,QAAQ,CAAC;AACvB,cAAc,cAAc,CAAC;AAC7B,cAAc,iBAAiB,CAAC;AAChC,cAAc,cAAc,CAAC;AAC7B,cAAc,YAAY,CAAC;AAC3B,cAAc,OAAO,CAAC;AACtB,cAAc,SAAS,CAAC;AACxB,cAAc,cAAc,CAAC;AAC7B,cAAc,UAAU,CAAC;AACzB,cAAc,WAAW,CAAC;AAC1B,cAAc,UAAU,CAAC;AACzB,cAAc,YAAY,CAAC;AAC3B,cAAc,WAAW,CAAC;AAC1B,cAAc,YAAY,CAAC;AAC3B,cAAc,eAAe,CAAC;AAC9B,cAAc,oBAAoB,CAAC;AACnC,cAAc,SAAS,CAAC;AACxB,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC;AACxB,cAAc,QAAQ,CAAC;AACvB,cAAc,UAAU,CAAC;AACzB,cAAc,cAAc,CAAC"}
|
@@ -0,0 +1,213 @@
|
|
1
|
+
import { __decorate } from "tslib";
|
2
|
+
import { customElement, FASTElement, observable, html, css } from '@microsoft/fast-element';
|
3
|
+
import { getCurrentDesignSystemPrefix } from '../design-system/design-system';
|
4
|
+
// Constants for time conversions
|
5
|
+
const MILLISECONDS_PER_SECOND = 1000;
|
6
|
+
const getPrefixedInactivityDialog = (prefix) => html `
|
7
|
+
<div class="inactivity-overlay" ?hidden=${(x) => !x.isVisible}>
|
8
|
+
<${prefix}-card class="dialog-card">
|
9
|
+
<div class="card-body">
|
10
|
+
<div class="dialog-header">
|
11
|
+
<${prefix}-icon name="warning" class="warning-icon"></${prefix}-icon>
|
12
|
+
<h2>Session Timeout Warning</h2>
|
13
|
+
</div>
|
14
|
+
<div class="dialog-content">
|
15
|
+
<p>
|
16
|
+
You have been inactive for too long. You will be automatically logged out in
|
17
|
+
<strong>${(x) => x.remainingSeconds}</strong>
|
18
|
+
seconds.
|
19
|
+
</p>
|
20
|
+
<p>
|
21
|
+
Click "Continue Session" to stay logged in, or "Logout Now" to end your session
|
22
|
+
immediately.
|
23
|
+
</p>
|
24
|
+
</div>
|
25
|
+
<div class="dialog-actions">
|
26
|
+
<${prefix}-button
|
27
|
+
appearance="accent"
|
28
|
+
@click=${(x) => x.handleContinue()}
|
29
|
+
class="continue-button"
|
30
|
+
>
|
31
|
+
Continue Session
|
32
|
+
</${prefix}-button>
|
33
|
+
<${prefix}-button
|
34
|
+
appearance="outline"
|
35
|
+
@click=${(x) => x.handleLogout()}
|
36
|
+
class="logout-button"
|
37
|
+
>
|
38
|
+
Logout Now
|
39
|
+
</${prefix}-button>
|
40
|
+
</div>
|
41
|
+
</div>
|
42
|
+
</${prefix}-card>
|
43
|
+
</div>
|
44
|
+
`;
|
45
|
+
let InactivityDialog = class InactivityDialog extends FASTElement {
|
46
|
+
constructor() {
|
47
|
+
super(...arguments);
|
48
|
+
this.isVisible = false;
|
49
|
+
this.remainingSeconds = 0;
|
50
|
+
this.startTime = 0;
|
51
|
+
this.totalSeconds = 0;
|
52
|
+
}
|
53
|
+
show(options) {
|
54
|
+
this.totalSeconds = options.remainingSeconds;
|
55
|
+
this.remainingSeconds = options.remainingSeconds;
|
56
|
+
this.onContinue = options.onContinue;
|
57
|
+
this.onLogout = options.onLogout;
|
58
|
+
this.isVisible = true;
|
59
|
+
this.startCountdown();
|
60
|
+
}
|
61
|
+
hide() {
|
62
|
+
this.isVisible = false;
|
63
|
+
this.stopCountdown();
|
64
|
+
}
|
65
|
+
startCountdown() {
|
66
|
+
this.startTime = Date.now();
|
67
|
+
this.countdownInterval = window.setInterval(() => {
|
68
|
+
try {
|
69
|
+
// Calculate actual elapsed time since dialog started
|
70
|
+
const elapsedMs = Date.now() - this.startTime;
|
71
|
+
const elapsedSeconds = Math.floor(elapsedMs / MILLISECONDS_PER_SECOND);
|
72
|
+
// Calculate remaining time based on actual elapsed time
|
73
|
+
this.remainingSeconds = Math.max(0, this.totalSeconds - elapsedSeconds);
|
74
|
+
if (this.remainingSeconds <= 0) {
|
75
|
+
this.stopCountdown();
|
76
|
+
this.handleLogout();
|
77
|
+
}
|
78
|
+
}
|
79
|
+
catch (error) {
|
80
|
+
// If there's an error during countdown, stop the timer
|
81
|
+
this.stopCountdown();
|
82
|
+
console.warn('Error during inactivity countdown:', error);
|
83
|
+
}
|
84
|
+
}, MILLISECONDS_PER_SECOND);
|
85
|
+
}
|
86
|
+
stopCountdown() {
|
87
|
+
if (this.countdownInterval) {
|
88
|
+
clearInterval(this.countdownInterval);
|
89
|
+
this.countdownInterval = undefined;
|
90
|
+
}
|
91
|
+
}
|
92
|
+
handleContinue() {
|
93
|
+
this.hide();
|
94
|
+
if (this.onContinue) {
|
95
|
+
this.onContinue();
|
96
|
+
}
|
97
|
+
}
|
98
|
+
handleLogout() {
|
99
|
+
this.hide();
|
100
|
+
if (this.onLogout) {
|
101
|
+
this.onLogout();
|
102
|
+
}
|
103
|
+
}
|
104
|
+
disconnectedCallback() {
|
105
|
+
super.disconnectedCallback();
|
106
|
+
this.stopCountdown();
|
107
|
+
}
|
108
|
+
};
|
109
|
+
__decorate([
|
110
|
+
observable
|
111
|
+
], InactivityDialog.prototype, "isVisible", void 0);
|
112
|
+
__decorate([
|
113
|
+
observable
|
114
|
+
], InactivityDialog.prototype, "remainingSeconds", void 0);
|
115
|
+
InactivityDialog = __decorate([
|
116
|
+
customElement({
|
117
|
+
name: 'inactivity-dialog',
|
118
|
+
template: html `
|
119
|
+
${(x) => getPrefixedInactivityDialog(getCurrentDesignSystemPrefix(x, 'rapid'))}
|
120
|
+
`,
|
121
|
+
styles: css `
|
122
|
+
.inactivity-overlay {
|
123
|
+
position: fixed;
|
124
|
+
inset: 0;
|
125
|
+
width: 100vw;
|
126
|
+
height: 100vh;
|
127
|
+
background-color: rgba(0, 0, 0, 0.6);
|
128
|
+
display: flex;
|
129
|
+
align-items: center;
|
130
|
+
justify-content: center;
|
131
|
+
z-index: 9999;
|
132
|
+
backdrop-filter: blur(2px);
|
133
|
+
}
|
134
|
+
|
135
|
+
.dialog-card {
|
136
|
+
height: 260px;
|
137
|
+
max-height: 260px;
|
138
|
+
max-width: 520px;
|
139
|
+
width: 92%;
|
140
|
+
}
|
141
|
+
|
142
|
+
.card-body {
|
143
|
+
padding: 24px;
|
144
|
+
box-sizing: border-box;
|
145
|
+
}
|
146
|
+
|
147
|
+
.dialog-header {
|
148
|
+
display: flex;
|
149
|
+
align-items: center;
|
150
|
+
gap: 12px;
|
151
|
+
margin-bottom: 16px;
|
152
|
+
}
|
153
|
+
|
154
|
+
.warning-icon {
|
155
|
+
color: var(--accent-fill-rest);
|
156
|
+
font-size: 24px;
|
157
|
+
}
|
158
|
+
|
159
|
+
.dialog-header h2 {
|
160
|
+
margin: 0;
|
161
|
+
color: var(--neutral-foreground-rest);
|
162
|
+
font-size: 20px;
|
163
|
+
font-weight: 600;
|
164
|
+
}
|
165
|
+
|
166
|
+
.dialog-content {
|
167
|
+
margin-bottom: 24px;
|
168
|
+
}
|
169
|
+
|
170
|
+
.dialog-content p {
|
171
|
+
margin: 0 0 12px 0;
|
172
|
+
color: var(--neutral-foreground-rest);
|
173
|
+
line-height: 1.5;
|
174
|
+
}
|
175
|
+
|
176
|
+
.dialog-content p:last-child {
|
177
|
+
margin-bottom: 0;
|
178
|
+
}
|
179
|
+
|
180
|
+
.dialog-content strong {
|
181
|
+
color: var(--accent-foreground-rest);
|
182
|
+
font-weight: 600;
|
183
|
+
}
|
184
|
+
|
185
|
+
.dialog-actions {
|
186
|
+
display: flex;
|
187
|
+
gap: 12px;
|
188
|
+
justify-content: flex-end;
|
189
|
+
}
|
190
|
+
|
191
|
+
.continue-button {
|
192
|
+
min-width: 140px;
|
193
|
+
}
|
194
|
+
.logout-button {
|
195
|
+
min-width: 120px;
|
196
|
+
}
|
197
|
+
|
198
|
+
@media (max-width: 480px) {
|
199
|
+
.card-body {
|
200
|
+
padding: 20px;
|
201
|
+
}
|
202
|
+
.dialog-actions {
|
203
|
+
flex-direction: column;
|
204
|
+
}
|
205
|
+
.continue-button,
|
206
|
+
.logout-button {
|
207
|
+
width: 100%;
|
208
|
+
}
|
209
|
+
}
|
210
|
+
`,
|
211
|
+
})
|
212
|
+
], InactivityDialog);
|
213
|
+
export { InactivityDialog };
|
@@ -0,0 +1,87 @@
|
|
1
|
+
import { InactivityService } from './inactivity-service';
|
2
|
+
export class InactivityManager {
|
3
|
+
constructor(config) {
|
4
|
+
this.dialog = null;
|
5
|
+
this.isDialogVisible = false;
|
6
|
+
this.config = config;
|
7
|
+
this.service = new InactivityService({
|
8
|
+
timeoutMinutes: config.timeoutMinutes,
|
9
|
+
warningMinutes: config.warningMinutes,
|
10
|
+
});
|
11
|
+
this.setupEventListeners();
|
12
|
+
console.log('InactivityManager constructor ', this.config);
|
13
|
+
}
|
14
|
+
setupEventListeners() {
|
15
|
+
this.service.on('inactivity-warning', (data) => {
|
16
|
+
this.showWarningDialog(data.remainingSeconds);
|
17
|
+
});
|
18
|
+
this.service.on('inactivity-timeout', () => {
|
19
|
+
this.handleTimeout();
|
20
|
+
});
|
21
|
+
}
|
22
|
+
showWarningDialog(remainingSeconds) {
|
23
|
+
if (this.isDialogVisible)
|
24
|
+
return;
|
25
|
+
if (!this.dialog) {
|
26
|
+
this.dialog = document.createElement('inactivity-dialog');
|
27
|
+
console.log('Creating dialog', this.dialog);
|
28
|
+
}
|
29
|
+
if (!this.dialog.isConnected) {
|
30
|
+
document.body.appendChild(this.dialog);
|
31
|
+
}
|
32
|
+
this.isDialogVisible = true;
|
33
|
+
this.dialog.show({
|
34
|
+
remainingSeconds,
|
35
|
+
onContinue: () => this.handleContinue(),
|
36
|
+
onLogout: () => this.handleLogout(),
|
37
|
+
});
|
38
|
+
}
|
39
|
+
hideWarningDialog() {
|
40
|
+
if (!this.isDialogVisible || !this.dialog)
|
41
|
+
return;
|
42
|
+
this.isDialogVisible = false;
|
43
|
+
this.dialog.hide();
|
44
|
+
// Remove from DOM
|
45
|
+
if (this.dialog.parentNode) {
|
46
|
+
this.dialog.parentNode.removeChild(this.dialog);
|
47
|
+
}
|
48
|
+
}
|
49
|
+
handleContinue() {
|
50
|
+
this.service.resetTimer();
|
51
|
+
this.hideWarningDialog();
|
52
|
+
}
|
53
|
+
handleLogout() {
|
54
|
+
this.hideWarningDialog();
|
55
|
+
this.service.stop();
|
56
|
+
if (this.config.onLogout)
|
57
|
+
this.config.onLogout();
|
58
|
+
}
|
59
|
+
handleTimeout() {
|
60
|
+
// If dialog is visible, let it handle the countdown
|
61
|
+
if (this.isDialogVisible)
|
62
|
+
return;
|
63
|
+
// Otherwise, logout immediately
|
64
|
+
this.handleLogout();
|
65
|
+
}
|
66
|
+
start() {
|
67
|
+
this.service.start();
|
68
|
+
}
|
69
|
+
stop() {
|
70
|
+
this.service.stop();
|
71
|
+
this.hideWarningDialog();
|
72
|
+
}
|
73
|
+
reset() {
|
74
|
+
this.service.resetTimer();
|
75
|
+
this.hideWarningDialog();
|
76
|
+
}
|
77
|
+
destroy() {
|
78
|
+
this.service.destroy();
|
79
|
+
this.hideWarningDialog();
|
80
|
+
}
|
81
|
+
getService() {
|
82
|
+
return this.service;
|
83
|
+
}
|
84
|
+
isWarningVisible() {
|
85
|
+
return this.isDialogVisible;
|
86
|
+
}
|
87
|
+
}
|
@@ -0,0 +1,128 @@
|
|
1
|
+
// Constants for time conversions
|
2
|
+
const SECONDS_PER_MINUTE = 60;
|
3
|
+
const MILLISECONDS_PER_SECOND = 1000;
|
4
|
+
const MILLISECONDS_PER_MINUTE = SECONDS_PER_MINUTE * MILLISECONDS_PER_SECOND;
|
5
|
+
export class InactivityService {
|
6
|
+
constructor(config) {
|
7
|
+
this.warningId = null;
|
8
|
+
this.timeoutId = null;
|
9
|
+
this.lastActivity = Date.now();
|
10
|
+
this.isActive = false;
|
11
|
+
this.eventListeners = new Map();
|
12
|
+
this.config = config;
|
13
|
+
this.setupActivityListeners();
|
14
|
+
}
|
15
|
+
setupActivityListeners() {
|
16
|
+
const events = [
|
17
|
+
'mousedown',
|
18
|
+
'mousemove',
|
19
|
+
'keydown',
|
20
|
+
'touchstart',
|
21
|
+
'click',
|
22
|
+
'focus',
|
23
|
+
'blur',
|
24
|
+
'wheel',
|
25
|
+
'touchmove',
|
26
|
+
];
|
27
|
+
const handler = () => this.resetTimer();
|
28
|
+
events.forEach((event) => {
|
29
|
+
document.addEventListener(event, handler, { passive: true });
|
30
|
+
});
|
31
|
+
// Handle scroll events
|
32
|
+
window.addEventListener('scroll', handler, { passive: true, capture: true });
|
33
|
+
document.addEventListener('scroll', handler, { passive: true, capture: true });
|
34
|
+
// Handle sleep/wake cycles
|
35
|
+
document.addEventListener('visibilitychange', () => {
|
36
|
+
if (document.visibilityState === 'visible' && this.isActive) {
|
37
|
+
this.checkTimeout();
|
38
|
+
}
|
39
|
+
});
|
40
|
+
}
|
41
|
+
checkTimeout() {
|
42
|
+
const timeSinceLastActivity = Date.now() - this.lastActivity;
|
43
|
+
const timeoutMs = this.config.timeoutMinutes * MILLISECONDS_PER_MINUTE;
|
44
|
+
const warningMs = (this.config.timeoutMinutes - this.config.warningMinutes) * MILLISECONDS_PER_MINUTE;
|
45
|
+
if (timeSinceLastActivity >= timeoutMs) {
|
46
|
+
this.emit('inactivity-timeout');
|
47
|
+
}
|
48
|
+
else if (timeSinceLastActivity >= warningMs) {
|
49
|
+
const remainingSeconds = Math.round((timeoutMs - timeSinceLastActivity) / MILLISECONDS_PER_SECOND);
|
50
|
+
this.emit('inactivity-warning', { remainingSeconds });
|
51
|
+
}
|
52
|
+
else {
|
53
|
+
this.resetTimer();
|
54
|
+
}
|
55
|
+
}
|
56
|
+
on(event, listener) {
|
57
|
+
if (!this.eventListeners.has(event))
|
58
|
+
this.eventListeners.set(event, []);
|
59
|
+
this.eventListeners.get(event).push(listener);
|
60
|
+
}
|
61
|
+
off(event, listener) {
|
62
|
+
const listeners = this.eventListeners.get(event);
|
63
|
+
if (!listeners)
|
64
|
+
return;
|
65
|
+
const i = listeners.indexOf(listener);
|
66
|
+
if (i > -1)
|
67
|
+
listeners.splice(i, 1);
|
68
|
+
}
|
69
|
+
emit(event, data) {
|
70
|
+
const listeners = this.eventListeners.get(event);
|
71
|
+
if (!listeners)
|
72
|
+
return;
|
73
|
+
for (const l of listeners) {
|
74
|
+
try {
|
75
|
+
l(data);
|
76
|
+
}
|
77
|
+
catch (e) {
|
78
|
+
console.error('InactivityService listener error', e);
|
79
|
+
}
|
80
|
+
}
|
81
|
+
}
|
82
|
+
start() {
|
83
|
+
this.isActive = true;
|
84
|
+
this.lastActivity = Date.now();
|
85
|
+
this.scheduleTimers();
|
86
|
+
}
|
87
|
+
stop() {
|
88
|
+
this.isActive = false;
|
89
|
+
this.clearTimers();
|
90
|
+
}
|
91
|
+
resetTimer() {
|
92
|
+
if (!this.isActive)
|
93
|
+
return;
|
94
|
+
this.lastActivity = Date.now();
|
95
|
+
this.clearTimers();
|
96
|
+
this.scheduleTimers();
|
97
|
+
this.emit('inactivity-reset');
|
98
|
+
}
|
99
|
+
scheduleTimers() {
|
100
|
+
const warningDelayMs = (this.config.timeoutMinutes - this.config.warningMinutes) * MILLISECONDS_PER_MINUTE;
|
101
|
+
this.warningId = window.setTimeout(() => {
|
102
|
+
if (this.isActive) {
|
103
|
+
const remainingSeconds = Math.round(this.config.warningMinutes * SECONDS_PER_MINUTE);
|
104
|
+
this.emit('inactivity-warning', { remainingSeconds });
|
105
|
+
}
|
106
|
+
}, warningDelayMs);
|
107
|
+
const timeoutDelayMs = this.config.timeoutMinutes * MILLISECONDS_PER_MINUTE;
|
108
|
+
this.timeoutId = window.setTimeout(() => {
|
109
|
+
if (this.isActive) {
|
110
|
+
this.emit('inactivity-timeout');
|
111
|
+
}
|
112
|
+
}, timeoutDelayMs);
|
113
|
+
}
|
114
|
+
clearTimers() {
|
115
|
+
if (this.warningId) {
|
116
|
+
clearTimeout(this.warningId);
|
117
|
+
this.warningId = null;
|
118
|
+
}
|
119
|
+
if (this.timeoutId) {
|
120
|
+
clearTimeout(this.timeoutId);
|
121
|
+
this.timeoutId = null;
|
122
|
+
}
|
123
|
+
}
|
124
|
+
destroy() {
|
125
|
+
this.stop();
|
126
|
+
this.eventListeners.clear();
|
127
|
+
}
|
128
|
+
}
|
package/dist/esm/index.js
CHANGED