Skip to content

Instantly share code, notes, and snippets.

@iloveitaly
Created July 6, 2011 17:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iloveitaly/1067880 to your computer and use it in GitHub Desktop.
Save iloveitaly/1067880 to your computer and use it in GitHub Desktop.
Instant Filtering for MooTools
var InstantSearchManager = new Class({
Implements: [Options],
options: {
searchDelay: 400,
baseURL: '/download/search'
},
initialize: function(holder, search, options) {
this.setOptions(options);
this.element = $(holder);
this.searchField = $(search)
this.searchField.addEvents({
keyup:this.scheduleSearch.bind(this),
blur:this.scheduleSearch.bind(this)
});
this.previousSearch = '';
this.searchRequest = null;
this.originalContent = this.element.get('html');
},
scheduleSearch: function() {
this.lastPress = Date.now();
this.search.bind(this).delay(this.options.searchDelay);
},
search: function() {
if(Date.now() - this.lastPress >= this.options.searchDelay) {
if(this.searchRequest && this.searchRequest.isRunning()) {
this.searchRequest.cancel();
}
var trimmedSearch = this.searchField.get('value').trim();
if(trimmedSearch.length > 0) {
if(trimmedSearch == this.previousSearch) return;
this.searchRequest = new Request.HTML({
url: this.options.baseURL,
update: this.element,
useSpinner: true
}).get(Object.toQueryString({keywords: this.searchField.get('value')}));
} else {
this.element.set('html', this.originalContent);
}
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment