Vulcan/packages/telescope-search/lib/client/templates/search.js
Charlie DeTar abab542c77 Increse specificity of keyup search event
It's hard to override the behavior of .search-field in a theme when the
event listener is listening to all ``keyup`` events in the template.
Make it more specific as ``keyup .search-field`` so a theme that doesn't
want the default event listener can override the input class.
2015-03-24 16:29:47 -06:00

55 lines
1.6 KiB
JavaScript

// see: http://stackoverflow.com/questions/1909441/jquery-keyup-delay
var delay = (function(){
var timer = 0;
return function(callback, ms){
clearTimeout (timer);
timer = setTimeout(callback, ms);
};
})();
Meteor.startup(function () {
Template[getTemplate('search')].helpers({
canSearch: function () {
return can.view(Meteor.user());
},
searchQuery: function () {
return Session.get("searchQuery");
},
searchQueryEmpty: function () {
return !!Session.get("searchQuery") ? '' : 'empty';
}
});
Template[getTemplate('search')].events({
'keyup .search-field, search .search-field': function(e){
console.log("telescope-handler");
e.preventDefault();
var val = $(e.target).val(),
$search = $('.search');
if (val === '') {
// if search field is empty, just do nothing and show an empty template
$search.addClass('empty');
Session.set('searchQuery', '');
Router.go('search', null, {replaceState: true});
} else {
$search.removeClass('empty');
// if search field is not empty, add a delay to avoid firing new searches for every keystroke
delay(function(){
Session.set('searchQuery', val);
// Update the querystring.
var opts = {query: {q: val}};
// if we're already on the search page, do a replaceState. Otherwise,
// just use the pushState default.
if(Router.current().route.getName() === 'search') {
opts.replaceState = true;
}
Router.go('search', null, opts);
}, 700 );
}
}
});
});