(function () { const cfg = window.MyAuto || null; if (!cfg) return; const minChars = cfg.minChars || 3; const limit = cfg.limit || 8; // MOD: "product" = tıklayınca ürün sayfasına git (HIZLI) // "search" = input doldur + form submit (YAVAŞ ama arama sonuçlarında kalır) const clickMode = cfg.clickMode || "product"; function log(...a){ if (cfg.debug) console.log(...a); } function initForInput(input) { if (!input || input.dataset.muAutoInit === '1') return; input.dataset.muAutoInit = '1'; const brandSelect = document.querySelector(cfg.brandSelector || 'select[name="product_brand"]'); const parent = input.parentElement; if (!parent) return; if (!parent.style.position) parent.style.position = 'relative'; // Dropdown box const box = document.createElement('div'); box.style.position = 'absolute'; box.style.left = '0'; box.style.right = '0'; box.style.top = 'calc(100% + 6px)'; box.style.background = '#fff'; box.style.border = '1px solid #e5e7eb'; box.style.borderRadius = '10px'; box.style.boxShadow = '0 10px 30px rgba(0,0,0,.08)'; box.style.zIndex = '99999'; box.style.overflow = 'hidden'; box.style.display = 'none'; parent.appendChild(box); // Basit inline CSS (hover net olsun) const style = document.createElement('style'); style.textContent = ` .mu-auto-row:hover { background: #f1f5f9 !important; } .mu-auto-row.is-active { background: #e2e8f0 !important; } .mu-auto-row { transition: background .06s linear; } `; document.head.appendChild(style); let timer = null; let items = []; let activeIndex = -1; // İstek iptali + cache let abortController = null; const cache = new Map(); // key -> items[] const CACHE_TTL_MS = 60 * 1000; const cacheTime = new Map(); function esc(s) { // Burada sadece gerçek HTML karakterlerini kaçırıyoruz return String(s).replace(/[&<>"']/g, (m) => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[m])); } function closeBox() { box.style.display = 'none'; box.innerHTML = ''; items = []; activeIndex = -1; } function openBox() { box.style.display = 'block'; } function setActive(idx) { activeIndex = idx; const rows = box.querySelectorAll('.mu-auto-row'); rows.forEach((r, i) => { if (i === activeIndex) r.classList.add('is-active'); else r.classList.remove('is-active'); }); } function showLoading() { box.innerHTML = `
Yükleniyor…
`; openBox(); } function render(list) { if (!list || list.length === 0) { closeBox(); return; } items = list; activeIndex = -1; box.innerHTML = list.map((it, idx) => { const img = it.img ? `` : `
`; return `
${img}
${esc(it.title)}
${esc(it.price || '')}
`; }).join(''); openBox(); } function cacheKey(q) { const b = (brandSelect && brandSelect.value) ? brandSelect.value : ''; return `${q}__${b}`; } function getCached(q) { const key = cacheKey(q); if (!cache.has(key)) return null; const t = cacheTime.get(key) || 0; if (Date.now() - t > CACHE_TTL_MS) { cache.delete(key); cacheTime.delete(key); return null; } return cache.get(key); } function setCached(q, list) { const key = cacheKey(q); cache.set(key, list); cacheTime.set(key, Date.now()); } async function fetchSuggestions(q) { const cached = getCached(q); if (cached) return cached; // Önceki isteği iptal et if (abortController) abortController.abort(); abortController = new AbortController(); const fd = new FormData(); fd.append('action', 'mu_onefile_product_autocomplete'); fd.append('nonce', cfg.nonce); fd.append('q', q); fd.append('limit', String(limit)); fd.append('brandTax', cfg.brandTax || 'product_brand'); if (brandSelect && brandSelect.value) fd.append('brand', brandSelect.value); const res = await fetch(cfg.ajaxUrl, { method: 'POST', credentials: 'same-origin', body: fd, signal: abortController.signal }); const json = await res.json(); log("AJAX:", json); const list = (json && json.success && json.data && json.data.items) ? json.data.items : []; setCached(q, list); return list; } function debounce(fn, ms) { clearTimeout(timer); timer = setTimeout(fn, ms); } // Yazınca input.addEventListener('input', () => { const q = (input.value || '').trim(); if (q.length < minChars) { closeBox(); return; } // 1) Hemen loading göster showLoading(); // 2) Daha kısa debounce (hız hissi) debounce(async () => { try { const list = await fetchSuggestions(q); render(list); } catch (e) { // abort edilen isteklerde hata basmayalım if (e && e.name === 'AbortError') return; closeBox(); } }, 120); }); // Marka değişince öneriyi güncelle if (brandSelect) { brandSelect.addEventListener('change', () => { const q = (input.value || '').trim(); if (q.length < minChars) { closeBox(); return; } showLoading(); debounce(async () => { try { const list = await fetchSuggestions(q); render(list); } catch (e) { if (e && e.name === 'AbortError') return; closeBox(); } }, 80); }); } // Tıklama ile seç box.addEventListener('mousemove', (e) => { const row = e.target.closest('.mu-auto-row'); if (!row) return; const idx = parseInt(row.getAttribute('data-idx'), 10); if (!Number.isNaN(idx)) setActive(idx); }); box.addEventListener('click', (e) => { const row = e.target.closest('.mu-auto-row'); if (!row) return; const idx = parseInt(row.getAttribute('data-idx'), 10); const it = items[idx]; if (!it) return; // HIZLI: ürün sayfasına git if (clickMode === "product" && it.url) { window.location.href = it.url; return; } // Alternatif: arama sayfasında kalsın (yavaş, sayfa reload) input.value = it.title; const form = input.closest('form'); if (form) form.submit(); closeBox(); }); // Klavye (ok/enter/esc) input.addEventListener('keydown', (e) => { if (box.style.display === 'none' || items.length === 0) return; if (e.key === 'Escape') { closeBox(); return; } if (e.key === 'ArrowDown') { e.preventDefault(); setActive(Math.min(items.length - 1, activeIndex + 1)); return; } if (e.key === 'ArrowUp') { e.preventDefault(); setActive(Math.max(0, activeIndex - 1)); return; } if (e.key === 'Enter') { if (activeIndex >= 0 && items[activeIndex]) { e.preventDefault(); const it = items[activeIndex]; if (clickMode === "product" && it.url) { window.location.href = it.url; return; } input.value = it.title; const form = input.closest('form'); if (form) form.submit(); closeBox(); } } }); // Dışarı tıklayınca kapat document.addEventListener('click', (e) => { if (e.target === input) return; if (box.contains(e.target)) return; closeBox(); }); } function boot() { // Birden fazla input olabilir: .my-smart-search ve .search-field const sel = cfg.inputSelector || '.my-smart-search'; const inputs = document.querySelectorAll(sel); inputs.forEach(initForInput); // input geç geliyorsa tekrar dene let tries = 0; const iv = setInterval(() => { tries++; const inputs2 = document.querySelectorAll(sel); inputs2.forEach(initForInput); if (tries >= 20) clearInterval(iv); }, 250); } if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', boot); } else { boot(); } })(); Page Not Found - Defans Teknoloji

Eyvah! Sayfa bulunamadı.

Burada birşey bulunamadı. Aramayı deneyebilirsiniz.