@nosto/search-js 3.10.0 → 3.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const w=require("../../AutocompletePageProvider-kQ8RqtRT.cjs"),T=require("../../CategoryPageProvider-DwqxtD8L.cjs"),m=require("../../useActions-DSWNCNrI.cjs"),x=require("../../SerpPageProvider-NSsaKQ7f.cjs"),u=require("../../jsxRuntime.module-B3sGoTIU.cjs"),E=require("../../bindInput-BnKvCky1.cjs"),P=require("../../unique-BXXNictB.cjs"),g=require("preact"),S=require("../../logger-Boh_C6Bz.cjs"),D=require("preact/hooks"),O=require("../../index.es-Dp6Iaxz3.cjs"),y=require("../../search-DTqo1D6Y.cjs");function B(e){let t;return n=>{t&&clearTimeout(t),t=setTimeout(n,e)}}function f({children:e}){const[t]=D.useErrorBoundary();return t&&S.logger.error("Error caught in ErrorBoundary",t),u.u(g.Fragment,{children:e})}function A(e){return Array.from(document.querySelectorAll(e))}function F(e){const t=[];let n=e.parentNode;for(;n&&n!==document&&n instanceof Element;)t.push(n),n=n.parentNode;return t}function
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const w=require("../../AutocompletePageProvider-kQ8RqtRT.cjs"),T=require("../../CategoryPageProvider-DwqxtD8L.cjs"),m=require("../../useActions-DSWNCNrI.cjs"),x=require("../../SerpPageProvider-NSsaKQ7f.cjs"),u=require("../../jsxRuntime.module-B3sGoTIU.cjs"),E=require("../../bindInput-BnKvCky1.cjs"),P=require("../../unique-BXXNictB.cjs"),g=require("preact"),S=require("../../logger-Boh_C6Bz.cjs"),D=require("preact/hooks"),O=require("../../index.es-Dp6Iaxz3.cjs"),y=require("../../search-DTqo1D6Y.cjs");function B(e){let t;return n=>{t&&clearTimeout(t),t=setTimeout(n,e)}}function f({children:e}){const[t]=D.useErrorBoundary();return t&&S.logger.error("Error caught in ErrorBoundary",t),u.u(g.Fragment,{children:e})}function A(e){return Array.from(document.querySelectorAll(e))}function F(e){const t=[];let n=e.parentNode;for(;n&&n!==document&&n instanceof Element;)t.push(n),n=n.parentNode;return t}function q([e,t],n){const o=r=>{const i=r.target;i instanceof HTMLElement&&e&&i!==e&&i!==t&&!F(i).includes(e)&&n()};return document.addEventListener("click",o),{destroy:()=>{document.removeEventListener("click",o)}}}function I(e,t){e.tabIndex=0,e.addEventListener("blur",t)}function b(e){return typeof e=="string"?{selector:e,position:"last"}:{position:"last",...e}}function H({selector:e,timeout:t=500}){return new Promise(n=>{const o=A(e);if(o.length>0)return n(o);const r=new MutationObserver(()=>{const s=A(e);s.length>0&&(r.disconnect(),clearTimeout(i),n(s))}),i=setTimeout(()=>{r.disconnect(),S.logger.warn(`Timed out (${t}) while waiting for element ${e}`),n([])},t);r.observe(document.body,{childList:!0,subtree:!0})})}const N=g.createContext({reportProductClick:()=>{},reportKeywordClick:()=>{},handleSubmit:()=>{},highlightedElementIndex:-1});function $(e,t){const{dropdown:n,history:o,store:r,input:i,onNavigateToSearch:s}=e,l=(c,h)=>{n.hide(),o.hide(),c&&(o.add(c),r.updateState({historyItems:o.get()}),h&&O.s(a=>a.recordSearchSubmit(c)),i.value=c)};return{reportProductClick:c=>{l(c.name,!1)},reportKeywordClick:c=>{l(c.keyword,!0)},handleSubmit:c=>{l(c.query,!1),s?.(c)},highlightedElementIndex:t.highlightedIndex()}}function k(e,t,n){t.style.display="none";const o=(()=>{if(e.parentElement&&e.parentElement.classList.contains("nosto-autocomplete-wrapper"))return e.parentElement;const a=document.createElement("div");return a.className="nosto-autocomplete-wrapper",e.parentNode?.insertBefore(a,e.nextSibling),a.appendChild(e),a})(),r=b(n),i=r&&document.querySelector(r.selector);if(i){const a=document.createElement("form");a.className="nosto-dropdown-form",a.appendChild(t),r.position==="first"?i.prepend(a):i.appendChild(a)}else o.appendChild(t);const s={index:-1,onChangeListeners:[]},l=(a,d)=>{const p=a.length;p===0&&(d=-1),s.index=d>=0?d%p:p-1,s.onChangeListeners.forEach(v=>v())},c=()=>{s.index=-1,s.onChangeListeners.forEach(a=>a())},h=()=>s.index;return{element:t,hide:()=>{t.style.display="none",c()},show:()=>{t.style.display="inherit"},isOpen:()=>t.style.display!=="none",goDown:()=>{const a=Array.from(t.getElementsByClassName("ns-autocomplete-element")),d=h();l(a,d+1)},goUp:()=>{const a=Array.from(t.getElementsByClassName("ns-autocomplete-element")),d=h();l(a,d-1)},highlight:l,highlightedIndex:h,submitHighlightedItem:a=>{Array.from(t.querySelectorAll(".ns-autocomplete-element"))[a]?.click()},onHighlightChange:a=>{s.onChangeListeners.push(a)}}}function K(e,t){const n=document.createElement("div");return n.className="nosto-autocomplete-dropdown",{...k(e,n,t),element:n}}const C="nosto:search-js:history";function Q(e,t,n){const o=document.createElement("div");return o.className="nosto-autocomplete-history",{...k(e,o,t),element:o,add:i=>{const l=(y.getLocalStorageItem(C)??[]).filter(c=>c!==i).slice(n?-n:0);l.push(i),y.setLocalStorageItem(C,l)},get:()=>(y.getLocalStorageItem(C)??[]).reverse().filter(s=>!!s)}}async function M(e,{config:t,dropdown:n,history:o,renderHistory:r,store:i}){const{historyEnabled:s,minQueryLength:l}=t;if(e.length<l&&s&&r&&!o.isOpen()){o.show();return}if(e.length<l)return;n.hide();const c=i.getState()?.query?.query;c&&c!==e&&await m.newSearch({config:t,store:i},{query:e}),n.show()}async function U(e,t){const{config:n,renderHistory:o,history:r}=t,{historyEnabled:i,minQueryLength:s}=n;if(!o||e.length>=s||!i||r.isOpen())return;const l=R(t,r);r.onHighlightChange(()=>{l(o,r.element)}),r.show();const c=P.measure("renderHistory");l(o,r.element),c()}async function _(e,{config:t,dropdown:n,history:o,store:r,debouncer:i}){const{minQueryLength:s,historyEnabled:l}=t;if(e.length<s&&!l){n.hide(),o.hide();return}if(e.length<s&&l){n.hide(),o.show();return}o.hide(),i(async()=>{m.newSearch({config:t,store:r},{query:e}),n.show()})}function z(e,t,n){const{config:o,debouncer:r}=n;if(r(()=>{}),e.length>=o.minQueryLength)return L(t,n.dropdown,n.history);if(o.historyEnabled)return L(t,n.history,n.dropdown)}function L(e,t,n){if(!t.isOpen()&&e==="ArrowDown"){t.show(),n.hide();return}if(e==="Escape"&&t.hide(),!!t.isOpen()){if(e==="ArrowDown")t.goDown();else if(e==="ArrowUp")t.goUp();else if(e==="Enter"){const o=t.highlightedIndex();return o>=0&&t.submitHighlightedItem(o),t.hide(),o>=0}}}function G(e,{config:t,dropdown:n,history:o,onNavigateToSearch:r,store:i}){n.hide(),o.hide(),!(e.length<t.minQueryLength)&&(o.add(e),i.updateState({historyItems:o.get()}),r?.({query:e}))}async function J(e,t){const{inputCssSelector:n,timeout:o}=e,r=b(n).selector,i=await H({selector:r,timeout:o});if(i.length===0)throw new Error(`No elements found for selector: ${r}`);i.forEach(s=>{V(s,e,t)})}async function V(e,t,n){const{config:o,dropdownCssSelector:r}=t,i=K(e,r),s=Q(e,r,o.historySize),l={...t,input:e,dropdown:i,history:s,store:n,debouncer:B(o.debounceDelay)};X(l),e.setAttribute("data-nosto-element","search-input"),n.updateState({historyItems:s.get()}),E.disableNativeAutocomplete(e),n.onInit(()=>{W(l)}),E.bindInput(e,{onInput:c=>_(c,l),onFocus:c=>U(c,l),onClick:c=>M(c,l),onSubmit:c=>G(c,l),onKeyDown:(c,h)=>z(c,h,l)}),I(s.element,s.hide),I(i.element,i.hide),q([s.element,e],s.hide),q([i.element,e],i.hide)}function W(e){const{dropdown:t,renderAutocomplete:n}=e;if(!n)return;const o=R(e,t);t.onHighlightChange(()=>{o(n,t.element)});const r=P.measure("renderAutocomplete");o(n,t.element),r()}async function X(e){const{input:t,renderSpeechToText:n,config:o,store:r}=e;if(!n)return;const i="ns-autocomplete-voice-position";if(!!t.parentElement?.querySelector(`.${i}`))return;const l=document.createElement("div");l.className=i,t.insertAdjacentElement("afterend",l);const c=await n();g.render(u.u(f,{children:u.u(w.AutocompletePageProvider,{config:o,store:r,children:c})}),l)}function R(e,t){const{config:n,store:o}=e,r=$(e,t);return t.onHighlightChange(()=>{r.highlightedElementIndex=t.highlightedIndex()}),(i,s)=>g.render(u.u(f,{children:u.u(w.AutocompletePageProvider,{config:n,store:o,children:u.u(N,{value:r,children:i()})})}),s)}async function j({cssSelector:e,timeout:t,renderComponent:n}){const o=b(e).selector,r=await H({selector:o,timeout:t??100});if(r.length===0)throw new Error(`No elements found for selector: ${o}`);r.length>1&&S.logger.warn(`Multiple (${r.length}) elements found for selector: ${o}`),g.render(n(),r[0])}async function Y(e,t){const{render:n}=e,o=await n();j({...e,renderComponent:()=>u.u(f,{children:u.u(T.CategoryPageProvider,{store:t,config:e.config,children:o})})})}async function Z(e,t){const{render:n}=e,o=await n();j({...e,renderComponent:()=>u.u(f,{children:u.u(x.SearchPageProvider,{store:t,config:e.config,children:o})})})}async function ee({autocomplete:e,category:t,serp:n}){const o={};if(e){const r=m.createStore({query:e.query});await J({...e,config:w.makeAutocompleteConfig(e.config)},r),o.autocomplete={store:r}}if(t){const r=m.createStore({query:t.query});await Y({...t,config:T.makeCategoryConfig(t.config)},r),o.category={store:r}}if(n){const r=m.createStore({query:n.query});await Z({...n,config:x.makeSerpConfig(n.config)},r),o.serp={store:r}}return o}exports.AutocompleteContext=N;exports.init=ee;
|
|
@@ -16,7 +16,7 @@ function z(e) {
|
|
|
16
16
|
t && clearTimeout(t), t = setTimeout(n, e);
|
|
17
17
|
};
|
|
18
18
|
}
|
|
19
|
-
function
|
|
19
|
+
function h({ children: e }) {
|
|
20
20
|
const [t] = U();
|
|
21
21
|
return t && C.error("Error caught in ErrorBoundary", t), /* @__PURE__ */ d(K, { children: e });
|
|
22
22
|
}
|
|
@@ -80,10 +80,10 @@ const J = Q({
|
|
|
80
80
|
highlightedElementIndex: -1
|
|
81
81
|
});
|
|
82
82
|
function V(e, t) {
|
|
83
|
-
const { dropdown: n, history: o, store: r, input: i, onNavigateToSearch: s } = e, a = (c,
|
|
83
|
+
const { dropdown: n, history: o, store: r, input: i, onNavigateToSearch: s } = e, a = (c, u) => {
|
|
84
84
|
n.hide(), o.hide(), c && (o.add(c), r.updateState({
|
|
85
85
|
historyItems: o.get()
|
|
86
|
-
}),
|
|
86
|
+
}), u && v((l) => l.recordSearchSubmit(c)), i.value = c);
|
|
87
87
|
};
|
|
88
88
|
return {
|
|
89
89
|
reportProductClick: (c) => {
|
|
@@ -116,10 +116,10 @@ function N(e, t, n) {
|
|
|
116
116
|
onChangeListeners: []
|
|
117
117
|
}, a = (l, m) => {
|
|
118
118
|
const f = l.length;
|
|
119
|
-
f === 0 && (m = -1), s.index = m >= 0 ? m % f : f - 1, s.onChangeListeners.forEach((
|
|
119
|
+
f === 0 && (m = -1), s.index = m >= 0 ? m % f : f - 1, s.onChangeListeners.forEach((P) => P());
|
|
120
120
|
}, c = () => {
|
|
121
121
|
s.index = -1, s.onChangeListeners.forEach((l) => l());
|
|
122
|
-
},
|
|
122
|
+
}, u = () => s.index;
|
|
123
123
|
return {
|
|
124
124
|
element: t,
|
|
125
125
|
hide: () => {
|
|
@@ -130,15 +130,15 @@ function N(e, t, n) {
|
|
|
130
130
|
},
|
|
131
131
|
isOpen: () => t.style.display !== "none",
|
|
132
132
|
goDown: () => {
|
|
133
|
-
const l = Array.from(t.getElementsByClassName("ns-autocomplete-element")), m =
|
|
133
|
+
const l = Array.from(t.getElementsByClassName("ns-autocomplete-element")), m = u();
|
|
134
134
|
a(l, m + 1);
|
|
135
135
|
},
|
|
136
136
|
goUp: () => {
|
|
137
|
-
const l = Array.from(t.getElementsByClassName("ns-autocomplete-element")), m =
|
|
137
|
+
const l = Array.from(t.getElementsByClassName("ns-autocomplete-element")), m = u();
|
|
138
138
|
a(l, m - 1);
|
|
139
139
|
},
|
|
140
140
|
highlight: a,
|
|
141
|
-
highlightedIndex:
|
|
141
|
+
highlightedIndex: u,
|
|
142
142
|
submitHighlightedItem: (l) => {
|
|
143
143
|
Array.from(t.querySelectorAll(".ns-autocomplete-element"))[l]?.click();
|
|
144
144
|
},
|
|
@@ -183,7 +183,7 @@ async function Z(e, t) {
|
|
|
183
183
|
const { config: n, renderHistory: o, history: r } = t, { historyEnabled: i, minQueryLength: s } = n;
|
|
184
184
|
if (!o || e.length >= s || !i || r.isOpen())
|
|
185
185
|
return;
|
|
186
|
-
const a =
|
|
186
|
+
const a = q(t, r);
|
|
187
187
|
r.onHighlightChange(() => {
|
|
188
188
|
a(o, r.element);
|
|
189
189
|
}), r.show();
|
|
@@ -264,14 +264,14 @@ async function oe(e, t, n) {
|
|
|
264
264
|
onFocus: (c) => Z(c, a),
|
|
265
265
|
onClick: (c) => Y(c, a),
|
|
266
266
|
onSubmit: (c) => te(c, a),
|
|
267
|
-
onKeyDown: (c,
|
|
267
|
+
onKeyDown: (c, u) => ee(c, u, a)
|
|
268
268
|
}), A(s.element, s.hide), A(i.element, i.hide), E([s.element, e], s.hide), E([i.element, e], i.hide);
|
|
269
269
|
}
|
|
270
270
|
function re(e) {
|
|
271
271
|
const { dropdown: t, renderAutocomplete: n } = e;
|
|
272
272
|
if (!n)
|
|
273
273
|
return;
|
|
274
|
-
const o =
|
|
274
|
+
const o = q(e, t);
|
|
275
275
|
t.onHighlightChange(() => {
|
|
276
276
|
o(n, t.element);
|
|
277
277
|
});
|
|
@@ -287,20 +287,20 @@ async function ie(e) {
|
|
|
287
287
|
a.className = i, t.insertAdjacentElement("afterend", a);
|
|
288
288
|
const c = await n();
|
|
289
289
|
y(
|
|
290
|
-
/* @__PURE__ */ d(
|
|
290
|
+
/* @__PURE__ */ d(h, { children: /* @__PURE__ */ d(L, { config: o, store: r, children: c }) }),
|
|
291
291
|
a
|
|
292
292
|
);
|
|
293
293
|
}
|
|
294
|
-
function
|
|
294
|
+
function q(e, t) {
|
|
295
295
|
const { config: n, store: o } = e, r = V(e, t);
|
|
296
296
|
return t.onHighlightChange(() => {
|
|
297
297
|
r.highlightedElementIndex = t.highlightedIndex();
|
|
298
298
|
}), (i, s) => y(
|
|
299
|
-
/* @__PURE__ */ d(
|
|
299
|
+
/* @__PURE__ */ d(h, { children: /* @__PURE__ */ d(L, { config: n, store: o, children: /* @__PURE__ */ d(J, { value: r, children: i() }) }) }),
|
|
300
300
|
s
|
|
301
301
|
);
|
|
302
302
|
}
|
|
303
|
-
async function
|
|
303
|
+
async function k({ cssSelector: e, timeout: t, renderComponent: n }) {
|
|
304
304
|
const o = w(e).selector, r = await H({
|
|
305
305
|
selector: o,
|
|
306
306
|
timeout: t ?? 100
|
|
@@ -311,38 +311,51 @@ async function P({ cssSelector: e, timeout: t, renderComponent: n }) {
|
|
|
311
311
|
}
|
|
312
312
|
async function se(e, t) {
|
|
313
313
|
const { render: n } = e, o = await n();
|
|
314
|
-
|
|
314
|
+
k({
|
|
315
315
|
...e,
|
|
316
|
-
renderComponent: () => /* @__PURE__ */ d(
|
|
316
|
+
renderComponent: () => /* @__PURE__ */ d(h, { children: /* @__PURE__ */ d(R, { store: t, config: e.config, children: o }) })
|
|
317
317
|
});
|
|
318
318
|
}
|
|
319
319
|
async function ce(e, t) {
|
|
320
320
|
const { render: n } = e, o = await n();
|
|
321
|
-
|
|
321
|
+
k({
|
|
322
322
|
...e,
|
|
323
|
-
renderComponent: () => /* @__PURE__ */ d(
|
|
323
|
+
renderComponent: () => /* @__PURE__ */ d(h, { children: /* @__PURE__ */ d(F, { store: t, config: e.config, children: o }) })
|
|
324
324
|
});
|
|
325
325
|
}
|
|
326
326
|
async function Se({ autocomplete: e, category: t, serp: n }) {
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
327
|
+
const o = {};
|
|
328
|
+
if (e) {
|
|
329
|
+
const r = g({ query: e.query });
|
|
330
|
+
await ne(
|
|
331
|
+
{
|
|
332
|
+
...e,
|
|
333
|
+
config: D(e.config)
|
|
334
|
+
},
|
|
335
|
+
r
|
|
336
|
+
), o.autocomplete = { store: r };
|
|
337
|
+
}
|
|
338
|
+
if (t) {
|
|
339
|
+
const r = g({ query: t.query });
|
|
340
|
+
await se(
|
|
341
|
+
{
|
|
342
|
+
...t,
|
|
343
|
+
config: B(t.config)
|
|
344
|
+
},
|
|
345
|
+
r
|
|
346
|
+
), o.category = { store: r };
|
|
347
|
+
}
|
|
348
|
+
if (n) {
|
|
349
|
+
const r = g({ query: n.query });
|
|
350
|
+
await ce(
|
|
351
|
+
{
|
|
352
|
+
...n,
|
|
353
|
+
config: O(n.config)
|
|
354
|
+
},
|
|
355
|
+
r
|
|
356
|
+
), o.serp = { store: r };
|
|
357
|
+
}
|
|
358
|
+
return o;
|
|
346
359
|
}
|
|
347
360
|
export {
|
|
348
361
|
J as AutocompleteContext,
|
|
@@ -57,6 +57,10 @@ export type PublicCategoryInjectConfig = {
|
|
|
57
57
|
* Configuration passthrough.
|
|
58
58
|
*/
|
|
59
59
|
config: PublicCategoryConfig;
|
|
60
|
+
/**
|
|
61
|
+
* Query to be used for category.
|
|
62
|
+
*/
|
|
63
|
+
query?: SearchQuery;
|
|
60
64
|
/**
|
|
61
65
|
* Maximum time (in milliseconds) to wait for the DOM to be available.
|
|
62
66
|
* If a selector is unable to find any elements after the timeout, an error will be thrown.
|
|
@@ -80,6 +84,10 @@ export type PublicSerpInjectConfig = {
|
|
|
80
84
|
* Configuration passthrough.
|
|
81
85
|
*/
|
|
82
86
|
config: PublicSerpConfig;
|
|
87
|
+
/**
|
|
88
|
+
* Query to be used for search.
|
|
89
|
+
*/
|
|
90
|
+
query?: SearchQuery;
|
|
83
91
|
/**
|
|
84
92
|
* Maximum time (in milliseconds) to wait for the DOM to be available.
|
|
85
93
|
* If a selector is unable to find any elements after the timeout, an error will be thrown.
|
|
@@ -1,2 +1,8 @@
|
|
|
1
|
+
import { Store } from '../../common/src/store/store';
|
|
1
2
|
import { InitConfig } from './config';
|
|
2
|
-
export
|
|
3
|
+
export type InitResult<T extends InitConfig> = {
|
|
4
|
+
[K in keyof T as T[K] extends undefined ? never : K]: {
|
|
5
|
+
store: Store;
|
|
6
|
+
};
|
|
7
|
+
};
|
|
8
|
+
export declare function init<T extends InitConfig>({ autocomplete, category, serp }: T): Promise<InitResult<T>>;
|