2021-09-19 12:28:21 +02:00
|
|
|
// noinspection JSUnusedGlobalSymbols
|
|
|
|
|
2021-08-02 13:16:10 +02:00
|
|
|
/**
|
2021-09-07 13:37:18 +02:00
|
|
|
* Check if class/object A is the same as or a subclass of class B.
|
2021-08-02 13:16:10 +02:00
|
|
|
*/
|
2021-09-29 15:32:31 +02:00
|
|
|
export function subclassOf(A: {...}, B: {...}): boolean {
|
2021-09-07 13:37:18 +02:00
|
|
|
// noinspection JSUnresolvedVariable
|
2021-08-02 13:16:10 +02:00
|
|
|
return A && (A === B || A.prototype instanceof B);
|
|
|
|
}
|
2021-09-19 12:28:21 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if a string contains HTML code/tags
|
|
|
|
*/
|
|
|
|
export function containsHTML(string_: string): boolean {
|
|
|
|
// eslint-disable-next-line unicorn/better-regex
|
|
|
|
return /<[a-z][\s\S]*>/i.test(string_);
|
|
|
|
}
|
2021-10-04 22:56:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Show a snackbar
|
|
|
|
*/
|
|
|
|
export async function showSnackbar(message: string, duration: number, acceptText = 'OK', cancelText = 'Annulla'): Promise<boolean> {
|
|
|
|
const snackbar = document.createElement('mwc-snackbar');
|
|
|
|
snackbar.label = message;
|
|
|
|
snackbar.timeoutMs = duration;
|
|
|
|
if (acceptText) {
|
|
|
|
const button = document.createElement('mwc-button');
|
|
|
|
button.label = acceptText;
|
|
|
|
button.slot = 'action';
|
|
|
|
snackbar.append(button);
|
|
|
|
}
|
|
|
|
if (cancelText) {
|
|
|
|
const button = document.createElement('mwc-button');
|
|
|
|
button.label = cancelText;
|
|
|
|
button.slot = 'cancel';
|
|
|
|
snackbar.append(button);
|
|
|
|
}
|
|
|
|
document.body.append(snackbar);
|
|
|
|
let resolve: (value?: boolean) => void;
|
|
|
|
const reasonPromise = new Promise()((response) => {
|
|
|
|
resolve = response;
|
|
|
|
});
|
|
|
|
snackbar.addEventListener('MDCSnackbar:closed', (event) => {
|
|
|
|
resolve(event?.detail?.reason === 'action' ?? false);
|
|
|
|
});
|
|
|
|
snackbar.open();
|
|
|
|
const acceptOrReject = await reasonPromise;
|
|
|
|
snackbar.remove();
|
|
|
|
return acceptOrReject;
|
|
|
|
}
|