Permalink
Browse files

Add live-search

  • Loading branch information...
1 parent e697561 commit ce42a9263986cadff95da03745c4f7a91c8d43d3 @rwdaigle committed Oct 19, 2012
@@ -0,0 +1,85 @@
+/*
+* TypeWatch 2.0 - Original by Denny Ferrassoli / Refactored by Charles Christolini
+*
+* Examples/Docs: github.com/dennyferra/TypeWatch
+*
+* Copyright(c) 2007 Denny Ferrassoli - DennyDotNet.com
+* Coprright(c) 2008 Charles Christolini - BinaryPie.com
+*
+* Dual licensed under the MIT and GPL licenses:
+* http://www.opensource.org/licenses/mit-license.php
+* http://www.gnu.org/licenses/gpl.html
+*/
+
+(function(jQuery) {
+ jQuery.fn.typeWatch = function(o) {
+ // Options
+ var options = jQuery.extend({
+ wait: 750,
+ callback: function() { },
+ highlight: true,
+ captureLength: 2
+ }, o);
+
+ function checkElement(timer, override) {
+ var elTxt = jQuery(timer.el).val();
+
+ // Fire if text >= options.captureLength AND text != saved txt OR if override AND text >= options.captureLength
+ if ((elTxt.length >= options.captureLength && elTxt.toUpperCase() != timer.text)
+ || (override && elTxt.length >= options.captureLength)) {
+ timer.text = elTxt.toUpperCase();
+ timer.cb(elTxt, timer.el);
+ }
+ };
+
+ function watchElement(elem) {
+ // Must be text or textarea
+ if (elem.type.toUpperCase() == "TEXT" || elem.type.toUpperCase() == "PASSWORD" || elem.nodeName.toUpperCase() == "TEXTAREA") {
+
+ // Allocate timer element
+ var timer = {
+ timer: null,
+ text: jQuery(elem).val().toUpperCase(),
+ cb: options.callback,
+ el: elem,
+ wait: options.wait
+ };
+
+ // Set focus action (highlight)
+ if (options.highlight) {
+ jQuery(elem).focus(
+ function() {
+ this.select();
+ });
+ }
+
+ // Key watcher / clear and reset the timer
+ var startWatch = function(evt) {
+ var timerWait = timer.wait;
+ var overrideBool = false;
+
+ if (evt.keyCode == 13 && (this.type.toUpperCase() == "TEXT" || this.type.toUpperCase() == "PASSWORD")) {
+ timerWait = 1;
+ overrideBool = true;
+ }
+
+ var timerCallbackFx = function() {
+ checkElement(timer, overrideBool)
+ }
+
+ // Clear timer
+ clearTimeout(timer.timer);
+ timer.timer = setTimeout(timerCallbackFx, timerWait);
+ };
+
+ jQuery(elem).keydown(startWatch);
+ }
+ };
+
+ // Watch Each Element
+ return this.each(function(index) {
+ watchElement(this);
+ });
+
+ };
+})(jQuery);
@@ -1,17 +1,15 @@
// Initial page load b/f Turbolinks kicks in
$(function() {
wireSearch();
- setInitialElementStates();
+ displayResults();
});
// Subsequent loads
$(window).bind('pjax:end', function() {
- setInitialElementStates();
+ displayResults();
});
-var setInitialElementStates = function() {
-
- searchField().select();
+var displayResults = function() {
// Make sure results html is displayed
if($("#results .choice").size() > 0) {
@@ -29,6 +27,16 @@ var wireSearch = function() {
var selectedClass = 'selected';
+ searchField().select();
+
+ // Live search
+ searchField().typeWatch({
+ callback: function() { $("#top_search_form").submit(); },
+ wait: 250,
+ highlight: false,
+ captureLength: 2
+ });
+
// Pass form submits through PJAX
$("#top_search_form").submit(function() {
form = $(this);
@@ -37,7 +45,9 @@ var wireSearch = function() {
container: '#results'
})
return false;
- });
+ });
+
+ // Keyboard nav
Mousetrap.bind(['down'], function(e) {
navigateList('down', $("#results .choice:first"));
@@ -56,7 +66,7 @@ var wireSearch = function() {
});
});
- Mousetrap.bind(['/'], function(e) {
+ Mousetrap.bind(['/', 'escape'], function(e) {
executeOutsideInputFields(e, function() {
searchField().focus().select();
$("#results .choice.selected").removeClass(selectedClass);
@@ -1,11 +1,12 @@
-- if @results.size > 0
- - for result in @results
+- if !params[:q].blank?
+ - if @results.size > 0
+ - for result in @results
+ .choice
+ %span.command= search_result_link(result)
+ %span.description= pluralize(result.comment_count, "comments")
+ .hints
+ %em keyboard:
+ ↑ = up, ↓ = down, enter = follow link, / = search
+ - else
.choice
- %span.command= search_result_link(result)
- %span.description= pluralize(result.comment_count, "comments")
- .hints
- %em keyboard:
- ↑ = up, ↓ = down, enter = follow link, / = search
-- else
- .no-results
- %span.command= "No results found. Bummer."
+ %span.command= "No results for \"#{params[:q]}\". Bummer."
@@ -1 +1 @@
-CACHE_VERSION="v002"
+CACHE_VERSION="v003"

0 comments on commit ce42a92

Please sign in to comment.