PageSourceSearch

https://www.spscommerce.com/wp-content/plugins/sps-algolia-search/js/algolia-autocomplete.js?ver=1790268592

js spscommerce.com collected 2026-09-24 17:20:45 UTC 5,495 bytes, 139 lines download raw bytes

1// Zero-results dropdown (EN v1). Edit these 3 links when Growth provides the final set.
2var ZERO_RESULTS_LINKS = [
3  { label: 'Products', url: '/products/' },
4  { label: 'Solutions', url: '/solutions/' },
5  { label: 'Resource Center', url: '/resources/' },
6];
7
8// Footer "Ask a question" when there are no matches (Intercom absent -> Contact page).
9var CONTACT_FALLBACK_URL = '/contact/';
10
11function onAskQuestionClick(event, query) {
12  if (window.Intercom) {
13    event.preventDefault();
14    window.Intercom('showNewMessage', 'I was looking for \'' + query + '\' and couldn\'t find it. Can you help?');
15  }
16}
17
18function spsAlgoliaInit() {
19  var container = document.querySelector('#sps-algolia-autocomplete');
20  if (!container || typeof spsAlgoliaConfig === 'undefined') {
21    return;
22  }
23
24  var autocomplete = window['@algolia/autocomplete-js'].autocomplete;
25  var getAlgoliaResults = window['@algolia/autocomplete-preset-algolia'].getAlgoliaResults;
26  var createAlgoliaInsightsPlugin = window['@algolia/autocomplete-plugin-algolia-insights'].createAlgoliaInsightsPlugin;
27  var searchClient = algoliasearch(spsAlgoliaConfig.appId, spsAlgoliaConfig.searchKey);
28
29  var algoliaInsightsPlugin = createAlgoliaInsightsPlugin({ insightsClient: window.aa });
30
31  autocomplete({
32    container: '#sps-algolia-autocomplete',
33    plugins: [algoliaInsightsPlugin],
34    placeholder: spsAlgoliaConfig.placeholder || 'Search',
35    openOnFocus: false,
36    panelContainer: 'body',
37    // Align panel's right edge with the search input (header search sits on the right).
38    panelPlacement: 'end',
39    getSources: function ({ query }) {
40      return [
41        {
42          sourceId: 'results',
43          getItems: function () {
44            return getAlgoliaResults({
45              searchClient: searchClient,
46              queries: [{
47                indexName: spsAlgoliaConfig.indexName,
48                query: query,
49                params: {
50                  hitsPerPage: 6,
51                  filters: 'locale:' + spsAlgoliaConfig.locale,
52                  clickAnalytics: true,
53                },
54              }],
55            });
56          },
57          getItemUrl: function ({ item }) {
58            return item.url;
59          },
60          templates: {
61            item: function ({ item, html }) {
62              return html`
63                <a href="${item.url}" onClick=${() => {
64                  try {
65                    sessionStorage.setItem('algoliaSearchAttribution', JSON.stringify({
66                      queryID: item.__autocomplete_queryID,
67                      objectID: item.objectID,
68                      indexName: item.__autocomplete_indexName || spsAlgoliaConfig.indexName,
69                      destinationPath: new URL(item.url, window.location.origin).pathname,
70                      timestamp: Date.now(),
71                    }));
72                  } catch (e) {}
73                }}>
74                  <span class="aa-ItemTitle">${item.title}</span>
75                  <span class="aa-ItemExcerpt">${item.meta_description}</span>
76                </a>
77              `;
78            },
79            noResults: function ({ html, state }) {
80              if (spsAlgoliaConfig.locale !== 'en') {
81                return 'No results found.';
82              }
83              var query = state.query;
84              return html`
85                <div class="aa-NoResultsPanel">
86                  <p class="aa-NoResultsMessage">No results for "${query}"</p>
87                  <ul class="aa-NoResultsLinks">
88                    ${ZERO_RESULTS_LINKS.map(function (link) {
89                      return html`<li><a href="${link.url}">${link.label}</a></li>`;
90                    })}
91                  </ul>
92                </div>
93              `;
94            },
95            footer: function ({ items, state, html }) {
96              var noResults = items.length === 0 && state.query;
97              if (noResults && spsAlgoliaConfig.locale === 'en') {
98                var query = state.query;
99                return html`<a href="${CONTACT_FALLBACK_URL}" onClick=${(event) => onAskQuestionClick(event, query)}>Chat with SPS</a>`;
100              }
101              var url =
102                spsAlgoliaConfig.resultsPageUrl + '?q=' + encodeURIComponent(state.query) + '&locale=' + encodeURIComponent(spsAlgoliaConfig.locale);
103              return html`<a href="${url}">See all results →</a>`;
104            },
105          },
106        },
107      ];
108    },
109    onSubmit: function ({ state }) {
110      window.location.href =
111        spsAlgoliaConfig.resultsPageUrl + '?q=' + encodeURIComponent(state.query) + '&locale=' + encodeURIComponent(spsAlgoliaConfig.locale);
112    },
113    navigator: {
114      navigate: function ({ itemUrl, item }) {
115        try {
116          sessionStorage.setItem('algoliaSearchAttribution', JSON.stringify({
117            queryID: item && item.__autocomplete_queryID,
118            objectID: item && item.objectID,
119            indexName: (item && item.__autocomplete_indexName) || spsAlgoliaConfig.indexName,
120            destinationPath: new URL(itemUrl, window.location.origin).pathname,
121            timestamp: Date.now(),
122          }));
123        } catch (e) {}
124        window.location.href = itemUrl;
125      },
126    },
127  });
128
129  var input = document.querySelector('#sps-algolia-autocomplete .aa-Input');
130  if (input) {
131    input.classList.add('et-search-field');
132  }
133}
134
135if (document.readyState === 'loading') {
136  document.addEventListener('DOMContentLoaded', spsAlgoliaInit);
137} else {
138  spsAlgoliaInit();
139}

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.