2014-05-07 21:28:10 +02:00
|
|
|
var $ = require('unopinionate').selector,
|
|
|
|
template = require('../entry.handlebars');
|
|
|
|
|
|
|
|
$(function() {
|
2014-05-07 23:51:03 +02:00
|
|
|
var $form = $('#search-form'),
|
|
|
|
$input = $form.find('input'),
|
2014-05-07 21:28:10 +02:00
|
|
|
$searchResults = $("#search-results"),
|
2014-05-07 23:51:03 +02:00
|
|
|
$body = $('body'),
|
|
|
|
$clear = $form.find('.clear'),
|
2014-05-07 21:28:10 +02:00
|
|
|
request;
|
|
|
|
|
|
|
|
$form.bind('submit keyup', function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
var q = $input.val();
|
|
|
|
|
|
|
|
$body.addClass('state-search');
|
|
|
|
|
2014-05-07 23:51:03 +02:00
|
|
|
//Switch the icons
|
|
|
|
$clear
|
|
|
|
[q ? 'addClass' : 'removeClass']('icon-cancel')
|
|
|
|
[!q ? 'addClass' : 'removeClass']('icon-search');
|
|
|
|
|
2014-05-07 21:28:10 +02:00
|
|
|
if(q) {
|
|
|
|
if(request) {
|
|
|
|
request.abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
request = $.getJSON('/-/search/' + q, function(results) {
|
|
|
|
if(results.length) {
|
|
|
|
var html = '';
|
|
|
|
|
|
|
|
$.each(results, function(i, package) {
|
|
|
|
html += template(package);
|
|
|
|
});
|
|
|
|
|
|
|
|
$searchResults.html(html);
|
|
|
|
}
|
|
|
|
else {
|
2014-05-07 22:36:03 +02:00
|
|
|
$searchResults.html("<div class='no-results'><big>No Results</big></div>");
|
2014-05-07 21:28:10 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$searchResults.html('');
|
|
|
|
$body.removeClass('state-search');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-05-07 23:51:03 +02:00
|
|
|
$clear.click(function(e) {
|
2014-05-07 21:28:10 +02:00
|
|
|
e.preventDefault();
|
|
|
|
$input.val('');
|
|
|
|
$form.keyup();
|
|
|
|
});
|
|
|
|
});
|