Files
zbiorki_app/static/js/zbiorka.js
Mateusz Gruszczyński 4515a1b391 zmiany w ux
2025-09-24 10:55:04 +02:00

67 lines
2.8 KiB
JavaScript

(function () {
// --- Formatowanie IBAN ---
const ibanEl = document.getElementById('ibanInput') || document.getElementById('ibanDisplay');
if (ibanEl) {
const raw = (('value' in ibanEl ? ibanEl.value : ibanEl.textContent) || '')
.toString().replace(/\s+/g, '').toUpperCase();
const digits = raw.replace(/^PL/, '').replace(/\D/g, '').slice(0, 26);
if (digits) {
const pretty = 'PL ' + digits.replace(/(.{4})/g, '$1 ').trim();
if ('value' in ibanEl) ibanEl.value = pretty; else ibanEl.textContent = pretty;
}
}
// --- Formatowanie BLIK ---
const blikEl = document.getElementById('blikInput') || document.getElementById('blikDisplay');
if (blikEl) {
const raw = (('value' in blikEl ? blikEl.value : blikEl.textContent) || '')
.toString().replace(/\D/g, '').slice(0, 9);
if (raw) {
const pretty = [raw.slice(0, 3), raw.slice(3, 6), raw.slice(6, 9)]
.filter(Boolean).join(' ');
if ('value' in blikEl) blikEl.value = pretty; else blikEl.textContent = pretty;
}
}
// --- Kopiowanie: wspiera data-copy-input i data-copy-target ---
const buttons = document.querySelectorAll('[data-copy-input], [data-copy-target]');
buttons.forEach(btn => {
btn.addEventListener('click', async () => {
const sel = btn.getAttribute('data-copy-input') || btn.getAttribute('data-copy-target');
const el = sel ? document.querySelector(sel) : null;
if (!el) return;
const textRaw = ('value' in el ? el.value : el.textContent || '')
.toString().replace(/\u00A0/g, ' ').trim();
const copyWithFallback = async (text) => {
try {
await navigator.clipboard.writeText(text);
return true;
} catch {
// Fallback: tymczasowy textarea
const ta = document.createElement('textarea');
ta.value = text;
ta.style.position = 'fixed';
ta.style.top = '-1000px';
ta.setAttribute('readonly', '');
document.body.appendChild(ta);
ta.select();
try { document.execCommand('copy'); } catch { /* ignore */ }
document.body.removeChild(ta);
return true;
}
};
const original = btn.textContent;
const ok = await copyWithFallback(textRaw);
if (ok) {
btn.textContent = 'Skopiowano!';
btn.disabled = true;
setTimeout(() => { btn.textContent = original; btn.disabled = false; }, 1200);
}
});
});
})();