@modernman00/shared-js-lib 1.2.83 โ 1.2.88
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/AddressAutocomplete.js +473 -0
- package/DigitalSignaturePad.js +545 -0
- package/OtpInputManager.js +429 -0
- package/PwaManager.js +128 -10
- package/config/version.php +1 -1
- package/index.js +5 -1
- package/package.json +5 -2
- package/release-history.md +27 -0
|
@@ -0,0 +1,429 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OtpInputManager - Centralized OTP & Security Verification Input Controller
|
|
3
|
+
* Part of @modernman00/shared-js-lib
|
|
4
|
+
*
|
|
5
|
+
* Provides cross-platform OTP handling:
|
|
6
|
+
* - iOS 17+ Apple Mail QuickType autofill multi-character distribution
|
|
7
|
+
* - Android Gmail notification copy & Gboard suggestion strip support
|
|
8
|
+
* - Single-keypress automatic focus progression & backspace navigation
|
|
9
|
+
* - Tab focus (visibilitychange) smart clipboard detection
|
|
10
|
+
* - 1-Click Magic Link URL query parameter extraction (?code=123456) with history sanitization
|
|
11
|
+
* - Eastern Arabic / Perso-Arabic numeral normalization
|
|
12
|
+
* - Automatic form submission upon completion
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
export class OtpInputManager {
|
|
16
|
+
/**
|
|
17
|
+
* @param {Object} options
|
|
18
|
+
* @param {string|NodeList|Array} [options.inputs='.otp-input'] - Selector or element collection for OTP inputs
|
|
19
|
+
* @param {string} [options.hiddenInputId='code'] - ID of hidden input storing full code
|
|
20
|
+
* @param {string} [options.submitBtnId='button'] - ID of submit button to trigger on completion
|
|
21
|
+
* @param {string} [options.pasteBtnId='pasteBtn'] - Optional ID of "Paste Code from Email" button
|
|
22
|
+
* @param {number} [options.length=6] - Expected length of verification code
|
|
23
|
+
* @param {boolean} [options.autoSubmit=true] - Auto-submit form when all digits are entered
|
|
24
|
+
* @param {number} [options.autoSubmitDelay=150] - Delay in ms before auto-submitting
|
|
25
|
+
* @param {boolean} [options.checkClipboardOnFocus=true] - Check clipboard on visibilitychange / focus
|
|
26
|
+
* @param {string} [options.urlParamKey='code'] - URL search param to check for magic link autofill
|
|
27
|
+
* @param {boolean} [options.cleanUrlAfterFill=true] - Strip ?code= param from URL bar to prevent leakage
|
|
28
|
+
* @param {Function} [options.onComplete=null] - Callback triggered when code is complete
|
|
29
|
+
* @param {Function} [options.onChange=null] - Callback triggered when any digit changes
|
|
30
|
+
*/
|
|
31
|
+
constructor(options = {}) {
|
|
32
|
+
this.options = {
|
|
33
|
+
inputs: '.otp-input',
|
|
34
|
+
hiddenInputId: 'code',
|
|
35
|
+
submitBtnId: 'button',
|
|
36
|
+
pasteBtnId: 'pasteBtn',
|
|
37
|
+
length: 6,
|
|
38
|
+
autoSubmit: true,
|
|
39
|
+
autoSubmitDelay: 150,
|
|
40
|
+
checkClipboardOnFocus: true,
|
|
41
|
+
urlParamKey: 'code',
|
|
42
|
+
cleanUrlAfterFill: true,
|
|
43
|
+
onComplete: null,
|
|
44
|
+
onChange: null,
|
|
45
|
+
...options
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
this.inputs = [];
|
|
49
|
+
this.hiddenInput = null;
|
|
50
|
+
this.submitBtn = null;
|
|
51
|
+
this.pasteBtn = null;
|
|
52
|
+
this.boundHandlers = [];
|
|
53
|
+
this.isSubmitting = false;
|
|
54
|
+
|
|
55
|
+
this._initElements();
|
|
56
|
+
if (this.inputs.length > 0) {
|
|
57
|
+
this._bindEvents();
|
|
58
|
+
this._checkUrlParameter();
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Normalizes Eastern Arabic / Perso-Arabic digits to standard ASCII numerals
|
|
64
|
+
* and strips non-alphanumeric characters.
|
|
65
|
+
*
|
|
66
|
+
* @param {string} str
|
|
67
|
+
* @returns {string}
|
|
68
|
+
*/
|
|
69
|
+
static normalizeDigits(str) {
|
|
70
|
+
if (!str || typeof str !== 'string') return '';
|
|
71
|
+
return str
|
|
72
|
+
.replace(/[\u0660-\u0669]/g, c => String.fromCharCode(c.charCodeAt(0) - 0x0660 + 48))
|
|
73
|
+
.replace(/[\u06F0-\u06F9]/g, c => String.fromCharCode(c.charCodeAt(0) - 0x06F0 + 48))
|
|
74
|
+
.replace(/[^a-zA-Z0-9]/g, '')
|
|
75
|
+
.toUpperCase();
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Resolve DOM elements
|
|
80
|
+
* @private
|
|
81
|
+
*/
|
|
82
|
+
_initElements() {
|
|
83
|
+
if (typeof document === 'undefined') return;
|
|
84
|
+
|
|
85
|
+
if (typeof this.options.inputs === 'string') {
|
|
86
|
+
this.inputs = Array.from(document.querySelectorAll(this.options.inputs));
|
|
87
|
+
} else if (this.options.inputs instanceof NodeList || Array.isArray(this.options.inputs)) {
|
|
88
|
+
this.inputs = Array.from(this.options.inputs);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// Single input mode fallback (e.g. LoanEasyFinance or TenantScore)
|
|
92
|
+
if (this.inputs.length === 0) {
|
|
93
|
+
const singleInput = document.getElementById(this.options.singleInputId || 'code') ||
|
|
94
|
+
document.querySelector('input[name="code"]');
|
|
95
|
+
if (singleInput) {
|
|
96
|
+
this.inputs = [singleInput];
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (this.options.hiddenInputId) {
|
|
101
|
+
this.hiddenInput = document.getElementById(this.options.hiddenInputId);
|
|
102
|
+
}
|
|
103
|
+
if (this.options.submitBtnId) {
|
|
104
|
+
this.submitBtn = document.getElementById(this.options.submitBtnId);
|
|
105
|
+
}
|
|
106
|
+
if (this.options.pasteBtnId) {
|
|
107
|
+
this.pasteBtn = document.getElementById(this.options.pasteBtnId);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Ensure inputs have autocomplete and inputmode configured for mobile devices
|
|
111
|
+
this.inputs.forEach((input, index) => {
|
|
112
|
+
if (!input.getAttribute('inputmode')) {
|
|
113
|
+
input.setAttribute('inputmode', 'numeric');
|
|
114
|
+
}
|
|
115
|
+
// On mobile (iOS / Android), the first box receives the autofill event
|
|
116
|
+
if (index === 0) {
|
|
117
|
+
input.setAttribute('autocomplete', 'one-time-code');
|
|
118
|
+
// Allow up to expected length in first box so mobile OS doesn't truncate the payload
|
|
119
|
+
input.setAttribute('maxlength', String(this.options.length));
|
|
120
|
+
} else {
|
|
121
|
+
input.setAttribute('autocomplete', 'off');
|
|
122
|
+
input.setAttribute('maxlength', '1');
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Bind event listeners
|
|
129
|
+
* @private
|
|
130
|
+
*/
|
|
131
|
+
_bindEvents() {
|
|
132
|
+
if (typeof document === 'undefined') return;
|
|
133
|
+
|
|
134
|
+
this.inputs.forEach((input, index) => {
|
|
135
|
+
// 1. Input event: Handles single keypress AND mobile OS multi-digit autofill (iOS 17 QuickType)
|
|
136
|
+
const onInput = (e) => this._handleInput(e, index);
|
|
137
|
+
input.addEventListener('input', onInput);
|
|
138
|
+
this.boundHandlers.push({ element: input, event: 'input', handler: onInput });
|
|
139
|
+
|
|
140
|
+
// 2. Keydown event: Handles Backspace, Delete, and Arrow navigation
|
|
141
|
+
const onKeydown = (e) => this._handleKeydown(e, index);
|
|
142
|
+
input.addEventListener('keydown', onKeydown);
|
|
143
|
+
this.boundHandlers.push({ element: input, event: 'keydown', handler: onKeydown });
|
|
144
|
+
|
|
145
|
+
// 3. Paste event: Handles explicit paste actions
|
|
146
|
+
const onPaste = (e) => this._handlePaste(e, index);
|
|
147
|
+
input.addEventListener('paste', onPaste);
|
|
148
|
+
this.boundHandlers.push({ element: input, event: 'paste', handler: onPaste });
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
// 4. Paste button binding
|
|
152
|
+
if (this.pasteBtn) {
|
|
153
|
+
const onPasteBtnClick = () => this.pasteFromClipboard();
|
|
154
|
+
this.pasteBtn.addEventListener('click', onPasteBtnClick);
|
|
155
|
+
this.boundHandlers.push({ element: this.pasteBtn, event: 'click', handler: onPasteBtnClick });
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// 5. Visibility change / Tab re-focus detection (Android / iOS app switching from email)
|
|
159
|
+
if (this.options.checkClipboardOnFocus && typeof document.addEventListener !== 'undefined') {
|
|
160
|
+
const onVisibilityChange = () => {
|
|
161
|
+
if (document.visibilityState === 'visible') {
|
|
162
|
+
this._checkClipboardOnResume();
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
document.addEventListener('visibilitychange', onVisibilityChange);
|
|
166
|
+
this.boundHandlers.push({ element: document, event: 'visibilitychange', handler: onVisibilityChange });
|
|
167
|
+
|
|
168
|
+
const onWindowFocus = () => this._checkClipboardOnResume();
|
|
169
|
+
window.addEventListener('focus', onWindowFocus);
|
|
170
|
+
this.boundHandlers.push({ element: window, event: 'focus', handler: onWindowFocus });
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Handle input event on an individual box
|
|
176
|
+
* @private
|
|
177
|
+
*/
|
|
178
|
+
_handleInput(e, index) {
|
|
179
|
+
const rawVal = e.target.value;
|
|
180
|
+
const clean = OtpInputManager.normalizeDigits(rawVal);
|
|
181
|
+
|
|
182
|
+
// Single input mode (e.g. LoanEasyFinance or TenantScore)
|
|
183
|
+
if (this.inputs.length === 1) {
|
|
184
|
+
e.target.value = clean.substring(0, this.options.length);
|
|
185
|
+
this._updateHiddenInput();
|
|
186
|
+
if (clean.length >= this.options.length) {
|
|
187
|
+
this._checkComplete();
|
|
188
|
+
}
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// CASE 1: Multi-character injection (iOS 17+ Mail QuickType, Android Gboard, autofill)
|
|
193
|
+
if (clean.length > 1) {
|
|
194
|
+
this.distributeCode(clean, index);
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// CASE 2: Single character entry
|
|
199
|
+
if (clean.length === 1) {
|
|
200
|
+
e.target.value = clean;
|
|
201
|
+
this._updateHiddenInput();
|
|
202
|
+
|
|
203
|
+
if (index < this.inputs.length - 1) {
|
|
204
|
+
this.inputs[index + 1].focus();
|
|
205
|
+
} else {
|
|
206
|
+
this._checkComplete();
|
|
207
|
+
}
|
|
208
|
+
} else {
|
|
209
|
+
e.target.value = '';
|
|
210
|
+
this._updateHiddenInput();
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Handle keydown event (Backspace, Arrow keys)
|
|
216
|
+
* @private
|
|
217
|
+
*/
|
|
218
|
+
_handleKeydown(e, index) {
|
|
219
|
+
if (e.key === 'Backspace') {
|
|
220
|
+
if (!e.target.value && index > 0) {
|
|
221
|
+
e.preventDefault();
|
|
222
|
+
this.inputs[index - 1].value = '';
|
|
223
|
+
this.inputs[index - 1].focus();
|
|
224
|
+
this._updateHiddenInput();
|
|
225
|
+
}
|
|
226
|
+
} else if (e.key === 'ArrowLeft' && index > 0) {
|
|
227
|
+
e.preventDefault();
|
|
228
|
+
this.inputs[index - 1].focus();
|
|
229
|
+
} else if (e.key === 'ArrowRight' && index < this.inputs.length - 1) {
|
|
230
|
+
e.preventDefault();
|
|
231
|
+
this.inputs[index + 1].focus();
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Handle explicit paste event
|
|
237
|
+
* @private
|
|
238
|
+
*/
|
|
239
|
+
_handlePaste(e, index) {
|
|
240
|
+
e.preventDefault();
|
|
241
|
+
const pasteData = (e.clipboardData || window.clipboardData)?.getData('text') || '';
|
|
242
|
+
if (pasteData) {
|
|
243
|
+
this.distributeCode(pasteData, index);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Distributes a multi-character string across input boxes starting from startIndex
|
|
249
|
+
*
|
|
250
|
+
* @param {string} rawCode
|
|
251
|
+
* @param {number} [startIndex=0]
|
|
252
|
+
*/
|
|
253
|
+
distributeCode(rawCode, startIndex = 0) {
|
|
254
|
+
const code = OtpInputManager.normalizeDigits(rawCode).substring(0, this.options.length);
|
|
255
|
+
if (!code) return;
|
|
256
|
+
|
|
257
|
+
if (this.inputs.length === 1) {
|
|
258
|
+
this.inputs[0].value = code;
|
|
259
|
+
this._updateHiddenInput();
|
|
260
|
+
this.inputs[0].focus();
|
|
261
|
+
if (code.length === this.options.length) {
|
|
262
|
+
this._checkComplete();
|
|
263
|
+
}
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
const chars = code.split('');
|
|
268
|
+
chars.forEach((char, i) => {
|
|
269
|
+
if (this.inputs[i]) {
|
|
270
|
+
this.inputs[i].value = char;
|
|
271
|
+
}
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
this._updateHiddenInput();
|
|
275
|
+
|
|
276
|
+
const targetIndex = Math.min(chars.length - 1, this.inputs.length - 1);
|
|
277
|
+
if (this.inputs[targetIndex]) {
|
|
278
|
+
this.inputs[targetIndex].focus();
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
if (code.length === this.options.length) {
|
|
282
|
+
this._checkComplete();
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Reads code from system clipboard
|
|
288
|
+
* @returns {Promise<boolean>}
|
|
289
|
+
*/
|
|
290
|
+
async pasteFromClipboard() {
|
|
291
|
+
if (typeof navigator === 'undefined' || !navigator.clipboard?.readText) {
|
|
292
|
+
return false;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
try {
|
|
296
|
+
const text = await navigator.clipboard.readText();
|
|
297
|
+
const code = OtpInputManager.normalizeDigits(text.trim());
|
|
298
|
+
if (code.length === this.options.length) {
|
|
299
|
+
this.distributeCode(code, 0);
|
|
300
|
+
return true;
|
|
301
|
+
}
|
|
302
|
+
} catch (_) {
|
|
303
|
+
// Permission denied or clipboard empty
|
|
304
|
+
}
|
|
305
|
+
return false;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* Background clipboard check on tab resume / focus
|
|
310
|
+
* @private
|
|
311
|
+
*/
|
|
312
|
+
async _checkClipboardOnResume() {
|
|
313
|
+
// Only auto-fill if all inputs are currently empty
|
|
314
|
+
const currentCode = this.getCode();
|
|
315
|
+
if (currentCode.length === this.options.length) return;
|
|
316
|
+
|
|
317
|
+
if (typeof navigator === 'undefined' || !navigator.clipboard?.readText) return;
|
|
318
|
+
|
|
319
|
+
try {
|
|
320
|
+
const text = await navigator.clipboard.readText();
|
|
321
|
+
const clean = OtpInputManager.normalizeDigits(text.trim());
|
|
322
|
+
// Strictly verify it matches the required code length exactly
|
|
323
|
+
if (clean.length === this.options.length) {
|
|
324
|
+
this.distributeCode(clean, 0);
|
|
325
|
+
}
|
|
326
|
+
} catch (_) {
|
|
327
|
+
// Silent catch: browsers without focus permission won't throw unhandled errors
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* Checks URL query parameters for magic link autofill (e.g., ?code=123456)
|
|
333
|
+
* @private
|
|
334
|
+
*/
|
|
335
|
+
_checkUrlParameter() {
|
|
336
|
+
if (typeof window === 'undefined') return;
|
|
337
|
+
|
|
338
|
+
try {
|
|
339
|
+
const search = this.options.searchQuery !== undefined ? this.options.searchQuery : window.location?.search;
|
|
340
|
+
if (!search) return;
|
|
341
|
+
|
|
342
|
+
const params = new URLSearchParams(search);
|
|
343
|
+
const urlCode = params.get(this.options.urlParamKey);
|
|
344
|
+
|
|
345
|
+
if (urlCode) {
|
|
346
|
+
const cleanCode = OtpInputManager.normalizeDigits(urlCode).substring(0, this.options.length);
|
|
347
|
+
if (cleanCode.length === this.options.length) {
|
|
348
|
+
this.distributeCode(cleanCode, 0);
|
|
349
|
+
|
|
350
|
+
// Strip the ?code= query parameter from URL bar to prevent token leakage in history/referrer
|
|
351
|
+
if (this.options.cleanUrlAfterFill && window.history?.replaceState && this.options.searchQuery === undefined) {
|
|
352
|
+
params.delete(this.options.urlParamKey);
|
|
353
|
+
const newSearch = params.toString();
|
|
354
|
+
const cleanUrl = (window.location?.pathname || '') + (newSearch ? `?${newSearch}` : '') + (window.location?.hash || '');
|
|
355
|
+
window.history.replaceState({}, document.title, cleanUrl);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
} catch (_) {}
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/**
|
|
363
|
+
* Synchronize the hidden input with the current values of all boxes
|
|
364
|
+
* @private
|
|
365
|
+
*/
|
|
366
|
+
_updateHiddenInput() {
|
|
367
|
+
const code = this.getCode();
|
|
368
|
+
if (this.hiddenInput) {
|
|
369
|
+
this.hiddenInput.value = code;
|
|
370
|
+
}
|
|
371
|
+
if (typeof this.options.onChange === 'function') {
|
|
372
|
+
this.options.onChange(code);
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/**
|
|
377
|
+
* Get concatenated value of all input boxes
|
|
378
|
+
* @returns {string}
|
|
379
|
+
*/
|
|
380
|
+
getCode() {
|
|
381
|
+
const raw = this.inputs.map(input => input.value).join('');
|
|
382
|
+
return OtpInputManager.normalizeDigits(raw);
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/**
|
|
386
|
+
* Clears all inputs and hidden input
|
|
387
|
+
*/
|
|
388
|
+
clear() {
|
|
389
|
+
this.inputs.forEach(input => {
|
|
390
|
+
input.value = '';
|
|
391
|
+
});
|
|
392
|
+
this._updateHiddenInput();
|
|
393
|
+
if (this.inputs[0]) {
|
|
394
|
+
this.inputs[0].focus();
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
/**
|
|
399
|
+
* Checks if all digits are filled and triggers completion / auto-submit
|
|
400
|
+
* @private
|
|
401
|
+
*/
|
|
402
|
+
_checkComplete() {
|
|
403
|
+
const code = this.getCode();
|
|
404
|
+
if (code.length === this.options.length) {
|
|
405
|
+
if (typeof this.options.onComplete === 'function') {
|
|
406
|
+
this.options.onComplete(code);
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
if (this.options.autoSubmit && this.submitBtn && !this.isSubmitting) {
|
|
410
|
+
this.isSubmitting = true;
|
|
411
|
+
setTimeout(() => {
|
|
412
|
+
if (this.submitBtn) {
|
|
413
|
+
this.submitBtn.click();
|
|
414
|
+
}
|
|
415
|
+
}, this.options.autoSubmitDelay);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
/**
|
|
421
|
+
* Cleans up all bound event listeners
|
|
422
|
+
*/
|
|
423
|
+
destroy() {
|
|
424
|
+
this.boundHandlers.forEach(({ element, event, handler }) => {
|
|
425
|
+
element.removeEventListener(event, handler);
|
|
426
|
+
});
|
|
427
|
+
this.boundHandlers = [];
|
|
428
|
+
}
|
|
429
|
+
}
|
package/PwaManager.js
CHANGED
|
@@ -82,6 +82,9 @@ export class PwaManager {
|
|
|
82
82
|
navigator.serviceWorker.addEventListener('message', (event) => {
|
|
83
83
|
if (event.data?.type === 'ON_APP_NOTIFICATION') {
|
|
84
84
|
this.syncAppBadge();
|
|
85
|
+
if (typeof window !== 'undefined' && window.dispatchEvent) {
|
|
86
|
+
window.dispatchEvent(new CustomEvent('pwa:notification-received', { detail: event.data.payload }));
|
|
87
|
+
}
|
|
85
88
|
}
|
|
86
89
|
});
|
|
87
90
|
}
|
|
@@ -118,11 +121,109 @@ export class PwaManager {
|
|
|
118
121
|
}));
|
|
119
122
|
}
|
|
120
123
|
|
|
124
|
+
if (this.options.enableUpdateToast) {
|
|
125
|
+
this.showUpdateToast(worker);
|
|
126
|
+
}
|
|
127
|
+
|
|
121
128
|
if (typeof this.options.onUpdateAvailable === 'function') {
|
|
122
129
|
this.options.onUpdateAvailable(worker);
|
|
123
130
|
}
|
|
124
131
|
}
|
|
125
132
|
|
|
133
|
+
/**
|
|
134
|
+
* Renders a discreet, glassmorphic floating update pill to prompt user refresh
|
|
135
|
+
*/
|
|
136
|
+
static showUpdateToast(worker) {
|
|
137
|
+
if (typeof document === 'undefined' || document.getElementById('pwaUniversalUpdateToast')) {
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const toast = document.createElement('div');
|
|
142
|
+
toast.id = 'pwaUniversalUpdateToast';
|
|
143
|
+
toast.setAttribute('role', 'status');
|
|
144
|
+
toast.setAttribute('aria-live', 'polite');
|
|
145
|
+
toast.setAttribute('aria-label', `${this.options.appName || 'App'} update available`);
|
|
146
|
+
|
|
147
|
+
toast.style.position = 'fixed';
|
|
148
|
+
toast.style.bottom = 'max(16px, env(safe-area-inset-bottom, 16px))';
|
|
149
|
+
toast.style.left = '50%';
|
|
150
|
+
toast.style.transform = 'translateX(-50%)';
|
|
151
|
+
toast.style.zIndex = '99999';
|
|
152
|
+
toast.style.display = 'flex';
|
|
153
|
+
toast.style.alignItems = 'center';
|
|
154
|
+
toast.style.gap = '12px';
|
|
155
|
+
toast.style.padding = '8px 14px 8px 16px';
|
|
156
|
+
toast.style.backgroundColor = 'rgba(15, 23, 42, 0.94)';
|
|
157
|
+
toast.style.backdropFilter = 'blur(12px)';
|
|
158
|
+
toast.style.webkitBackdropFilter = 'blur(12px)';
|
|
159
|
+
toast.style.border = '1px solid rgba(255, 255, 255, 0.12)';
|
|
160
|
+
toast.style.borderRadius = '9999px';
|
|
161
|
+
toast.style.boxShadow = '0 12px 32px rgba(0, 0, 0, 0.45)';
|
|
162
|
+
toast.style.color = '#ffffff';
|
|
163
|
+
toast.style.fontFamily = 'system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif';
|
|
164
|
+
toast.style.fontSize = '14px';
|
|
165
|
+
toast.style.lineHeight = '1.4';
|
|
166
|
+
toast.style.maxWidth = 'calc(100vw - 32px)';
|
|
167
|
+
toast.style.boxSizing = 'border-box';
|
|
168
|
+
toast.style.transition = 'all 0.25s cubic-bezier(0.16, 1, 0.3, 1)';
|
|
169
|
+
|
|
170
|
+
const appTitle = this.options.appName || 'App';
|
|
171
|
+
|
|
172
|
+
toast.innerHTML = `
|
|
173
|
+
<div style="display: flex; align-items: center; gap: 8px; white-space: nowrap;">
|
|
174
|
+
<span style="display: inline-block; width: 8px; height: 8px; border-radius: 50%; background-color: #10b981; box-shadow: 0 0 8px #10b981;"></span>
|
|
175
|
+
<span style="font-weight: 500;">${appTitle} updated</span>
|
|
176
|
+
</div>
|
|
177
|
+
<button id="pwaUpdateActionBtn" type="button" style="
|
|
178
|
+
background: #2563eb;
|
|
179
|
+
color: #ffffff;
|
|
180
|
+
border: none;
|
|
181
|
+
padding: 6px 14px;
|
|
182
|
+
border-radius: 9999px;
|
|
183
|
+
font-size: 13px;
|
|
184
|
+
font-weight: 600;
|
|
185
|
+
cursor: pointer;
|
|
186
|
+
display: inline-flex;
|
|
187
|
+
align-items: center;
|
|
188
|
+
justify-content: center;
|
|
189
|
+
min-height: 36px;
|
|
190
|
+
transition: background 0.2s;
|
|
191
|
+
white-space: nowrap;
|
|
192
|
+
">Refresh</button>
|
|
193
|
+
<button id="pwaUpdateDismissBtn" type="button" style="
|
|
194
|
+
background: transparent;
|
|
195
|
+
color: #94a3b8;
|
|
196
|
+
border: none;
|
|
197
|
+
font-size: 16px;
|
|
198
|
+
cursor: pointer;
|
|
199
|
+
padding: 6px;
|
|
200
|
+
min-width: 36px;
|
|
201
|
+
min-height: 36px;
|
|
202
|
+
display: inline-flex;
|
|
203
|
+
align-items: center;
|
|
204
|
+
justify-content: center;
|
|
205
|
+
line-height: 1;
|
|
206
|
+
" aria-label="Dismiss">โ</button>
|
|
207
|
+
`;
|
|
208
|
+
|
|
209
|
+
document.body.appendChild(toast);
|
|
210
|
+
|
|
211
|
+
const updateBtn = document.getElementById('pwaUpdateActionBtn');
|
|
212
|
+
if (updateBtn) {
|
|
213
|
+
updateBtn.addEventListener('click', () => {
|
|
214
|
+
toast.remove();
|
|
215
|
+
this.applyUpdate(worker);
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
const dismissBtn = document.getElementById('pwaUpdateDismissBtn');
|
|
220
|
+
if (dismissBtn) {
|
|
221
|
+
dismissBtn.addEventListener('click', () => {
|
|
222
|
+
toast.remove();
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
126
227
|
/**
|
|
127
228
|
* Sends skipWaiting postMessage to the waiting worker and triggers clean reload
|
|
128
229
|
*/
|
|
@@ -167,8 +268,11 @@ export class PwaManager {
|
|
|
167
268
|
* @returns {Promise<void>}
|
|
168
269
|
*/
|
|
169
270
|
static setAppBadge(count) {
|
|
271
|
+
const num = Math.max(0, parseInt(count, 10) || 0);
|
|
272
|
+
if (typeof window !== 'undefined' && window.dispatchEvent) {
|
|
273
|
+
window.dispatchEvent(new CustomEvent('pwa:badge-updated', { detail: { count: num } }));
|
|
274
|
+
}
|
|
170
275
|
if (typeof navigator !== 'undefined' && 'setAppBadge' in navigator) {
|
|
171
|
-
const num = Math.max(0, parseInt(count, 10) || 0);
|
|
172
276
|
return (num > 0 ? navigator.setAppBadge(num) : navigator.clearAppBadge()).catch(() => {});
|
|
173
277
|
}
|
|
174
278
|
return Promise.resolve();
|
|
@@ -180,6 +284,9 @@ export class PwaManager {
|
|
|
180
284
|
* @returns {Promise<void>}
|
|
181
285
|
*/
|
|
182
286
|
static clearAppBadge() {
|
|
287
|
+
if (typeof window !== 'undefined' && window.dispatchEvent) {
|
|
288
|
+
window.dispatchEvent(new CustomEvent('pwa:badge-updated', { detail: { count: 0 } }));
|
|
289
|
+
}
|
|
183
290
|
if (typeof navigator !== 'undefined' && 'clearAppBadge' in navigator) {
|
|
184
291
|
return navigator.clearAppBadge().catch(() => {});
|
|
185
292
|
}
|
|
@@ -194,9 +301,6 @@ export class PwaManager {
|
|
|
194
301
|
*/
|
|
195
302
|
static async syncAppBadge(endpoint = null) {
|
|
196
303
|
const url = endpoint || this.options.badgeEndpoint || '/api/notifications/unread';
|
|
197
|
-
if (typeof navigator === 'undefined' || !('setAppBadge' in navigator)) {
|
|
198
|
-
return;
|
|
199
|
-
}
|
|
200
304
|
|
|
201
305
|
try {
|
|
202
306
|
const res = await fetch(url, { headers: { Accept: 'application/json' } });
|
|
@@ -209,14 +313,28 @@ export class PwaManager {
|
|
|
209
313
|
return;
|
|
210
314
|
}
|
|
211
315
|
const data = await res.json();
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
if (
|
|
215
|
-
|
|
216
|
-
} else {
|
|
217
|
-
|
|
316
|
+
let count = 0;
|
|
317
|
+
if (data) {
|
|
318
|
+
if (typeof data.unread_count === 'number') {
|
|
319
|
+
count = data.unread_count;
|
|
320
|
+
} else if (typeof data.count === 'number') {
|
|
321
|
+
count = data.count;
|
|
322
|
+
} else if (typeof data.unreadCount === 'number') {
|
|
323
|
+
count = data.unreadCount;
|
|
324
|
+
} else if (data.status === 'success' && Array.isArray(data.message)) {
|
|
325
|
+
count = data.message.length;
|
|
326
|
+
} else if (Array.isArray(data.data)) {
|
|
327
|
+
count = data.data.length;
|
|
328
|
+
} else if (Array.isArray(data.notifications)) {
|
|
329
|
+
count = data.notifications.length;
|
|
218
330
|
}
|
|
219
331
|
}
|
|
332
|
+
const num = Math.max(0, parseInt(count, 10) || 0);
|
|
333
|
+
if (num > 0) {
|
|
334
|
+
await this.setAppBadge(num);
|
|
335
|
+
} else {
|
|
336
|
+
await this.clearAppBadge();
|
|
337
|
+
}
|
|
220
338
|
} catch (e) {
|
|
221
339
|
// Harmless no-op on network failure or offline mode
|
|
222
340
|
}
|
package/config/version.php
CHANGED
package/index.js
CHANGED
|
@@ -28,4 +28,8 @@ export * from './AcctMgt/recaptchaToken.js';
|
|
|
28
28
|
export * from './AcctMgt/AdminAuthForm.js';
|
|
29
29
|
export * from './general.js';
|
|
30
30
|
export * from './CardCanvasGenerator.js';
|
|
31
|
-
export * from './ShareManager.js';
|
|
31
|
+
export * from './ShareManager.js';
|
|
32
|
+
export * from './WebAuthnClient.js';
|
|
33
|
+
export * from './OtpInputManager.js';
|
|
34
|
+
export * from './AddressAutocomplete.js';
|
|
35
|
+
export * from './DigitalSignaturePad.js';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@modernman00/shared-js-lib",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.88",
|
|
4
4
|
"description": "Reusable JS utilities for numerous js problems",
|
|
5
5
|
"homepage": "https://github.com/modernman00/shared-js-lib#readme",
|
|
6
6
|
"keywords": [
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"url": "git+https://github.com/modernman00/shared-js-lib.git"
|
|
18
18
|
},
|
|
19
19
|
"license": "MIT",
|
|
20
|
-
"author": "
|
|
20
|
+
"author": "Core Platform Engineering Team",
|
|
21
21
|
"main": "index.js",
|
|
22
22
|
"scripts": {
|
|
23
23
|
"test": "jest"
|
|
@@ -62,6 +62,9 @@
|
|
|
62
62
|
"PwaInstall.js",
|
|
63
63
|
"CardCanvasGenerator.js",
|
|
64
64
|
"ShareManager.js",
|
|
65
|
+
"OtpInputManager.js",
|
|
66
|
+
"AddressAutocomplete.js",
|
|
67
|
+
"DigitalSignaturePad.js",
|
|
65
68
|
"templates/"
|
|
66
69
|
],
|
|
67
70
|
"devDependencies": {
|
package/release-history.md
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
|
|
2
|
+
## v1.2.86 โ 2026-10-12 22:38
|
|
3
|
+
### Changelog for v1.2.86
|
|
4
|
+
- ๐ feat: Add AddressAutocomplete class and setupAddressAutocomplete helper for zero-cost UK geocoding (Photon OSM + Postcodes.io)
|
|
5
|
+
- ๐ก๏ธ Add keyboard navigation, defensive textContent escaping (XSS immune), AbortController race protection, and full Jest test suite
|
|
6
|
+
|
|
2
7
|
## v1.2.15 โ 2025-08-18 21:56
|
|
3
8
|
### Changelog for v1.2.15
|
|
4
9
|
|
|
@@ -136,3 +141,25 @@
|
|
|
136
141
|
- ๐ Bump version to 1.2.83 _(by modernman00)_
|
|
137
142
|
- feat(pwa): centralize resilient OfflineQueue, PwaManager and standardized Service Worker template _(by modernman00)_
|
|
138
143
|
- feat: enhance release script with interactive npm login and improved workflow _(by modernman00)_
|
|
144
|
+
|
|
145
|
+
## v1.2.84 โ 2026-10-09 16:10
|
|
146
|
+
### Changelog for v1.2.84
|
|
147
|
+
|
|
148
|
+
- ๐งน Auto-commit before release _(by modernman00)_
|
|
149
|
+
- feat(otp): add OtpInputManager supporting iOS QuickType autofill and magic link auto-submit _(by modernman00)_
|
|
150
|
+
|
|
151
|
+
## v1.2.87 โ 2026-10-12 23:28
|
|
152
|
+
### Changelog for v1.2.87
|
|
153
|
+
|
|
154
|
+
- chore(release): 1.2.87 _(by github-actions)_
|
|
155
|
+
- fix(ci): add permissions contents write and continue-on-error for artifact upload _(by modernman00)_
|
|
156
|
+
- fix(ci): convert babel config to cjs and upgrade github actions to v4 with node 20 _(by modernman00)_
|
|
157
|
+
- feat: add AddressAutocomplete component (Photon + Postcodes.io) v1.2.86 _(by modernman00)_
|
|
158
|
+
- feat(pwa): harden PwaManager badging sync, support diverse envelopes, and dispatch pwa:badge-updated event _(by modernman00)_
|
|
159
|
+
- ๐ Release v1.2.84 _(by modernman00)_
|
|
160
|
+
- ๐งน Auto-commit before release _(by modernman00)_
|
|
161
|
+
|
|
162
|
+
## v1.2.88 โ 2026-10-14 18:00
|
|
163
|
+
### Changelog for v1.2.88
|
|
164
|
+
|
|
165
|
+
- ๐งน Auto-commit before release _(by modernman00)_
|