|
| 1 | +requirejs([ |
| 2 | + 'algoliaBundle', |
| 3 | + 'Develo_Typesense/js/typesense.min', |
| 4 | + 'domReady!' |
| 5 | +], function (algoliaBundle, Typesense) { |
| 6 | + algoliaBundle.$(function ($) { |
| 7 | + |
| 8 | + const {autocomplete} = algoliaBundle; |
| 9 | + const {resultURL} = algoliaConfig; |
| 10 | + |
| 11 | + if (algoliaConfig.autocomplete.nbOfProductsSuggestions > 0) { |
| 12 | + algoliaConfig.autocomplete.sections.unshift({ |
| 13 | + hitsPerPage: algoliaConfig.autocomplete.nbOfProductsSuggestions, |
| 14 | + label: algoliaConfig.translations.products, |
| 15 | + name: "products" |
| 16 | + }); |
| 17 | + } |
| 18 | + |
| 19 | + if (algoliaConfig.autocomplete.nbOfCategoriesSuggestions > 0) { |
| 20 | + algoliaConfig.autocomplete.sections.unshift({ |
| 21 | + hitsPerPage: algoliaConfig.autocomplete.nbOfCategoriesSuggestions, |
| 22 | + label: algoliaConfig.translations.categories, |
| 23 | + name: "categories" |
| 24 | + }); |
| 25 | + } |
| 26 | + |
| 27 | + if (algoliaConfig.autocomplete.nbOfQueriesSuggestions > 0) { |
| 28 | + algoliaConfig.autocomplete.sections.unshift({ |
| 29 | + hitsPerPage: algoliaConfig.autocomplete.nbOfQueriesSuggestions, |
| 30 | + label: '', |
| 31 | + name: "suggestions" |
| 32 | + }); |
| 33 | + } |
| 34 | + |
| 35 | + algoliaConfig.autocomplete.templates = { |
| 36 | + products: algoliaBundle.Hogan.compile($('#autocomplete_products_template').html()), |
| 37 | + categories: algoliaBundle.Hogan.compile($('#autocomplete_categories_template').html()), |
| 38 | + pages: algoliaBundle.Hogan.compile($('#autocomplete_pages_template').html()) |
| 39 | + }; |
| 40 | + |
| 41 | + const getQueryBy = function (name) { |
| 42 | + if ( |
| 43 | + typeof algoliaConfig.typesense_searchable !== 'undefined' && |
| 44 | + typeof algoliaConfig.typesense_searchable[name] !== 'undefined' |
| 45 | + ) { |
| 46 | + return algoliaConfig.typesense_searchable[name]; |
| 47 | + } |
| 48 | + |
| 49 | + return 'name' |
| 50 | + } |
| 51 | + |
| 52 | +// taken from common.js (autocomplete v0) and adopted to autocomplete v1 |
| 53 | + const getAutocompleteSource = function ({section, setContext}) { |
| 54 | + if (section.hitsPerPage <= 0) |
| 55 | + return null; |
| 56 | + |
| 57 | + var options = { |
| 58 | + hitsPerPage: section.hitsPerPage, |
| 59 | + analyticsTags: 'autocomplete', |
| 60 | + clickAnalytics: true |
| 61 | + }; |
| 62 | + |
| 63 | + var source = {}; |
| 64 | + |
| 65 | + var templates = {}; |
| 66 | + |
| 67 | + switch (section.name) { |
| 68 | + case 'products': |
| 69 | + options.numericFilters = 'visibility_search=1'; |
| 70 | + options.ruleContexts = ['magento_filters', '']; // Empty context to keep BC for already create rules in dashboard |
| 71 | + break; |
| 72 | + case 'categories': |
| 73 | + if (algoliaConfig.showCatsNotIncludedInNavigation === false) { |
| 74 | + options.numericFilters = 'include_in_menu=1'; |
| 75 | + } |
| 76 | + break; |
| 77 | + } |
| 78 | + |
| 79 | + templates = { |
| 80 | + header({html}) { |
| 81 | + return html`<h3>${section.label}</h3>`; |
| 82 | + }, |
| 83 | + item({item, html}) { |
| 84 | + const innerHtml = algoliaConfig.autocomplete.templates[section.name].render(item); |
| 85 | + |
| 86 | + return html`<div dangerouslySetInnerHTML=${{ __html: innerHtml }}></div>` |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + source = { |
| 91 | + ...options, |
| 92 | + indexName: algoliaConfig.indexName + "_" + section.name, |
| 93 | + name: section.name, |
| 94 | + templates |
| 95 | + }; |
| 96 | + |
| 97 | + return source; |
| 98 | + }; |
| 99 | + |
| 100 | + const plugins = [] |
| 101 | + |
| 102 | + if (window.initExtraAlgoliaConfiguration) { |
| 103 | + const {plugins: extraPlugins} = initExtraAlgoliaConfiguration(algoliaConfig) |
| 104 | + plugins.push(...extraPlugins) |
| 105 | + } |
| 106 | + |
| 107 | + autocomplete({ |
| 108 | + container: '#algoliaAutocomplete', |
| 109 | + placeholder: algoliaConfig.translations.placeholder, |
| 110 | + debug: algoliaConfig.autocomplete.isDebugEnabled, |
| 111 | + plugins, |
| 112 | + detachedMediaQuery: 'none', |
| 113 | + onSubmit: (params) => { |
| 114 | + window.location.href = `${resultURL}?q=${params.state.query}` |
| 115 | + }, |
| 116 | + classNames: { |
| 117 | + list: 'w-full flex flex-wrap py-4 px-2', |
| 118 | + item: 'w-full lg:w-1/2 p-2 hover:bg-gray-200', |
| 119 | + sourceHeader: 'px-2 py-4 uppercase tracking-widest text-blue-500', |
| 120 | + source: 'flex flex-col', |
| 121 | + panel: 'mx-4 absolute w-full bg-white z-50 border border-gray-300', |
| 122 | + input: 'w-full p-2 text-base lg:text-lg leading-7 tracking-wider border border-gray-300', |
| 123 | + form: 'w-full relative flex items-center', |
| 124 | + inputWrapper: 'flex-grow px-4', |
| 125 | + inputWrapperPrefix: 'hidden', |
| 126 | + inputWrapperSuffix: 'hidden', |
| 127 | + label: 'm-0 leading-none', |
| 128 | + submitButton: 'leading-none' |
| 129 | + }, |
| 130 | + async getSources({query, setContext}) { |
| 131 | + /** Setup autocomplete data sources **/ |
| 132 | + var sources = []; |
| 133 | + for (let i = 0; i < algoliaConfig.autocomplete.sections.length; i++) { |
| 134 | + |
| 135 | + let section = algoliaConfig.autocomplete.sections[i]; |
| 136 | + |
| 137 | + var source = getAutocompleteSource({section, setContext}); |
| 138 | + |
| 139 | + // autocomplete v1 adapter |
| 140 | + if (source) { |
| 141 | + |
| 142 | + let typesenseClient = new Typesense.Client(algoliaConfig.typesense.config) |
| 143 | + |
| 144 | + const results = await typesenseClient.collections(source.indexName).documents().search({ |
| 145 | + q: query, |
| 146 | + query_by: getQueryBy(source.name), |
| 147 | + per_page: source.hitsPerPage |
| 148 | + }) |
| 149 | + |
| 150 | + sources.push({ |
| 151 | + sourceId: source.name, |
| 152 | + query, |
| 153 | + getItems() { |
| 154 | + return results.hits.map(hit => ( |
| 155 | + hit.document |
| 156 | + )); |
| 157 | + }, |
| 158 | + templates: source.templates |
| 159 | + }); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + return sources; |
| 164 | + }, |
| 165 | + |
| 166 | + render({elements, render, html}, root) { |
| 167 | + const {categories, pages, products} = elements; |
| 168 | + |
| 169 | + render( |
| 170 | + html`<div class="relative w-full flex flex-col lg:flex-row"> |
| 171 | + <div class="w-full lg:order-1 lg:border-l-2">${products}</div> |
| 172 | + <div class="w-full px-1 pb-10 md:px-2">${categories} ${pages}</div> |
| 173 | + </div>`, |
| 174 | + root |
| 175 | + ); |
| 176 | + }, |
| 177 | + |
| 178 | + renderNoResults({state, render, html}, root) { |
| 179 | + const suggestions = []; |
| 180 | + |
| 181 | + if (algoliaConfig.showSuggestionsOnNoResultsPage && algoliaConfig.popularQueries.length > 0) { |
| 182 | + algoliaConfig.popularQueries |
| 183 | + .slice(0, Math.min(3, algoliaConfig.popularQueries.length)) |
| 184 | + .forEach(function (query) { |
| 185 | + suggestions.push({ |
| 186 | + url: algoliaConfig.baseUrl + '/catalogsearch/result/?q=' + encodeURIComponent(query), |
| 187 | + query |
| 188 | + }); |
| 189 | + }); |
| 190 | + } |
| 191 | + |
| 192 | + render(html` |
| 193 | + <div class="p-4 lg:p-6"> |
| 194 | + <div class="lx:mb-2"> |
| 195 | + <span class="pr-1 text-base xl:text-lg font-bold tracking-wide">${algoliaConfig.translations.noProducts}</span> |
| 196 | + <span class="text-base font-bold tracking-wide">"${state.query}"</span> |
| 197 | + </div> |
| 198 | + <div class="see-all"> |
| 199 | + ${(algoliaConfig.showSuggestionsOnNoResultsPage && suggestions.length > 0 ? |
| 200 | + html`<div class="py-4 lg:py-6"> |
| 201 | + <span class="pr-1 text-sm xl:text-base font-bold tracking-wider">${algoliaConfig.translations.popularQueries}</span> |
| 202 | + ${suggestions.map(({url, query}) => html`<a class="text-sm xl:text-base text-gray-600 tracking-wide font-semibold hover:underline" href="${url}">${query}</a>`)} |
| 203 | + </div>` : '') |
| 204 | + } |
| 205 | + <a class="py-2 text-sm xl:text-base text-gray-600 tracking-wide font-bold hover:underline" href="${algoliaConfig.baseUrl}/catalogsearch/result/?q=__empty__">${algoliaConfig.translations.seeAll}</a> |
| 206 | + </div> |
| 207 | + </div>`, root); |
| 208 | + }, |
| 209 | + } |
| 210 | + ); |
| 211 | + }) |
| 212 | +}); |
0 commit comments