Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rpheath committed Sep 18, 2009
0 parents commit 9e7a263
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.textile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
h1. Searchbox: a live search plugin for jQuery
71 changes: 71 additions & 0 deletions searchbox.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
(function($) {
$.searchbox = {}

// defaults
$.extend(true, $.searchbox, {
settings: {
url: '/search',
param: 'query',
dom_id: '#results',
delay: 250,
loading_css: '#loading'
},

// while the search is being performed
loading: function() {
$($.searchbox.settings.loading_css).show()
},

// clear outdated timers
resetTimer: function(timer) {
if (timer) clearTimeout(timer)
},

// before/after performing search
idle: function() {
$($.searchbox.settings.loading_css).hide()
},

// submits the query and handles response
process: function(terms) {
var path = $.searchbox.settings.url.split('?'),
query = [$.searchbox.settings.param, '=', terms].join(''),
base = path[0], params = path[1], query_string = query

if (params) query_string = [params.replace('&', '&'), query].join('&')

$.get([base, '?', query_string].join(''), function(data) {
$($.searchbox.settings.dom_id).html(data)
})
}
})

// Example:
// $('input.search').searchbox({ url: '/your/search/url' })

$.fn.searchbox = function(config) {
var settings = $.extend(true, $.searchbox.settings, config || {})

return this.each(function() {
var $input = $(this)

$.searchbox.idle()

$input
.focus()
.ajaxStart(function() { $.searchbox.loading() })
.ajaxStop(function() { $.searchbox.idle() })
.keyup(function() {
if ($input.val() != this.previousValue) {
$.searchbox.resetTimer(this.timer)

this.timer = setTimeout(function() {
$.searchbox.process($input.val())
}, $.searchbox.settings.delay)

this.previousValue = $input.val()
}
})
})
}
})(jQuery);

0 comments on commit 9e7a263

Please sign in to comment.