Moved some more common code into lnapp

This commit is contained in:
Deon George
2014-02-17 11:29:11 +11:00
parent 7a78a9a7d6
commit b2912e4007
11 changed files with 190 additions and 6 deletions

53
media/js/search.js Normal file
View File

@@ -0,0 +1,53 @@
$(document).ready(function() {
$("input[name=search-query]").typeahead({
minLength: 2,
source: function (query,process) {
if(query.length==0) return false;
search('u/search/ajaxlist',query,process);
},
matcher: function () { return true; },
updater: function (item) {
window.parent.location.href = site_url+users[item];
},
});
});
var c=0;
var search = _.debounce(function(url,query,process){
$.ajax({
url : site_url+url,
type : 'GET',
data : 'term=' + query,
dataType : 'JSON',
async : true,
cache : false,
beforeSend : function() {
if (c++ == 0)
$('img[name=searching]').css('visibility', 'visible');
},
success : function(data) {
// if json is null, means no match, won't do again.
if(data==null || (data.length===0)) return;
users = {};
userLabels = [];
_.each(data,function(item,ix,list) {
if (_.contains(users,item.label))
item.label = item.label + ' #' + item.value;
userLabels.push(item.label);
users[item.label] = item.value;
});
process(userLabels);
},
complete : function() {
if (--c == 0)
$('img[name=searching]').css('visibility', 'hidden');
}
})
}, 500);