From 90121a0c926fa50ed8354e10e1d923945e1809dc Mon Sep 17 00:00:00 2001 From: Casey Doolittle Date: Tue, 6 Aug 2013 13:41:40 -0600 Subject: [PATCH 1/3] Added functionality to allow an ignore class You can add an ignore to the options that allows the filter to ignore children that have that class. The ignore defaults to filter-ignore. To support this changed to use the jQuery.text() function. --- jquery.fastLiveFilter.js | 114 +++++++++++++++++++-------------------- 1 file changed, 57 insertions(+), 57 deletions(-) diff --git a/jquery.fastLiveFilter.js b/jquery.fastLiveFilter.js index 141448e..060d3ce 100644 --- a/jquery.fastLiveFilter.js +++ b/jquery.fastLiveFilter.js @@ -1,57 +1,57 @@ -/** - * fastLiveFilter jQuery plugin 1.0.3 - * - * Copyright (c) 2011, Anthony Bush - * License: - * Project Website: http://anthonybush.com/projects/jquery_fast_live_filter/ - **/ - -jQuery.fn.fastLiveFilter = function(list, options) { - // Options: input, list, timeout, callback - options = options || {}; - list = jQuery(list); - var input = this; - var timeout = options.timeout || 0; - var callback = options.callback || function() {}; - - var keyTimeout; - - // NOTE: because we cache lis & len here, users would need to re-init the plugin - // if they modify the list in the DOM later. This doesn't give us that much speed - // boost, so perhaps it's not worth putting it here. - var lis = list.children(); - var len = lis.length; - var oldDisplay = len > 0 ? lis[0].style.display : "block"; - callback(len); // do a one-time callback on initialization to make sure everything's in sync - - input.change(function() { - // var startTime = new Date().getTime(); - var filter = input.val().toLowerCase(); - var li; - var numShown = 0; - for (var i = 0; i < len; i++) { - li = lis[i]; - if ((li.textContent || li.innerText || "").toLowerCase().indexOf(filter) >= 0) { - if (li.style.display == "none") { - li.style.display = oldDisplay; - } - numShown++; - } else { - if (li.style.display != "none") { - li.style.display = "none"; - } - } - } - callback(numShown); - // var endTime = new Date().getTime(); - // console.log('Search for ' + filter + ' took: ' + (endTime - startTime) + ' (' + numShown + ' results)'); - return false; - }).keydown(function() { - // TODO: one point of improvement could be in here: currently the change event is - // invoked even if a change does not occur (e.g. by pressing a modifier key or - // something) - clearTimeout(keyTimeout); - keyTimeout = setTimeout(function() { input.change(); }, timeout); - }); - return this; // maintain jQuery chainability -} +/** + * fastLiveFilter jQuery plugin 1.1.0 + * + * Copyright (c) 2011, Anthony Bush + * License: + * Project Website: http://anthonybush.com/projects/jquery_fast_live_filter/ + **/ + +jQuery.fn.fastLiveFilter = function(list, options) { + // Options: input, list, timeout, callback + options = options || {}; + list = jQuery(list); + var input = this; + var timeout = options.timeout || 0; + var callback = options.callback || function() {}; + var ignore = options.ignore || 'filter-ignore'; + var keyTimeout; + + // NOTE: because we cache lis & len here, users would need to re-init the plugin + // if they modify the list in the DOM later. This doesn't give us that much speed + // boost, so perhaps it's not worth putting it here. + var lis = list.children(); + var len = lis.length; + var oldDisplay = len > 0 ? lis[0].style.display : "block"; + callback(len); // do a one-time callback on initialization to make sure everything's in sync + + input.change(function() { + // var startTime = new Date().getTime(); + var filter = input.val().toLowerCase(); + var li; + var numShown = 0; + for (var i = 0; i < len; i++) { + li = lis[i]; + if (($(li).children(":not(." + ignore + ")").text() || "").toLowerCase().indexOf(filter) >= 0) { + if (li.style.display == "none") { + li.style.display = oldDisplay; + } + numShown++; + } else { + if (li.style.display != "none") { + li.style.display = "none"; + } + } + } + callback(numShown); + // var endTime = new Date().getTime(); + // console.log('Search for ' + filter + ' took: ' + (endTime - startTime) + ' (' + numShown + ' results)'); + return false; + }).keydown(function() { + // TODO: one point of improvement could be in here: currently the change event is + // invoked even if a change does not occur (e.g. by pressing a modifier key or + // something) + clearTimeout(keyTimeout); + keyTimeout = setTimeout(function() { input.change(); }, timeout); + }); + return this; // maintain jQuery chainability +} From dc0ce1096edd76c439b14f6f3651d8d9edc8f37a Mon Sep 17 00:00:00 2001 From: Casey Doolittle Date: Tue, 6 Aug 2013 14:21:26 -0600 Subject: [PATCH 2/3] changed children to contents to support text nodes Changed the .children to .contents to support text nodes. --- jquery.fastLiveFilter.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jquery.fastLiveFilter.js b/jquery.fastLiveFilter.js index 060d3ce..84dac15 100644 --- a/jquery.fastLiveFilter.js +++ b/jquery.fastLiveFilter.js @@ -31,7 +31,7 @@ jQuery.fn.fastLiveFilter = function(list, options) { var numShown = 0; for (var i = 0; i < len; i++) { li = lis[i]; - if (($(li).children(":not(." + ignore + ")").text() || "").toLowerCase().indexOf(filter) >= 0) { + if (($(li).contents(":not(." + ignore + ")").text() || "").toLowerCase().indexOf(filter) >= 0) { if (li.style.display == "none") { li.style.display = oldDisplay; } From ec36bb5e603e582ed1789373243df56b9f476f83 Mon Sep 17 00:00:00 2001 From: Casey Doolittle Date: Thu, 29 Aug 2013 14:22:58 -0600 Subject: [PATCH 3/3] Change to jQuery over $ Change to jQuery over $ in testing found that it was causing problems with other frameworks for example prototype. To be safe, change to use the jQuery instead of the $ alias --- jquery.fastLiveFilter.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jquery.fastLiveFilter.js b/jquery.fastLiveFilter.js index 84dac15..5ed040d 100644 --- a/jquery.fastLiveFilter.js +++ b/jquery.fastLiveFilter.js @@ -31,7 +31,7 @@ jQuery.fn.fastLiveFilter = function(list, options) { var numShown = 0; for (var i = 0; i < len; i++) { li = lis[i]; - if (($(li).contents(":not(." + ignore + ")").text() || "").toLowerCase().indexOf(filter) >= 0) { + if ((jQuery(li).contents(":not(." + ignore + ")").text() || "").toLowerCase().indexOf(filter) >= 0) { if (li.style.display == "none") { li.style.display = oldDisplay; }