0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2025-01-06 22:40:26 -05:00
verdaccio/lib/GUI/js/search.js

68 lines
1.4 KiB
JavaScript
Raw Normal View History

2014-05-07 14:28:10 -05:00
var $ = require('unopinionate').selector,
2014-05-08 14:47:24 -05:00
template = require('../entry.hbs');
2014-05-07 14:28:10 -05:00
$(function() {
2014-05-08 14:47:24 -05:00
'use strict';
2014-05-07 16:51:03 -05:00
var $form = $('#search-form'),
$input = $form.find('input'),
2014-05-07 14:28:10 -05:00
$searchResults = $("#search-results"),
2014-05-07 16:51:03 -05:00
$body = $('body'),
$clear = $form.find('.clear'),
2014-05-08 14:47:24 -05:00
request,
currentResults;
2014-05-07 14:28:10 -05:00
$form.bind('submit keyup', function(e) {
e.preventDefault();
var q = $input.val();
$body.addClass('state-search');
2014-05-07 16:51:03 -05:00
//Switch the icons
$clear
[q ? 'addClass' : 'removeClass']('icon-cancel')
[!q ? 'addClass' : 'removeClass']('icon-search');
2014-05-07 14:28:10 -05:00
if(q) {
if(request) {
request.abort();
}
2014-05-08 14:47:24 -05:00
if(!currentResults) {
$searchResults.html("<img class='search-ajax' src='/-/static/ajax.gif' alt='Spinner'/>");
}
2014-05-07 14:28:10 -05:00
request = $.getJSON('/-/search/' + q, function(results) {
2014-05-08 14:47:24 -05:00
currentResults = results;
2014-05-07 14:28:10 -05:00
if(results.length) {
var html = '';
2014-05-08 14:47:24 -05:00
$.each(results, function(i, entry) {
html += template(entry);
2014-05-07 14:28:10 -05:00
});
$searchResults.html(html);
}
else {
2014-05-07 15:36:03 -05:00
$searchResults.html("<div class='no-results'><big>No Results</big></div>");
2014-05-07 14:28:10 -05:00
}
});
}
else {
2014-05-08 14:47:24 -05:00
console.log("YAY");
request.abort();
currentResults = null;
2014-05-07 14:28:10 -05:00
$searchResults.html('');
$body.removeClass('state-search');
}
});
2014-05-07 16:51:03 -05:00
$clear.click(function(e) {
2014-05-07 14:28:10 -05:00
e.preventDefault();
$input.val('');
$form.keyup();
});
});