2016-11-02 11:08:59 +01:00

124 lines
4.1 KiB
JavaScript

var cache_categories_product = [];
var cache_categories_post = [];
$(document).ready(function() {
// $('#search_query_block').keyup(function() {
// var search_input_content = $(this).val();
// $.get('http://eu1-search.doofinder.com/5/search?hashid=8483db4a599703a6e069ad2ecf59e17d&query=' + search_input_content);
// });
$('#search_query_block').keyup(function() {
var search_input_content = $(this).val();
searchDoofinder(search_input_content, cache_categories_product, cache_categories_post);
});
$(document).on('click', '#search_result_container_categories_products .content, #search_result_container_categories_articles .content', function() {
var search_input_content = $('#search_query_block').val();
var category = $(this).text();
var type = $(this).parent().data('type');
if (type == 'post') {
if (cache_categories_post.indexOf(category) == -1) {
cache_categories_post.push(category);
}
}
if (type == 'product') {
if (cache_categories_product.indexOf(category) == -1) {
cache_categories_product.push(category);
}
}
searchDoofinder(search_input_content, cache_categories_product, cache_categories_post);
});
$(document).on('click', '#search_result_container_categories_products .remove, #search_result_container_categories_articles .remove', function() {
var search_input_content = $('#search_query_block').val();
var category = $(this).parent().find('.content').text();
var type = $(this).parent().data('type');
if (type == 'post') {
var index = cache_categories_post.indexOf(category);
delete cache_categories_post[index];
}
if (type == 'product') {
var index = cache_categories_product.indexOf(category);
delete cache_categories_product[index];
}
searchDoofinder(search_input_content, cache_categories_product, cache_categories_post);
});
$('#search_result_container').hide();
// getCategories();
});
function searchDoofinder(search_content, cache_categories_product, cache_categories_post) {
$.ajax({
url: '/modules/antadis_doofinder/ajax.php',
dataType: 'JSON',
data: {
action: 'search',
search_content: search_content,
categories: {
category_product : cache_categories_product,
category_post : cache_categories_post
}
},
success: searchBindData,
error: function() {
$('#search_result_container_result').empty();
$('#search_result_container').hide();
}
});
}
function searchBindData(response) {
$('#search_result_container_result').empty();
$('#search_result_container').show();
$('#search_result_container_categories_articles').empty();
$('#search_result_container_categories_products').empty();
if (response.category_post) {
var res = response.category_post.terms;
for (var i in res) {
if (cache_categories_post.indexOf(res[i].term) == -1) {
$('#search_result_container_categories_articles').append('<li class="facets" data-type="post"><span class="content">' + res[i].term + '</span><span class="number">'+res[i].count+'</span></li>');
} else {
$('#search_result_container_categories_articles').append('<li class="facets" data-type="post"><span class="content">' + res[i].term + '</span><span class="number">'+res[i].count+'</span><span class="remove">X</span></li>');
}
}
}
if (response.category_product) {
var res = response.category_product.terms;
for (var i in res) {
if (cache_categories_product.indexOf(res[i].term) == -1) {
$('#search_result_container_categories_products').append('<li class="facets" data-type="product"><span class="content">' + res[i].term + '</span><span class="number">'+res[i].count+'</span></li>');
} else {
$('#search_result_container_categories_products').append('<li class="facets" data-type="product"><span class="content">' + res[i].term + '</span><span class="number">'+res[i].count+'</span><span class="remove">X</span></li>');
}
}
}
if (response.results) {
var results = response.results
for (var i in results) {
$('#search_result_container_result').append('<li> <img src="' + results[i].image_link + '"> <a href="'+results[i].link+'">' + results[i].title + '</a> </li>');
}
}
}