$(function () {
// Search
$("#search").on('submit', function (e) {
e.preventDefault();
var query = $('#search-box').val();
if (query.length <= 0) {
} else {
$.ajax({
url: '/api/v2/store/platforms/search/?q=' + query,
type: 'GET'
}).done(function (data) { // Success, deal with our success.
var results = data.results;
analytics.track('Searched Products', {
query: query,
results: results.length
});
if (results.length > 0) {
var html = '';
var dropdown = $('.search-results-dropdown');
for (var i = 0; i < results.length; i++) {
html += "
"
+ "" + results[i].name + "
"
+ results[i].author.name + "
"
+ results[i].author.title + "
";
}
dropdown.show().find('ul').html(html);
} else {
var html = 'No results found.';
var dropdown = $('.search-results-dropdown');
dropdown.show().find('ul').html(html);
}
}).fail(function (data) { // Failure, deal with our problems.
console.log(data)
})
}
});
$('.search-results-dropdown').on('click', function (e) {
e.stopPropagation();
});
$('body').click(function (e) {
if (!$(e.target).is('.search-results-dropdown *')) {
$('.search-results-dropdown').hide();
}
});
});