53 lines
2.1 KiB
JavaScript
53 lines
2.1 KiB
JavaScript
document.addEventListener('DOMContentLoaded', () => {
|
|
const toastElement = document.getElementById('appToast');
|
|
const toastBody = toastElement ? toastElement.querySelector('.toast-body') : null;
|
|
const appToast = toastElement && window.bootstrap ? new bootstrap.Toast(toastElement, { delay: 2600 }) : null;
|
|
|
|
const showToast = (message) => {
|
|
if (!toastBody || !appToast) return;
|
|
toastBody.textContent = message;
|
|
appToast.show();
|
|
};
|
|
|
|
document.querySelectorAll('.copy-trigger').forEach((button) => {
|
|
button.addEventListener('click', async () => {
|
|
const text = button.getAttribute('data-copy-text') || '';
|
|
const successMessage = button.getAttribute('data-copy-success') || 'Teks berhasil disalin.';
|
|
|
|
if (!text) return;
|
|
|
|
try {
|
|
if (navigator.clipboard && navigator.clipboard.writeText) {
|
|
await navigator.clipboard.writeText(text);
|
|
} else {
|
|
const temp = document.createElement('textarea');
|
|
temp.value = text;
|
|
temp.setAttribute('readonly', 'readonly');
|
|
temp.style.position = 'absolute';
|
|
temp.style.left = '-9999px';
|
|
document.body.appendChild(temp);
|
|
temp.select();
|
|
document.execCommand('copy');
|
|
document.body.removeChild(temp);
|
|
}
|
|
showToast(successMessage);
|
|
} catch (error) {
|
|
showToast('Gagal menyalin otomatis. Salin manual dari halaman detail.');
|
|
}
|
|
});
|
|
});
|
|
|
|
document.querySelectorAll('.auto-dismiss-alert').forEach((alertElement) => {
|
|
window.setTimeout(() => {
|
|
if (!window.bootstrap) return;
|
|
const instance = bootstrap.Alert.getOrCreateInstance(alertElement);
|
|
instance.close();
|
|
}, 5200);
|
|
});
|
|
|
|
const firstInvalidField = document.querySelector('.is-invalid');
|
|
if (firstInvalidField instanceof HTMLElement) {
|
|
firstInvalidField.focus();
|
|
}
|
|
});
|