1requirejs(['algoliaBundle'], function(algoliaBundle) { 2 algoliaBundle.$(function ($) { 3 4 /** We have nothing to do here if autocomplete is disabled **/ 5 if (!algoliaConfig.autocomplete.enabled) { 6 return; 7 } 8 9 /** 10 * Set autocomplete templates 11 * For templating is used Hogan library 12 * Docs: http://twitter.github.io/hogan.js/ 13 **/ 14 algoliaConfig.autocomplete.templates = { 15 suggestions: algoliaBundle.Hogan.compile($('#autocomplete_suggestions_template').html()), 16 products: algoliaBundle.Hogan.compile($('#autocomplete_products_template').html()), 17 categories: algoliaBundle.Hogan.compile($('#autocomplete_categories_template').html()), 18 pages: algoliaBundle.Hogan.compile($('#autocomplete_pages_template').html()), 19 additionalSection: algoliaBundle.Hogan.compile($('#autocomplete_extra_template').html()) 20 }; 21 22 /** 23 * Initialise Algolia client 24 * Docs: https://www.algolia.com/doc/api-client/getting-started/instantiate-client-index/ 25 **/ 26 var algolia_client = algoliaBundle.algoliasearch(algoliaConfig.applicationId, algoliaConfig.apiKey); 27 algolia_client.addAlgoliaAgent('Magento2 integration (' + algoliaConfig.extensionVersion + ')'); 28 29 /** Add products and categories that are required sections **/ 30 /** Add autocomplete menu sections **/ 31 if (algoliaConfig.autocomplete.nbOfProductsSuggestions > 0) { 32 algoliaConfig.autocomplete.sections.unshift({ hitsPerPage: algoliaConfig.autocomplete.nbOfProductsSuggestions, label: algoliaConfig.translations.products, name: "products"}); 33 } 34 35 if (algoliaConfig.autocomplete.nbOfCategoriesSuggestions > 0) { 36 algoliaConfig.autocomplete.sections.unshift({ hitsPerPage: algoliaConfig.autocomplete.nbOfCategoriesSuggestions, label: algoliaConfig.translations.categories, name: "categories"}); 37 } 38 39 if (algoliaConfig.autocomplete.nbOfQueriesSuggestions > 0) { 40 algoliaConfig.autocomplete.sections.unshift({ hitsPerPage: algoliaConfig.autocomplete.nbOfQueriesSuggestions, label: '', name: "suggestions"}); 41 } 42 43 /** Setup autocomplete data sources **/ 44 var sources = [], 45 i = 0; 46 $.each(algoliaConfig.autocomplete.sections, function (name, section) { 47 var source = getAutocompleteSource(section, algolia_client, $, i); 48 49 if (source) { 50 sources.push(source); 51 } 52 53 /** Those sections have already specific placeholder, so do not use the default aa-dataset-{i} class **/ 54 if (section.name !== 'suggestions' && section.name !== 'products') { 55 i++; 56 } 57 }); 58 59 /** 60 * Setup the autocomplete search input 61 * For autocomplete feature is used Algolia's autocomplete.js library 62 * Docs: https://github.com/algolia/autocomplete.js 63 **/ 64 $(algoliaConfig.autocomplete.selector).each(function (i) { 65 var menu = $(this); 66 var options = { 67 hint: false, 68 templates: { 69 dropdownMenu: '#menu-template' 70 }, 71 dropdownMenuContainer: "#algolia-autocomplete-container", 72 debug: algoliaConfig.autocomplete.isDebugEnabled 73 }; 74 75 if (isMobile() === true) { 76 // Set debug to true, to be able to remove keyboard and be able to scroll in autocomplete menu 77 options.debug = true; 78 } 79 80 if (algoliaConfig.removeBranding === false) { 81 options.templates.footer = '<div class="footer_algolia"><a href="https://www.algolia.com/?utm_source=magento&utm_medium=link&utm_campaign=magento_autocompletion_menu" title="Search by Algolia" target="_blank"><img src="' +algoliaConfig.urls.logo + '" alt="Search by Algolia" /></a></div>'; 82 } 83 84 sources = algolia.triggerHooks('beforeAutocompleteSources', sources, algolia_client, algoliaBundle); 85 options = algolia.triggerHooks('beforeAutocompleteOptions', options); 86 87 // Keep for backward compatibility 88 if (typeof algoliaHookBeforeAutocompleteStart === 'function') { 89 console.warn('Deprecated! You are using an old API for Algolia\'s front end hooks. ' + 90 'Please, replace your hook method with new hook API. ' + 91 'More information you can find on https://www.algolia.com/doc/integration/magento-2/customize/custom-front-end-events/'); 92 93 var hookResult = algoliaHookBeforeAutocompleteStart(sources, options, algolia_client); 94 95 sources = hookResult.shift(); 96 options = hookResult.shift(); 97 } 98 99 /** Bind autocomplete feature to the input */ 100 var algoliaAutocompleteInstance = $(this).autocomplete(options, sources); 101 algoliaAutocompleteInstance = algolia.triggerHooks('afterAutocompleteStart', algoliaAutocompleteInstance); 102 103 algoliaAutocompleteInstance 104 .parent() 105 .attr('id', 'algolia-autocomplete-tt') 106 .on('autocomplete:updated', function (e) { 107 fixAutocompleteCssSticky(menu); 108 fixAutocompleteCssHeight(menu); 109 }).on('autocomplete:selected', function (e, suggestion, dataset) { 110 location.assign(suggestion.url); 111 }); 112 113 $(window).resize(function () { 114 fixAutocompleteCssSticky(menu); 115 }); 116 }); 117 118 // Hack to handle buggy onclick event on iOS 119 $(algoliaConfig.autocomplete.selector).each(function () { 120 var data = $(this).data('aaAutocomplete'); 121 var dropdown = data.dropdown;
122 var suggestionClass = '.' + dropdown.cssClasses.prefix + dropdown.cssClasses.suggestion; 123 124 var touchmoved; 125 dropdown.$menu.on('touchend', suggestionClass, function (e) { 126 if(touchmoved === false) { 127 e.preventDefault(); 128 e.stopPropagation(); 129 130 var url = $(this).find('a').attr('href'); 131 location.assign(url); 132 } 133 }).on('touchmove', function (){ 134 touchmoved = true; 135 }).on('touchstart', function(){ 136 touchmoved = false; 137 }); 138 }); 139 }); 140});
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.