UNPKG

@nosto/autocomplete

Version:

Library designed to simplify the implementation of search autocomplete functionality

1,050 lines (1,046 loc) 25.6 kB
import F from "handlebars"; var z = `<div class="ns-autocomplete-results"> {{#if response.keywords.hits.length}} <div class="ns-autocomplete-keywords"> <div class="ns-autocomplete-header"> Keywords </div> {{#each response.keywords.hits}} <div class="ns-autocomplete-keyword" data-ns-hit="{{toJson this}}" data-testid="keyword"> {{#if _highlight.keyword}} <span>{{{_highlight.keyword}}}</span> {{else}} <span>{{keyword}}</span> {{/if}} </div> {{/each}} </div> {{/if}} {{#if response.products.hits.length}} <div class="ns-autocomplete-products"> <div class="ns-autocomplete-header"> Products </div> {{#each response.products.hits}} <a class="ns-autocomplete-product" href="{{url}}" data-ns-hit="{{toJson this}}" data-testid="product"></a> <img class="ns-autocomplete-product-image" src="{{#if imageUrl}}{{ imageUrl }}{{else}}{{ imagePlaceholder }}{{/if}}" alt="{{name}}" width="60" height="40" /> <div class="ns-autocomplete-product-info"> {{#if brand}} <div class="ns-autocomplete-product-brand"> {{brand}} </div> {{/if}} <div class="ns-autocomplete-product-name"> {{name}} </div> <div> <span> {{price}}&euro; </span> {{#if showListPrice}} <span class="ns-autocomplete-product-list-price"> {{listPrice}}&euro; </span> {{/if}} </div> </div> </a> {{/each}} </div> {{/if}} {{#if history.length}} <div class="ns-autocomplete-history"> <div class="ns-autocomplete-header"> Recently searched </div> {{#each history}} <div class="ns-autocomplete-history-item" data-ns-hit="{{toJson this}}" data-testid="history"> {{item}} <a href="#" class="ns-autocomplete-history-item-remove" data-ns-remove-history="{{item}}"> &#x2715; </a> </div> {{/each}} </div> <div class="ns-autocomplete-history-clear"> <button type="button" class="ns-autocomplete-button" data-ns-remove-history="all"> Clear history </button> </div> {{/if}} {{#if response.keywords.hits.length}} {{#if response.products.hits.length}} <div class="ns-autocomplete-submit"> <button type="submit" class="ns-autocomplete-button"> See all search results </button> </div> {{/if}} {{/if}} </div> `; const fe = { helpers: { toJson: function() { return JSON.stringify(this); }, showListPrice: function() { return this.listPrice !== this.price; } } }; function G(e, t) { if (F === void 0) throw new Error( "Handlebars is not defined. Please include the Handlebars dependency or library in your page." ); const n = F.compile(e); return (o, r) => (o.innerHTML = n({ ...r, imagePlaceholder: "https://cdn.nosto.com/nosto/9/mock" }, t ?? fe), Promise.resolve()); } function R() { window.nostojs = window.nostojs ?? function(e) { (window.nostojs.q = window.nostojs.q ?? []).push(e); }; } async function T(e) { return window.nostojs(e); } typeof window < "u" && (R(), T((e) => { e.internal.getSettings(); })), typeof window < "u" && R(); function pe(e) { T((t) => t.recordSearchClick("autocomplete", e)); } function me(e) { T((t) => t.recordSearchSubmit(e)); } function K() { window.nostojs = window.nostojs ?? function(e) { (window.nostojs.q = window.nostojs.q ?? []).push(e); }; } async function C(e) { return window.nostojs(e); } typeof window < "u" && (K(), C((e) => { e.internal.getSettings(); })), typeof window < "u" && K(); const B = (e) => String(e) === "[object Object]"; function Q(e) { if (!B(e)) return !1; const t = e.constructor; if (t === void 0) return !0; const n = t.prototype; return !(!B(n) || !n.hasOwnProperty("isPrototypeOf")); } function E(e, t) { if (e === t) return !0; if (e instanceof Date && t instanceof Date) return e.getTime() === t.getTime(); if (Array.isArray(e) && Array.isArray(t)) return e.length !== t.length ? !1 : e.every((n, o) => E(n, t[o])); if (Q(e) && Q(t)) { const n = Object.entries(e); return n.length !== Object.keys(t).length ? !1 : n.every(([o, r]) => E(r, t[o])); } return !1; } function L(e) { return (...t) => { C((n) => { (n.internal.context.mode.isPreview() ? console[e] : n.internal.logger[e])(...t); }); }; } const g = { debug: L("debug"), info: L("info"), warn: L("warn"), error: L("error") }; async function ye(e, { hitDecorators: t, ...n }, o) { var r, i; const a = await o(e, n); if (!((i = (r = a.products) == null ? void 0 : r.hits) != null && i.length) || !(t != null && t.length)) return a; const c = (f) => t.reduce((l, s) => s(l), f); return { ...a, products: { ...a.products, hits: a.products.hits.map(c) } }; } function ge(e, t, n) { const o = JSON.stringify(t); try { n.setItem(e, o); } catch (r) { g.warn(r); } } function he(e, t) { try { const n = t.getItem(e); if (n) return JSON.parse(n); } catch (n) { g.warn(n); } } function we(e, t) { ge(e, t, sessionStorage); } function ve(e) { return he(e, sessionStorage); } const V = "nosto:search-js:cache", be = 60 * 1e3; function Se(e, t) { we(V, { query: e, result: t, created: Date.now() }); } function ke(e) { const t = ve(V); if (!t || !Ae(t)) return null; const n = W(t.query); return !E(W(e), n) || Date.now() - t.created > be ? null : t.result; } function W(e) { const t = { ...e, time: void 0, products: { ...e.products, size: void 0 } }; return JSON.parse(JSON.stringify(t)); } function Ae(e) { return typeof e == "object" && e !== null && "query" in e && "result" in e && "created" in e; } async function Pe(e, { usePersistentCache: t, ...n }, o) { if (!t) return o(e, n); const r = await Ce(e, n, o); return Se(e, r), r; } async function Ce(e, t, n) { var o, r, i, a, c, f; const { from: l = 0, size: s = 0 } = e.products || {}, u = ke(e); if (!u) return await n(e, t); const p = ((o = u?.products) == null ? void 0 : o.size) ?? 0, w = ((r = u?.products) == null ? void 0 : r.hits) ?? []; if (s === p) return u; if (s < p) return { ...u, products: { ...u.products, size: s, hits: w.slice(0, s), total: ((i = u.products) == null ? void 0 : i.total) || 0 } }; const A = s - w.length, v = l > 0 ? l + 1 : s - A, b = { ...e, products: { ...e.products, from: v, size: A } }, S = await n(b, t); return { ...u, products: { ...u.products, size: s, hits: [...((a = u.products) == null ? void 0 : a.hits) || [], ...((c = S.products) == null ? void 0 : c.hits) || []], total: ((f = S.products) == null ? void 0 : f.total) || 0 } }; } const Ee = 3e4, q = /* @__PURE__ */ new Map(); function Le(e, t) { const n = q.get(e); if (!n) return; const o = Date.now() - n.created > Ee, r = E(t, n.query); if (o || !r) { q.delete(e); return; } return n.result; } function De(e, t, n) { q.set(e, { query: t, result: n, created: Date.now() }); } async function Ie(e, t, n) { if (!t.useMemoryCache) return n(e, t); const o = JSON.stringify(e), r = Le(o, e); if (r) return r; const i = await n(e, t); return De(o, e, i), i; } function He(e) { return new Promise((t) => setTimeout(t, e)); } async function Ne(e, { maxRetries: t = 0, retryInterval: n = 0, ...o }, r) { let i = 0; for (; ; ) try { return await r(e, o); } catch (a) { if (i >= t) throw a; if (!Te(a)) throw g.info("Skipping retry logic for", a), a; i++, await He(n); } } function Te(e) { return !e || typeof e != "object" ? !1 : !("status" in e) || qe(e.status); } function qe(e) { return typeof e == "number" && (e < 400 || e >= 500); } async function Oe(e, t = {}) { const n = await new Promise(C); return $e(n.search, Ne, Ie, Pe, ye)(e, t); } function $e(e, ...t) { return t.reduce((n, o) => (r, i) => o(r, i, n), e); } const je = [ "productId", "url", "name", "imageUrl", "imageHash", "thumbUrl", "description", "brand", "variantId", "availability", "price", "priceText", "categoryIds", "categories", "customFields.key", "customFields.value", "priceCurrencyCode", "datePublished", "listPrice", "unitPricingBaseMeasure", "unitPricingUnit", "unitPricingMeasure", "googleCategory", "gtin", "ageGroup", "gender", "condition", "alternateImageUrls", "ratingValue", "reviewCount", "inventoryLevel", "skus.id", "skus.name", "skus.price", "skus.listPrice", "skus.priceText", "skus.url", "skus.imageUrl", "skus.inventoryLevel", "skus.customFields.key", "skus.customFields.value", "skus.availability", "pid", "onDiscount", "extra.key", "extra.value", "saleable", "available", "tags1", "tags2", "tags3" ]; async function O(e, t = {}) { const n = e.products?.fields ?? je, o = e.products?.facets ?? ["*"], r = e.products?.size ?? 20, i = e.products?.from ?? 0, a = await Oe( { ...e, products: { ...e.products, fields: n, facets: o, size: r, from: i } }, t ); return { query: e, response: a }; } const $ = { serpPath: "/search", queryParamName: "query", enabled: !0 }; function Z() { return { minQueryLength: 2, historyEnabled: !0, historySize: 5, hitDecorators: [], nostoAnalytics: !0, googleAnalytics: $, routingHandler: (e) => { location.href = e; }, nativeSubmit: !1, submit: (e, t, n) => { e.length >= (t.minQueryLength ?? Z().minQueryLength) && O( { query: e }, { redirect: !0, track: t.nostoAnalytics ? "serp" : void 0, hitDecorators: t.hitDecorators, ...n } ); } }; } function Ue(e) { e.setAttribute("autocomplete", "off"); } function _e(e, { onClick: t, onFocus: n, onInput: o, onKeyDown: r, onSubmit: i }, { form: a = e.form ?? void 0, nativeSubmit: c } = {}) { const f = []; function l(s, u, p) { s.addEventListener(u, p), f.push(() => s.removeEventListener(u, p)); } return (r || i) && l(e, "keydown", (s) => { r?.(e.value, s.key), r && (s.key === "ArrowDown" || s.key === "ArrowUp") ? s.preventDefault() : i && s.key === "Enter" && (e.value !== "" && !s.repeat && i(e.value), c || s.preventDefault()); }), i && a && (l(a, "submit", (s) => { c || s.preventDefault(), i(e.value); }), a.querySelectorAll("[type=submit]").forEach((s) => { l(s, "click", (u) => { c || u.preventDefault(), i(e.value); }); })), t && l(e, "click", () => t(e.value)), n && l(e, "focus", () => n(e.value)), o && l(e, "input", () => o(e.value)), { destroy() { f.forEach((s) => s()); } }; } function Me(e, t, n, o, r, i, a) { let c = [], f = [], l = !0, s = -1; function u(d) { const y = d?.dataset?.nsHit; if (y) { const m = j(y); if (k(), m?.item) { o(m.item); return; } if (m?.keyword) { o(m.keyword, { redirect: !!m?._redirect, isKeyword: !0 }), m?._redirect && i(m._redirect); return; } m?.url && i(m.url); } } function p() { l = !e.innerHTML.trim(), l || (c = Array.from( e.querySelectorAll("[data-ns-hit]") ).map((d) => (A(d), d))); } function w() { Object.entries(a ?? {}).map(([d, y]) => { const m = `[data-ns-${d.replace(/([A-Z])/g, "-$1").toLowerCase()}]`; Array.from(e.querySelectorAll(m)).map((h) => { const de = h?.dataset?.[`ns${d.charAt(0).toUpperCase() + d.slice(1)}`], x = () => { y({ data: de, el: h, update: S }); }; h.addEventListener("click", x), f.push(() => { h.removeEventListener("click", x); }); }); }); } function A(d) { const y = () => { u(d); }; d.addEventListener("click", y), f.push(() => { d.removeEventListener("click", y); }); } function v(d, y) { if (typeof y == "number" && c[y] && c[y].classList.remove("selected"), typeof d == "number" && c[d]) { c[d]?.classList.add("selected"); const m = c[d]?.dataset?.nsHit; if (m) { const h = j(m); if (h.item) { r(h.item); return; } if (h.keyword) { r(h.keyword); return; } } } } function b() { N(), c = [], f.forEach((d) => d()), f = []; } async function S(d) { b(), await n(e, d), setTimeout(() => { p(), w(), M(); }, 0); } function k() { N(), e.style.display = "none"; } function M() { l ? k() : e.style.display = ""; } function re() { b(), l = !0, k(); } function J() { return e.style.display !== "none"; } function ie() { let d = s; s === c.length - 1 ? s = 0 : d = s++, v(s, d); } function se() { if (P()) { let d = s; s === 0 ? s = c.length - 1 : d = s--, v(s, d); } else s = c.length - 1, v(s); } function ce() { J() && P() && c[s] && u(c[s]); } function P() { return s > -1; } function ae() { return c[s]; } function N() { P() && (c[s]?.classList.remove("selected"), s = -1); } function ue() { b(), l = !0, e.innerHTML = ""; } async function le() { const d = await Promise.resolve(t); await Promise.resolve(n(e, d)), setTimeout(() => { p(), w(), k(); }, 0); } return le(), { update: S, clear: re, isOpen: J, goDown: ie, goUp: se, handleSubmit: ce, destroy: ue, show: M, hide: k, resetHighlight: N, hasHighlight: P, getHighlight: ae, container: e }; } function j(e) { try { return JSON.parse(e) ?? {}; } catch (t) { return g.warn("Could not parse hit", t), {}; } } class X extends Error { } function Je(e) { let t = !1; return { promise: new Promise((n, o) => e.then( (r) => t ? o(new X("cancelled promise")) : n(r), (r) => o(t ? new X("cancelled promise") : r) )), cancel() { t = !0; } }; } function xe({ config: e, history: t, input: n }) { let o; const r = (s, u, p) => typeof u.fetch == "function" ? u.fetch(s) : O( { query: s, ...u.fetch }, { track: u.nostoAnalytics ? "autocomplete" : void 0, redirect: !1, hitDecorators: u.hitDecorators, ...p } ); function i(s) { return Promise.resolve({ query: { query: s }, history: t?.getItems() }); } function a(s, u) { return o?.cancel(), s && s.length >= e.minQueryLength ? (o = Je(r(s, e, u)), o.promise) : t ? i(s ?? "") : ( // @ts-expect-error type mismatch o?.promise ?? Promise.resolve({}) ); } function c(s) { return t && t.add(s), i(n.value); } function f(s) { return t && t.remove(s), i(n.value); } function l() { return t && t.clear(), i(n.value); } return { updateState: a, addHistoryItem: c, removeHistoryItem: f, clearHistory: l }; } function D(e, t) { return (typeof e == "string" ? Array.from(document.querySelectorAll(e)) : [e]).filter( (n) => t ? n instanceof t : !0 ); } function Y(e, t) { let n = []; return D(e).forEach((o) => { const r = o.parentNode; r !== document && r instanceof Element && (n.push(r), n = n.concat(Y(r))); }), n.filter( (o) => t === void 0 ); } function Fe([e, t], n) { const o = (r) => { const i = r.target; i instanceof HTMLElement && e && i !== e && i !== t && !Y(i).includes(e) && n(); }; return document.addEventListener("click", o), { destroy: () => { document.removeEventListener("click", o); } }; } const U = "nosto:autocomplete:gaEvent"; function I(e) { const { delay: t = !1, title: n = document.title, location: o = window.location.href } = e || {}; if (t) ze(n, o); else { if ("gtag" in window && typeof window.gtag == "function") { const r = "google_tag_manager" in window && typeof window.google_tag_manager == "object" ? Object.keys(window.google_tag_manager || []).filter((i) => i.substring(0, 2) == "G-") : []; if (r.length > 1) for (let i = 0; i < r.length; i++) window.gtag("event", "page_view", { page_title: n, page_location: o, send_to: r[i] }); else window.gtag("event", "page_view", { page_title: n, page_location: o }); } if ("ga" in window && typeof window.ga == "function" && "getAll" in window.ga && typeof window.ga.getAll == "function") try { const r = new URL(o), i = window.ga.getAll(); i?.length > 0 && i[0]?.send("pageview", r.pathname + r.search); } catch (r) { g.warn("Could not send pageview to GA", r); } } } const H = (e) => typeof e.googleAnalytics == "boolean" ? e.googleAnalytics : typeof e.googleAnalytics == "object" && e.googleAnalytics.enabled, _ = (e, t) => { const n = H(t) ? typeof t.googleAnalytics == "boolean" ? $ : { ...$, ...t.googleAnalytics } : void 0; if (e && n) try { return new URL( `${n?.serpPath || location.pathname}?${`${encodeURIComponent( n.queryParamName )}=${encodeURIComponent(e).replace(/%20/g, "+")}`}`, window.location.origin ).toString(); } catch (o) { g.warn("Could not create track url", o); return; } }; function ze(e, t) { localStorage.setItem(U, JSON.stringify({ title: e, location: t })); } function Ge() { const e = localStorage.getItem(U); if (typeof e == "string") { localStorage.removeItem(U); try { const t = JSON.parse(e); I(t); } catch (t) { g.warn("Could not consume pageView", t); } } } function Re(e) { let t, n = !1; return (o) => { n ? (t && clearTimeout(t), t = setTimeout(async () => { await o(), n = !1; }, e)) : (o(), n = !0); }; } function Ke(e) { const t = "nosto:autocomplete:history"; let n = o(); function o() { try { return JSON.parse(localStorage.getItem(t) ?? "[]") ?? []; } catch (l) { return g.error("Could not get history items.", l), []; } } function r(l) { try { localStorage.setItem(t, JSON.stringify(l)); } catch (s) { g.error("Could not set history items.", s); } } function i(l) { r( n = [{ item: l }, ...n?.filter((s) => s.item !== l) || []].slice( 0, e ) ); } function a() { r(n = []); } function c(l) { r(n = n.filter((s) => s.item !== l)); } function f() { return n; } return { add: i, clear: a, remove: c, getItems: f }; } function ee(e) { const t = { ...Z(), ...e }, n = t.historyEnabled ? Ke(t.historySize) : void 0, o = Re(300); H(e) && setTimeout(Ge, 1e3); const r = D(e.inputSelector, HTMLInputElement).map( (i) => { const a = xe({ config: t, history: n, input: i }), c = Be({ input: i, config: t, actions: a }); if (!c) return; Ue(i); const f = _e(i, { onInput: async (s) => { o(async () => { const u = await a.updateState(s); c.update(u); }); }, onClick() { c.resetHighlight(); }, onFocus() { c.show(); }, onSubmit() { if (c.isOpen() && c.hasHighlight()) { const s = c.getHighlight()?.dataset?.nsHit; s && te({ config: t, data: s, query: i.value }), c.handleSubmit(); } else ne({ actions: a, config: t })(i.value); }, onKeyDown(s, u) { u === "Escape" ? c.hide() : u === "ArrowDown" ? c.isOpen() ? c.goDown() : c.show() : u === "ArrowUp" && c.isOpen() && c.goUp(); } }, { nativeSubmit: t.nativeSubmit }), l = Fe( [c.container, i], () => { c.hide(); } ); return { open() { c.show(); }, close() { c.hide(); }, destroy() { f.destroy(), l.destroy(), c.destroy(); } }; } ); return { destroy() { r.forEach((i) => i?.destroy()); }, open() { r.forEach((i) => i?.open()); }, close() { r.forEach((i) => i?.close()); } }; } function Be({ input: e, config: t, actions: n }) { const o = typeof t.dropdownSelector == "function" ? D(t.dropdownSelector(e), HTMLElement) : D(t.dropdownSelector, HTMLElement); if (o.length === 0) { g.error(`No dropdown element found for input ${e}`); return; } else o.length > 1 && g.error( `Multiple dropdown elements found for input ${e}, using the first element` ); const r = o[0]; return Me( r, n.updateState(e.value), t.render, ne({ actions: n, config: t }), (i) => e.value = i, t.routingHandler, { removeHistory: async function({ data: i, update: a }) { if (i === "all") { const c = await n.clearHistory(); return a(c); } else if (i) { const c = await n.removeHistoryItem(i); return a(c); } }, hit: function({ data: i }) { i && te({ config: t, data: i, query: e.value }); } } ); } async function te({ config: e, data: t, query: n }) { if (!e.googleAnalytics && !e.nostoAnalytics) return; const o = j(t); e.nostoAnalytics && o && pe(o), H(e) && (o._redirect && I({ delay: !0, location: _(o.keyword, e) }), o.url && I({ delay: !0, location: _(n, e) })); } function ne(e) { return async (t, n) => { const { config: o, actions: r } = e, { redirect: i = !1 } = n ?? {}; t.length > 0 && (o.historyEnabled && r.addHistoryItem(t), o.nostoAnalytics && me(t), H(o) && I({ delay: !0, location: _(t, o) }), !i && typeof o?.submit == "function" && o.submit(t, o, n)); }; } const Qe = { defaultCurrency: "EUR", defaultLocale: "en-US", /** @hidden */ currencySettings: {} }, Ve = { EUR: "de-DE", GBP: "en-GB", USD: "en-US", AUD: "en-AU", CAD: "en-CA", //India, Afghanistan, Bangladesh, Bhutan, Myanmar, Nepal, and Pakistan uses lakhs and crores notation INR: "en-IN", AFN: "en-IN", BDT: "en-IN", BTN: "en-IN", MMK: "en-IN", NPR: "en-IN", PKR: "en-IN" }; function We(e = {}) { const t = { ...Qe, ...e }; e.currencySettings || C((o) => { t.currencySettings = o.internal.getSettings().currencySettings ?? {}; }); function n(o, r) { const { defaultCurrency: i, currencySettings: a, defaultLocale: c } = t, f = r ?? i, l = Ve[f] ?? c; if (f in a) { const s = a[f], u = new Intl.NumberFormat(l, { useGrouping: !!s.groupingSeparator, minimumFractionDigits: s.decimalPlaces, maximumFractionDigits: s.decimalPlaces }).formatToParts(o).map((p) => p.type === "group" ? s.groupingSeparator : p.type === "decimal" ? s.decimalCharacter : p.value).join(""); return s != null && s.currencyBeforeAmount ? `${s.currencyToken}${u}` : `${u}${s?.currencyToken}`; } return new Intl.NumberFormat(l, { style: "currency", currency: f }).format(o); } return { formatCurrency: n }; } function Ze(e) { const { formatCurrency: t } = We(e); function n(r, i) { const a = {}; return r.price !== void 0 && (a.priceText = t(r.price, i)), r.listPrice !== void 0 && (a.listPriceText = t(r.listPrice, i)), Object.assign({}, r, a); } function o(r) { return r.price !== void 0 || r.listPrice !== void 0; } return function(r) { if (!o(r)) return r; const i = n(r, r.priceCurrencyCode); return i.skus && i.skus.some(o) && (i.skus = i.skus.map((a) => o(a) ? n(a, r.priceCurrencyCode) : a)), i; }; } async function Xe(e, { handler: t, defaultTemplate: n }) { const o = e.querySelector( "script[autocomplete-template]" )?.textContent, r = Ye(e); if (!Object.keys(r).length) throw new Error("NostoAutocomplete: Missing required config."); return ee({ ...r, nativeSubmit: !0, render: t(o ?? n), submit(i) { e.querySelector(r.inputSelector).value = i, e.querySelector("form")?.submit(); } }); } function Ye(e) { const t = e.querySelector("script[autocomplete-config]"); return t ? JSON.parse(t.textContent) : {}; } class oe extends HTMLElement { async connectedCallback() { return Xe(this, { handler: G, defaultTemplate: z }); } } customElements.define("nosto-autocomplete", oe); export { oe as NostoAutocomplete, ee as autocomplete, z as defaultHandlebarsTemplate, G as fromHandlebarsTemplate, Ze as priceDecorator, O as search }; //# sourceMappingURL=autocomplete.mjs.map