Last active
August 10, 2016 15:26
-
-
Save m0zziter/b7c2f00afd3684e0642060d19617ab2f to your computer and use it in GitHub Desktop.
algolia magento 2 - commonjs.phtml minified html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php /** @var \Algolia\AlgoliaSearch\Block\Algolia $block */ $config = $block->getConfigHelper(); $catalogSearchHelper = $block->getCatalogSearchHelper(); $product_helper = $block->getProductHelper(); $algolia_helper = $block->getAlgoliaHelper(); $base_url = rtrim($block->getBaseUrl(), '/'); $isSearchPage = false; $isCategoryPage = false; $currency_code = $block->getCurrencyCode(); $group_id = $block->getGroupId(); $price_key = $block->getPriceKey(); $allDepartments = "All departments"; $query = ''; $refinement_key = ''; $refinement_value = ''; $path = ''; /** * Handle category replacement */ if ($config->isInstantEnabled() && $config->replaceCategories() && $block->getRequest()->getControllerName() == 'category') { $category = $block->getCurrentCategory(); if ($category && $category->getDisplayMode() !== 'PAGE') { $category->getUrlInstance()->setStore($block->getStoreId()); foreach ($category->getPathIds() as $treeCategoryId) { if ($path != '') { $path .= ' } $path .= $product_helper->getCategoryName($treeCategoryId, $block->getStoreId()); } $indexName = $product_helper->getIndexName($block->getStoreId()); $category_url = $category->getUrl($category); $isSearchPage = true; $isCategoryPage = true; } } /** * Handle search */ if ($config->isInstantEnabled()) { $pageIdentifier = $block->getRequest()->getFullActionName(); if ($pageIdentifier === 'catalogsearch_result_index') { $query = $catalogSearchHelper->getEscapedQueryText(); if ($query == '__empty__') { $query = ''; } $product_helper = $block->getProductHelper(); $refinement_key = $block->getRequest()->getParam('refinement_key'); if ($refinement_key !== null) { $refinement_value = $query; $query = ""; } else { $refinement_key = ""; } $isSearchPage = true; } } if ($config->isInstantEnabled() && $isSearchPage) { echo ' <script> | |
document.write(\'<style type="text/css"> ' . $config->getInstantSelector() . '{display:none}</style>\');</script>'; } ?> <script> | |
requirejs(['algoliaBundle'], function(algoliaBundle) { | |
algoliaBundle.$(function ($) { | |
window.algoliaConfig = { | |
instant: { | |
enabled: <?php echo $config->isInstantEnabled() ? "true" : "false"; ?>, | |
selector: '<?php echo $config->getInstantSelector(); ?>', | |
isAddToCartEnabled: <?php echo $config->isAddToCartEnable() ? "true" : "false"; ?> | |
}, | |
autocomplete: { | |
enabled: <?php echo $config->isAutoCompleteEnabled() ? "true" : "false"; ?>, | |
selector: '<?php echo $config->getAutocompleteSelector(); ?>', | |
sections: <?php echo json_encode($config->getAutocompleteSections()); ?>, | |
nbOfProductsSuggestions: '<?php echo $config->getNumberOfProductsSuggestions(); ?>', | |
nbOfCategoriesSuggestions: '<?php echo $config->getNumberOfCategoriesSuggestions(); ?>', | |
nbOfQueriesSuggestions: '<?php echo $config->getNumberOfQueriesSuggestions(); ?>' | |
}, | |
extensionVersion: '<?php echo $config->getExtensionVersion() ?>', | |
applicationId: '<?php echo $config->getApplicationID() ?>', | |
indexName: '<?php echo $product_helper->getBaseIndexName(); ?>', | |
apiKey: '<?php echo $algolia_helper->generateSearchSecuredApiKey($config->getSearchOnlyAPIKey(), $config->getAttributesToRetrieve($group_id)) ?>', | |
facets: <?php echo json_encode($config->getFacets()); ?>, | |
hitsPerPage: <?php echo (int) $config->getNumberOfProductResults() ?>, | |
sortingIndices: <?php echo json_encode(array_values($config->getSortingIndices())); ?>, | |
isSearchPage: <?php echo $isSearchPage ? "true" : "false" ?>, | |
isCategoryPage: <?php echo $isCategoryPage ? "true" : "false" ?>, | |
removeBranding: <?php echo $config->isRemoveBranding() ? "true" : "false"; ?>, | |
priceKey: '<?php echo $price_key; ?>', | |
currencySymbol: '<?php echo $block->getCurrencySymbol(); ?>', | |
currency_code: '<?php echo $currency_code; ?>', | |
maxValuesPerFacet: <?php echo (int) $config->getMaxValuesPerFacet(); ?>, | |
autofocus: true, | |
request: { | |
query: <?php echo json_encode(array("value" => html_entity_decode($query))); ?>.value, | |
refinement_key: '<?php echo $refinement_key; ?>', | |
refinement_value: '<?php echo $refinement_value; ?>', | |
path: '<?php echo $path; ?>' | |
}, | |
show_cats_not_included_in_navigation: <?php echo $config->showCatsNotIncludedInNavigation() ? "true" : "false"; ?>, | |
showSuggestionsOnNoResultsPage: <?php echo $config->showSuggestionsOnNoResultsPage() ? "true" : "false"; ?>, | |
baseUrl: '<?php echo $base_url ?>', | |
popularQueries: <?php echo json_encode($config->getPopularQueries()); ?> | |
}; | |
window.transformHit = function (hit, price_key) { | |
if (Array.isArray(hit.categories)) | |
hit.categories = hit.categories.join(', '); | |
if (hit._highlightResult.categories_without_path && Array.isArray(hit.categories_without_path)) { | |
hit.categories_without_path = $.map(hit._highlightResult.categories_without_path, function (category) { | |
return category.value; | |
}); | |
hit.categories_without_path = hit.categories_without_path.join(', '); | |
} | |
if (Array.isArray(hit.color)) { | |
var colors = []; | |
$.each(hit._highlightResult.color, function (i, color) { | |
if (color.matchLevel === 'none') { | |
return; | |
} | |
colors.push(color.value); | |
}); | |
colors = colors.join(', '); | |
hit._highlightResult.color = {value: colors}; | |
} | |
else { | |
if (hit._highlightResult.color && hit._highlightResult.color.matchLevel === 'none') { | |
hit._highlightResult.color = {value: ''}; | |
} | |
} | |
if (hit._highlightResult.color && hit._highlightResult.color.value && hit.categories_without_path) { | |
if (hit.categories_without_path.indexOf('<em>') === -1 && hit._highlightResult.color.value.indexOf('<em>') !== -1) { | |
hit.categories_without_path = ''; | |
} | |
} | |
if (Array.isArray(hit._highlightResult.name)) | |
hit._highlightResult.name = hit._highlightResult.name[0]; | |
if (Array.isArray(hit.price)) | |
hit.price = hit.price[0]; | |
if (price_key !== '.' + algoliaConfig.currency_code + '.default' && hit['price'] !== undefined && | |
hit['price'][algoliaConfig.currency_code][price_key.substr(1) + '_formated'] !== hit['price'][algoliaConfig.currency_code]['default_formated']) { | |
hit['price'][algoliaConfig.currency_code][price_key.substr(1) + '_original_formated'] = hit['price'][algoliaConfig.currency_code]['default_formated']; | |
} | |
return hit; | |
}; | |
window.getFacetWidget = function (facet, templates) { | |
if (facet.type === 'priceRanges') { | |
return algoliaBundle.instantsearch.widgets.priceRanges({ | |
container: facet.wrapper.appendChild(document.createElement('div')), | |
attributeName: facet.attribute, | |
labels: { | |
currency: algoliaConfig.currencySymbol, | |
separator: '<?php echo __("to"); ?>', | |
button: '<?php echo __("Go"); ?>' | |
}, | |
templates: templates, | |
cssClasses: { | |
root: 'facet conjunctive' | |
} | |
}) | |
} | |
var listItemTemplate = '<label class="{{cssClasses.label}}">' + | |
'<input type="checkbox" class="{{cssClasses.checkbox}}" value="{{name}}" {{#isRefined}}checked{{/isRefined}} />{{name}}' + | |
'{{#isRefined}}<img class="cross-circle" src="<?php echo $block->getViewFileUrl('Algolia_AlgoliaSearch::cross-circle.png'); ?>"/>{{/isRefined}}' + | |
'<span class="{{cssClasses.count}}">{{#helpers.formatNumber}}{{count}}{{/helpers.formatNumber}}</span>' + | |
'</label>'; | |
if (facet.type === 'conjunctive') { | |
templates.item = listItemTemplate; | |
return algoliaBundle.instantsearch.widgets.refinementList({ | |
container: facet.wrapper.appendChild(document.createElement('div')), | |
attributeName: facet.attribute, | |
limit: algoliaConfig.maxValuesPerFacet, | |
operator: 'and', | |
templates: templates, | |
cssClasses: { | |
root: 'facet conjunctive' | |
} | |
}); | |
} | |
if (facet.type === 'disjunctive') { | |
templates.item = listItemTemplate; | |
return algoliaBundle.instantsearch.widgets.refinementList({ | |
container: facet.wrapper.appendChild(document.createElement('div')), | |
attributeName: facet.attribute, | |
limit: algoliaConfig.maxValuesPerFacet, | |
operator: 'or', | |
templates: templates, | |
cssClasses: { | |
root: 'facet disjunctive' | |
} | |
}); | |
} | |
if (facet.type == 'slider') { | |
return algoliaBundle.instantsearch.widgets.rangeSlider({ | |
container: facet.wrapper.appendChild(document.createElement('div')), | |
attributeName: facet.attribute, | |
templates: templates, | |
cssClasses: { | |
root: 'facet slider' | |
}, | |
tooltips: { | |
format: function(formattedValue) { | |
return parseInt(formattedValue); | |
} | |
} | |
}); | |
} | |
}; | |
window.getAutocompleteSource = function (section, algolia_client, $, i) { | |
if (section.hitsPerPage <= 0) | |
return null; | |
var options = { | |
hitsPerPage: section.hitsPerPage, | |
analyticsTags: 'autocomplete' | |
}; | |
var source; | |
if (section.name === "products") { | |
options.facets = ['categories.level0']; | |
options.numericFilters = 'visibility_search=1'; | |
source = { | |
source: $.fn.autocomplete.sources.hits(algolia_client.initIndex(algoliaConfig.indexName + "_" + section.name), options), | |
name: section.name, | |
templates: { | |
empty: function (query) { | |
var template = '<div class="aa-no-results-products">' + | |
'<div class="title"><?php echo __('No products for query'); ?> "' + $("<div>").text(query.query).html() + '"</div>'; | |
var suggestions = []; | |
if (algoliaConfig.showSuggestionsOnNoResultsPage && algoliaConfig.popularQueries.length > 0) { | |
$.each(algoliaConfig.popularQueries.slice(0, Math.min(3, algoliaConfig.popularQueries.length)), function (i, query) { | |
query = $('<div>').html(query).text(); | |
suggestions.push('<a href="' + algoliaConfig.baseUrl + '/catalogsearch/result/?q=' + encodeURIComponent(query) + '">' + query + '</a>'); | |
}); | |
template += '<div class="suggestions"><div><?php echo __('You can try one of the popular search queries'); ?></div>'; | |
template += '<div>' + suggestions.join(', ') + '</div>'; | |
template += '</div>'; | |
} | |
template += '<div class="see-all">' + (suggestions.length > 0 ? '<?php echo __('or'); ?> ' : '') + '<a href="' + algoliaConfig.baseUrl + '/catalogsearch/result/?q=__empty__"><?php echo __('See all products'); ?></a></div>' + | |
'</div>'; | |
return template; | |
}, | |
suggestion: function (hit) { | |
hit = transformHit(hit, algoliaConfig.priceKey) | |
hit.displayKey = hit.displayKey || hit.name; | |
return algoliaConfig.autocomplete.templates[section.name].render(hit); | |
} | |
} | |
}; | |
} | |
else if (section.name === "categories" || section.name === "pages") | |
{ | |
if (section.name === "categories" && algoliaConfig.show_cats_not_included_in_navigation == false) { | |
options.numericFilters = 'include_in_menu=1'; | |
} | |
source = { | |
source: $.fn.autocomplete.sources.hits(algolia_client.initIndex(algoliaConfig.indexName + "_" + section.name), options), | |
name: i, | |
templates: { | |
empty: '<div class="aa-no-results"><?php echo __("No results"); ?></div>', | |
suggestion: function (hit) { | |
if (section.name === 'categories') { | |
hit.displayKey = hit.path; | |
} | |
if (hit._snippetResult && hit._snippetResult.content && hit._snippetResult.content.value.length > 0) { | |
hit.content = hit._snippetResult.content.value; | |
if (hit.content.charAt(0).toUpperCase() !== hit.content.charAt(0)) { | |
hit.content = '… ' + hit.content; | |
} | |
if ($.inArray(hit.content.charAt(hit.content.length - 1), ['.', '!', '?'])) { | |
hit.content = hit.content + ' …'; | |
} | |
if (hit.content.indexOf('<em>') === -1) { | |
hit.content = ''; | |
} | |
} | |
hit.displayKey = hit.displayKey || hit.name; | |
return algoliaConfig.autocomplete.templates[section.name].render(hit); | |
} | |
} | |
}; | |
} | |
else if (section.name === "suggestions") | |
{ | |
var suggestions_index = algolia_client.initIndex(algoliaConfig.indexName + "_suggestions"); | |
var products_index = algolia_client.initIndex(algoliaConfig.indexName + "_products"); | |
source = { | |
source: $.fn.autocomplete.sources.popularIn(suggestions_index, { | |
hitsPerPage: section.hitsPerPage | |
}, { | |
source: 'query', | |
index: products_index, | |
facets: ['categories.level0'], | |
hitsPerPage: 0, | |
typoTolerance: false, | |
maxValuesPerFacet: 1, | |
analytics: false | |
}, { | |
includeAll: true, | |
allTitle: '<?php echo __($allDepartments) ?>' | |
}), | |
displayKey: 'query', | |
name: section.name, | |
templates: { | |
suggestion: function (hit) { | |
if (hit.facet) { | |
hit.category = hit.facet.value; | |
} | |
if (hit.facet && hit.facet.value !== "<?php echo $allDepartments; ?>") { | |
hit.url = algoliaConfig.baseUrl + '/catalogsearch/result/?q=' + hit.query + '#q=' + hit.query + '&hFR[categories.level0][0]=' + encodeURIComponent(hit.category) + '&idx=' + algoliaConfig.indexName + '_products'; | |
} else { | |
hit.url = algoliaConfig.baseUrl + '/catalogsearch/result/?q=' + hit.query; | |
} | |
return algoliaConfig.autocomplete.templates.suggestions.render(hit); | |
} | |
} | |
}; | |
} else { | |
/** If is not products, categories, pages or suggestions, it's additional section **/ | |
var index = algolia_client.initIndex(algoliaConfig.indexName + "_section_" + section.name); | |
source = { | |
source: $.fn.autocomplete.sources.hits(index, { | |
hitsPerPage: section.hitsPerPage, | |
analyticsTags: 'autocomplete' | |
}), | |
displayKey: 'value', | |
name: i, | |
templates: { | |
suggestion: function (hit) { | |
hit.url = algoliaConfig.baseUrl + '/catalogsearch/result/?q=' + hit.value + '&refinement_key=' + section.name; | |
return algoliaConfig.autocomplete.templates.additionnalSection.render(hit); | |
} | |
} | |
}; | |
} | |
if (section.name === 'products') { | |
source.templates.footer = function (query, content) { | |
var keys = []; | |
for (var key in content.facets['categories.level0']) { | |
var url = algoliaConfig.baseUrl + '/catalogsearch/result/?q=' + encodeURIComponent(query.query) + '#q=' + encodeURIComponent(query.query) + '&hFR[categories.level0][0]=' + encodeURIComponent(key) + '&idx=' + algoliaConfig.indexName + '_products'; | |
keys.push({ | |
key: key, | |
value: content.facets['categories.level0'][key], | |
url: url | |
}); | |
} | |
keys.sort(function (a, b) { | |
return b.value - a.value; | |
}); | |
var ors = ''; | |
if (keys.length > 0) { | |
ors += '<span><a href="' + keys[0].url + '">' + keys[0].key + '</a></span>'; | |
} | |
if (keys.length > 1) { | |
ors += ', <span><a href="' + keys[1].url + '">' + keys[1].key + '</a></span>'; | |
} | |
var allUrl = algoliaConfig.baseUrl + '/catalogsearch/result/?q=' + encodeURIComponent(query.query); | |
var returnFooter = '<div id="autocomplete-products-footer"><?php echo __('See products in'); ?> <span><a href="' + allUrl + '"><?php echo __('All departments'); ?></a></span> (' + content.nbHits + ')'; | |
if(ors && algoliaConfig.instant.enabled) { | |
returnFooter += ' <?php echo __('or in'); ?> ' + ors; | |
} | |
returnFooter += '</div>'; | |
return returnFooter; | |
} | |
} | |
if (section.name !== 'suggestions' && section.name !== 'products') { | |
source.templates.header = '<div class="category">' + (section.label ? section.label : section.name) + '</div>'; | |
} | |
return source; | |
}; | |
window.fixAutocompleteCssHeight = function () { | |
if ($(document).width() > 768) { | |
$(".other-sections").css('min-height', '0'); | |
$(".aa-dataset-products").css('min-height', '0'); | |
var height = Math.max($(".other-sections").outerHeight(), $(".aa-dataset-products").outerHeight()); | |
$(".aa-dataset-products").css('min-height', height); | |
} | |
}; | |
window.fixAutocompleteCssSticky = function (menu) { | |
var dropdown_menu = $('#algolia-autocomplete-container .aa-dropdown-menu'); | |
var autocomplete_container = $('#algolia-autocomplete-container'); | |
autocomplete_container.removeClass('reverse'); | |
/** Reset computation **/ | |
dropdown_menu.css('top', '0px'); | |
/** Stick menu vertically to the input **/ | |
var targetOffset = Math.round(menu.offset().top + menu.outerHeight()); | |
var currentOffset = Math.round(autocomplete_container.offset().top); | |
dropdown_menu.css('top', (targetOffset - currentOffset) + 'px'); | |
if (menu.offset().left + menu.outerWidth() / 2 > $(document).width() / 2) { | |
/** Stick menu horizontally align on right to the input **/ | |
dropdown_menu.css('right', '0px'); | |
dropdown_menu.css('left', 'auto'); | |
var targetOffset = Math.round(menu.offset().left + menu.outerWidth()); | |
var currentOffset = Math.round(autocomplete_container.offset().left + autocomplete_container.outerWidth()); | |
dropdown_menu.css('right', (currentOffset - targetOffset) + 'px'); | |
} | |
else { | |
/** Stick menu horizontally align on left to the input **/ | |
dropdown_menu.css('left', 'auto'); | |
dropdown_menu.css('right', '0px'); | |
autocomplete_container.addClass('reverse'); | |
var targetOffset = Math.round(menu.offset().left); | |
var currentOffset = Math.round(autocomplete_container.offset().left); | |
dropdown_menu.css('left', (targetOffset - currentOffset) + 'px'); | |
} | |
}; | |
$(algoliaConfig.autocomplete.selector).each(function () { | |
$(this).closest('form').submit(function (e) { | |
var query = $(this).find(algoliaConfig.autocomplete.selector).val(); | |
if (algoliaConfig.instant.enabled && query == '') | |
query = '__empty__'; | |
window.location = $(this).attr('action') + '?q=' + query; | |
return false; | |
}); | |
}); | |
function handleInputCrossAutocomplete(input) { | |
if (input.val().length > 0) { | |
input.closest('#algolia-searchbox').find('.clear-query-autocomplete').show(); | |
input.closest('#algolia-searchbox').find('svg').hide(); | |
} | |
else { | |
input.closest('#algolia-searchbox').find('.clear-query-autocomplete').hide(); | |
input.closest('#algolia-searchbox').find('svg').show(); | |
} | |
} | |
window.focusInstantSearchBar = function (search, instant_search_bar) { | |
if ($(window).width() > 992) { | |
instant_search_bar.focusWithoutScrolling(); | |
if (algoliaConfig.autofocus === false) { | |
instant_search_bar.focus().val(''); | |
} | |
} | |
instant_search_bar.val(search.helper.state.query); | |
}; | |
window.handleInputCrossInstant = function (input) { | |
if (input.val().length > 0) { | |
input.closest('#instant-search-box').find('.clear-query-instant').show(); | |
} | |
else { | |
input.closest('#instant-search-box').find('.clear-query-instant').hide(); | |
} | |
}; | |
var instant_selector = !algoliaConfig.autocomplete.enabled ? ".algolia-search-input" : "#instant-search-bar"; | |
$(document).on('input', algoliaConfig.autocomplete.selector, function () { | |
handleInputCrossAutocomplete($(this)); | |
}); | |
$(document).on('input', instant_selector, function () { | |
handleInputCrossInstant($(this)); | |
}); | |
$(document).on('click', '.clear-query-instant', function () { | |
var input = $(this).closest('#instant-search-box').find('input'); | |
input.val(''); | |
input.get(0).dispatchEvent(new Event('input')); | |
handleInputCrossInstant(input); | |
}); | |
$(document).on('click', '.clear-query-autocomplete', function () { | |
var input = $(this).closest('#algolia-searchbox').find('input'); | |
input.val(''); | |
if(!algoliaConfig.autocomplete.enabled && algoliaConfig.instant.enabled) { | |
input.get(0).dispatchEvent(new Event('input')); | |
} | |
handleInputCrossAutocomplete(input); | |
}); | |
/** Handle small screen **/ | |
$('body').on('click', '#refine-toggle', function () { | |
$('#instant-search-facets-container').toggleClass('hidden-sm').toggleClass('hidden-xs'); | |
if ($(this).html()[0] === '+') | |
$(this).html('- <?php echo __("Refine"); ?>'); | |
else | |
$(this).html('+ <?php echo __("Refine"); ?>'); | |
}); | |
$.fn.focusWithoutScrolling = function(){ | |
var x = window.scrollX, y = window.scrollY; | |
this.focus(); | |
window.scrollTo(x, y); | |
}; | |
}); | |
});</script><!--[if lte IE 9]><script> | |
document.addEventListener("DOMContentLoaded", function(event) { | |
algoliaBundle.$(function ($) { | |
window.algoliaConfig.autofocus = false; | |
}); | |
});</script><![endif]--> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment