Client Account Opening
PENSATO & CIE
2, Blv d’Italie MC 98000 MONACO – Tel: +377 99 90 2 90 – Email: accounting@pensato.com
(function () {
// ========= CONFIG =========
// Slug de la case à cocher qui active la copie
const CHECKBOX_SLUG = 'checkbox-1'; // fonctionne aussi si le name est "checkbox-1[]"
// Groupes source -> cibles (2 cibles par source dans ton cas)
// Pour le phone, on accepte 'phonne-2' OU 'phone-2' (le premier trouvé sera utilisé).
const GROUPS = [
{ src: 'text-1', tgts: ['text-2', 'text-3'] },
{ src: 'name-1', tgts: ['name-6', 'name-7'] },
{ src: 'email-1', tgts: ['email-2', 'email-3'] },
{ src: 'phone-1', tgts: [['phonne-2','phone-2'], 'phone-3'] }
];
// ===========================
// ---- Utils
const $ = (sel, root=document) => root.querySelector(sel);
const $$ = (sel, root=document) => Array.from(root.querySelectorAll(sel));
function getField(slug, root=document){
return (
$('#forminator-field-' + slug, root) ||
$('[name="' + slug + '"]', root) ||
$('[name="' + slug + '[]"]', root) ||
$('[id^="forminator-field-' + slug + '-"]', root) // variantes pour checkbox options
);
}
function getFirstExisting(cand, root){
// cand peut être un string ou un tableau de slugs alternatifs
if (Array.isArray(cand)) {
for (const s of cand) { const el = getField(s, root); if (el) return el; }
return null;
}
return getField(cand, root);
}
function getCheckboxes(slug, root=document){
return $$('[name="' + slug + '"], [name="' + slug + '[]"]', root);
}
function findFormRoot(){
// Essaie de se caler sur le conteneur du formulaire qui contient au moins un champ connu
for (const g of GROUPS){
const el = getField(g.src);
if (el){
const root = el.closest('.forminator-custom-form') || el.closest('form') || document;
return root;
}
}
return document;
}
function copyValue(fromEl, toEl){
if (!fromEl || !toEl) return;
toEl.value = fromEl.value;
toEl.dispatchEvent(new Event('input', { bubbles:true }));
toEl.dispatchEvent(new Event('change', { bubbles:true }));
}
function copyGroupOnce(group, root){
const from = getField(group.src, root);
if (!from) return;
group.tgts.forEach(t => {
const to = getFirstExisting(t, root);
if (to) copyValue(from, to);
});
}
let unsubs = [];
function enableSync(root){
// Copie initiale
GROUPS.forEach(g => copyGroupOnce(g, root));
// Sync live
GROUPS.forEach(g => {
const from = getField(g.src, root);
if (!from) return;
const h = () => copyGroupOnce(g, root);
from.addEventListener('input', h);
from.addEventListener('change', h);
unsubs.push(() => {
from.removeEventListener('input', h);
from.removeEventListener('change', h);
});
});
}
function disableSync(){ while (unsubs.length) unsubs.pop()(); }
function init(){
const root = findFormRoot();
const boxes = getCheckboxes(CHECKBOX_SLUG, root);
if (!boxes.length) return false;
const apply = () => {
const checked = boxes.some(b => b.checked);
if (checked) { enableSync(root); }
else { disableSync(); }
};
boxes.forEach(b => b.addEventListener('change', apply));
apply();
console.log('[Forminator] duplication active (root détecté)', root);
return true;
}
// Attendre que le formulaire soit injecté (AJAX) et re-tenter
const start = Date.now(), TIMEOUT = 12000;
(function wait(){
const haveSources = GROUPS.every(g => getField(g.src));
const haveCheckbox = getCheckboxes(CHECKBOX_SLUG).length > 0;
if (haveSources && haveCheckbox && init()) return;
if (Date.now() - start > TIMEOUT) {
console.warn('[Forminator] champs/checkbox introuvables. Vérifie les slugs et la présence de checkbox-1.');
return;
}
setTimeout(wait, 200);
})();
})();
