';
}
/* Pestaña por defecto: Perfumes; si no hay perfumes pero sí marcas, Marcas. Se recuerda la última elegida. */
var tab = w.__lsTab || 'products';
if (tab === 'products' && !products.length) tab = 'brands';
if (tab === 'brands' && !brands.length) tab = 'products';
html += '
' +
'' +
'' +
'
';
html += '
' +
'
' + products.map(productCardHTML).join('') + '
' +
'
';
html += '
' +
'
' + brands.map(brandCardHTML).join('') + '
';
res.innerHTML = html;
res.scrollTop = 0;
}
function onType(w) {
var input = q$(w, '.ls-input');
var q = input.value.trim();
q$(w, '.ls-clear').style.display = q ? 'block' : 'none';
clearTimeout(w.__lsTimer);
if (w.__lsCtrl) { try { w.__lsCtrl.abort(); } catch (e) {} }
if (q.length < 2) {
w.__lsQ = '';
w.classList.remove('is-loading');
q$(w, '.ls-home').style.display = '';
q$(w, '.ls-results').innerHTML = '';
return;
}
w.__lsQ = q;
q$(w, '.ls-home').style.display = 'none';
var key = 'q:' + q.toLowerCase();
var hit = cacheGet(key);
if (hit) { w.classList.remove('is-loading'); renderResults(w, q, hit); return; }
w.classList.add('is-loading');
w.__lsTimer = setTimeout(function () {
var c = window.AbortController ? new AbortController() : null;
w.__lsCtrl = c;
getJSON(API + '?q=' + encodeURIComponent(q), c && c.signal)
.then(function (d) {
cacheSet(key, d);
if (w.__lsQ !== q) return;
w.classList.remove('is-loading');
renderResults(w, q, d);
})
.catch(function (err) {
if (err && err.name === 'AbortError') return;
if (w.__lsQ !== q) return;
w.classList.remove('is-loading');
console.error('[buscador] búsqueda:', err);
q$(w, '.ls-results').innerHTML = '
No se ha podido buscar (' + esc(err.message || 'error') + ').
';
});
}, 200);
}
document.addEventListener('input', function (e) {
var inp = e.target && e.target.closest ? e.target.closest('.ls-input') : null;
if (inp) onType(inp.closest('.ls-wrap'));
});
/* Enter: no navega a la página de resultados de WooCommerce. Los resultados en vivo son la búsqueda.
En móvil cierra el teclado para que se vean; si hay una sola coincidencia exacta, va a ella. */
document.addEventListener('keydown', function (e) {
if (e.key !== 'Enter') return;
var inp = e.target && e.target.closest ? e.target.closest('.ls-input') : null;
if (!inp) return;
e.preventDefault();
var w = inp.closest('.ls-wrap');
var cards = w.querySelectorAll('.ls-pane.is-active .ls-card, .ls-pane.is-active .ls-bcard');
if (cards.length === 1) { window.location.href = cards[0].getAttribute('href'); return; }
inp.blur();
});
document.addEventListener('click', function (e) {
var t = e.target && e.target.closest ? e.target : null;
if (!t) return;
var w = t.closest('.ls-wrap');
if (!w) return;
var arrow = t.closest('.ls-arrow[data-dir]');
if (arrow) {
var row = arrow.closest('.ls-rowwrap').querySelector('.ls-row');
var step = Math.round(row.clientWidth * 0.8) * (arrow.getAttribute('data-dir') === 'next' ? 1 : -1);
row.scrollBy({ left: step, behavior: 'smooth' });
return;
}
var tabBtn = t.closest('.ls-tab[data-tab]');
if (tabBtn && !tabBtn.disabled) {
var name = tabBtn.getAttribute('data-tab');
w.__lsTab = name;
Array.prototype.forEach.call(w.querySelectorAll('.ls-tab'), function (b) { b.classList.toggle('is-active', b === tabBtn); });
Array.prototype.forEach.call(w.querySelectorAll('.ls-pane'), function (p) { p.classList.toggle('is-active', p.getAttribute('data-pane') === name); });
q$(w, '.ls-results').scrollTop = 0;
return;
}
var chip = t.closest('.ls-chip[data-q], .ls-link[data-q]');
if (chip) {
var inp = q$(w, '.ls-input');
inp.value = chip.getAttribute('data-q');
onType(w);
inp.focus();
return;
}
if (t.closest('.ls-clear')) {
var inp2 = q$(w, '.ls-input');
inp2.value = '';
onType(w);
inp2.focus();
return;
}
if (t.closest('.ls-close')) { closeSearch(w, e); }
});
/* ---------- Cierre robusto: botón de Elementor → API de Elementor → cierre manual ---------- */
function closeSearch(w, e) {
var modal = w.closest('.elementor-popup-modal');
var done = false;
try { var x = modal && modal.querySelector('.dialog-close-button'); if (x) { x.click(); done = true; } } catch (err) {}
if (!done) { try { window.elementorProFrontend.modules.popup.closePopup({}, e || {}); done = true; } catch (err2) {} }
/* Si a los 150 ms sigue visible, cierre manual */
setTimeout(function () {
if (modal && getComputedStyle(modal).display !== 'none') {
modal.style.display = 'none';
try { document.body.classList.remove('elementor-popup-modal-open'); } catch (err3) {}
}
onClose(w);
unlockScroll();
}, 150);
}
document.addEventListener('keydown', function (e) {
if (e.key !== 'Escape') return;
allWraps().forEach(function (w) { if (w.__lsOpen) closeSearch(w, e); });
});
/* ---------- Apertura / cierre ---------- */
var lockedY = null;
function lockScroll() {
if (lockedY !== null) return;
lockedY = window.scrollY || 0;
var b = document.body.style;
b.overflow = 'hidden'; b.position = 'fixed'; b.top = '-' + lockedY + 'px'; b.width = '100%';
}
function unlockScroll() {
if (lockedY === null) return;
var y = lockedY; lockedY = null;
var b = document.body.style;
b.overflow = ''; b.position = ''; b.top = ''; b.width = '';
window.scrollTo(0, y);
}
var openCount = 0;
function onOpen(w) {
if (w.__lsOpen) return;
w.__lsOpen = true;
openCount++;
lockScroll();
renderPopular(w);
loadPopular();
var inp = q$(w, '.ls-input');
setTimeout(function () { try { inp.focus({ preventScroll: true }); } catch (e) { inp.focus(); } }, 120);
}
function onClose(w) {
if (!w.__lsOpen) return;
w.__lsOpen = false;
openCount = Math.max(0, openCount - 1);
if (!openCount) unlockScroll();
}
if (window.jQuery) {
window.jQuery(document).on('elementor/popup/show', function () {
allWraps().forEach(function (w) { setupWrap(w); if (w.offsetParent !== null) onOpen(w); });
});
}
/* Precarga de los populares al primer toque en la web */
document.addEventListener('pointerdown', function pre() {
document.removeEventListener('pointerdown', pre, true);
setTimeout(loadPopular, 300);
}, true);
})();
Usamos cookies para asegurar que te damos la mejor experiencia en nuestra web. Si continúas usando este sitio, asumiremos que estás de acuerdo con ello.