39496-vm/assets/js/main.js
2026-04-08 06:59:39 +00:00

92 lines
3.5 KiB
JavaScript

document.addEventListener('DOMContentLoaded', () => {
document.querySelectorAll('[data-auto-toast]').forEach((element) => {
const toast = new bootstrap.Toast(element, { delay: 3200 });
toast.show();
});
document.querySelectorAll('[data-copy-text]').forEach((button) => {
button.addEventListener('click', async () => {
const text = button.getAttribute('data-copy-text') || '';
try {
await navigator.clipboard.writeText(text);
const original = button.textContent;
button.textContent = 'Copied';
setTimeout(() => {
button.textContent = original;
}, 1200);
} catch (error) {
console.error('Copy failed', error);
}
});
});
const searchInput = document.querySelector('[data-subject-search]');
if (searchInput) {
const items = Array.from(document.querySelectorAll('.subject-grid-item'));
const emptyState = document.querySelector('[data-empty-state]');
const applyFilter = () => {
const query = searchInput.value.trim().toLowerCase();
let visible = 0;
items.forEach((item) => {
const text = item.getAttribute('data-filter-text') || '';
const match = text.includes(query);
item.classList.toggle('d-none', !match);
if (match) visible += 1;
});
if (emptyState) {
emptyState.classList.toggle('d-none', visible !== 0);
}
};
searchInput.addEventListener('input', applyFilter);
}
// Convert alerts to SweetAlert2 popups
(async function() {
if (typeof Swal === 'undefined') return;
const alerts = document.querySelectorAll('.alert');
for (const alertEl of alerts) {
// Don't convert if it contains interactive elements
if (alertEl.querySelector('form, ul, .btn, .alert-heading, a')) continue;
// Skip empty states / full page notices
const siblings = Array.from(alertEl.parentElement.children).filter(el => el !== alertEl && el.tagName !== 'SCRIPT' && el.tagName !== 'STYLE' && el.tagName !== 'BR');
if (siblings.length === 0 && (alertEl.parentElement.classList.contains('container') || alertEl.parentElement.tagName === 'MAIN' || alertEl.parentElement.classList.contains('col-12'))) {
continue;
}
// Skip heavily padded elements (likely structural empty states)
if (alertEl.classList.contains('py-4') || alertEl.classList.contains('py-5') || alertEl.classList.contains('py-3') || alertEl.classList.contains('alert-info')) {
continue; // Also explicitly skipping alert-info because they are usually informational structure blocks
}
let type = 'info';
let titleStr = document.documentElement.lang === 'ar' ? 'ملاحظة' : 'Note';
if (alertEl.classList.contains('alert-success')) {
type = 'success';
titleStr = document.documentElement.lang === 'ar' ? 'نجاح' : 'Success';
} else if (alertEl.classList.contains('alert-danger')) {
type = 'error';
titleStr = document.documentElement.lang === 'ar' ? 'خطأ' : 'Error';
} else if (alertEl.classList.contains('alert-warning')) {
type = 'warning';
titleStr = document.documentElement.lang === 'ar' ? 'تنبيه' : 'Warning';
}
const text = alertEl.innerText.trim();
if (!text) continue;
alertEl.style.display = 'none';
await Swal.fire({
icon: type,
title: titleStr,
text: text,
confirmButtonText: document.documentElement.lang === 'ar' ? 'حسنًا' : 'OK',
confirmButtonColor: '#0d6efd'
});
}
})();
});