1/* 2 * Copyright (c) 2008 Greg Weber greg at gregweber.info 3 * Dual licensed under the MIT and GPLv2 licenses just as jQuery is: 4 * http://jquery.org/license 5 * 6 * documentation at http://gregweber.info/projects/uitablefilter 7 * 8 * allows table rows to be filtered (made invisible) 9 * <code> 10 * t = $('table') 11 * $.uiTableFilter( t, phrase ) 12 * </code> 13 * arguments: 14 * jQuery object containing table rows 15 * phrase to search for 16 * optional arguments: 17 * column to limit search too (the column title in the table header) 18 * ifHidden - callback to execute if one or more elements was hidden 19 */ 20(function($) { 21 $.uiTableFilter = function(jq, phrase, column, ifHidden){ 22 var new_hidden = false; 23 if( this.last_phrase === phrase ) return false; 24 25 var phrase_length = phrase.length; 26 var words = phrase.toLowerCase().split(" "); 27 28 // these function pointers may change 29 var matches = function(elem) { elem.show() } 30 var noMatch = function(elem) { elem.hide(); new_hidden = true } 31 var getText = function(elem) { return elem.text() } 32 33 if( column ) { 34 var index = null; 35 jq.find("thead > tr:last > th").each( function(i){ 36 if( $.trim($(this).text()) == column ){ 37 index = i; return false; 38 } 39 }); 40 if( index == null ) throw("given column: " + column + " not found") 41 42 getText = function(elem){ return $(elem.find( 43 ("td:eq(" + index + ")") )).text() 44 } 45 } 46 47 // if added one letter to last time, 48 // just check newest word and only need to hide 49 if( (words.size > 1) && (phrase.substr(0, phrase_length - 1) === 50 this.last_phrase) ) { 51 52 if( phrase[-1] === " " ) 53 { this.last_phrase = phrase; return false; } 54 55 var words = words[-1]; // just search for the newest word 56 57 // only hide visible rows 58 matches = function(elem) {;} 59 var elems = jq.find("tbody:first > tr:visible") 60 } 61 else { 62 new_hidden = true; 63 var elems = jq.find("tbody:first > tr") 64 } 65 66 elems.each(function(){ 67 var elem = $(this); 68 $.uiTableFilter.has_words( getText(elem), words, false ) ? 69 matches(elem) : noMatch(elem); 70 }); 71 72 last_phrase = phrase; 73 if( ifHidden && new_hidden ) ifHidden(); 74 return jq; 75 }; 76 77 // caching for speedup 78 $.uiTableFilter.last_phrase = "" 79
80 // not jQuery dependent 81 // "" [""] -> Boolean 82 // "" [""] Boolean -> Boolean 83 $.uiTableFilter.has_words = function( str, words, caseSensitive ) 84 { 85 var text = caseSensitive ? str : str.toLowerCase(); 86 for (var i=0; i < words.length; i++) { 87 if (text.indexOf(words[i]) === -1) return false; 88 } 89 return true; 90 } 91}) (jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.