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

82 lines
1.8 KiB
JavaScript
Raw Normal View History

2014-05-07 14:28:10 -05:00
var $ = require('unopinionate').selector,
template = require('../entry.hbs'),
onScroll = require('onscroll');
2014-05-07 14:28:10 -05:00
$(function() {
2014-05-08 14:47:24 -05:00
'use strict';
(function( window, document ) {
2014-05-07 14:28:10 -05:00
var $form = $('#search-form');
var $input = $form.find('input');
var $body = $('body');
var $clear = $form.find('.clear');
var $searchResults = $("#search-results");
var $pkgListing = $("#all-packages");
var $searchBtn = $('.js-search-btn');
var request;
var currentResults;
2014-05-07 14:28:10 -05:00
var toggle = function( validQuery ) {
$searchResults.toggleClass( 'show', validQuery );
$pkgListing.toggleClass( 'hide', validQuery );
2014-05-07 14:28:10 -05:00
$searchBtn.find('i').toggleClass( 'icon-cancel', validQuery );
$searchBtn.find('i').toggleClass( 'icon-search', !validQuery );
};
2014-05-07 16:51:03 -05:00
$form.bind('submit keyup', function(e) {
var q, qBool;
e.preventDefault();
q = $input.val();
qBool = q !== '';
toggle( qBool );
if( !qBool ) {
if( request && typeof request.abort === 'function' ) {
request.abort();
}
currentResults = null;
$searchResults.html('');
return;
}
if( request && typeof request.abort === 'function' ) {
2014-05-07 14:28:10 -05:00
request.abort();
}
if( !currentResults ) {
$searchResults.html( "<img class='search-ajax' src='/-/static/ajax.gif' alt='Spinner'/>" );
2014-05-08 14:47:24 -05:00
}
request = $.getJSON('/-/search/' + q, function( results ) {
2014-05-08 14:47:24 -05:00
currentResults = results;
if( results.length > 0 ) {
2014-05-07 14:28:10 -05:00
var html = '';
$.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
}
});
});
$( document ).on( 'click', '.icon-cancel', function( e ) {
e.preventDefault();
$input.val('');
$form.keyup();
});
})( window, window.document );
2014-05-07 14:28:10 -05:00
});