1
0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-11-13 03:35:52 +01:00
verdaccio/lib/GUI/js/search.js

67 lines
1.3 KiB
JavaScript
Raw Normal View History

2014-05-07 21:28:10 +02:00
var $ = require('unopinionate').selector,
2014-05-08 21:47:24 +02:00
template = require('../entry.hbs');
2014-05-07 21:28:10 +02:00
$(function() {
2014-05-08 21:47:24 +02:00
'use strict';
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-08 21:47:24 +02:00
request,
currentResults;
2014-05-07 21:28:10 +02:00
$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();
}
2014-05-08 21:47:24 +02:00
if(!currentResults) {
$searchResults.html("<img class='search-ajax' src='/-/static/ajax.gif' alt='Spinner'/>");
}
2014-05-07 21:28:10 +02:00
request = $.getJSON('/-/search/' + q, function(results) {
2014-05-08 21:47:24 +02:00
currentResults = results;
2014-05-07 21:28:10 +02:00
if(results.length) {
var html = '';
2014-05-08 21:47:24 +02:00
$.each(results, function(i, entry) {
html += template(entry);
2014-05-07 21:28:10 +02:00
});
$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 {
2014-05-08 21:47:24 +02:00
request.abort();
currentResults = null;
2014-05-07 21:28:10 +02:00
$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();
});
});