@optionfactory/fml 8.0.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/LICENSE.md +21 -0
- package/README.md +22 -0
- package/dist/client-errors.iife.js +109 -0
- package/dist/client-errors.iife.js.map +1 -0
- package/dist/client-errors.iife.min.js +2 -0
- package/dist/client-errors.iife.min.js.map +1 -0
- package/dist/fml.css +2 -0
- package/dist/fml.css.map +1 -0
- package/dist/fml.d.mts +1241 -0
- package/dist/fml.iife.js +8340 -0
- package/dist/fml.iife.js.map +1 -0
- package/dist/fml.iife.min.js +2 -0
- package/dist/fml.iife.min.js.map +1 -0
- package/dist/fml.min.mjs +2 -0
- package/dist/fml.min.mjs.map +1 -0
- package/dist/fml.mjs +8284 -0
- package/dist/fml.mjs.map +1 -0
- package/dist/ftl.d.mts +361 -0
- package/dist/ftl.iife.js +4719 -0
- package/dist/ftl.iife.js.map +1 -0
- package/dist/ftl.iife.min.js +2 -0
- package/dist/ftl.iife.min.js.map +1 -0
- package/dist/ftl.min.mjs +2 -0
- package/dist/ftl.min.mjs.map +1 -0
- package/dist/ftl.mjs +4700 -0
- package/dist/ftl.mjs.map +1 -0
- package/dist/ful.css +2 -0
- package/dist/ful.css.map +1 -0
- package/dist/ful.d.mts +581 -0
- package/dist/ful.iife.js +2814 -0
- package/dist/ful.iife.js.map +1 -0
- package/dist/ful.iife.min.js +2 -0
- package/dist/ful.iife.min.js.map +1 -0
- package/dist/ful.min.mjs +2 -0
- package/dist/ful.min.mjs.map +1 -0
- package/dist/ful.mjs +2780 -0
- package/dist/ful.mjs.map +1 -0
- package/dist/httpc.d.mts +306 -0
- package/dist/httpc.iife.js +755 -0
- package/dist/httpc.iife.js.map +1 -0
- package/dist/httpc.iife.min.js +2 -0
- package/dist/httpc.iife.min.js.map +1 -0
- package/dist/httpc.min.mjs +2 -0
- package/dist/httpc.min.mjs.map +1 -0
- package/dist/httpc.mjs +743 -0
- package/dist/httpc.mjs.map +1 -0
- package/package.json +72 -0
package/dist/ful.mjs
ADDED
|
@@ -0,0 +1,2780 @@
|
|
|
1
|
+
import { ParsedElement, Attributes, registry, Fragments, Templates, Nodes, Rendering } from './ftl.mjs';
|
|
2
|
+
import { Failure, HttpClient } from './httpc.mjs';
|
|
3
|
+
|
|
4
|
+
class LocalStorage extends Storage {
|
|
5
|
+
static save(k, v) {
|
|
6
|
+
localStorage.setItem(k, JSON.stringify(v));
|
|
7
|
+
}
|
|
8
|
+
static load(k) {
|
|
9
|
+
const got = localStorage.getItem(k);
|
|
10
|
+
return got === null ? undefined : JSON.parse(got);
|
|
11
|
+
}
|
|
12
|
+
static remove(k) {
|
|
13
|
+
localStorage.removeItem(k);
|
|
14
|
+
}
|
|
15
|
+
static pop(k) {
|
|
16
|
+
const decoded = LocalStorage.load(k);
|
|
17
|
+
LocalStorage.remove(k);
|
|
18
|
+
return decoded;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
class SessionStorage extends Storage {
|
|
23
|
+
static save(k, v) {
|
|
24
|
+
sessionStorage.setItem(k, JSON.stringify(v));
|
|
25
|
+
}
|
|
26
|
+
static load(k) {
|
|
27
|
+
const got = sessionStorage.getItem(k);
|
|
28
|
+
return got === null ? undefined : JSON.parse(got);
|
|
29
|
+
}
|
|
30
|
+
static remove(k) {
|
|
31
|
+
sessionStorage.removeItem(k);
|
|
32
|
+
}
|
|
33
|
+
static pop(k) {
|
|
34
|
+
const decoded = SessionStorage.load(k);
|
|
35
|
+
SessionStorage.remove(k);
|
|
36
|
+
return decoded;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
class VersionedLocalStorage {
|
|
41
|
+
static save(key, revision, data) {
|
|
42
|
+
LocalStorage.save(key, { revision, data });
|
|
43
|
+
}
|
|
44
|
+
static load(key, revision) {
|
|
45
|
+
const stored = LocalStorage.load(key);
|
|
46
|
+
if (stored === undefined) {
|
|
47
|
+
return undefined;
|
|
48
|
+
}
|
|
49
|
+
if (stored.revision !== revision) {
|
|
50
|
+
localStorage.removeItem(key);
|
|
51
|
+
return undefined;
|
|
52
|
+
}
|
|
53
|
+
return stored.data;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
class VersionedSessionStorage {
|
|
58
|
+
static save(key, revision, data) {
|
|
59
|
+
SessionStorage.save(key, { revision, data });
|
|
60
|
+
}
|
|
61
|
+
static load(key, revision) {
|
|
62
|
+
const stored = SessionStorage.load(key);
|
|
63
|
+
if (stored === undefined) {
|
|
64
|
+
return undefined;
|
|
65
|
+
}
|
|
66
|
+
if (stored.revision !== revision) {
|
|
67
|
+
localStorage.removeItem(key);
|
|
68
|
+
return undefined;
|
|
69
|
+
}
|
|
70
|
+
return stored.data;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* @typedef {Object} AsyncExtension
|
|
76
|
+
* @property {Promise<any>[]} promises
|
|
77
|
+
* @typedef {Event & { async?: AsyncExtension }} AsyncEvent
|
|
78
|
+
*/
|
|
79
|
+
class AsyncEvents {
|
|
80
|
+
/**
|
|
81
|
+
* Dispatches an event and handles asynchronous resolution based on the execution mode.
|
|
82
|
+
* @param {HTMLElement} el - The target element dispatching the event.
|
|
83
|
+
* @param {AsyncEvent} evt - The event instance.
|
|
84
|
+
* @param {{mode?: 'broadcast' | 'pipeline' | 'delegate'}} [options] - Configuration options (defaults to 'broadcast').
|
|
85
|
+
* @returns {Promise<any>} Resolves with an array of values for broadcasts, a single value for pipelines/delegates, or undefined.
|
|
86
|
+
*/
|
|
87
|
+
static async fireAsync(el, evt, options) {
|
|
88
|
+
el.dispatchEvent(evt);
|
|
89
|
+
const promises = evt.async?.promises ?? [];
|
|
90
|
+
const mode = options?.mode ?? 'broadcast';
|
|
91
|
+
if (mode === 'pipeline' && promises.length > 1) {
|
|
92
|
+
throw new Error(
|
|
93
|
+
`[AsyncEvents] Event "${evt.type}" is configured in 'pipeline' mode and expects at most one async listener, but ${promises.length} listeners were triggered on this element.`,
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
if (mode === 'delegate' && promises.length !== 1) {
|
|
97
|
+
throw new Error(
|
|
98
|
+
`[AsyncEvents] Event "${evt.type}" is configured in 'delegate' mode and requires exactly one async listener, but ${promises.length} were registered.`,
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
return mode === 'broadcast' ? Promise.all(promises) : Promise.resolve(promises[0]);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Registers an asynchronous event listener wrapper.
|
|
106
|
+
* @param {HTMLElement} el - The target element.
|
|
107
|
+
* @param {string} type - The event name/type.
|
|
108
|
+
* @param {Function} fn - The async listener middleware function returning the execution result.
|
|
109
|
+
* @param {AddEventListenerOptions} [options] - Native addEventListener options.
|
|
110
|
+
* @returns {EventListener} The underlying proxy listener function needed for cleanup via asyncOff.
|
|
111
|
+
*/
|
|
112
|
+
static asyncOn(el, type, fn, options) {
|
|
113
|
+
/** @type {(evt: Event) => Promise<void>} */
|
|
114
|
+
const listener = async (event) => {
|
|
115
|
+
const ae = /** @type {AsyncEvent} */ (event);
|
|
116
|
+
if (!ae.async) {
|
|
117
|
+
ae.async = { promises: [] };
|
|
118
|
+
}
|
|
119
|
+
const { promise, resolve, reject } = Promise.withResolvers();
|
|
120
|
+
ae.async.promises.push(promise);
|
|
121
|
+
try {
|
|
122
|
+
resolve(await fn(ae));
|
|
123
|
+
} catch (e) {
|
|
124
|
+
reject(e);
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
el.addEventListener(type, listener, options);
|
|
129
|
+
return listener;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Unregisters an asynchronous event listener proxy.
|
|
134
|
+
* @param {HTMLElement} el - The target element.
|
|
135
|
+
* @param {string} type - The event name/type.
|
|
136
|
+
* @param {EventListener} listener - The proxy listener instance previously returned by asyncOn.
|
|
137
|
+
* @param {EventListenerOptions} [options] - Native removeEventListener options.
|
|
138
|
+
*/
|
|
139
|
+
static asyncOff(el, type, listener, options) {
|
|
140
|
+
el.removeEventListener(type, listener, options);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Mixes the asynchronous execution engine extensions into target class prototypes.
|
|
145
|
+
* @param {...Function} classes - The target class constructors to decorate.
|
|
146
|
+
*/
|
|
147
|
+
static mixInto(...classes) {
|
|
148
|
+
for (const k of classes) {
|
|
149
|
+
Object.assign(k.prototype, {
|
|
150
|
+
/**
|
|
151
|
+
* @this {HTMLElement}
|
|
152
|
+
* @param {AsyncEvent} evt
|
|
153
|
+
* @param {{mode?: 'broadcast' | 'pipeline' | 'delegate'}} [options]
|
|
154
|
+
* @returns {Promise<any>}
|
|
155
|
+
*/
|
|
156
|
+
async fireAsync(evt, options) {
|
|
157
|
+
return await AsyncEvents.fireAsync(this, evt, options);
|
|
158
|
+
},
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* @this {HTMLElement}
|
|
162
|
+
* @param {string} type
|
|
163
|
+
* @param {Function} fn
|
|
164
|
+
* @param {AddEventListenerOptions} [options]
|
|
165
|
+
* @returns {EventListener}
|
|
166
|
+
*/
|
|
167
|
+
asyncOn(type, fn, options) {
|
|
168
|
+
return AsyncEvents.asyncOn(this, type, fn, options);
|
|
169
|
+
},
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* @this {HTMLElement}
|
|
173
|
+
* @param {string} type
|
|
174
|
+
* @param {EventListener} listener
|
|
175
|
+
* @param {EventListenerOptions} [options]
|
|
176
|
+
* @returns {void}
|
|
177
|
+
*/
|
|
178
|
+
asyncOff(type, listener, options) {
|
|
179
|
+
AsyncEvents.asyncOff(this, type, listener, options);
|
|
180
|
+
},
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
class Timing {
|
|
187
|
+
static sleep(ms) {
|
|
188
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
189
|
+
}
|
|
190
|
+
static DEBOUNCE_DEFAULT = 0;
|
|
191
|
+
static DEBOUNCE_IMMEDIATE = 1;
|
|
192
|
+
/**
|
|
193
|
+
* Executes only after a period of inactivity (pause in events).
|
|
194
|
+
* Respond to the "end" of a series of events.
|
|
195
|
+
* @param {*} timeoutMs
|
|
196
|
+
* @param {*} func
|
|
197
|
+
* @param {*} [options]
|
|
198
|
+
* @returns {[function, function]}
|
|
199
|
+
*/
|
|
200
|
+
static debounce(timeoutMs, func, options) {
|
|
201
|
+
const opts = options ?? Timing.DEBOUNCE_DEFAULT;
|
|
202
|
+
let tid = null;
|
|
203
|
+
let args = [];
|
|
204
|
+
let previousTimestamp = 0;
|
|
205
|
+
|
|
206
|
+
const later = () => {
|
|
207
|
+
const elapsed = performance.now() - previousTimestamp;
|
|
208
|
+
if (timeoutMs > elapsed) {
|
|
209
|
+
tid = setTimeout(later, timeoutMs - elapsed);
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
tid = null;
|
|
213
|
+
if (opts !== Timing.DEBOUNCE_IMMEDIATE) {
|
|
214
|
+
func(...args);
|
|
215
|
+
}
|
|
216
|
+
// This check is needed because `func` can recursively invoke `debounced`.
|
|
217
|
+
if (tid === null) {
|
|
218
|
+
args = [];
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
const debounced = function () {
|
|
223
|
+
args = [...arguments];
|
|
224
|
+
previousTimestamp = performance.now();
|
|
225
|
+
if (tid === null) {
|
|
226
|
+
tid = setTimeout(later, timeoutMs);
|
|
227
|
+
if (opts === Timing.DEBOUNCE_IMMEDIATE) {
|
|
228
|
+
func(...args);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
};
|
|
232
|
+
const abort = () => clearTimeout(tid);
|
|
233
|
+
return [debounced, abort];
|
|
234
|
+
}
|
|
235
|
+
static THROTTLE_DEFAULT = 0;
|
|
236
|
+
static THROTTLE_NO_LEADING = 1;
|
|
237
|
+
static THROTTLE_NO_TRAILING = 2;
|
|
238
|
+
/**
|
|
239
|
+
* Executes at most once per specified time interval, regardless of ongoing events.
|
|
240
|
+
* @param {*} timeoutMs
|
|
241
|
+
* @param {*} func
|
|
242
|
+
* @param {*} [options]
|
|
243
|
+
* @returns {[function, function]}
|
|
244
|
+
*/
|
|
245
|
+
static throttle(timeoutMs, func, options) {
|
|
246
|
+
const opts = options ?? Timing.THROTTLE_DEFAULT;
|
|
247
|
+
let tid = null;
|
|
248
|
+
let args = [];
|
|
249
|
+
let previousTimestamp = 0;
|
|
250
|
+
|
|
251
|
+
const later = () => {
|
|
252
|
+
previousTimestamp = opts & Timing.THROTTLE_NO_LEADING ? 0 : performance.now();
|
|
253
|
+
tid = null;
|
|
254
|
+
func(...args);
|
|
255
|
+
if (tid === null) {
|
|
256
|
+
args = [];
|
|
257
|
+
}
|
|
258
|
+
};
|
|
259
|
+
const throttled = function () {
|
|
260
|
+
const now = performance.now();
|
|
261
|
+
if (!previousTimestamp && opts & Timing.THROTTLE_NO_LEADING) {
|
|
262
|
+
previousTimestamp = now;
|
|
263
|
+
}
|
|
264
|
+
const remaining = previousTimestamp === 0 ? 0 : timeoutMs - (now - previousTimestamp);
|
|
265
|
+
args = [...arguments];
|
|
266
|
+
if (remaining <= 0 || remaining > timeoutMs) {
|
|
267
|
+
if (tid !== null) {
|
|
268
|
+
clearTimeout(tid);
|
|
269
|
+
tid = null;
|
|
270
|
+
}
|
|
271
|
+
previousTimestamp = now;
|
|
272
|
+
func(...args);
|
|
273
|
+
if (tid === null) {
|
|
274
|
+
args = [];
|
|
275
|
+
}
|
|
276
|
+
} else if (tid === null && !(opts & Timing.THROTTLE_NO_TRAILING)) {
|
|
277
|
+
tid = setTimeout(later, remaining);
|
|
278
|
+
}
|
|
279
|
+
};
|
|
280
|
+
const abort = () => clearTimeout(tid);
|
|
281
|
+
return [throttled, abort];
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
class Bindings {
|
|
286
|
+
/**
|
|
287
|
+
* @param {{ [x: string]: any; }} obj
|
|
288
|
+
* @param {string} prefix
|
|
289
|
+
* @param {Set<String>} stops
|
|
290
|
+
* @return {{ [x: string]: any; }}
|
|
291
|
+
*/
|
|
292
|
+
static flatten(obj, prefix, stops) {
|
|
293
|
+
return Object.keys(obj).reduce((acc, k) => {
|
|
294
|
+
const pre = prefix.length ? prefix + '.' + k : k;
|
|
295
|
+
if (!stops.has(pre) && typeof obj[k] === 'object' && obj[k] !== null) {
|
|
296
|
+
Object.assign(acc, Bindings.flatten(obj[k], pre, stops));
|
|
297
|
+
} else {
|
|
298
|
+
acc[pre] = obj[k];
|
|
299
|
+
}
|
|
300
|
+
return acc;
|
|
301
|
+
}, {});
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* @param {any} result
|
|
306
|
+
* @param {string} path
|
|
307
|
+
* @param {any} value
|
|
308
|
+
*/
|
|
309
|
+
static providePath(result, path, value) {
|
|
310
|
+
const keys = path.split('.').map((k) => (/^[0-9]+$/.test(k) ? +k : k));
|
|
311
|
+
let current = result ?? {};
|
|
312
|
+
let previous = null;
|
|
313
|
+
for (let i = 0; ; ++i) {
|
|
314
|
+
const ckey = keys[i];
|
|
315
|
+
const pkey = keys[i - 1];
|
|
316
|
+
if (Number.isInteger(ckey) && !Array.isArray(current)) {
|
|
317
|
+
if (previous !== null) {
|
|
318
|
+
previous[pkey] = current = [];
|
|
319
|
+
} else {
|
|
320
|
+
result = current = [];
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
if (i === keys.length - 1) {
|
|
324
|
+
//when value is undefined we only want to define the property if it's not defined
|
|
325
|
+
current[ckey] = value !== undefined ? value : ckey in current ? current[ckey] : null;
|
|
326
|
+
return result;
|
|
327
|
+
}
|
|
328
|
+
if (current[ckey] === undefined) {
|
|
329
|
+
current[ckey] = {};
|
|
330
|
+
}
|
|
331
|
+
previous = current;
|
|
332
|
+
current = current[ckey];
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
*
|
|
337
|
+
* @param {Element & {dataset?: any} & {checked?: boolean} & {value?: any}} el
|
|
338
|
+
* @returns
|
|
339
|
+
*/
|
|
340
|
+
static extract(el) {
|
|
341
|
+
if (el.getAttribute('type') === 'radio') {
|
|
342
|
+
if (!el.checked) {
|
|
343
|
+
return undefined;
|
|
344
|
+
}
|
|
345
|
+
return el.dataset['fulBindType'] === 'boolean' ? el.value === 'true' : el.value;
|
|
346
|
+
}
|
|
347
|
+
if (el.getAttribute('type') === 'checkbox') {
|
|
348
|
+
return el.checked;
|
|
349
|
+
}
|
|
350
|
+
if (el.dataset['fulBindType'] === 'boolean') {
|
|
351
|
+
return !el.value ? null : el.value === 'true';
|
|
352
|
+
}
|
|
353
|
+
if (el.tagName === 'INPUT' || el.tagName === 'SELECT' || el.tagName === 'TEXTAREA') {
|
|
354
|
+
return el.value === '' || el.value === undefined ? null : el.value;
|
|
355
|
+
}
|
|
356
|
+
return el.value;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/**
|
|
360
|
+
*
|
|
361
|
+
* @param {HTMLFormElement} form
|
|
362
|
+
* @param {HTMLElement} [submitter]
|
|
363
|
+
* @returns
|
|
364
|
+
*/
|
|
365
|
+
static extractFrom(form, submitter) {
|
|
366
|
+
let result = {};
|
|
367
|
+
for (const el of form.elements) {
|
|
368
|
+
// we are assuming submitters are disabled during submit.
|
|
369
|
+
if (!el.hasAttribute('name') || (el.matches(':disabled') && el !== submitter)) {
|
|
370
|
+
continue;
|
|
371
|
+
}
|
|
372
|
+
result = Bindings.providePath(
|
|
373
|
+
result,
|
|
374
|
+
/** @type {string} */ (el.getAttribute('name')),
|
|
375
|
+
Bindings.extract(el),
|
|
376
|
+
);
|
|
377
|
+
}
|
|
378
|
+
return result;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/**
|
|
382
|
+
*
|
|
383
|
+
* @param {Element & {checked?: boolean} & {value?: any}} el
|
|
384
|
+
* @returns
|
|
385
|
+
*/
|
|
386
|
+
static mutate(el, raw) {
|
|
387
|
+
if (el.getAttribute('type') === 'radio') {
|
|
388
|
+
el.checked = el.getAttribute('value') === raw;
|
|
389
|
+
return;
|
|
390
|
+
}
|
|
391
|
+
if (el.getAttribute('type') === 'checkbox') {
|
|
392
|
+
el.checked = raw;
|
|
393
|
+
return;
|
|
394
|
+
}
|
|
395
|
+
el.value = raw;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
static mutateIn(form, values) {
|
|
399
|
+
const names = Array.from(form.elements)
|
|
400
|
+
.map((el) => el.getAttribute('name'))
|
|
401
|
+
.filter((n) => n);
|
|
402
|
+
for (const [flattenedKey, value] of Object.entries(Bindings.flatten(values, '', new Set(names)))) {
|
|
403
|
+
for (const el of form.querySelectorAll(`[name='${CSS.escape(flattenedKey)}']`)) {
|
|
404
|
+
Bindings.mutate(el, value);
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
static errors(form, es, scrollOnError) {
|
|
410
|
+
const fieldErrors = es.filter((e) => e.type === 'FIELD_ERROR' || e.type === 'INVALID_FORMAT');
|
|
411
|
+
const globalErrors = es.filter((e) => e.type !== 'FIELD_ERROR' && e.type !== 'INVALID_FORMAT');
|
|
412
|
+
form.querySelectorAll(`[name]`).forEach((el) => el.setCustomValidity?.(''));
|
|
413
|
+
form.querySelectorAll('ful-errors').forEach((el) => {
|
|
414
|
+
el.replaceChildren();
|
|
415
|
+
el.setAttribute('hidden', '');
|
|
416
|
+
});
|
|
417
|
+
fieldErrors.forEach((e) => {
|
|
418
|
+
const name = e.context.replace(/\[/g, '.').replace(/\]\./g, '.').replace(/\]/g, '');
|
|
419
|
+
const parts = name.split('.');
|
|
420
|
+
for (let i = parts.length; i != 0; --i) {
|
|
421
|
+
const prefix = parts.slice(0, i).join('.');
|
|
422
|
+
const suffix = parts.slice(i, parts.length).join('.');
|
|
423
|
+
form.querySelectorAll(`[name='${CSS.escape(prefix)}']`).forEach((input) =>
|
|
424
|
+
input.setCustomValidity?.(e.reason, suffix),
|
|
425
|
+
);
|
|
426
|
+
}
|
|
427
|
+
});
|
|
428
|
+
form.querySelectorAll('ful-errors').forEach((el) => {
|
|
429
|
+
const hel = /** @type HTMLElement} */ (el);
|
|
430
|
+
hel.innerText = globalErrors.map((e) => e.reason).join('\n');
|
|
431
|
+
if (globalErrors.length !== 0) {
|
|
432
|
+
el.removeAttribute('hidden');
|
|
433
|
+
}
|
|
434
|
+
});
|
|
435
|
+
if (es.length == 0 || !scrollOnError) {
|
|
436
|
+
return;
|
|
437
|
+
}
|
|
438
|
+
Array.from(form.querySelectorAll(`:invalid`))
|
|
439
|
+
.sort((a, b) => a.getBoundingClientRect().y - b.getBoundingClientRect().y)[0]
|
|
440
|
+
?.focus();
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
class RemoteJsonFormLoader {
|
|
445
|
+
#http;
|
|
446
|
+
#url;
|
|
447
|
+
#method;
|
|
448
|
+
#requestMapper;
|
|
449
|
+
#responseMapper;
|
|
450
|
+
constructor(http, url, method, requestMapper, responseMapper) {
|
|
451
|
+
this.#http = http;
|
|
452
|
+
this.#url = url;
|
|
453
|
+
this.#method = method;
|
|
454
|
+
this.#requestMapper = requestMapper;
|
|
455
|
+
this.#responseMapper = responseMapper;
|
|
456
|
+
}
|
|
457
|
+
prepare(values, form) {
|
|
458
|
+
return this.#requestMapper(values, form);
|
|
459
|
+
}
|
|
460
|
+
async submit(values, form) {
|
|
461
|
+
return await this.#http.request(this.#method, this.#url).json(values).fetch();
|
|
462
|
+
}
|
|
463
|
+
transform(response, form) {
|
|
464
|
+
return this.#responseMapper(response, form);
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
class LocalFormLoader {
|
|
469
|
+
#requestMapper;
|
|
470
|
+
#responseMapper;
|
|
471
|
+
constructor(requestMapper, responseMapper) {
|
|
472
|
+
this.#requestMapper = requestMapper;
|
|
473
|
+
this.#responseMapper = responseMapper;
|
|
474
|
+
}
|
|
475
|
+
async prepare(values, form) {
|
|
476
|
+
return await this.#requestMapper(values, form);
|
|
477
|
+
}
|
|
478
|
+
async submit(values, form, response) {
|
|
479
|
+
return response;
|
|
480
|
+
}
|
|
481
|
+
async transform(response, form) {
|
|
482
|
+
return await this.#responseMapper(response, form);
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
class FormLoader {
|
|
487
|
+
static create(el, conf) {
|
|
488
|
+
const http = registry.component('http-client');
|
|
489
|
+
const requestMapper = el.hasAttribute('request-mapper')
|
|
490
|
+
? registry.component(el.getAttribute('request-mapper'))
|
|
491
|
+
: (v) => v;
|
|
492
|
+
const responseMapper = el.hasAttribute('response-mapper')
|
|
493
|
+
? registry.component(el.getAttribute('response-mapper'))
|
|
494
|
+
: (v) => v;
|
|
495
|
+
const url = el.getAttribute('action');
|
|
496
|
+
if (!url) {
|
|
497
|
+
return new LocalFormLoader(requestMapper, responseMapper);
|
|
498
|
+
}
|
|
499
|
+
const method = el.getAttribute('method') ?? 'POST';
|
|
500
|
+
return new RemoteJsonFormLoader(http, url, method, requestMapper, responseMapper);
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
class Form extends ParsedElement {
|
|
505
|
+
form;
|
|
506
|
+
render() {
|
|
507
|
+
const form = (this.form = document.createElement('form'));
|
|
508
|
+
form.setAttribute('novalidate', '');
|
|
509
|
+
Attributes.forward('form-', this, form);
|
|
510
|
+
form.replaceChildren(...this.childNodes);
|
|
511
|
+
form.addEventListener('submit', async (e) => {
|
|
512
|
+
e.preventDefault();
|
|
513
|
+
e.stopPropagation();
|
|
514
|
+
await this.submit(e.submitter ?? undefined);
|
|
515
|
+
});
|
|
516
|
+
if (this.hasAttribute('clear-invalid-on-change')) {
|
|
517
|
+
this.addEventListener('change', (/** @type any */ evt) => {
|
|
518
|
+
evt.target.setCustomValidity?.('');
|
|
519
|
+
});
|
|
520
|
+
}
|
|
521
|
+
this.replaceChildren(form);
|
|
522
|
+
}
|
|
523
|
+
/**
|
|
524
|
+
*
|
|
525
|
+
* @param {HTMLElement} [submitter]
|
|
526
|
+
* @returns
|
|
527
|
+
*/
|
|
528
|
+
async submit(submitter) {
|
|
529
|
+
this.spinner(true);
|
|
530
|
+
try {
|
|
531
|
+
const loader = registry.component(this.getAttribute('loader') ?? 'loaders:form').create(this);
|
|
532
|
+
const values = Bindings.extractFrom(this.form, submitter);
|
|
533
|
+
let request = await loader.prepare(values, this);
|
|
534
|
+
try {
|
|
535
|
+
const se = new CustomEvent('submit', {
|
|
536
|
+
bubbles: true,
|
|
537
|
+
cancelable: true,
|
|
538
|
+
detail: { submitter, values, request },
|
|
539
|
+
});
|
|
540
|
+
if (!this.dispatchEvent(se)) {
|
|
541
|
+
return;
|
|
542
|
+
}
|
|
543
|
+
this.errors = [];
|
|
544
|
+
const sre = new CustomEvent('submit:requested', {
|
|
545
|
+
bubbles: true,
|
|
546
|
+
cancelable: false,
|
|
547
|
+
detail: { submitter, values: se.detail.values, request: se.detail.request },
|
|
548
|
+
});
|
|
549
|
+
let response = await AsyncEvents.fireAsync(this, sre, { mode: 'pipeline' });
|
|
550
|
+
request = sre.detail.request;
|
|
551
|
+
|
|
552
|
+
response = await loader.submit(request, this, response);
|
|
553
|
+
const mapped = await loader.transform(response, this);
|
|
554
|
+
this.dispatchEvent(
|
|
555
|
+
new CustomEvent('submit:success', {
|
|
556
|
+
bubbles: true,
|
|
557
|
+
cancelable: false,
|
|
558
|
+
detail: { submitter, values, request, response: mapped },
|
|
559
|
+
}),
|
|
560
|
+
);
|
|
561
|
+
} catch (e) {
|
|
562
|
+
this.dispatchEvent(
|
|
563
|
+
new CustomEvent('submit:failure', {
|
|
564
|
+
bubbles: true,
|
|
565
|
+
cancelable: false,
|
|
566
|
+
detail: { submitter, values, request, exception: e },
|
|
567
|
+
}),
|
|
568
|
+
);
|
|
569
|
+
if (e instanceof Failure) {
|
|
570
|
+
this.errors = e.problems;
|
|
571
|
+
}
|
|
572
|
+
console.warn('failed to submit form', this, 'reason:', e);
|
|
573
|
+
}
|
|
574
|
+
} finally {
|
|
575
|
+
this.spinner(false);
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
reset() {
|
|
579
|
+
this.form.reset();
|
|
580
|
+
}
|
|
581
|
+
spinner(spin) {
|
|
582
|
+
this.querySelectorAll('ful-spinner').forEach((el) => {
|
|
583
|
+
const hel = /** @type HTMLElement */ (el);
|
|
584
|
+
hel.hidden = !spin;
|
|
585
|
+
});
|
|
586
|
+
this.querySelectorAll('input,button').forEach((el) => {
|
|
587
|
+
const hel = /** @type HTMLButtonElement|HTMLInputElement */ (el);
|
|
588
|
+
if (hel.type !== 'submit' && hel.type !== 'reset') {
|
|
589
|
+
return;
|
|
590
|
+
}
|
|
591
|
+
if (spin) {
|
|
592
|
+
hel.dataset.wd = String(hel.disabled);
|
|
593
|
+
hel.disabled = true;
|
|
594
|
+
} else {
|
|
595
|
+
hel.disabled = hel.dataset.wd === 'true';
|
|
596
|
+
delete hel.dataset.wd;
|
|
597
|
+
}
|
|
598
|
+
});
|
|
599
|
+
}
|
|
600
|
+
set values(vs) {
|
|
601
|
+
Bindings.mutateIn(this.form, vs);
|
|
602
|
+
}
|
|
603
|
+
get values() {
|
|
604
|
+
return Bindings.extractFrom(this.form);
|
|
605
|
+
}
|
|
606
|
+
set errors(es) {
|
|
607
|
+
Bindings.errors(this.form, es, this.hasAttribute('scroll-on-error'));
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
class Input extends ParsedElement {
|
|
612
|
+
static observed = ['value', 'readonly:presence', 'required:presence'];
|
|
613
|
+
static slots = true;
|
|
614
|
+
static template = `
|
|
615
|
+
<div class="form-label">
|
|
616
|
+
<label>{{{{ slots.default }}}}</label>
|
|
617
|
+
{{{{ slots.info }}}}
|
|
618
|
+
</div>
|
|
619
|
+
<div class="input-group">
|
|
620
|
+
<span data-tpl-if="slots.ibefore" class="input-group-text">{{{{ slots.ibefore }}}}</span>
|
|
621
|
+
{{{{ slots.before }}}}
|
|
622
|
+
<input data-tpl-if="type != 'textarea'" class="form-control" data-tpl-type="type" placeholder=" " form="">
|
|
623
|
+
<textarea data-tpl-if="type == 'textarea'" class="form-control" placeholder=" " form=""></textarea>
|
|
624
|
+
{{{{ slots.after }}}}
|
|
625
|
+
<span data-tpl-if="slots.iafter" class="input-group-text">{{{{ slots.iafter }}}}</span>
|
|
626
|
+
</div>
|
|
627
|
+
<ful-field-error></ful-field-error>
|
|
628
|
+
`;
|
|
629
|
+
static formAssociated = true;
|
|
630
|
+
_input;
|
|
631
|
+
_fieldError;
|
|
632
|
+
constructor() {
|
|
633
|
+
super();
|
|
634
|
+
this.internals = this.attachInternals();
|
|
635
|
+
this.internals.role = 'presentation';
|
|
636
|
+
}
|
|
637
|
+
_type() {
|
|
638
|
+
return this.getAttribute('type') ?? 'text';
|
|
639
|
+
}
|
|
640
|
+
_fragment(type, slots) {
|
|
641
|
+
return this.template().withOverlay({ type, slots }).render();
|
|
642
|
+
}
|
|
643
|
+
render({ slots, observed, disabled, skipObservedSetup }) {
|
|
644
|
+
const type = this._type();
|
|
645
|
+
const fragment = this._fragment(type, slots);
|
|
646
|
+
this._input = fragment.querySelector('input,textarea');
|
|
647
|
+
|
|
648
|
+
Attributes.forward('input-', this, this._input);
|
|
649
|
+
if (!skipObservedSetup) {
|
|
650
|
+
this.disabled = disabled;
|
|
651
|
+
this.readonly = observed.readonly;
|
|
652
|
+
this.required = observed.required;
|
|
653
|
+
this.value = observed.value;
|
|
654
|
+
}
|
|
655
|
+
this._input.addEventListener('keydown', (evt) => {
|
|
656
|
+
if (evt.key !== 'Enter' || this._type() === 'textarea') {
|
|
657
|
+
return;
|
|
658
|
+
}
|
|
659
|
+
const form = this.internals.form;
|
|
660
|
+
if (!form) {
|
|
661
|
+
return;
|
|
662
|
+
}
|
|
663
|
+
const candidates = /** @type [HTMLButtonElement|HTMLInputElement] */ (
|
|
664
|
+
Array.from(form.querySelectorAll('button:not(:disabled), input:not(:disabled)'))
|
|
665
|
+
);
|
|
666
|
+
const submitter = candidates.find((el) => el.type === 'submit');
|
|
667
|
+
form.requestSubmit(submitter);
|
|
668
|
+
});
|
|
669
|
+
this._input.addEventListener('input', (evt) => {
|
|
670
|
+
const re = this.getAttribute('mask');
|
|
671
|
+
if (!re) {
|
|
672
|
+
return;
|
|
673
|
+
}
|
|
674
|
+
const before = evt.target.value;
|
|
675
|
+
const after = before.replace(new RegExp(re, 'g'), '');
|
|
676
|
+
if (before === after) {
|
|
677
|
+
return;
|
|
678
|
+
}
|
|
679
|
+
const start = evt.target.selectionStart;
|
|
680
|
+
const offset = before.length - after.length;
|
|
681
|
+
evt.target.value = after;
|
|
682
|
+
evt.target.setSelectionRange(start - offset, start - offset);
|
|
683
|
+
});
|
|
684
|
+
this._input.addEventListener('change', (evt) => {
|
|
685
|
+
evt.stopPropagation();
|
|
686
|
+
this.dispatchEvent(
|
|
687
|
+
new CustomEvent('change', {
|
|
688
|
+
bubbles: true,
|
|
689
|
+
cancelable: false,
|
|
690
|
+
detail: {
|
|
691
|
+
value: this.value,
|
|
692
|
+
},
|
|
693
|
+
}),
|
|
694
|
+
);
|
|
695
|
+
});
|
|
696
|
+
const label = fragment.querySelector('label');
|
|
697
|
+
label.addEventListener('click', () => this.focus());
|
|
698
|
+
this._fieldError = fragment.querySelector('ful-field-error');
|
|
699
|
+
this._input.ariaDescribedByElements = [this._fieldError];
|
|
700
|
+
this._input.ariaLabelledByElements = [label];
|
|
701
|
+
this.replaceChildren(fragment);
|
|
702
|
+
}
|
|
703
|
+
get value() {
|
|
704
|
+
const uppercase = this.hasAttribute('uppercase');
|
|
705
|
+
const trim = this.hasAttribute('trim');
|
|
706
|
+
const v = this._input.value;
|
|
707
|
+
const uppercased = uppercase ? v.toUpperCase() : v;
|
|
708
|
+
const trimmed = trim ? uppercased.trim() : uppercased;
|
|
709
|
+
return trimmed === '' ? null : trimmed;
|
|
710
|
+
}
|
|
711
|
+
set value(value) {
|
|
712
|
+
this._input.value = value === '' ? null : value;
|
|
713
|
+
}
|
|
714
|
+
get readonly() {
|
|
715
|
+
return this._input.readOnly;
|
|
716
|
+
}
|
|
717
|
+
set readonly(v) {
|
|
718
|
+
this._input.readOnly = v;
|
|
719
|
+
this.reflect(() => {
|
|
720
|
+
Attributes.toggle(this, 'readonly', v);
|
|
721
|
+
});
|
|
722
|
+
}
|
|
723
|
+
get disabled() {
|
|
724
|
+
return this._input.hasAttribute('disabled');
|
|
725
|
+
}
|
|
726
|
+
set disabled(d) {
|
|
727
|
+
Attributes.toggle(this._input, 'disabled', d);
|
|
728
|
+
}
|
|
729
|
+
get required() {
|
|
730
|
+
return this._input.getAttribute('aria-required') === 'true';
|
|
731
|
+
}
|
|
732
|
+
set required(d) {
|
|
733
|
+
Attributes.set(this._input, 'aria-required', d ? 'true' : null);
|
|
734
|
+
this.reflect(() => {
|
|
735
|
+
Attributes.toggle(this, 'required', d);
|
|
736
|
+
});
|
|
737
|
+
}
|
|
738
|
+
focus(options) {
|
|
739
|
+
this._input.focus(options);
|
|
740
|
+
}
|
|
741
|
+
setCustomValidity(error) {
|
|
742
|
+
if (!error) {
|
|
743
|
+
this.internals.setValidity({});
|
|
744
|
+
this._fieldError.innerText = '';
|
|
745
|
+
return;
|
|
746
|
+
}
|
|
747
|
+
this.internals.setValidity({ customError: true }, ' ');
|
|
748
|
+
this._fieldError.innerText = error;
|
|
749
|
+
}
|
|
750
|
+
formResetCallback() {
|
|
751
|
+
this.value = this.unmarshal('value', this.getAttribute('value'));
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
class LocalDate extends ParsedElement {
|
|
756
|
+
render() {
|
|
757
|
+
const content = this.innerHTML.trim();
|
|
758
|
+
if (content === '') {
|
|
759
|
+
this.innerHTML = this.getAttribute('default') ?? '';
|
|
760
|
+
return;
|
|
761
|
+
}
|
|
762
|
+
const locale = this.getAttribute('locale') ?? Intl.DateTimeFormat().resolvedOptions().locale;
|
|
763
|
+
const formatter = new Intl.DateTimeFormat(locale, { year: 'numeric', month: 'numeric', day: 'numeric' });
|
|
764
|
+
const [y, m, d] = content.split('-').map(Number);
|
|
765
|
+
this.innerHTML = formatter.format(new Date(y, m - 1, d));
|
|
766
|
+
}
|
|
767
|
+
}
|
|
768
|
+
|
|
769
|
+
class Instant extends ParsedElement {
|
|
770
|
+
render() {
|
|
771
|
+
const content = this.innerHTML.trim();
|
|
772
|
+
if (content === '') {
|
|
773
|
+
this.innerHTML = this.getAttribute('default') ?? '';
|
|
774
|
+
return;
|
|
775
|
+
}
|
|
776
|
+
const locale = this.getAttribute('locale') ?? Intl.DateTimeFormat().resolvedOptions().locale;
|
|
777
|
+
const format = new Intl.DateTimeFormat(locale, {
|
|
778
|
+
year: 'numeric',
|
|
779
|
+
month: 'numeric',
|
|
780
|
+
day: 'numeric',
|
|
781
|
+
hour: 'numeric',
|
|
782
|
+
minute: 'numeric',
|
|
783
|
+
second: 'numeric',
|
|
784
|
+
hour12: false,
|
|
785
|
+
});
|
|
786
|
+
this.innerHTML = format.format(new Date(Instant.isoToLocal(content)));
|
|
787
|
+
}
|
|
788
|
+
static isoToLocal(iso) {
|
|
789
|
+
//this is so sad
|
|
790
|
+
const d = new Date(iso);
|
|
791
|
+
const pad = (n, v) => String(v).padStart(n, '0');
|
|
792
|
+
const date = `${d.getFullYear()}-${pad(2, d.getMonth() + 1)}-${pad(2, d.getDate())}`;
|
|
793
|
+
const time = `${pad(2, d.getHours())}:${pad(2, d.getMinutes())}:${pad(2, d.getSeconds())}.${pad(3, d.getMilliseconds())}`;
|
|
794
|
+
return `${date}T${time}`;
|
|
795
|
+
}
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
class InputLocalDate extends Input {
|
|
799
|
+
static observed = ['value', 'readonly:presence', 'required:presence', 'min', 'max', 'step'];
|
|
800
|
+
_type() {
|
|
801
|
+
return 'date';
|
|
802
|
+
}
|
|
803
|
+
render(conf) {
|
|
804
|
+
const { observed } = conf;
|
|
805
|
+
super.render(conf);
|
|
806
|
+
this.min = observed.min;
|
|
807
|
+
this.max = observed.max;
|
|
808
|
+
this.step = observed.step;
|
|
809
|
+
}
|
|
810
|
+
get min() {
|
|
811
|
+
const v = this._input.min;
|
|
812
|
+
return v === '' ? null : v;
|
|
813
|
+
}
|
|
814
|
+
set min(v) {
|
|
815
|
+
this._input.min = InputLocalDate.#fromIsoOrOffset(v);
|
|
816
|
+
}
|
|
817
|
+
get max() {
|
|
818
|
+
const v = this._input.max;
|
|
819
|
+
return v === '' ? null : v;
|
|
820
|
+
}
|
|
821
|
+
set max(v) {
|
|
822
|
+
this._input.max = InputLocalDate.#fromIsoOrOffset(v);
|
|
823
|
+
}
|
|
824
|
+
get step() {
|
|
825
|
+
const v = this._input.step;
|
|
826
|
+
return v === '' ? null : v;
|
|
827
|
+
}
|
|
828
|
+
set step(v) {
|
|
829
|
+
this._input.step = v ?? '';
|
|
830
|
+
}
|
|
831
|
+
static #fromIsoOrOffset(v) {
|
|
832
|
+
if (!v) {
|
|
833
|
+
return '';
|
|
834
|
+
}
|
|
835
|
+
//this could be date.toLocaleDateString('en-CA')
|
|
836
|
+
const formatLocalDate = (date) =>
|
|
837
|
+
new Date(date.getTime() - date.getTimezoneOffset() * 60000).toISOString().split('T')[0];
|
|
838
|
+
if (v === 'now') {
|
|
839
|
+
return formatLocalDate(new Date());
|
|
840
|
+
}
|
|
841
|
+
const re = /^([+-])(\d+)([dmy])$/;
|
|
842
|
+
const match = re.exec(v);
|
|
843
|
+
if (!match) {
|
|
844
|
+
return v;
|
|
845
|
+
}
|
|
846
|
+
const sign = match[1] === '-' ? -1 : 1;
|
|
847
|
+
const offset = +match[2];
|
|
848
|
+
const r = new Date();
|
|
849
|
+
r.setHours(0, 0, 0, 0);
|
|
850
|
+
switch (match[3]) {
|
|
851
|
+
case 'd':
|
|
852
|
+
r.setDate(r.getDate() + offset * sign);
|
|
853
|
+
break;
|
|
854
|
+
case 'm':
|
|
855
|
+
const originalDay = r.getDate();
|
|
856
|
+
r.setMonth(r.getMonth() + offset * sign);
|
|
857
|
+
if (r.getDate() !== originalDay) {
|
|
858
|
+
r.setDate(0);
|
|
859
|
+
}
|
|
860
|
+
break;
|
|
861
|
+
case 'y':
|
|
862
|
+
r.setFullYear(r.getFullYear() + offset * sign);
|
|
863
|
+
break;
|
|
864
|
+
}
|
|
865
|
+
return formatLocalDate(r);
|
|
866
|
+
}
|
|
867
|
+
}
|
|
868
|
+
|
|
869
|
+
class InputLocalTime extends InputLocalDate {
|
|
870
|
+
_type() {
|
|
871
|
+
return 'time';
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
class InputInstant extends Input {
|
|
876
|
+
static observed = ['value', 'readonly:presence', 'required:presence', 'min', 'max', 'step'];
|
|
877
|
+
_type() {
|
|
878
|
+
return 'datetime-local';
|
|
879
|
+
}
|
|
880
|
+
render(conf) {
|
|
881
|
+
const { observed } = conf;
|
|
882
|
+
super.render(conf);
|
|
883
|
+
this.min = observed.min;
|
|
884
|
+
this.max = observed.max;
|
|
885
|
+
this.step = observed.step;
|
|
886
|
+
}
|
|
887
|
+
get value() {
|
|
888
|
+
const v = this._input.value;
|
|
889
|
+
return v === '' ? null : new Date(v).toISOString();
|
|
890
|
+
}
|
|
891
|
+
set value(v) {
|
|
892
|
+
this._input.value = v ? Instant.isoToLocal(v) : '';
|
|
893
|
+
}
|
|
894
|
+
get min() {
|
|
895
|
+
const v = this._input.min;
|
|
896
|
+
return v === '' ? null : new Date(v).toISOString();
|
|
897
|
+
}
|
|
898
|
+
set min(v) {
|
|
899
|
+
this._input.min = v ? Instant.isoToLocal(v) : '';
|
|
900
|
+
}
|
|
901
|
+
get max() {
|
|
902
|
+
const v = this._input.max;
|
|
903
|
+
return v === '' ? null : new Date(v).toISOString();
|
|
904
|
+
}
|
|
905
|
+
set max(v) {
|
|
906
|
+
this._input.max = v ? Instant.isoToLocal(v) : '';
|
|
907
|
+
}
|
|
908
|
+
get step() {
|
|
909
|
+
const v = this._input.step;
|
|
910
|
+
return v === '' ? null : v;
|
|
911
|
+
}
|
|
912
|
+
set step(v) {
|
|
913
|
+
this._input.step = v ?? '';
|
|
914
|
+
}
|
|
915
|
+
}
|
|
916
|
+
|
|
917
|
+
class InputFile extends Input {
|
|
918
|
+
static l10n = {
|
|
919
|
+
en: {
|
|
920
|
+
dropzonelabel: 'Click or drop your files here',
|
|
921
|
+
unaccepptablefiletype: 'Only files of type {0} are supported',
|
|
922
|
+
maxfilesizeexceeded: 'Maximum supported file size is {0}',
|
|
923
|
+
maxtotalsizeexceeded: 'Maximum supported total file size is {0}',
|
|
924
|
+
maxfilesexceeded: 'Maximum files count exceeded',
|
|
925
|
+
},
|
|
926
|
+
it: {
|
|
927
|
+
dropzonelabel: 'Clicca o trascina i file qui',
|
|
928
|
+
unaccepptablefiletype: 'Solo i file di tipo {0} sono supportati',
|
|
929
|
+
maxfilesizeexceeded: 'La dimensione massima di un file è di {0}',
|
|
930
|
+
maxtotalsizeexceeded: 'La dimensione massima complessiva dei file è di {0}',
|
|
931
|
+
maxfilesexceeded: 'Numero massimo di file superato',
|
|
932
|
+
},
|
|
933
|
+
es: {
|
|
934
|
+
dropzonelabel: 'Haz clic o arrastra tus archivos aquí',
|
|
935
|
+
unaccepptablefiletype: 'Solo se admiten archivos de tipo {0}',
|
|
936
|
+
maxfilesizeexceeded: 'El tamaño máximo de archivo admitido es {0}',
|
|
937
|
+
maxtotalsizeexceeded: 'El tamaño total máximo admitido es {0}',
|
|
938
|
+
maxfilesexceeded: 'Se ha superado el número máximo de archivos',
|
|
939
|
+
},
|
|
940
|
+
fr: {
|
|
941
|
+
dropzonelabel: 'Cliquez ou déposez vos fichiers ici',
|
|
942
|
+
unaccepptablefiletype: 'Seuls les fichiers de type {0} sont pris en charge',
|
|
943
|
+
maxfilesizeexceeded: 'La taille maximale de fichier prise en charge est {0}',
|
|
944
|
+
maxtotalsizeexceeded: 'La taille totale maximale prise en charge est {0}',
|
|
945
|
+
maxfilesexceeded: 'Nombre maximal de fichiers dépassé',
|
|
946
|
+
},
|
|
947
|
+
};
|
|
948
|
+
static observed = [
|
|
949
|
+
'value',
|
|
950
|
+
'readonly:presence',
|
|
951
|
+
'required:presence',
|
|
952
|
+
'accept:csv',
|
|
953
|
+
'multiple:presence',
|
|
954
|
+
'itemlist:presence',
|
|
955
|
+
'dropzone:presence',
|
|
956
|
+
'maxfiles:number',
|
|
957
|
+
'maxfilesize:number',
|
|
958
|
+
'maxtotalsize:number',
|
|
959
|
+
];
|
|
960
|
+
#accept;
|
|
961
|
+
#items;
|
|
962
|
+
#dropzone;
|
|
963
|
+
#warnings;
|
|
964
|
+
_type() {
|
|
965
|
+
return 'file';
|
|
966
|
+
}
|
|
967
|
+
static template = `
|
|
968
|
+
<div class="form-label">
|
|
969
|
+
<label>{{{{ slots.default }}}}</label>
|
|
970
|
+
{{{{ slots.info }}}}
|
|
971
|
+
</div>
|
|
972
|
+
<div class="input-group">
|
|
973
|
+
<span data-tpl-if="slots.ibefore" class="input-group-text">{{{{ slots.ibefore }}}}</span>
|
|
974
|
+
{{{{ slots.before }}}}
|
|
975
|
+
<input class="form-control" data-tpl-type="type" placeholder=" " form="">
|
|
976
|
+
{{{{ slots.after }}}}
|
|
977
|
+
<span data-tpl-if="slots.iafter" class="input-group-text">{{{{ slots.iafter }}}}</span>
|
|
978
|
+
</div>
|
|
979
|
+
<div data-ref="dropzone" class="dropzone" data-tpl-if="slots.dropzone">
|
|
980
|
+
{{{{ slots.dropzone }}}}
|
|
981
|
+
</div>
|
|
982
|
+
<div data-ref="dropzone" class="default-dropzone" data-tpl-if="!slots.dropzone">
|
|
983
|
+
{{ #l10n:t('dropzonelabel') }}
|
|
984
|
+
</div>
|
|
985
|
+
<ful-item-list></ful-item-list>
|
|
986
|
+
<ful-field-warnings></ful-field-warnings>
|
|
987
|
+
<ful-field-error></ful-field-error>
|
|
988
|
+
`;
|
|
989
|
+
static templates = {
|
|
990
|
+
items: `
|
|
991
|
+
<ful-item data-tpl-each="files" data-tpl-var="file" data-tpl-data-name="file.name">
|
|
992
|
+
<div>{{ file.name }}</div>
|
|
993
|
+
<div>{{ #bytes:format(file.size) }}</div>
|
|
994
|
+
<button type="button" class="btn btn-sm btn-outline-danger bi bi-x-lg" alt="Rimuovi"></button>
|
|
995
|
+
</ful-item>
|
|
996
|
+
`,
|
|
997
|
+
warning: `<ful-field-warning>{{ #l10n:t(key, args) }}</ful-field-warning>`,
|
|
998
|
+
};
|
|
999
|
+
render(conf) {
|
|
1000
|
+
const { observed } = conf;
|
|
1001
|
+
super.render(conf);
|
|
1002
|
+
this.#items = this.querySelector('ful-item-list');
|
|
1003
|
+
this.#dropzone = this.querySelector('[data-ref=dropzone]');
|
|
1004
|
+
this.#warnings = this.querySelector('ful-field-warnings');
|
|
1005
|
+
this.accept = observed.accept;
|
|
1006
|
+
this.multiple = observed.multiple;
|
|
1007
|
+
this.itemlist = observed.itemlist;
|
|
1008
|
+
this.dropzone = observed.dropzone;
|
|
1009
|
+
this.maxfiles = observed.maxfiles;
|
|
1010
|
+
this.maxfilesize = observed.maxfilesize;
|
|
1011
|
+
this.maxtotalsize = observed.maxtotalsize;
|
|
1012
|
+
this.#warnings.addEventListener('animationend', (e) => {
|
|
1013
|
+
e.target.remove();
|
|
1014
|
+
});
|
|
1015
|
+
this.#items.addEventListener('click', (e) => {
|
|
1016
|
+
if (!e.target.closest('button')) {
|
|
1017
|
+
return;
|
|
1018
|
+
}
|
|
1019
|
+
const fileName = e.target.closest('ful-item').dataset.name;
|
|
1020
|
+
const dt = new DataTransfer();
|
|
1021
|
+
[...this.files].filter((f) => f.name !== fileName).forEach((f) => dt.items.add(f));
|
|
1022
|
+
this.files = dt.files;
|
|
1023
|
+
this.#update();
|
|
1024
|
+
});
|
|
1025
|
+
this.#dropzone.addEventListener('click', (e) => {
|
|
1026
|
+
this.querySelector('input')?.click();
|
|
1027
|
+
});
|
|
1028
|
+
|
|
1029
|
+
this.#dropzone.addEventListener('dragover', (e) => {
|
|
1030
|
+
e.preventDefault();
|
|
1031
|
+
});
|
|
1032
|
+
this.#dropzone.addEventListener('drop', (e) => {
|
|
1033
|
+
e.preventDefault();
|
|
1034
|
+
const dt = new DataTransfer();
|
|
1035
|
+
[...e.dataTransfer.items].filter((i) => i.kind === 'file').forEach((i) => dt.items.add(i.getAsFile()));
|
|
1036
|
+
this.files = dt.files;
|
|
1037
|
+
this.#update();
|
|
1038
|
+
});
|
|
1039
|
+
this._input.addEventListener('change', (e) => {
|
|
1040
|
+
this.#update();
|
|
1041
|
+
});
|
|
1042
|
+
}
|
|
1043
|
+
#formatByteSize(v) {
|
|
1044
|
+
return v > 1024 * 1024
|
|
1045
|
+
? `${Math.round((v / 1024 / 1024) * 100) / 100}MiB`
|
|
1046
|
+
: v > 1024
|
|
1047
|
+
? `${Math.round((v / 1024) * 100) / 100}KiB`
|
|
1048
|
+
: `${v}B`;
|
|
1049
|
+
}
|
|
1050
|
+
#update() {
|
|
1051
|
+
this.setCustomValidity();
|
|
1052
|
+
this.#ensureAcceptable();
|
|
1053
|
+
this.#ensureFileSizes();
|
|
1054
|
+
this.#ensureTotalSize();
|
|
1055
|
+
this.#ensureFilesCount();
|
|
1056
|
+
this.template('items')
|
|
1057
|
+
.withOverlay({ files: this.files })
|
|
1058
|
+
.withModule('bytes', { format: this.#formatByteSize })
|
|
1059
|
+
.renderTo(this.#items);
|
|
1060
|
+
}
|
|
1061
|
+
warning(key, args) {
|
|
1062
|
+
this.template('warning').withOverlay({ key, args }).renderTo(this.#warnings);
|
|
1063
|
+
}
|
|
1064
|
+
#ensureAcceptable() {
|
|
1065
|
+
if (!this.#accept.length) {
|
|
1066
|
+
return;
|
|
1067
|
+
}
|
|
1068
|
+
const unacceptable = [...this.files].filter(
|
|
1069
|
+
(file) => !this.#accept.some((type) => file.name.toLowerCase().endsWith(type.toLowerCase())),
|
|
1070
|
+
);
|
|
1071
|
+
|
|
1072
|
+
if (unacceptable.length === 0) {
|
|
1073
|
+
return;
|
|
1074
|
+
}
|
|
1075
|
+
this.warning('unaccepptablefiletype', this.#accept.join(', '));
|
|
1076
|
+
const dt = new DataTransfer();
|
|
1077
|
+
[...this.files].filter((f) => !unacceptable.includes(f)).forEach((f) => dt.items.add(f));
|
|
1078
|
+
this.files = dt.files;
|
|
1079
|
+
}
|
|
1080
|
+
#ensureFilesCount() {
|
|
1081
|
+
if (this.#maxfiles === null) {
|
|
1082
|
+
return;
|
|
1083
|
+
}
|
|
1084
|
+
if (this.files.length <= this.#maxfiles) {
|
|
1085
|
+
return;
|
|
1086
|
+
}
|
|
1087
|
+
this.warning('maxfilesexceeded');
|
|
1088
|
+
const dt = new DataTransfer();
|
|
1089
|
+
this.files = dt.files;
|
|
1090
|
+
}
|
|
1091
|
+
|
|
1092
|
+
#ensureFileSizes() {
|
|
1093
|
+
if (this.#maxfilesize === null) {
|
|
1094
|
+
return;
|
|
1095
|
+
}
|
|
1096
|
+
const oversized = [...this.files].filter((file) => file.size > this.#maxfilesize);
|
|
1097
|
+
if (oversized.length === 0) {
|
|
1098
|
+
return;
|
|
1099
|
+
}
|
|
1100
|
+
this.warning('maxfilesizeexceeded', this.#formatByteSize(this.#maxfilesize));
|
|
1101
|
+
const dt = new DataTransfer();
|
|
1102
|
+
[...this.files].filter((f) => !oversized.includes(f)).forEach((f) => dt.items.add(f));
|
|
1103
|
+
this.files = dt.files;
|
|
1104
|
+
}
|
|
1105
|
+
#ensureTotalSize() {
|
|
1106
|
+
if (this.#maxtotalsize === null) {
|
|
1107
|
+
return;
|
|
1108
|
+
}
|
|
1109
|
+
const totalSize = [...this.files].reduce((acc, file) => acc + file.size, 0);
|
|
1110
|
+
if (totalSize <= this.#maxtotalsize) {
|
|
1111
|
+
return;
|
|
1112
|
+
}
|
|
1113
|
+
this.warning('maxtotalsizeexceeded', this.#formatByteSize(this.#maxtotalsize));
|
|
1114
|
+
this.files = new DataTransfer().files;
|
|
1115
|
+
}
|
|
1116
|
+
get accept() {
|
|
1117
|
+
return this.#accept;
|
|
1118
|
+
}
|
|
1119
|
+
set accept(vs) {
|
|
1120
|
+
this._input.accept = vs.join(',');
|
|
1121
|
+
this.#accept = vs;
|
|
1122
|
+
this.reflect(() => {
|
|
1123
|
+
this.setAttribute('accept', this._input.accept);
|
|
1124
|
+
});
|
|
1125
|
+
}
|
|
1126
|
+
get multiple() {
|
|
1127
|
+
return this._input.multiple;
|
|
1128
|
+
}
|
|
1129
|
+
set multiple(v) {
|
|
1130
|
+
this._input.multiple = v;
|
|
1131
|
+
this.reflect(() => {
|
|
1132
|
+
Attributes.toggle(this, 'multiple', v);
|
|
1133
|
+
});
|
|
1134
|
+
}
|
|
1135
|
+
get files() {
|
|
1136
|
+
return this._input.files;
|
|
1137
|
+
}
|
|
1138
|
+
set files(vs) {
|
|
1139
|
+
this._input.files = vs;
|
|
1140
|
+
}
|
|
1141
|
+
get file() {
|
|
1142
|
+
return this.files[0] ?? null;
|
|
1143
|
+
}
|
|
1144
|
+
set file(v) {
|
|
1145
|
+
const dt = new DataTransfer();
|
|
1146
|
+
if (v) {
|
|
1147
|
+
dt.items.add(v);
|
|
1148
|
+
}
|
|
1149
|
+
this.files = dt.files;
|
|
1150
|
+
}
|
|
1151
|
+
get value() {
|
|
1152
|
+
const names = Array.from(this._input.files).map((f) => f.name);
|
|
1153
|
+
return this.multiple ? names : (names[0] ?? null);
|
|
1154
|
+
}
|
|
1155
|
+
set value(v) {
|
|
1156
|
+
if (v) {
|
|
1157
|
+
return;
|
|
1158
|
+
}
|
|
1159
|
+
this.files = new DataTransfer().files;
|
|
1160
|
+
this.#update();
|
|
1161
|
+
}
|
|
1162
|
+
get totalsize() {
|
|
1163
|
+
return Array.from(this.files).reduce((a, f) => a + f.size, 0);
|
|
1164
|
+
}
|
|
1165
|
+
#maxfiles;
|
|
1166
|
+
get maxfiles() {
|
|
1167
|
+
return this.#maxfiles;
|
|
1168
|
+
}
|
|
1169
|
+
set maxfiles(v) {
|
|
1170
|
+
this.#maxfiles = v;
|
|
1171
|
+
this.reflect(() => {
|
|
1172
|
+
Attributes.set(this, 'maxfiles', v);
|
|
1173
|
+
});
|
|
1174
|
+
}
|
|
1175
|
+
#maxfilesize;
|
|
1176
|
+
get maxfilesize() {
|
|
1177
|
+
return this.#maxfilesize;
|
|
1178
|
+
}
|
|
1179
|
+
set maxfilesize(v) {
|
|
1180
|
+
this.#maxfilesize = v;
|
|
1181
|
+
this.reflect(() => {
|
|
1182
|
+
Attributes.set(this, 'maxfilesize', v);
|
|
1183
|
+
});
|
|
1184
|
+
}
|
|
1185
|
+
#maxtotalsize;
|
|
1186
|
+
get maxtotalsize() {
|
|
1187
|
+
return this.#maxtotalsize;
|
|
1188
|
+
}
|
|
1189
|
+
set maxtotalsize(v) {
|
|
1190
|
+
this.#maxtotalsize = v;
|
|
1191
|
+
this.reflect(() => {
|
|
1192
|
+
Attributes.set(this, 'maxtotalsize', v);
|
|
1193
|
+
});
|
|
1194
|
+
}
|
|
1195
|
+
#useItemlist;
|
|
1196
|
+
get itemlist() {
|
|
1197
|
+
return this.#useItemlist;
|
|
1198
|
+
}
|
|
1199
|
+
set itemlist(v) {
|
|
1200
|
+
this.#useItemlist = v;
|
|
1201
|
+
this.reflect(() => {
|
|
1202
|
+
Attributes.toggle(this, 'itemlist', v);
|
|
1203
|
+
});
|
|
1204
|
+
}
|
|
1205
|
+
#useDropzone;
|
|
1206
|
+
get dropzone() {
|
|
1207
|
+
return this.#useDropzone;
|
|
1208
|
+
}
|
|
1209
|
+
set dropzone(v) {
|
|
1210
|
+
this.#useDropzone = v;
|
|
1211
|
+
this.reflect(() => {
|
|
1212
|
+
Attributes.toggle(this, 'dropzone', v);
|
|
1213
|
+
});
|
|
1214
|
+
}
|
|
1215
|
+
}
|
|
1216
|
+
|
|
1217
|
+
class RemoteLoader {
|
|
1218
|
+
#http;
|
|
1219
|
+
#url;
|
|
1220
|
+
#method;
|
|
1221
|
+
#responseMapper;
|
|
1222
|
+
#prefetch;
|
|
1223
|
+
#revision;
|
|
1224
|
+
#data;
|
|
1225
|
+
constructor({ http, url, method, responseMapper, prefetch, revision }) {
|
|
1226
|
+
this.#http = http;
|
|
1227
|
+
this.#url = url;
|
|
1228
|
+
this.#method = method;
|
|
1229
|
+
this.#responseMapper = responseMapper;
|
|
1230
|
+
this.#prefetch = prefetch;
|
|
1231
|
+
this.#revision = revision;
|
|
1232
|
+
this.#data = null;
|
|
1233
|
+
}
|
|
1234
|
+
async prefetch() {
|
|
1235
|
+
if (!this.#prefetch) {
|
|
1236
|
+
return;
|
|
1237
|
+
}
|
|
1238
|
+
await this.#ensureFetched();
|
|
1239
|
+
}
|
|
1240
|
+
async exact(...keys) {
|
|
1241
|
+
await this.#ensureFetched();
|
|
1242
|
+
return this.#data.filter(([k, v]) => keys.some((r) => r == k));
|
|
1243
|
+
}
|
|
1244
|
+
async load(needle) {
|
|
1245
|
+
await this.#ensureFetched();
|
|
1246
|
+
return this.#data.filter(([k, v]) => (v ?? '').toLowerCase().includes(needle?.toLowerCase()));
|
|
1247
|
+
}
|
|
1248
|
+
async reconfigureUrl(url) {
|
|
1249
|
+
this.#data = null;
|
|
1250
|
+
this.#url = url;
|
|
1251
|
+
}
|
|
1252
|
+
async #ensureFetched() {
|
|
1253
|
+
if (this.#data !== null) {
|
|
1254
|
+
return;
|
|
1255
|
+
}
|
|
1256
|
+
const raw = await RemoteLoader.#revisionedData(this.#http, this.#method, this.#url, this.#revision);
|
|
1257
|
+
this.#data = this.#responseMapper(raw);
|
|
1258
|
+
}
|
|
1259
|
+
static async #revisionedData(http, method, url, revision) {
|
|
1260
|
+
const storageKey = `${method}@${url}`;
|
|
1261
|
+
if (revision !== null) {
|
|
1262
|
+
const data = VersionedLocalStorage.load(storageKey, revision);
|
|
1263
|
+
if (data !== undefined) {
|
|
1264
|
+
return data;
|
|
1265
|
+
}
|
|
1266
|
+
}
|
|
1267
|
+
const data = await http.request(method, url).fetchJson();
|
|
1268
|
+
if (revision !== null) {
|
|
1269
|
+
VersionedLocalStorage.save(storageKey, revision, data);
|
|
1270
|
+
}
|
|
1271
|
+
return data;
|
|
1272
|
+
}
|
|
1273
|
+
}
|
|
1274
|
+
|
|
1275
|
+
class PartialRemoteLoader {
|
|
1276
|
+
#http;
|
|
1277
|
+
#url;
|
|
1278
|
+
#method;
|
|
1279
|
+
#responseMapper;
|
|
1280
|
+
constructor({ http, url, method, responseMapper }) {
|
|
1281
|
+
this.#http = http;
|
|
1282
|
+
this.#url = url;
|
|
1283
|
+
this.#method = method;
|
|
1284
|
+
this.#responseMapper = responseMapper;
|
|
1285
|
+
}
|
|
1286
|
+
async exact(...keys) {
|
|
1287
|
+
const response = await this.#http
|
|
1288
|
+
.request(this.#method, this.#url)
|
|
1289
|
+
.param('k', ...keys)
|
|
1290
|
+
.fetchJson();
|
|
1291
|
+
return this.#responseMapper(response);
|
|
1292
|
+
}
|
|
1293
|
+
async load(needle) {
|
|
1294
|
+
const response = await this.#http.request(this.#method, this.#url).param('s', needle).fetchJson();
|
|
1295
|
+
return this.#responseMapper(response);
|
|
1296
|
+
}
|
|
1297
|
+
}
|
|
1298
|
+
|
|
1299
|
+
class InMemoryLoader {
|
|
1300
|
+
#data;
|
|
1301
|
+
constructor(data) {
|
|
1302
|
+
this.#data = data;
|
|
1303
|
+
}
|
|
1304
|
+
update(data) {
|
|
1305
|
+
this.#data = data;
|
|
1306
|
+
}
|
|
1307
|
+
exact(...keys) {
|
|
1308
|
+
return this.#data.filter(([k, v]) => keys.some((r) => r == k));
|
|
1309
|
+
}
|
|
1310
|
+
load(needle) {
|
|
1311
|
+
return this.#data.filter(([k, v]) => (v ?? '').toLowerCase().includes(needle?.toLowerCase()));
|
|
1312
|
+
}
|
|
1313
|
+
}
|
|
1314
|
+
|
|
1315
|
+
class SelectLoader {
|
|
1316
|
+
static create(el, conf) {
|
|
1317
|
+
if (!el.hasAttribute('src')) {
|
|
1318
|
+
const els = Array.from(conf.options?.querySelectorAll('option') ?? []);
|
|
1319
|
+
const data = els.map((e) => {
|
|
1320
|
+
return [e.getAttribute('value') ?? e.innerText.trim(), e.innerText.trim()];
|
|
1321
|
+
});
|
|
1322
|
+
return new InMemoryLoader(data);
|
|
1323
|
+
}
|
|
1324
|
+
const http = registry.component('http-client');
|
|
1325
|
+
const responseMapper = SelectLoader.#responseMapperFrom(el);
|
|
1326
|
+
|
|
1327
|
+
if ('chunked' == el.getAttribute('mode')) {
|
|
1328
|
+
return new PartialRemoteLoader({
|
|
1329
|
+
http,
|
|
1330
|
+
url: el.getAttribute('src'),
|
|
1331
|
+
method: el.getAttribute('method') ?? 'POST',
|
|
1332
|
+
responseMapper,
|
|
1333
|
+
});
|
|
1334
|
+
}
|
|
1335
|
+
return new RemoteLoader({
|
|
1336
|
+
http,
|
|
1337
|
+
url: el.getAttribute('src'),
|
|
1338
|
+
method: el.getAttribute('method') ?? 'POST',
|
|
1339
|
+
responseMapper,
|
|
1340
|
+
prefetch: el.hasAttribute('preload'),
|
|
1341
|
+
revision: el.getAttribute('revision'),
|
|
1342
|
+
});
|
|
1343
|
+
}
|
|
1344
|
+
static #responseMapperFrom(el) {
|
|
1345
|
+
if (el.hasAttribute('k-expr') && el.hasAttribute('l-expr')) {
|
|
1346
|
+
return (response) => {
|
|
1347
|
+
const rows = registry
|
|
1348
|
+
.evaluator()
|
|
1349
|
+
.withOverlay(response)
|
|
1350
|
+
.evaluateExpression(el.getAttribute('d-expr') ?? 'self');
|
|
1351
|
+
return rows.map((row) => {
|
|
1352
|
+
const evaluator = registry.evaluator().withOverlay(row);
|
|
1353
|
+
return [
|
|
1354
|
+
evaluator.evaluateExpression(el.getAttribute('k-expr')),
|
|
1355
|
+
evaluator.evaluateExpression(el.getAttribute('l-expr')),
|
|
1356
|
+
evaluator.evaluateExpression(el.getAttribute('m-expr') ?? 'self'),
|
|
1357
|
+
];
|
|
1358
|
+
});
|
|
1359
|
+
};
|
|
1360
|
+
}
|
|
1361
|
+
if (el.hasAttribute('response-mapper')) {
|
|
1362
|
+
return registry.component(el.getAttribute('response-mapper'));
|
|
1363
|
+
}
|
|
1364
|
+
return (response) => response;
|
|
1365
|
+
}
|
|
1366
|
+
}
|
|
1367
|
+
|
|
1368
|
+
class Dropdown extends ParsedElement {
|
|
1369
|
+
static slots = true;
|
|
1370
|
+
static template = `
|
|
1371
|
+
<ful-spinner class="centered" hidden></ful-spinner>
|
|
1372
|
+
<menu tabindex="-1" role="listbox" hidden></menu>
|
|
1373
|
+
`;
|
|
1374
|
+
static templates = {
|
|
1375
|
+
options: `
|
|
1376
|
+
<li data-tpl-each="self" data-tpl-selected="index == 0" data-tpl-value="index" role="option" data-tpl-aria-selected="index == 0 ? 'true' : 'false'">
|
|
1377
|
+
{{ label }}
|
|
1378
|
+
</li>
|
|
1379
|
+
`,
|
|
1380
|
+
};
|
|
1381
|
+
#spinner;
|
|
1382
|
+
#menu;
|
|
1383
|
+
#optionstemplate;
|
|
1384
|
+
#options = new Map();
|
|
1385
|
+
render({ slots }) {
|
|
1386
|
+
const fragment = this.template().render();
|
|
1387
|
+
this.#optionstemplate = Fragments.isBlank(slots.default)
|
|
1388
|
+
? this.template('options')
|
|
1389
|
+
: Templates.fromFragment(slots.default);
|
|
1390
|
+
this.#spinner = fragment.querySelector('ful-spinner');
|
|
1391
|
+
this.#menu = fragment.querySelector('menu');
|
|
1392
|
+
this.#menu.addEventListener('click', (evt) => {
|
|
1393
|
+
evt.stopPropagation();
|
|
1394
|
+
const li = evt.target.closest('li');
|
|
1395
|
+
if (!li) {
|
|
1396
|
+
this.hide();
|
|
1397
|
+
return;
|
|
1398
|
+
}
|
|
1399
|
+
this.#change(li);
|
|
1400
|
+
});
|
|
1401
|
+
this.replaceChildren(fragment);
|
|
1402
|
+
}
|
|
1403
|
+
acceptSelection() {
|
|
1404
|
+
const selected = this.#menu.querySelector('[selected]') ?? this.#menu.firstElementChild;
|
|
1405
|
+
this.#change(selected);
|
|
1406
|
+
}
|
|
1407
|
+
update(values) {
|
|
1408
|
+
if (values === undefined) {
|
|
1409
|
+
throw new Error('null data');
|
|
1410
|
+
}
|
|
1411
|
+
this.#options = new Map(values.map((v, i) => [String(i), v]));
|
|
1412
|
+
const data = values.map(([key, label, metadata], index) => ({ index, key, label, metadata }));
|
|
1413
|
+
this.#optionstemplate.withOverlay(data).renderTo(this.#menu);
|
|
1414
|
+
}
|
|
1415
|
+
#change(target) {
|
|
1416
|
+
const index = target.getAttribute('value');
|
|
1417
|
+
const data = this.#options.get(index);
|
|
1418
|
+
this.hide();
|
|
1419
|
+
this.dispatchEvent(
|
|
1420
|
+
new CustomEvent('change', {
|
|
1421
|
+
bubbles: true,
|
|
1422
|
+
cancelable: false,
|
|
1423
|
+
detail: { index, data },
|
|
1424
|
+
}),
|
|
1425
|
+
);
|
|
1426
|
+
}
|
|
1427
|
+
hide() {
|
|
1428
|
+
this.setAttribute('hidden', '');
|
|
1429
|
+
}
|
|
1430
|
+
get shown() {
|
|
1431
|
+
return !this.hasAttribute('hidden');
|
|
1432
|
+
}
|
|
1433
|
+
async show(loader) {
|
|
1434
|
+
this.removeAttribute('hidden');
|
|
1435
|
+
this.#menu.setAttribute('hidden', '');
|
|
1436
|
+
this.#spinner.removeAttribute('hidden');
|
|
1437
|
+
try {
|
|
1438
|
+
const data = await loader();
|
|
1439
|
+
this.update(data);
|
|
1440
|
+
} finally {
|
|
1441
|
+
this.#spinner.setAttribute('hidden', '');
|
|
1442
|
+
this.#menu.removeAttribute('hidden');
|
|
1443
|
+
}
|
|
1444
|
+
}
|
|
1445
|
+
async moveOrShow(forward, loader) {
|
|
1446
|
+
if (this.shown) {
|
|
1447
|
+
const selected = this.#menu.querySelector('[selected]') ?? this.#menu.firstElementChild;
|
|
1448
|
+
const candidate = selected[`${forward ? 'next' : 'previous'}ElementSibling`];
|
|
1449
|
+
if (candidate) {
|
|
1450
|
+
selected.removeAttribute('selected');
|
|
1451
|
+
candidate.setAttribute('selected', '');
|
|
1452
|
+
candidate.scrollIntoView({ block: 'nearest', behavior: 'smooth' });
|
|
1453
|
+
}
|
|
1454
|
+
return;
|
|
1455
|
+
}
|
|
1456
|
+
await this.show(loader);
|
|
1457
|
+
}
|
|
1458
|
+
}
|
|
1459
|
+
|
|
1460
|
+
class Select extends ParsedElement {
|
|
1461
|
+
static observed = ['value:csvm', 'readonly:presence', 'required:presence', 'itemlist:presence'];
|
|
1462
|
+
static slots = true;
|
|
1463
|
+
static template = `
|
|
1464
|
+
<div class="form-label">
|
|
1465
|
+
<label>{{{{ slots.default }}}}</label>
|
|
1466
|
+
{{{{ slots.info }}}}
|
|
1467
|
+
</div>
|
|
1468
|
+
<div class="input-group flex-nowrap" tabindex="-1">
|
|
1469
|
+
<span data-tpl-if="slots.ibefore" class="input-group-text">{{{{ slots.ibefore }}}}</span>
|
|
1470
|
+
{{{{ slots.before }}}}
|
|
1471
|
+
<div class="ful-select-input-container">
|
|
1472
|
+
<div class="ful-select-input">
|
|
1473
|
+
<badges></badges>
|
|
1474
|
+
<input type="text" form="" role="combobox" aria-autocomplete="list" aria-haspopup="listbox" aria-expanded="false">
|
|
1475
|
+
</div>
|
|
1476
|
+
<ful-dropdown hidden popover="manual">{{{{ slots.dropdown }}}}</ful-dropdown>
|
|
1477
|
+
</div>
|
|
1478
|
+
{{{{ slots.after }}}}
|
|
1479
|
+
<span data-tpl-if="slots.iafter" class="input-group-text">{{{{ slots.iafter }}}}</span>
|
|
1480
|
+
</div>
|
|
1481
|
+
<ful-item-list></ful-item-list>
|
|
1482
|
+
<ful-field-error></ful-field-error>
|
|
1483
|
+
`;
|
|
1484
|
+
static templates = {
|
|
1485
|
+
items: `
|
|
1486
|
+
<ful-item data-tpl-each="entries" data-tpl-var="entry" data-tpl-data-key="entry[0]">
|
|
1487
|
+
<div>{{ entry[1][0] }}</div>
|
|
1488
|
+
<button type="button" class="btn btn-sm btn-outline-danger bi bi-x-lg"></button>
|
|
1489
|
+
</ful-item>
|
|
1490
|
+
`,
|
|
1491
|
+
};
|
|
1492
|
+
static formAssociated = true;
|
|
1493
|
+
internals;
|
|
1494
|
+
#loader;
|
|
1495
|
+
#badges;
|
|
1496
|
+
#ddmenu;
|
|
1497
|
+
#input;
|
|
1498
|
+
#items;
|
|
1499
|
+
#multiple;
|
|
1500
|
+
#fieldError;
|
|
1501
|
+
#values = new Map();
|
|
1502
|
+
constructor() {
|
|
1503
|
+
super();
|
|
1504
|
+
this.internals = this.attachInternals();
|
|
1505
|
+
this.internals.role = 'presentation';
|
|
1506
|
+
}
|
|
1507
|
+
async render({ slots, observed, disabled }) {
|
|
1508
|
+
const name = this.getAttribute('name');
|
|
1509
|
+
this.#loader = registry
|
|
1510
|
+
.component(this.getAttribute('loader') ?? 'loaders:select')
|
|
1511
|
+
.create(this, { options: slots.options });
|
|
1512
|
+
|
|
1513
|
+
this.#multiple = this.hasAttribute('multiple');
|
|
1514
|
+
await this.#loader.prefetch?.();
|
|
1515
|
+
const fragment = this.template().withOverlay({ slots, name }).render();
|
|
1516
|
+
this.#input = fragment.querySelector('input');
|
|
1517
|
+
this.#items = fragment.querySelector('ful-item-list');
|
|
1518
|
+
Attributes.forward('input-', this, this.#input);
|
|
1519
|
+
this.#badges = fragment.querySelector('badges');
|
|
1520
|
+
|
|
1521
|
+
this.value = observed.value;
|
|
1522
|
+
this.disabled = disabled;
|
|
1523
|
+
this.readonly = observed.readonly;
|
|
1524
|
+
this.required = observed.required;
|
|
1525
|
+
this.itemlist = observed.itemlist;
|
|
1526
|
+
|
|
1527
|
+
this.#ddmenu = fragment.querySelector('ful-dropdown');
|
|
1528
|
+
const label = fragment.querySelector('label');
|
|
1529
|
+
label.addEventListener('click', () => this.focus());
|
|
1530
|
+
this.#fieldError = fragment.querySelector('ful-field-error');
|
|
1531
|
+
this.#input.ariaDescribedByElements = [this.#fieldError];
|
|
1532
|
+
this.#input.ariaLabelledByElements = [label];
|
|
1533
|
+
|
|
1534
|
+
const self = this;
|
|
1535
|
+
const [dload, abortdload] = Timing.throttle(400, () => {
|
|
1536
|
+
self.#input.setAttribute('aria-expanded', 'true');
|
|
1537
|
+
self.#ddmenu.show(() => self.#loader.load(self.#input.value));
|
|
1538
|
+
});
|
|
1539
|
+
this.addEventListener('click', (/** @type any */ e) => {
|
|
1540
|
+
if (e.target.matches('input')) {
|
|
1541
|
+
return;
|
|
1542
|
+
}
|
|
1543
|
+
if (this.disabled || this.readonly) {
|
|
1544
|
+
return;
|
|
1545
|
+
}
|
|
1546
|
+
if (this.#ddmenu.shown) {
|
|
1547
|
+
this.#input.setAttribute('aria-expanded', 'false');
|
|
1548
|
+
this.#ddmenu.hide();
|
|
1549
|
+
return;
|
|
1550
|
+
}
|
|
1551
|
+
this.#input.focus();
|
|
1552
|
+
dload();
|
|
1553
|
+
});
|
|
1554
|
+
this.#items.addEventListener('click', (e) => {
|
|
1555
|
+
e.stopPropagation();
|
|
1556
|
+
if (!e.target.closest('button')) {
|
|
1557
|
+
return;
|
|
1558
|
+
}
|
|
1559
|
+
if (this.disabled || this.readonly) {
|
|
1560
|
+
return;
|
|
1561
|
+
}
|
|
1562
|
+
const idx = [...this.#items.children].indexOf(e.target.closest('ful-item'));
|
|
1563
|
+
if (idx === -1) {
|
|
1564
|
+
return;
|
|
1565
|
+
}
|
|
1566
|
+
this.#values.delete(Array.from(this.#values.keys())[idx]);
|
|
1567
|
+
this.#changed();
|
|
1568
|
+
this.#syncBadges();
|
|
1569
|
+
});
|
|
1570
|
+
this.#badges.addEventListener('click', (e) => {
|
|
1571
|
+
e.stopPropagation();
|
|
1572
|
+
if (this.disabled || this.readonly) {
|
|
1573
|
+
return;
|
|
1574
|
+
}
|
|
1575
|
+
const idx = [...this.#badges.children].indexOf(e.target);
|
|
1576
|
+
if (idx === -1) {
|
|
1577
|
+
return;
|
|
1578
|
+
}
|
|
1579
|
+
this.#values.delete(Array.from(this.#values.keys())[idx]);
|
|
1580
|
+
this.#changed();
|
|
1581
|
+
this.#syncBadges();
|
|
1582
|
+
});
|
|
1583
|
+
this.#input.addEventListener('change', (e) => {
|
|
1584
|
+
e.stopPropagation();
|
|
1585
|
+
});
|
|
1586
|
+
this.#input.addEventListener('blur', (e) => {
|
|
1587
|
+
e.stopPropagation();
|
|
1588
|
+
if (e.relatedTarget && this.contains(e.relatedTarget)) {
|
|
1589
|
+
return;
|
|
1590
|
+
}
|
|
1591
|
+
abortdload();
|
|
1592
|
+
this.#input.setAttribute('aria-expanded', 'false');
|
|
1593
|
+
this.#ddmenu.hide();
|
|
1594
|
+
this.#input.value = '';
|
|
1595
|
+
});
|
|
1596
|
+
this.#input.addEventListener('keydown', (e) => {
|
|
1597
|
+
if (this.disabled || this.readonly) {
|
|
1598
|
+
return;
|
|
1599
|
+
}
|
|
1600
|
+
switch (e.code) {
|
|
1601
|
+
case 'ArrowUp': {
|
|
1602
|
+
e.preventDefault();
|
|
1603
|
+
this.#input.setAttribute('aria-expanded', 'true');
|
|
1604
|
+
this.#ddmenu.moveOrShow(false, () => self.#loader.load(self.#input.value));
|
|
1605
|
+
break;
|
|
1606
|
+
}
|
|
1607
|
+
case 'ArrowDown': {
|
|
1608
|
+
e.preventDefault();
|
|
1609
|
+
this.#input.setAttribute('aria-expanded', 'true');
|
|
1610
|
+
this.#ddmenu.moveOrShow(true, () => self.#loader.load(self.#input.value));
|
|
1611
|
+
break;
|
|
1612
|
+
}
|
|
1613
|
+
case 'Escape': {
|
|
1614
|
+
this.#input.setAttribute('aria-expanded', 'false');
|
|
1615
|
+
this.#ddmenu.hide();
|
|
1616
|
+
break;
|
|
1617
|
+
}
|
|
1618
|
+
case 'Enter': {
|
|
1619
|
+
e.preventDefault();
|
|
1620
|
+
this.#input.setAttribute('aria-expanded', 'false');
|
|
1621
|
+
this.#ddmenu.acceptSelection();
|
|
1622
|
+
this.#input.value = '';
|
|
1623
|
+
break;
|
|
1624
|
+
}
|
|
1625
|
+
case 'Backspace': {
|
|
1626
|
+
//remove last if caret at position 0
|
|
1627
|
+
if (this.#values.size && this.#input.selectionStart === 0 && this.#input.selectionEnd === 0) {
|
|
1628
|
+
this.#values.delete(Array.from(this.#values.keys()).pop());
|
|
1629
|
+
this.#changed();
|
|
1630
|
+
this.#syncBadges();
|
|
1631
|
+
}
|
|
1632
|
+
break;
|
|
1633
|
+
}
|
|
1634
|
+
case 'Tab': {
|
|
1635
|
+
this.#input.setAttribute('aria-expanded', 'false');
|
|
1636
|
+
this.#ddmenu.hide();
|
|
1637
|
+
abortdload();
|
|
1638
|
+
break;
|
|
1639
|
+
}
|
|
1640
|
+
}
|
|
1641
|
+
});
|
|
1642
|
+
this.#input.addEventListener('input', (e) => {
|
|
1643
|
+
e.stopPropagation();
|
|
1644
|
+
if (this.disabled || this.readonly) {
|
|
1645
|
+
return;
|
|
1646
|
+
}
|
|
1647
|
+
dload();
|
|
1648
|
+
});
|
|
1649
|
+
this.#ddmenu.addEventListener('change', (e) => {
|
|
1650
|
+
e.stopPropagation();
|
|
1651
|
+
if (!this.#multiple) {
|
|
1652
|
+
this.#values.clear();
|
|
1653
|
+
}
|
|
1654
|
+
this.#values.set(e.detail.data[0], e.detail.data.slice(1));
|
|
1655
|
+
this.#changed();
|
|
1656
|
+
this.#syncBadges();
|
|
1657
|
+
this.#input.focus();
|
|
1658
|
+
this.#input.setAttribute('aria-expanded', 'false');
|
|
1659
|
+
this.#ddmenu.hide();
|
|
1660
|
+
this.#input.value = '';
|
|
1661
|
+
});
|
|
1662
|
+
this.replaceChildren(fragment);
|
|
1663
|
+
}
|
|
1664
|
+
async withLoader(fn) {
|
|
1665
|
+
return await fn(this.#loader);
|
|
1666
|
+
}
|
|
1667
|
+
#changed() {
|
|
1668
|
+
const selection = [...this.#values.entries()].map((e) => ({
|
|
1669
|
+
key: e[0],
|
|
1670
|
+
label: e[1][0],
|
|
1671
|
+
metadata: e[1].slice(1),
|
|
1672
|
+
}));
|
|
1673
|
+
const value = this.#multiple ? selection : (selection[0] ?? null);
|
|
1674
|
+
this.dispatchEvent(
|
|
1675
|
+
new CustomEvent('change', {
|
|
1676
|
+
bubbles: true,
|
|
1677
|
+
cancelable: false,
|
|
1678
|
+
detail: { value },
|
|
1679
|
+
}),
|
|
1680
|
+
);
|
|
1681
|
+
}
|
|
1682
|
+
#syncBadges() {
|
|
1683
|
+
const badges = Array.from(this.#values.entries()).map(([k, v]) => {
|
|
1684
|
+
const b = document.createElement('badge');
|
|
1685
|
+
b.setAttribute('role', 'button');
|
|
1686
|
+
b.setAttribute('value', k);
|
|
1687
|
+
b.innerText = v[0];
|
|
1688
|
+
return b;
|
|
1689
|
+
});
|
|
1690
|
+
this.#badges.replaceChildren();
|
|
1691
|
+
this.#badges.append(...badges);
|
|
1692
|
+
this.#items.replaceChildren();
|
|
1693
|
+
this.template('items').withOverlay({ entries: this.#values.entries() }).renderTo(this.#items);
|
|
1694
|
+
}
|
|
1695
|
+
set value(vs) {
|
|
1696
|
+
if (vs === null) {
|
|
1697
|
+
this.#values = new Map();
|
|
1698
|
+
this.#syncBadges();
|
|
1699
|
+
return;
|
|
1700
|
+
}
|
|
1701
|
+
(async () => {
|
|
1702
|
+
const entries = await (this.#multiple ? this.#loader.exact(...vs) : this.#loader.exact(vs));
|
|
1703
|
+
this.#values = new Map(entries.map((e) => [e[0], e.slice(1)]));
|
|
1704
|
+
this.#syncBadges();
|
|
1705
|
+
})();
|
|
1706
|
+
}
|
|
1707
|
+
get value() {
|
|
1708
|
+
if (this.#multiple) {
|
|
1709
|
+
return [...this.#values.keys()];
|
|
1710
|
+
}
|
|
1711
|
+
return [...this.#values.keys()][0] ?? null;
|
|
1712
|
+
}
|
|
1713
|
+
get entry() {
|
|
1714
|
+
if (this.#multiple) {
|
|
1715
|
+
return [...this.#values.entries()];
|
|
1716
|
+
}
|
|
1717
|
+
return [...this.#values.entries()][0] ?? null;
|
|
1718
|
+
}
|
|
1719
|
+
get disabled() {
|
|
1720
|
+
return this.#input.hasAttribute('disabled');
|
|
1721
|
+
}
|
|
1722
|
+
set disabled(d) {
|
|
1723
|
+
Attributes.toggle(this.#input, 'disabled', d);
|
|
1724
|
+
}
|
|
1725
|
+
get readonly() {
|
|
1726
|
+
return this.#input.readOnly;
|
|
1727
|
+
}
|
|
1728
|
+
set readonly(v) {
|
|
1729
|
+
this.#input.readOnly = v;
|
|
1730
|
+
this.reflect(() => {
|
|
1731
|
+
Attributes.toggle(this, 'readonly', v);
|
|
1732
|
+
});
|
|
1733
|
+
}
|
|
1734
|
+
get required() {
|
|
1735
|
+
return this.#input.getAttribute('aria-required') === 'true';
|
|
1736
|
+
}
|
|
1737
|
+
set required(d) {
|
|
1738
|
+
Attributes.set(this.#input, 'aria-required', d ? 'true' : null);
|
|
1739
|
+
this.reflect(() => {
|
|
1740
|
+
Attributes.toggle(this, 'required', d);
|
|
1741
|
+
});
|
|
1742
|
+
}
|
|
1743
|
+
#useItemlist;
|
|
1744
|
+
get itemlist() {
|
|
1745
|
+
return this.#useItemlist;
|
|
1746
|
+
}
|
|
1747
|
+
set itemlist(v) {
|
|
1748
|
+
this.#useItemlist = v;
|
|
1749
|
+
this.reflect(() => {
|
|
1750
|
+
Attributes.toggle(this, 'itemlist', v);
|
|
1751
|
+
});
|
|
1752
|
+
}
|
|
1753
|
+
focus(options) {
|
|
1754
|
+
this.#input.focus(options);
|
|
1755
|
+
}
|
|
1756
|
+
setCustomValidity(error) {
|
|
1757
|
+
if (!error) {
|
|
1758
|
+
this.internals.setValidity({});
|
|
1759
|
+
this.#fieldError.innerText = '';
|
|
1760
|
+
return;
|
|
1761
|
+
}
|
|
1762
|
+
this.internals.setValidity({ customError: true }, ' ');
|
|
1763
|
+
this.#fieldError.innerText = error;
|
|
1764
|
+
}
|
|
1765
|
+
}
|
|
1766
|
+
|
|
1767
|
+
class RadioGroup extends ParsedElement {
|
|
1768
|
+
static observed = ['value', 'readonly:presence', 'required:presence'];
|
|
1769
|
+
static slots = true;
|
|
1770
|
+
static template = `
|
|
1771
|
+
<fieldset>
|
|
1772
|
+
<legend class="form-label">
|
|
1773
|
+
{{{{ slots.default }}}}
|
|
1774
|
+
</legend>
|
|
1775
|
+
<header data-tpl-if="slots.header">
|
|
1776
|
+
{{{{ slots.header }}}}
|
|
1777
|
+
</header>
|
|
1778
|
+
<section>
|
|
1779
|
+
<div class="label-wrapper" data-tpl-each="inputsAndLabels" data-tpl-var="ial">
|
|
1780
|
+
<label>
|
|
1781
|
+
{{{{ ial[0] }}}}
|
|
1782
|
+
<div>{{{{ ial[1] }}}}</div>
|
|
1783
|
+
</label>
|
|
1784
|
+
</div>
|
|
1785
|
+
</section>
|
|
1786
|
+
<ful-field-error></ful-field-error>
|
|
1787
|
+
<footer data-tpl-if="slots.footer">
|
|
1788
|
+
{{{{ slots.footer }}}}
|
|
1789
|
+
</footer>
|
|
1790
|
+
</fieldset>
|
|
1791
|
+
`;
|
|
1792
|
+
static formAssociated = true;
|
|
1793
|
+
#fieldset;
|
|
1794
|
+
#fieldError;
|
|
1795
|
+
#firstRadio;
|
|
1796
|
+
#booleanType;
|
|
1797
|
+
constructor() {
|
|
1798
|
+
super();
|
|
1799
|
+
this.internals = this.attachInternals();
|
|
1800
|
+
this.internals.role = 'radiogroup';
|
|
1801
|
+
}
|
|
1802
|
+
render({ slots, observed, disabled }) {
|
|
1803
|
+
const name = this.getAttribute('name') ?? Attributes.uid('ful-radiogroup');
|
|
1804
|
+
const radioEls = Array.from(slots.default.querySelectorAll('ful-radio'));
|
|
1805
|
+
const inputsAndLabels = radioEls.map((el) => {
|
|
1806
|
+
const input = document.createElement('input');
|
|
1807
|
+
input.setAttribute('type', 'radio');
|
|
1808
|
+
Attributes.forward('input-', this, input);
|
|
1809
|
+
Attributes.forward('', el, input);
|
|
1810
|
+
input.setAttribute('name', `${name}-ignore`);
|
|
1811
|
+
input.setAttribute('form', ``);
|
|
1812
|
+
input.addEventListener('change', (evt) => {
|
|
1813
|
+
evt.stopPropagation();
|
|
1814
|
+
//change is not cancelable
|
|
1815
|
+
this.dispatchEvent(
|
|
1816
|
+
new CustomEvent('change', {
|
|
1817
|
+
bubbles: true,
|
|
1818
|
+
cancelable: false,
|
|
1819
|
+
detail: {
|
|
1820
|
+
value: this.value,
|
|
1821
|
+
},
|
|
1822
|
+
}),
|
|
1823
|
+
);
|
|
1824
|
+
});
|
|
1825
|
+
const label = Fragments.fromChildNodes(el);
|
|
1826
|
+
return [input, label];
|
|
1827
|
+
});
|
|
1828
|
+
|
|
1829
|
+
radioEls.forEach((el) => el.remove());
|
|
1830
|
+
this.template().withOverlay({ name, slots, inputsAndLabels }).renderTo(this);
|
|
1831
|
+
this.#fieldset = this.firstElementChild;
|
|
1832
|
+
this.disabled = disabled;
|
|
1833
|
+
this.readonly = observed.readonly;
|
|
1834
|
+
this.required = observed.required;
|
|
1835
|
+
this.value = observed.value;
|
|
1836
|
+
this.#fieldError = this.querySelector('ful-field-error');
|
|
1837
|
+
this.ariaDescribedByElements = [this.#fieldError];
|
|
1838
|
+
this.#firstRadio = this.querySelector('input[type=radio]');
|
|
1839
|
+
this.#booleanType = this.getAttribute('type') === 'boolean';
|
|
1840
|
+
}
|
|
1841
|
+
get value() {
|
|
1842
|
+
/** @type {HTMLInputElement|null} */
|
|
1843
|
+
const checked = this.querySelector('input[type=radio]:checked');
|
|
1844
|
+
return checked ? (this.#booleanType ? checked.value === 'true' : checked.value) : null;
|
|
1845
|
+
}
|
|
1846
|
+
set value(value) {
|
|
1847
|
+
if (value === null) {
|
|
1848
|
+
this.querySelectorAll(`input[type=radio]`).forEach((el) => {
|
|
1849
|
+
/** @type {HTMLInputElement} */ (el).checked = false;
|
|
1850
|
+
});
|
|
1851
|
+
return;
|
|
1852
|
+
}
|
|
1853
|
+
/** @type {HTMLInputElement|null} */
|
|
1854
|
+
const el = this.querySelector(`input[type=radio][value=${CSS.escape(String(value))}]`);
|
|
1855
|
+
if (el) {
|
|
1856
|
+
el.checked = true;
|
|
1857
|
+
}
|
|
1858
|
+
}
|
|
1859
|
+
get readonly() {
|
|
1860
|
+
return this.#fieldset.inert;
|
|
1861
|
+
}
|
|
1862
|
+
set readonly(v) {
|
|
1863
|
+
this.#fieldset.inert = v;
|
|
1864
|
+
this.reflect(() => {
|
|
1865
|
+
Attributes.toggle(this, 'readonly', v);
|
|
1866
|
+
});
|
|
1867
|
+
}
|
|
1868
|
+
get disabled() {
|
|
1869
|
+
return this.#fieldset.hasAttribute('disabled');
|
|
1870
|
+
}
|
|
1871
|
+
set disabled(d) {
|
|
1872
|
+
Attributes.toggle(this.#fieldset, 'disabled', d);
|
|
1873
|
+
}
|
|
1874
|
+
get required() {
|
|
1875
|
+
return this.#fieldset.getAttribute('aria-required') === 'true';
|
|
1876
|
+
}
|
|
1877
|
+
set required(d) {
|
|
1878
|
+
Attributes.set(this.#fieldset, 'aria-required', d ? 'true' : null);
|
|
1879
|
+
this.reflect(() => {
|
|
1880
|
+
Attributes.toggle(this, 'required', d);
|
|
1881
|
+
});
|
|
1882
|
+
}
|
|
1883
|
+
focus(options) {
|
|
1884
|
+
this.#firstRadio.focus(options);
|
|
1885
|
+
}
|
|
1886
|
+
setCustomValidity(error) {
|
|
1887
|
+
if (!error) {
|
|
1888
|
+
this.internals.setValidity({});
|
|
1889
|
+
this.#fieldError.innerText = '';
|
|
1890
|
+
return;
|
|
1891
|
+
}
|
|
1892
|
+
this.internals.setValidity({ customError: true }, ' ');
|
|
1893
|
+
this.#fieldError.innerText = error;
|
|
1894
|
+
}
|
|
1895
|
+
}
|
|
1896
|
+
|
|
1897
|
+
class Checkbox extends ParsedElement {
|
|
1898
|
+
static observed = ['value:bool', 'readonly:presence', 'required:presence'];
|
|
1899
|
+
static slots = true;
|
|
1900
|
+
static template = `
|
|
1901
|
+
<div data-tpl-class="klass">
|
|
1902
|
+
<div class="input-container">
|
|
1903
|
+
<input class="form-check-input" type="checkbox" data-tpl-role="isSwitch ? 'switch' : false" form="" placeholder=" ">
|
|
1904
|
+
</div>
|
|
1905
|
+
<div class="form-check-label">
|
|
1906
|
+
<label>{{{{ slots.default }}}}</label>
|
|
1907
|
+
{{{{ slots.info }}}}
|
|
1908
|
+
</div>
|
|
1909
|
+
</div>
|
|
1910
|
+
<ful-field-error></ful-field-error>
|
|
1911
|
+
`;
|
|
1912
|
+
#container;
|
|
1913
|
+
#input;
|
|
1914
|
+
#fieldError;
|
|
1915
|
+
static formAssociated = true;
|
|
1916
|
+
constructor() {
|
|
1917
|
+
super();
|
|
1918
|
+
this.internals = this.attachInternals();
|
|
1919
|
+
this.internals.role = 'presentation';
|
|
1920
|
+
}
|
|
1921
|
+
render({ slots, observed, disabled }) {
|
|
1922
|
+
const isSwitch = this.getAttribute('type') == 'switch';
|
|
1923
|
+
const klass = isSwitch ? 'form-check form-switch' : 'form-check';
|
|
1924
|
+
const fragment = this.template().withOverlay({ slots, klass, isSwitch }).render();
|
|
1925
|
+
this.#container = fragment.firstElementChild;
|
|
1926
|
+
this.#input = fragment.querySelector('input');
|
|
1927
|
+
Attributes.forward('input-', this, this.#input);
|
|
1928
|
+
this.disabled = disabled;
|
|
1929
|
+
this.readonly = observed.readonly;
|
|
1930
|
+
this.required = observed.required;
|
|
1931
|
+
this.value = observed.value;
|
|
1932
|
+
this.#input.addEventListener('change', (evt) => {
|
|
1933
|
+
evt.stopPropagation();
|
|
1934
|
+
this.dispatchEvent(
|
|
1935
|
+
new CustomEvent('change', {
|
|
1936
|
+
bubbles: true,
|
|
1937
|
+
cancelable: false,
|
|
1938
|
+
detail: {
|
|
1939
|
+
value: this.value,
|
|
1940
|
+
},
|
|
1941
|
+
}),
|
|
1942
|
+
);
|
|
1943
|
+
});
|
|
1944
|
+
const label = fragment.querySelector('label');
|
|
1945
|
+
label.addEventListener('click', () => {
|
|
1946
|
+
this.focus();
|
|
1947
|
+
if (this.disabled || this.readonly) {
|
|
1948
|
+
return;
|
|
1949
|
+
}
|
|
1950
|
+
this.value = !this.value;
|
|
1951
|
+
this.dispatchEvent(
|
|
1952
|
+
new CustomEvent('change', {
|
|
1953
|
+
bubbles: true,
|
|
1954
|
+
cancelable: false,
|
|
1955
|
+
detail: {
|
|
1956
|
+
value: this.value,
|
|
1957
|
+
},
|
|
1958
|
+
}),
|
|
1959
|
+
);
|
|
1960
|
+
});
|
|
1961
|
+
this.#fieldError = fragment.querySelector('ful-field-error');
|
|
1962
|
+
this.#input.ariaDescribedByElements = [this.#fieldError];
|
|
1963
|
+
this.#input.ariaLabelledByElements = [label];
|
|
1964
|
+
this.replaceChildren(fragment);
|
|
1965
|
+
}
|
|
1966
|
+
get value() {
|
|
1967
|
+
return this.#input.checked;
|
|
1968
|
+
}
|
|
1969
|
+
set value(value) {
|
|
1970
|
+
this.#input.checked = value;
|
|
1971
|
+
}
|
|
1972
|
+
get readonly() {
|
|
1973
|
+
return this.#container.inert;
|
|
1974
|
+
}
|
|
1975
|
+
set readonly(v) {
|
|
1976
|
+
this.#container.inert = v;
|
|
1977
|
+
this.reflect(() => {
|
|
1978
|
+
Attributes.toggle(this, 'readonly', v);
|
|
1979
|
+
});
|
|
1980
|
+
}
|
|
1981
|
+
get disabled() {
|
|
1982
|
+
return this.#input.hasAttribute('disabled');
|
|
1983
|
+
}
|
|
1984
|
+
set disabled(d) {
|
|
1985
|
+
Attributes.toggle(this.#input, 'disabled', d);
|
|
1986
|
+
}
|
|
1987
|
+
get required() {
|
|
1988
|
+
return this.#input.getAttribute('aria-required') === 'true';
|
|
1989
|
+
}
|
|
1990
|
+
set required(d) {
|
|
1991
|
+
Attributes.set(this.#input, 'aria-required', d ? 'true' : null);
|
|
1992
|
+
this.reflect(() => {
|
|
1993
|
+
Attributes.toggle(this, 'required', d);
|
|
1994
|
+
});
|
|
1995
|
+
}
|
|
1996
|
+
focus(options) {
|
|
1997
|
+
this.#input.focus(options);
|
|
1998
|
+
}
|
|
1999
|
+
setCustomValidity(error) {
|
|
2000
|
+
if (!error) {
|
|
2001
|
+
this.internals.setValidity({});
|
|
2002
|
+
this.#fieldError.innerText = '';
|
|
2003
|
+
return;
|
|
2004
|
+
}
|
|
2005
|
+
this.internals.setValidity({ customError: true }, ' ');
|
|
2006
|
+
this.#fieldError.innerText = error;
|
|
2007
|
+
}
|
|
2008
|
+
}
|
|
2009
|
+
|
|
2010
|
+
class Spinner extends ParsedElement {
|
|
2011
|
+
static slots = true;
|
|
2012
|
+
static template = `
|
|
2013
|
+
<div class="ful-spinner-wrapper" role="status">
|
|
2014
|
+
<div class="ful-spinner-text">{{{{ slots.default }}}}</div>
|
|
2015
|
+
<div class="ful-spinner-icon"></div>
|
|
2016
|
+
</div>
|
|
2017
|
+
`;
|
|
2018
|
+
render({ slots }) {
|
|
2019
|
+
this.template().withOverlay({ slots }).renderTo(this);
|
|
2020
|
+
}
|
|
2021
|
+
}
|
|
2022
|
+
|
|
2023
|
+
class SortButton extends ParsedElement {
|
|
2024
|
+
static observed = ['order'];
|
|
2025
|
+
#order;
|
|
2026
|
+
render() {
|
|
2027
|
+
const sorter = this.getAttribute('sorter');
|
|
2028
|
+
const orders = ['asc', 'desc', null];
|
|
2029
|
+
this.addEventListener('click', () => {
|
|
2030
|
+
const nextOrder = orders[(orders.indexOf(this.order) + 1) % 3];
|
|
2031
|
+
this.dispatchEvent(
|
|
2032
|
+
new CustomEvent('sort-requested', {
|
|
2033
|
+
bubbles: true,
|
|
2034
|
+
cancelable: true,
|
|
2035
|
+
detail: {
|
|
2036
|
+
value: { sorter, order: nextOrder },
|
|
2037
|
+
},
|
|
2038
|
+
}),
|
|
2039
|
+
);
|
|
2040
|
+
});
|
|
2041
|
+
}
|
|
2042
|
+
|
|
2043
|
+
get order() {
|
|
2044
|
+
return this.#order || null;
|
|
2045
|
+
}
|
|
2046
|
+
|
|
2047
|
+
set order(value) {
|
|
2048
|
+
this.#order = value || null;
|
|
2049
|
+
this.reflect(() => {
|
|
2050
|
+
if (this.#order) {
|
|
2051
|
+
this.setAttribute('order', value);
|
|
2052
|
+
} else {
|
|
2053
|
+
this.removeAttribute('order');
|
|
2054
|
+
}
|
|
2055
|
+
});
|
|
2056
|
+
}
|
|
2057
|
+
}
|
|
2058
|
+
|
|
2059
|
+
class Pagination extends ParsedElement {
|
|
2060
|
+
static observed = ['total:number', 'current:number'];
|
|
2061
|
+
static l10n = {
|
|
2062
|
+
en: {
|
|
2063
|
+
showing: 'Page {0} of {1}',
|
|
2064
|
+
navigation: 'Page navigation',
|
|
2065
|
+
previous: 'Previous',
|
|
2066
|
+
next: 'Next',
|
|
2067
|
+
},
|
|
2068
|
+
it: {
|
|
2069
|
+
showing: 'Pagina {0} di {1}',
|
|
2070
|
+
navigation: 'Navigazione pagine',
|
|
2071
|
+
previous: 'Precedente',
|
|
2072
|
+
next: 'Successivo',
|
|
2073
|
+
},
|
|
2074
|
+
es: {
|
|
2075
|
+
showing: 'Página {0} de {1}',
|
|
2076
|
+
navigation: 'Navegación de páginas',
|
|
2077
|
+
previous: 'Anterior',
|
|
2078
|
+
next: 'Siguiente',
|
|
2079
|
+
},
|
|
2080
|
+
fr: {
|
|
2081
|
+
showing: 'Page {0} sur {1}',
|
|
2082
|
+
navigation: 'Navigation des pages',
|
|
2083
|
+
previous: 'Précédent',
|
|
2084
|
+
next: 'Suivant',
|
|
2085
|
+
},
|
|
2086
|
+
};
|
|
2087
|
+
static config = {
|
|
2088
|
+
prevIcon: 'bi bi-chevron-left',
|
|
2089
|
+
nextIcon: 'bi bi-chevron-right',
|
|
2090
|
+
reloadIcon: 'bi bi-arrow-clockwise',
|
|
2091
|
+
};
|
|
2092
|
+
static template = `
|
|
2093
|
+
<nav data-tpl-aria-label="#l10n:t('navigation')" class="user-select-none">
|
|
2094
|
+
<ul class="pagination">
|
|
2095
|
+
<li class="page-item ms-auto me-2 pagination-index"> {{ #l10n:t('showing', curr.label, total) }}</li>
|
|
2096
|
+
<li class="page-item me-2 reload"><a role="button"><i data-tpl-class="config.reloadIcon"></i></a></li>
|
|
2097
|
+
<li class="page-item prev">
|
|
2098
|
+
<a data-tpl-class="prev.enabled?'page-link':'page-link disabled'" data-tpl-aria-label="#l10n:t('previous')" role="button" data-tpl-data-page="prev.index">
|
|
2099
|
+
<i aria-hidden="true" data-tpl-class="config.prevIcon"></i>
|
|
2100
|
+
</a>
|
|
2101
|
+
</li>
|
|
2102
|
+
<li class="page-item" data-tpl-each="pages" data-tpl-var="page">
|
|
2103
|
+
<a data-tpl-class="curr.index != page.index ? 'page-link': 'page-link disabled'" role="button" data-tpl-data-page="page.index" >
|
|
2104
|
+
{{ page.label }}
|
|
2105
|
+
</a>
|
|
2106
|
+
</li>
|
|
2107
|
+
<li class="page-item next">
|
|
2108
|
+
<a data-tpl-class="next.enabled?'page-link':'page-link disabled'" data-tpl-aria-label="#l10n:t('next')" role="button" data-tpl-data-page="next.index">
|
|
2109
|
+
<i aria-hidden="true" data-tpl-class="config.nextIcon"></i>
|
|
2110
|
+
</a>
|
|
2111
|
+
</li>
|
|
2112
|
+
</ul>
|
|
2113
|
+
</nav>
|
|
2114
|
+
`;
|
|
2115
|
+
#total = 0;
|
|
2116
|
+
#current = 0;
|
|
2117
|
+
render({ observed }) {
|
|
2118
|
+
this.update(observed.current ?? 0, observed.total ?? 0);
|
|
2119
|
+
this.addEventListener('click', (/** @type any */ evt) => {
|
|
2120
|
+
const el = evt.target.closest('a');
|
|
2121
|
+
if (!el) {
|
|
2122
|
+
return;
|
|
2123
|
+
}
|
|
2124
|
+
this.dispatchEvent(
|
|
2125
|
+
new CustomEvent('page-requested', {
|
|
2126
|
+
bubbles: true,
|
|
2127
|
+
cancelable: true,
|
|
2128
|
+
detail: {
|
|
2129
|
+
value: Number(el.dataset.page ?? this.#current),
|
|
2130
|
+
},
|
|
2131
|
+
}),
|
|
2132
|
+
);
|
|
2133
|
+
});
|
|
2134
|
+
}
|
|
2135
|
+
update(current, total) {
|
|
2136
|
+
const maxRender = Number(this.getAttribute('pages') ?? '5');
|
|
2137
|
+
const prev = { index: Math.max(0, current - 1), enabled: current > 0 };
|
|
2138
|
+
const curr = { index: current, label: current + 1 };
|
|
2139
|
+
const next = { index: Math.min(total, current + 1), enabled: current + 1 < total };
|
|
2140
|
+
const pages = [
|
|
2141
|
+
{
|
|
2142
|
+
index: current,
|
|
2143
|
+
label: current + 1,
|
|
2144
|
+
},
|
|
2145
|
+
];
|
|
2146
|
+
for (let mid = current, offset = 1; offset !== maxRender && pages.length != maxRender; ++offset) {
|
|
2147
|
+
const p = mid - offset;
|
|
2148
|
+
if (p >= 0) {
|
|
2149
|
+
pages.unshift({ index: p, label: p + 1 });
|
|
2150
|
+
}
|
|
2151
|
+
const n = mid + offset;
|
|
2152
|
+
if (n < total) {
|
|
2153
|
+
pages.push({ index: n, label: n + 1 });
|
|
2154
|
+
}
|
|
2155
|
+
}
|
|
2156
|
+
this.template().withOverlay({ total, prev, curr, next, pages }).renderTo(this);
|
|
2157
|
+
}
|
|
2158
|
+
get total() {
|
|
2159
|
+
return this.#total;
|
|
2160
|
+
}
|
|
2161
|
+
set total(value) {
|
|
2162
|
+
this.#total = value;
|
|
2163
|
+
this.reflect(() => {
|
|
2164
|
+
this.setAttribute('total', String(value));
|
|
2165
|
+
this.update(this.#current ?? 0, this.#total);
|
|
2166
|
+
});
|
|
2167
|
+
}
|
|
2168
|
+
get current() {
|
|
2169
|
+
return this.#current;
|
|
2170
|
+
}
|
|
2171
|
+
set current(value) {
|
|
2172
|
+
this.#current = value;
|
|
2173
|
+
this.reflect(() => {
|
|
2174
|
+
this.setAttribute('current', String(value));
|
|
2175
|
+
this.update(this.#current, this.#total ?? 0);
|
|
2176
|
+
});
|
|
2177
|
+
}
|
|
2178
|
+
}
|
|
2179
|
+
|
|
2180
|
+
class TableSchemaParser {
|
|
2181
|
+
static parse(nodeOrFragment, template) {
|
|
2182
|
+
const schema = Nodes.queryChildren(nodeOrFragment, 'schema');
|
|
2183
|
+
if (!schema) {
|
|
2184
|
+
throw new Error(`missing expected <schema> in ${nodeOrFragment}`);
|
|
2185
|
+
}
|
|
2186
|
+
const headersTr = document.createElement('tr');
|
|
2187
|
+
const rowsTr = document.createElement('tr');
|
|
2188
|
+
rowsTr.setAttribute('data-tpl-each', 'rows');
|
|
2189
|
+
for (const attr of schema.getAttributeNames()) {
|
|
2190
|
+
const value = schema.getAttribute(attr);
|
|
2191
|
+
headersTr.setAttribute(attr, value ?? '');
|
|
2192
|
+
rowsTr.setAttribute(attr, value ?? '');
|
|
2193
|
+
}
|
|
2194
|
+
const columns = Nodes.queryChildrenAll(schema, 'column');
|
|
2195
|
+
const sort =
|
|
2196
|
+
columns
|
|
2197
|
+
.filter((v) => v.hasAttribute('order'))
|
|
2198
|
+
.map((v) => ({ sorter: v.getAttribute('sorter'), order: v.getAttribute('order') }))[0] ?? null;
|
|
2199
|
+
for (var column of columns) {
|
|
2200
|
+
const maybeTitleTag = Nodes.queryChildren(column, 'title');
|
|
2201
|
+
const sorter = column.getAttribute('sorter');
|
|
2202
|
+
const order = column.getAttribute('order');
|
|
2203
|
+
const titleNode = maybeTitleTag ?? document.createTextNode(column.getAttribute('title') ?? '');
|
|
2204
|
+
maybeTitleTag?.remove();
|
|
2205
|
+
column.removeAttribute('sorter');
|
|
2206
|
+
column.removeAttribute('order');
|
|
2207
|
+
column.removeAttribute('title');
|
|
2208
|
+
const wrappedTitleNode =
|
|
2209
|
+
!sorter && !order
|
|
2210
|
+
? titleNode
|
|
2211
|
+
: (() => {
|
|
2212
|
+
const fulSorter = document.createElement('ful-sorter');
|
|
2213
|
+
if (sorter) {
|
|
2214
|
+
fulSorter.setAttribute('sorter', sorter);
|
|
2215
|
+
}
|
|
2216
|
+
if (order) {
|
|
2217
|
+
fulSorter.setAttribute('order', order);
|
|
2218
|
+
}
|
|
2219
|
+
fulSorter.append(titleNode);
|
|
2220
|
+
return fulSorter;
|
|
2221
|
+
})();
|
|
2222
|
+
const th = document.createElement('th');
|
|
2223
|
+
const td = document.createElement('td');
|
|
2224
|
+
for (const attr of column.getAttributeNames()) {
|
|
2225
|
+
const value = column.getAttribute(attr);
|
|
2226
|
+
th.setAttribute(attr, value ?? '');
|
|
2227
|
+
td.setAttribute(attr, value ?? '');
|
|
2228
|
+
}
|
|
2229
|
+
th.append(wrappedTitleNode);
|
|
2230
|
+
td.append(...column.childNodes);
|
|
2231
|
+
headersTr.append(th);
|
|
2232
|
+
rowsTr.append(td);
|
|
2233
|
+
}
|
|
2234
|
+
|
|
2235
|
+
return {
|
|
2236
|
+
headersTemplate: template
|
|
2237
|
+
.withOverlay({ inHeaders: true, inRows: false })
|
|
2238
|
+
.withFragment(Fragments.from(headersTr)),
|
|
2239
|
+
rowsTemplate: template.withOverlay({ inHeaders: false, inRows: true }).withFragment(Fragments.from(rowsTr)),
|
|
2240
|
+
sort: sort,
|
|
2241
|
+
length: columns.length,
|
|
2242
|
+
};
|
|
2243
|
+
}
|
|
2244
|
+
}
|
|
2245
|
+
|
|
2246
|
+
class InMemoryTableLoader {
|
|
2247
|
+
#data;
|
|
2248
|
+
constructor(data) {
|
|
2249
|
+
this.#data = data;
|
|
2250
|
+
}
|
|
2251
|
+
async load(pageRequest, sortRequest, filterRequest) {
|
|
2252
|
+
const begin = pageRequest.page * pageRequest.size;
|
|
2253
|
+
const end = begin + pageRequest.size;
|
|
2254
|
+
const page = this.#data.slice(begin, end);
|
|
2255
|
+
const totalElements = this.#data.length;
|
|
2256
|
+
return {
|
|
2257
|
+
data: page,
|
|
2258
|
+
size: totalElements
|
|
2259
|
+
};
|
|
2260
|
+
}
|
|
2261
|
+
update(data) {
|
|
2262
|
+
this.#data = data;
|
|
2263
|
+
}
|
|
2264
|
+
}
|
|
2265
|
+
|
|
2266
|
+
class RemoteTableLoader {
|
|
2267
|
+
#http;
|
|
2268
|
+
#url;
|
|
2269
|
+
#method;
|
|
2270
|
+
constructor(http, url, method) {
|
|
2271
|
+
this.#http = http;
|
|
2272
|
+
this.#url = url;
|
|
2273
|
+
this.#method = method;
|
|
2274
|
+
}
|
|
2275
|
+
async load(pageRequest, sortRequest, filterRequest) {
|
|
2276
|
+
const filters = Object.entries(filterRequest).filter(([k, v]) => v);
|
|
2277
|
+
return await this.#http
|
|
2278
|
+
.request(this.#method, this.#url)
|
|
2279
|
+
.param('page', pageRequest.page)
|
|
2280
|
+
.param('size', pageRequest.size)
|
|
2281
|
+
.param('sort', sortRequest ? `${sortRequest.sorter},${sortRequest.order}` : null)
|
|
2282
|
+
.param('filters', filters.length > 0 ? JSON.stringify(Object.fromEntries(filters)) : null)
|
|
2283
|
+
.fetchJson();
|
|
2284
|
+
}
|
|
2285
|
+
}
|
|
2286
|
+
|
|
2287
|
+
class TableLoader {
|
|
2288
|
+
static create(el, conf) {
|
|
2289
|
+
const url = el.getAttribute('src');
|
|
2290
|
+
if (url) {
|
|
2291
|
+
const http = registry.component('http-client');
|
|
2292
|
+
const method = el.getAttribute('method') ?? 'GET';
|
|
2293
|
+
return new RemoteTableLoader(http, url, method);
|
|
2294
|
+
}
|
|
2295
|
+
return new InMemoryTableLoader([]);
|
|
2296
|
+
}
|
|
2297
|
+
}
|
|
2298
|
+
|
|
2299
|
+
class Table extends ParsedElement {
|
|
2300
|
+
static slots = true;
|
|
2301
|
+
static l10n = {
|
|
2302
|
+
en: {
|
|
2303
|
+
initial: 'Start searching to see results.',
|
|
2304
|
+
error: 'Error while loading data:',
|
|
2305
|
+
nodata: 'No elements found.',
|
|
2306
|
+
},
|
|
2307
|
+
it: {
|
|
2308
|
+
initial: 'Avvia la ricerca per visualizzare i risultati.',
|
|
2309
|
+
error: 'Errore nel caricamento dei dati:',
|
|
2310
|
+
nodata: 'Nessun elemento trovato.',
|
|
2311
|
+
},
|
|
2312
|
+
es: {
|
|
2313
|
+
initial: 'Inicia la búsqueda para ver los resultados.',
|
|
2314
|
+
error: 'Error al cargar los datos:',
|
|
2315
|
+
nodata: 'No se encontraron elementos.',
|
|
2316
|
+
},
|
|
2317
|
+
fr: {
|
|
2318
|
+
initial: 'Lancez la recherche pour voir les résultats.',
|
|
2319
|
+
error: 'Erreur lors du chargement des données :',
|
|
2320
|
+
nodata: 'Aucun élément trouvé.',
|
|
2321
|
+
},
|
|
2322
|
+
};
|
|
2323
|
+
static config = {
|
|
2324
|
+
searchIcon: 'bi bi-search',
|
|
2325
|
+
};
|
|
2326
|
+
static template = `
|
|
2327
|
+
<ful-form data-tpl-if="slots.filters">
|
|
2328
|
+
{{{{ slots.filters }}}}
|
|
2329
|
+
</ful-form>
|
|
2330
|
+
<div class="table-wrapper">
|
|
2331
|
+
<table class="table">
|
|
2332
|
+
<caption data-tpl-if="slots.caption">{{{{ slots.caption }}}}</caption>
|
|
2333
|
+
<thead></thead>
|
|
2334
|
+
<tbody></tbody>
|
|
2335
|
+
<tbody data-ref="initial">
|
|
2336
|
+
<tr>
|
|
2337
|
+
<td data-tpl-colspan="schema.length">
|
|
2338
|
+
<div>
|
|
2339
|
+
<p data-tpl-if="config.searchIcon"><i data-tpl-class="config.searchIcon"></i></p>
|
|
2340
|
+
{{{ #l10n:t('initial') }}}
|
|
2341
|
+
</div>
|
|
2342
|
+
</td>
|
|
2343
|
+
</tr>
|
|
2344
|
+
</tbody>
|
|
2345
|
+
<tbody data-ref="loading" hidden>
|
|
2346
|
+
<tr>
|
|
2347
|
+
<td data-tpl-colspan="schema.length">
|
|
2348
|
+
<ful-spinner class="big"></ful-spinner>
|
|
2349
|
+
</td>
|
|
2350
|
+
</tr>
|
|
2351
|
+
</tbody>
|
|
2352
|
+
<tbody data-ref="feedback" hidden>
|
|
2353
|
+
<tr>
|
|
2354
|
+
<td data-tpl-colspan="schema.length">
|
|
2355
|
+
<div class="alert alert-danger">
|
|
2356
|
+
<p>{{ #l10n:t('error') }}</p>
|
|
2357
|
+
<div data-ref="feedback-error"></div>
|
|
2358
|
+
</div>
|
|
2359
|
+
</td>
|
|
2360
|
+
</tr>
|
|
2361
|
+
</tbody>
|
|
2362
|
+
<tfoot data-tpl-if="slots.footer">
|
|
2363
|
+
{{{{ slots.footer }}}}
|
|
2364
|
+
</tfoot>
|
|
2365
|
+
</table>
|
|
2366
|
+
</div>
|
|
2367
|
+
<ful-pagination current="0" total="1"></ful-pagination>
|
|
2368
|
+
`;
|
|
2369
|
+
static templates = {
|
|
2370
|
+
row: `
|
|
2371
|
+
<tr data-tpl-if="pageResponse.data.length == 0">
|
|
2372
|
+
<td data-tpl-colspan="schema.length" class="text-center align-middle p-4">
|
|
2373
|
+
{{ #l10n:t('nodata') }}
|
|
2374
|
+
</td>
|
|
2375
|
+
</tr>
|
|
2376
|
+
{{{{ schema.rowsTemplate.withOverlay({'rows': pageResponse.data}).render() }}}}
|
|
2377
|
+
`,
|
|
2378
|
+
};
|
|
2379
|
+
#loader;
|
|
2380
|
+
#schema;
|
|
2381
|
+
#body;
|
|
2382
|
+
#loading;
|
|
2383
|
+
#noAutoload;
|
|
2384
|
+
#feedback;
|
|
2385
|
+
#paginator;
|
|
2386
|
+
#sorters;
|
|
2387
|
+
#latestRequest;
|
|
2388
|
+
async render({ slots, observed }) {
|
|
2389
|
+
const template = this.template();
|
|
2390
|
+
const schema = TableSchemaParser.parse(slots.schema, template);
|
|
2391
|
+
const fragment = template.withOverlay({ slots, schema }).render();
|
|
2392
|
+
const tableWrapper = /** @type HTMLTableElement */ (Nodes.queryChildren(fragment, '.table-wrapper'));
|
|
2393
|
+
const table = /** @type HTMLTableElement */ (tableWrapper.querySelector('table'));
|
|
2394
|
+
Attributes.forward('table-', this, table);
|
|
2395
|
+
this.#loader = registry.component(this.getAttribute('loader') ?? 'loaders:table').create(this);
|
|
2396
|
+
|
|
2397
|
+
this.#schema = schema;
|
|
2398
|
+
this.#body = table.querySelector(':scope > tbody');
|
|
2399
|
+
this.#loading = table.querySelector(':scope > tbody[data-ref=loading]');
|
|
2400
|
+
this.#noAutoload = table.querySelector(':scope > tbody[data-ref=initial]');
|
|
2401
|
+
this.#feedback = table.querySelector(':scope > tbody[data-ref=feedback]');
|
|
2402
|
+
this.#paginator = Nodes.queryChildren(fragment, 'ful-pagination');
|
|
2403
|
+
this.#sorters = table.querySelectorAll(':scope > thead ful-sorter') ?? [];
|
|
2404
|
+
this.replaceChildren(fragment);
|
|
2405
|
+
schema.headersTemplate.renderTo(this.querySelector('thead'));
|
|
2406
|
+
await Rendering.waitForChildren(this);
|
|
2407
|
+
|
|
2408
|
+
const maybeForm = /** @type any */ (Nodes.queryChildren(this, 'ful-form'));
|
|
2409
|
+
this.#latestRequest = {
|
|
2410
|
+
pageRequest: {
|
|
2411
|
+
page: 0,
|
|
2412
|
+
size: this.getAttribute('page-size') ? Number(this.getAttribute('page-size')) : 10,
|
|
2413
|
+
},
|
|
2414
|
+
sortRequest: schema.sort,
|
|
2415
|
+
filterRequest: maybeForm?.values ?? {},
|
|
2416
|
+
};
|
|
2417
|
+
maybeForm?.addEventListener('submit:success', async (evt) => {
|
|
2418
|
+
await this.load(
|
|
2419
|
+
{
|
|
2420
|
+
page: 0,
|
|
2421
|
+
size: this.#latestRequest.pageRequest.size,
|
|
2422
|
+
},
|
|
2423
|
+
this.#latestRequest.sortRequest,
|
|
2424
|
+
evt.detail.request,
|
|
2425
|
+
);
|
|
2426
|
+
});
|
|
2427
|
+
this.addEventListener('page-requested', async (/** @type any */ e) => {
|
|
2428
|
+
await this.load(
|
|
2429
|
+
{
|
|
2430
|
+
page: e.detail.value,
|
|
2431
|
+
size: this.#latestRequest.pageRequest.size,
|
|
2432
|
+
},
|
|
2433
|
+
this.#latestRequest.sortRequest,
|
|
2434
|
+
this.#latestRequest.filterRequest,
|
|
2435
|
+
);
|
|
2436
|
+
});
|
|
2437
|
+
this.addEventListener('sort-requested', async (/** @type any */ e) => {
|
|
2438
|
+
const sortRequest = e.detail.value.order ? e.detail.value : null;
|
|
2439
|
+
await this.load(this.#latestRequest.pageRequest, sortRequest, this.#latestRequest.filterRequest);
|
|
2440
|
+
this.#sorters.forEach((s) => (s.order = null));
|
|
2441
|
+
e.target.order = e.detail.value.order;
|
|
2442
|
+
});
|
|
2443
|
+
if (this.hasAttribute('autoload')) {
|
|
2444
|
+
await this.reload();
|
|
2445
|
+
}
|
|
2446
|
+
}
|
|
2447
|
+
|
|
2448
|
+
async reload() {
|
|
2449
|
+
return await this.load(
|
|
2450
|
+
this.#latestRequest.pageRequest,
|
|
2451
|
+
this.#latestRequest.sortRequest,
|
|
2452
|
+
this.#latestRequest.filterRequest,
|
|
2453
|
+
);
|
|
2454
|
+
}
|
|
2455
|
+
async load(pageRequest, sortRequest, filterRequest) {
|
|
2456
|
+
this.#body.replaceChildren();
|
|
2457
|
+
this.#loading.removeAttribute('hidden', '');
|
|
2458
|
+
this.#feedback.setAttribute('hidden', '');
|
|
2459
|
+
this.#noAutoload.setAttribute('hidden', '');
|
|
2460
|
+
try {
|
|
2461
|
+
const pageResponse = await this.#loader.load(pageRequest, sortRequest, filterRequest);
|
|
2462
|
+
this.#latestRequest = { pageRequest, sortRequest, filterRequest };
|
|
2463
|
+
this.#update(pageRequest, sortRequest, filterRequest, pageResponse);
|
|
2464
|
+
} catch (/** @type any */ error) {
|
|
2465
|
+
this.#loading.setAttribute('hidden', '');
|
|
2466
|
+
this.#feedback.removeAttribute('hidden', '');
|
|
2467
|
+
if (!error.problems) {
|
|
2468
|
+
this.#feedback.querySelector('[data-ref=feedback-error]').textContent = error;
|
|
2469
|
+
} else {
|
|
2470
|
+
this.#feedback.querySelector('[data-ref=feedback-error]').textContent = error.problems.map(
|
|
2471
|
+
(p) => `${p.reason}`,
|
|
2472
|
+
);
|
|
2473
|
+
}
|
|
2474
|
+
throw error;
|
|
2475
|
+
}
|
|
2476
|
+
}
|
|
2477
|
+
async withLoader(fn) {
|
|
2478
|
+
return await fn(this.#loader);
|
|
2479
|
+
}
|
|
2480
|
+
async resetWithFilter(filterRequest) {
|
|
2481
|
+
return await this.load(
|
|
2482
|
+
{
|
|
2483
|
+
page: 0,
|
|
2484
|
+
size: this.#latestRequest.pageRequest.size,
|
|
2485
|
+
},
|
|
2486
|
+
this.#latestRequest.sortRequest,
|
|
2487
|
+
filterRequest,
|
|
2488
|
+
);
|
|
2489
|
+
}
|
|
2490
|
+
#update(pageRequest, sortRequest, filterRequest, pageResponse) {
|
|
2491
|
+
this.#loading.setAttribute('hidden', '');
|
|
2492
|
+
this.#body.replaceChildren(
|
|
2493
|
+
this.template('row')
|
|
2494
|
+
.withOverlay({
|
|
2495
|
+
schema: this.#schema,
|
|
2496
|
+
pageRequest,
|
|
2497
|
+
filterRequest,
|
|
2498
|
+
pageResponse,
|
|
2499
|
+
})
|
|
2500
|
+
.render(),
|
|
2501
|
+
);
|
|
2502
|
+
this.#paginator.current = pageRequest.page;
|
|
2503
|
+
this.#paginator.total = Math.ceil(pageResponse.size / pageRequest.size);
|
|
2504
|
+
}
|
|
2505
|
+
}
|
|
2506
|
+
|
|
2507
|
+
class InstantFilter extends Input {
|
|
2508
|
+
static observed = ['value:json', 'readonly:presence', 'required:presence'];
|
|
2509
|
+
static template = `
|
|
2510
|
+
<div class="form-label">
|
|
2511
|
+
<label>{{{{ slots.default }}}}</label>
|
|
2512
|
+
{{{{ slots.info }}}}
|
|
2513
|
+
</div>
|
|
2514
|
+
<div class="input-group">
|
|
2515
|
+
<span data-tpl-if="slots.ibefore" class="input-group-text">{{{{ slots.ibefore }}}}</span>
|
|
2516
|
+
{{{{ slots.before }}}}
|
|
2517
|
+
<button data-ref="operator" class="btn btn-outline-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false" value="LTE" form="">≼</button>
|
|
2518
|
+
<ul class="dropdown-menu">
|
|
2519
|
+
<li><a class="dropdown-item" role="button" value="EQ">=</a></li>
|
|
2520
|
+
<li><a class="dropdown-item" role="button" value="NEQ">≠</a></li>
|
|
2521
|
+
<li><a class="dropdown-item" role="button" value="LT">≺</a></li>
|
|
2522
|
+
<li><a class="dropdown-item" role="button" value="GT">≻</a></li>
|
|
2523
|
+
<li><a class="dropdown-item" role="button" value="LTE">≼</a></li>
|
|
2524
|
+
<li><a class="dropdown-item" role="button" value="GTE">≽</a></li>
|
|
2525
|
+
<li><a class="dropdown-item" role="button" value="BETWEEN">↔</a></li>
|
|
2526
|
+
</ul>
|
|
2527
|
+
<input data-ref="value1" type="datetime-local" class="form-control" form="">
|
|
2528
|
+
<input data-ref="value2" type="datetime-local" class="form-control" form="" hidden>
|
|
2529
|
+
{{{{ slots.after }}}}
|
|
2530
|
+
<span data-tpl-if="slots.iafter" class="input-group-text">{{{{ slots.iafter }}}}</span>
|
|
2531
|
+
</div>
|
|
2532
|
+
<ful-field-error></ful-field-error>
|
|
2533
|
+
`;
|
|
2534
|
+
#operator;
|
|
2535
|
+
#value1;
|
|
2536
|
+
#value2;
|
|
2537
|
+
render(conf) {
|
|
2538
|
+
super.render({ ...conf, skipObservedSetup: true });
|
|
2539
|
+
this.#operator = this.querySelector('[data-ref=operator]');
|
|
2540
|
+
this.#value1 = this.querySelector('[data-ref=value1]');
|
|
2541
|
+
this.#value2 = this.querySelector('[data-ref=value2]');
|
|
2542
|
+
|
|
2543
|
+
this.disabled = conf.disabled;
|
|
2544
|
+
this.readonly = conf.observed.readonly;
|
|
2545
|
+
this.required = conf.observed.required;
|
|
2546
|
+
this.value = conf.observed.value;
|
|
2547
|
+
|
|
2548
|
+
this.addEventListener('click', (evt) => {
|
|
2549
|
+
const target = /** @type HTMLElement */ (evt.target);
|
|
2550
|
+
if (!target.matches('ul > li > a')) {
|
|
2551
|
+
return;
|
|
2552
|
+
}
|
|
2553
|
+
const btn = /** @type HTMLButtonElement */ (target.closest('ul')?.previousElementSibling);
|
|
2554
|
+
const value = /** @type String */ (target.getAttribute('value'));
|
|
2555
|
+
Attributes.toggle(this.#value2, 'hidden', value !== 'BETWEEN');
|
|
2556
|
+
btn.setAttribute('value', value);
|
|
2557
|
+
btn.innerHTML = target.innerHTML;
|
|
2558
|
+
});
|
|
2559
|
+
}
|
|
2560
|
+
|
|
2561
|
+
get value() {
|
|
2562
|
+
const operator = this.#operator.getAttribute('value');
|
|
2563
|
+
const values = operator === 'BETWEEN' ? [this.#value1.value, this.#value2.value] : [this.#value1.value];
|
|
2564
|
+
return values.some((v) => v === '') ? undefined : [operator, ...values.map((v) => new Date(v).toISOString())];
|
|
2565
|
+
}
|
|
2566
|
+
set value(v) {
|
|
2567
|
+
if (v == null) {
|
|
2568
|
+
this.#value1.value = '';
|
|
2569
|
+
this.#value2.value = '';
|
|
2570
|
+
return;
|
|
2571
|
+
}
|
|
2572
|
+
const [operator, ...values] = v;
|
|
2573
|
+
this.#operator.setAttribute('value', operator);
|
|
2574
|
+
this.#value1.value = values[0] ? Instant.isoToLocal(values[0]) : values[0];
|
|
2575
|
+
this.#value2.value = values[1] ? Instant.isoToLocal(values[1]) : values[1];
|
|
2576
|
+
}
|
|
2577
|
+
set readonly(v) {
|
|
2578
|
+
this.#value2.readOnly = v;
|
|
2579
|
+
super.readonly = v;
|
|
2580
|
+
}
|
|
2581
|
+
set disabled(d) {
|
|
2582
|
+
Attributes.toggle(this.#value2, 'disabled', d);
|
|
2583
|
+
super.disabled = d;
|
|
2584
|
+
}
|
|
2585
|
+
}
|
|
2586
|
+
|
|
2587
|
+
class LocalDateFilter extends Input {
|
|
2588
|
+
static observed = ['value:json', 'readonly:presence', 'required:presence'];
|
|
2589
|
+
static template = `
|
|
2590
|
+
<div class="form-label">
|
|
2591
|
+
<label>{{{{ slots.default }}}}</label>
|
|
2592
|
+
{{{{ slots.info }}}}
|
|
2593
|
+
</div>
|
|
2594
|
+
<div class="input-group">
|
|
2595
|
+
<span data-tpl-if="slots.ibefore" class="input-group-text">{{{{ slots.ibefore }}}}</span>
|
|
2596
|
+
{{{{ slots.before }}}}
|
|
2597
|
+
<button data-ref="operator" class="btn btn-outline-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false" value="EQ" form="">=</button>
|
|
2598
|
+
<ul class="dropdown-menu">
|
|
2599
|
+
<li><a class="dropdown-item" role="button" value="EQ">=</a></li>
|
|
2600
|
+
<li><a class="dropdown-item" role="button" value="NEQ">≠</a></li>
|
|
2601
|
+
<li><a class="dropdown-item" role="button" value="LT">≺</a></li>
|
|
2602
|
+
<li><a class="dropdown-item" role="button" value="GT">≻</a></li>
|
|
2603
|
+
<li><a class="dropdown-item" role="button" value="LTE">≼</a></li>
|
|
2604
|
+
<li><a class="dropdown-item" role="button" value="GTE">≽</a></li>
|
|
2605
|
+
<li><a class="dropdown-item" role="button" value="BETWEEN">↔</a></li>
|
|
2606
|
+
</ul>
|
|
2607
|
+
<input data-ref="value1" type="date" class="form-control" form="">
|
|
2608
|
+
<input data-ref="value2" type="date" class="form-control" form="" hidden>
|
|
2609
|
+
{{{{ slots.after }}}}
|
|
2610
|
+
<span data-tpl-if="slots.iafter" class="input-group-text">{{{{ slots.iafter }}}}</span>
|
|
2611
|
+
</div>
|
|
2612
|
+
<ful-field-error></ful-field-error>
|
|
2613
|
+
`;
|
|
2614
|
+
#operator;
|
|
2615
|
+
#value1;
|
|
2616
|
+
#value2;
|
|
2617
|
+
render(conf) {
|
|
2618
|
+
super.render({ ...conf, skipObservedSetup: true });
|
|
2619
|
+
|
|
2620
|
+
this.#operator = this.querySelector('[data-ref=operator]');
|
|
2621
|
+
this.#value1 = this.querySelector('[data-ref=value1]');
|
|
2622
|
+
this.#value2 = this.querySelector('[data-ref=value2]');
|
|
2623
|
+
|
|
2624
|
+
this.disabled = conf.disabled;
|
|
2625
|
+
this.readonly = conf.observed.readonly;
|
|
2626
|
+
this.required = conf.observed.required;
|
|
2627
|
+
this.value = conf.observed.value;
|
|
2628
|
+
|
|
2629
|
+
this.addEventListener('click', (evt) => {
|
|
2630
|
+
const target = /** @type HTMLElement */ (evt.target);
|
|
2631
|
+
if (!target.matches('ul > li > a')) {
|
|
2632
|
+
return;
|
|
2633
|
+
}
|
|
2634
|
+
const btn = /** @type HTMLButtonElement */ (target.closest('ul')?.previousElementSibling);
|
|
2635
|
+
const value = /** @type String */ (target.getAttribute('value'));
|
|
2636
|
+
Attributes.toggle(this.#value2, 'hidden', value !== 'BETWEEN');
|
|
2637
|
+
btn.setAttribute('value', value);
|
|
2638
|
+
btn.innerHTML = target.innerHTML;
|
|
2639
|
+
});
|
|
2640
|
+
}
|
|
2641
|
+
get value() {
|
|
2642
|
+
const operator = this.#operator.getAttribute('value');
|
|
2643
|
+
const values = operator == 'BETWEEN' ? [this.#value1.value, this.#value2.value] : [this.#value1.value];
|
|
2644
|
+
return values.some((v) => v === '') ? undefined : [operator, ...values];
|
|
2645
|
+
}
|
|
2646
|
+
set value(v) {
|
|
2647
|
+
if (v == null) {
|
|
2648
|
+
this.#value1.value = '';
|
|
2649
|
+
this.#value2.value = '';
|
|
2650
|
+
return;
|
|
2651
|
+
}
|
|
2652
|
+
const [operator, ...values] = v;
|
|
2653
|
+
this.#operator.setAttribute('value', operator);
|
|
2654
|
+
this.#value1.value = values[0];
|
|
2655
|
+
this.#value2.value = values[1];
|
|
2656
|
+
}
|
|
2657
|
+
set readonly(v) {
|
|
2658
|
+
this.#value2.readOnly = v;
|
|
2659
|
+
super.readonly = v;
|
|
2660
|
+
}
|
|
2661
|
+
set disabled(d) {
|
|
2662
|
+
Attributes.toggle(this.#value2, 'disabled', d);
|
|
2663
|
+
super.disabled = d;
|
|
2664
|
+
}
|
|
2665
|
+
}
|
|
2666
|
+
|
|
2667
|
+
class TextFilter extends Input {
|
|
2668
|
+
static observed = ['value:json', 'readonly:presence', 'required:presence'];
|
|
2669
|
+
static template = `
|
|
2670
|
+
<div class="form-label">
|
|
2671
|
+
<label>{{{{ slots.default }}}}</label>
|
|
2672
|
+
{{{{ slots.info }}}}
|
|
2673
|
+
</div>
|
|
2674
|
+
<div class="input-group">
|
|
2675
|
+
<span data-tpl-if="slots.ibefore" class="input-group-text">{{{{ slots.ibefore }}}}</span>
|
|
2676
|
+
{{{{ slots.before }}}}
|
|
2677
|
+
<button data-ref="operator" class="btn btn-outline-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false" value="CONTAINS" form="">…a…</button>
|
|
2678
|
+
<ul class="dropdown-menu">
|
|
2679
|
+
<li><a class="dropdown-item" role="button" value="CONTAINS">…a…</a></li>
|
|
2680
|
+
<li><a class="dropdown-item" role="button" value="STARTS_WITH">a…</a></li>
|
|
2681
|
+
<li><a class="dropdown-item" role="button" value="ENDS_WITH">…a</a></li>
|
|
2682
|
+
<li><a class="dropdown-item" role="button" value="EQ">=</a></li>
|
|
2683
|
+
</ul>
|
|
2684
|
+
<input data-ref="value" type="text" class="form-control" form="">
|
|
2685
|
+
{{{{ slots.after }}}}
|
|
2686
|
+
<span data-tpl-if="slots.iafter" class="input-group-text">{{{{ slots.iafter }}}}</span>
|
|
2687
|
+
</div>
|
|
2688
|
+
<ful-field-error></ful-field-error>
|
|
2689
|
+
`;
|
|
2690
|
+
#operator;
|
|
2691
|
+
#value;
|
|
2692
|
+
render(conf) {
|
|
2693
|
+
super.render({ ...conf, skipObservedSetup: true });
|
|
2694
|
+
|
|
2695
|
+
this.#operator = this.querySelector('[data-ref=operator]');
|
|
2696
|
+
this.#value = this.querySelector('[data-ref=value]');
|
|
2697
|
+
|
|
2698
|
+
this.disabled = conf.disabled;
|
|
2699
|
+
this.readonly = conf.observed.readonly;
|
|
2700
|
+
this.required = conf.observed.required;
|
|
2701
|
+
this.value = conf.observed.value;
|
|
2702
|
+
|
|
2703
|
+
this.addEventListener('click', (evt) => {
|
|
2704
|
+
const target = /** @type HTMLElement */ (evt.target);
|
|
2705
|
+
if (!target.matches('ul > li > a')) {
|
|
2706
|
+
return;
|
|
2707
|
+
}
|
|
2708
|
+
const btn = /** @type HTMLButtonElement */ (target.closest('ul')?.previousElementSibling);
|
|
2709
|
+
const value = /** @type String */ (target.getAttribute('value'));
|
|
2710
|
+
btn.setAttribute('value', value);
|
|
2711
|
+
btn.innerHTML = target.innerHTML;
|
|
2712
|
+
});
|
|
2713
|
+
}
|
|
2714
|
+
get value() {
|
|
2715
|
+
const operator = this.#operator.getAttribute('value');
|
|
2716
|
+
return this.#value.value === '' ? undefined : [operator, 'IGNORE_CASE', this.#value.value];
|
|
2717
|
+
}
|
|
2718
|
+
set value(v) {
|
|
2719
|
+
if (v == null) {
|
|
2720
|
+
this.#value.value = '';
|
|
2721
|
+
return;
|
|
2722
|
+
}
|
|
2723
|
+
const [operator, sensitivity, value] = v;
|
|
2724
|
+
this.#operator.setAttribute('value', operator);
|
|
2725
|
+
this.#value.value = value;
|
|
2726
|
+
}
|
|
2727
|
+
}
|
|
2728
|
+
|
|
2729
|
+
class LocalizationModule {
|
|
2730
|
+
static t(k, ...args) {
|
|
2731
|
+
//@ts-ignore
|
|
2732
|
+
const format = this.l10n?.[this.language]?.[k] ?? this.l10n?.['en']?.[k] ?? k;
|
|
2733
|
+
if (args.length === 0) {
|
|
2734
|
+
return format;
|
|
2735
|
+
}
|
|
2736
|
+
return format.replace(/{(\d+)}/g, (m, is) => {
|
|
2737
|
+
return args[Number(is)];
|
|
2738
|
+
});
|
|
2739
|
+
}
|
|
2740
|
+
static tl(k, args = []) {
|
|
2741
|
+
return LocalizationModule.t.apply(this, [k, ...args]);
|
|
2742
|
+
}
|
|
2743
|
+
}
|
|
2744
|
+
|
|
2745
|
+
class Plugin {
|
|
2746
|
+
configure(registry) {
|
|
2747
|
+
const httpClient = HttpClient.builder().withCsrfToken().withRedirectOnUnauthorized('/').build();
|
|
2748
|
+
registry
|
|
2749
|
+
.defineModule('l10n', LocalizationModule)
|
|
2750
|
+
.defineComponent('http-client', httpClient)
|
|
2751
|
+
.defineElement('ful-spinner', Spinner)
|
|
2752
|
+
.defineElement('ful-form', Form)
|
|
2753
|
+
.defineElement('ful-checkbox', Checkbox)
|
|
2754
|
+
.defineElement('ful-input', Input)
|
|
2755
|
+
.defineElement('ful-input-file', InputFile)
|
|
2756
|
+
.defineElement('ful-local-date', LocalDate)
|
|
2757
|
+
.defineElement('ful-instant', Instant)
|
|
2758
|
+
.defineElement('ful-input-local-date', InputLocalDate)
|
|
2759
|
+
.defineElement('ful-input-local-time', InputLocalTime)
|
|
2760
|
+
.defineElement('ful-input-instant', InputInstant)
|
|
2761
|
+
.defineElement('ful-radio-group', RadioGroup)
|
|
2762
|
+
.defineElement('ful-table', Table)
|
|
2763
|
+
.defineElement('ful-pagination', Pagination)
|
|
2764
|
+
.defineElement('ful-sorter', SortButton)
|
|
2765
|
+
.defineElement('ful-filter-instant', InstantFilter)
|
|
2766
|
+
.defineElement('ful-filter-local-date', LocalDateFilter)
|
|
2767
|
+
.defineElement('ful-filter-text', TextFilter)
|
|
2768
|
+
.defineElement('ful-select', Select)
|
|
2769
|
+
.defineElement('ful-dropdown', Dropdown)
|
|
2770
|
+
.defineComponent('loaders:select', SelectLoader)
|
|
2771
|
+
.defineComponent('loaders:form', FormLoader)
|
|
2772
|
+
.defineComponent('loaders:table', TableLoader)
|
|
2773
|
+
.defineOverlay({
|
|
2774
|
+
language: navigator?.language?.split('-')?.[0] ?? 'en',
|
|
2775
|
+
});
|
|
2776
|
+
}
|
|
2777
|
+
}
|
|
2778
|
+
|
|
2779
|
+
export { AsyncEvents, Bindings, Checkbox, Dropdown, Form, FormLoader, Input, InputFile, InputInstant, InputLocalDate, InputLocalTime, Instant, InstantFilter, LocalDate, LocalDateFilter, LocalStorage, LocalizationModule, Pagination, Plugin, RadioGroup, Select, SelectLoader, SessionStorage, SortButton, Spinner, Table, TableSchemaParser, TextFilter, Timing, VersionedLocalStorage, VersionedSessionStorage };
|
|
2780
|
+
//# sourceMappingURL=ful.mjs.map
|