2017-04-23 13:02:26 -05:00
|
|
|
let $ = require('unopinionate').selector;
|
|
|
|
let template = require('../entry.hbs');
|
2014-05-07 14:28:10 -05:00
|
|
|
|
|
|
|
$(function() {
|
2014-11-12 06:14:37 -05:00
|
|
|
;(function(window, document) {
|
|
|
|
var $form = $('#search-form')
|
|
|
|
var $input = $form.find('input')
|
|
|
|
var $searchResults = $('#search-results')
|
|
|
|
var $pkgListing = $('#all-packages')
|
|
|
|
var $searchBtn = $('.js-search-btn')
|
|
|
|
var request
|
2017-04-20 22:36:28 -05:00
|
|
|
var lastQuery = ''
|
2014-11-12 06:14:37 -05:00
|
|
|
|
|
|
|
var toggle = function(validQuery) {
|
|
|
|
$searchResults.toggleClass('show', validQuery)
|
|
|
|
$pkgListing.toggleClass('hide', validQuery)
|
|
|
|
|
|
|
|
$searchBtn.find('i').toggleClass('icon-cancel', validQuery)
|
|
|
|
$searchBtn.find('i').toggleClass('icon-search', !validQuery)
|
|
|
|
}
|
|
|
|
|
|
|
|
$form.bind('submit keyup', function(e) {
|
2017-04-20 22:36:28 -05:00
|
|
|
var query, isValidQuery
|
2014-11-12 06:14:37 -05:00
|
|
|
|
2017-04-23 13:02:26 -05:00
|
|
|
|
|
|
|
e.preventDefault();
|
2014-11-12 06:14:37 -05:00
|
|
|
|
2017-04-20 22:36:28 -05:00
|
|
|
query = $input.val()
|
|
|
|
isValidQuery = (query !== '')
|
2014-11-12 06:14:37 -05:00
|
|
|
|
2017-04-20 22:36:28 -05:00
|
|
|
toggle(isValidQuery)
|
2014-11-12 06:14:37 -05:00
|
|
|
|
2017-04-20 22:36:28 -05:00
|
|
|
if (!isValidQuery) {
|
2014-11-12 06:14:37 -05:00
|
|
|
if (request && typeof request.abort === 'function') {
|
2017-04-23 13:02:26 -05:00
|
|
|
request.abort();
|
2014-11-12 06:14:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
$searchResults.html('')
|
2017-04-23 13:02:26 -05:00
|
|
|
return;
|
2014-11-12 06:14:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (request && typeof request.abort === 'function') {
|
2017-04-23 13:02:26 -05:00
|
|
|
request.abort();
|
2014-11-12 06:14:37 -05:00
|
|
|
}
|
|
|
|
|
2017-04-20 22:36:28 -05:00
|
|
|
if (query !== lastQuery) {
|
|
|
|
lastQuery = query
|
2014-11-12 06:14:37 -05:00
|
|
|
$searchResults.html(
|
2017-04-23 13:02:26 -05:00
|
|
|
'<img class=\'search-ajax\' src=\'-/static/ajax.gif\' alt=\'Spinner\'/>');
|
2014-11-12 06:14:37 -05:00
|
|
|
}
|
|
|
|
|
2017-04-20 22:36:28 -05:00
|
|
|
request = $.getJSON('-/search/' + query, function( results ) {
|
2014-11-12 06:14:37 -05:00
|
|
|
if (results.length > 0) {
|
2017-04-23 13:02:26 -05:00
|
|
|
let html = '';
|
2014-11-12 06:14:37 -05:00
|
|
|
|
|
|
|
$.each(results, function(i, entry) {
|
2017-04-23 13:02:26 -05:00
|
|
|
html += template(entry);
|
|
|
|
});
|
2014-11-12 06:14:37 -05:00
|
|
|
|
2017-04-23 13:02:26 -05:00
|
|
|
$searchResults.html(html);
|
2014-11-12 06:14:37 -05:00
|
|
|
} else {
|
|
|
|
$searchResults.html(
|
2017-04-23 13:02:26 -05:00
|
|
|
'<div class=\'no-results\'><big>No Results</big></div>');
|
2014-11-12 06:14:37 -05:00
|
|
|
}
|
2016-02-04 13:52:09 -05:00
|
|
|
}).fail(function () {
|
|
|
|
$searchResults.html(
|
|
|
|
"<div class='no-results'><big>No Results</big></div>")
|
2014-11-12 06:14:37 -05:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
$(document).on('click', '.icon-cancel', function(e) {
|
2017-04-23 13:02:26 -05:00
|
|
|
e.preventDefault();
|
|
|
|
$input.val('');
|
|
|
|
$form.keyup();
|
|
|
|
});
|
|
|
|
})(window, window.document);
|
|
|
|
});
|