1 |
/* |
2 |
Fast filtering function using pre-sorted list elements |
3 |
Dobrica Pavlinusic, dpavlin@rot13.org 2004-09-06 |
4 |
*/ |
5 |
|
6 |
function bfilter_init() { |
7 |
show_status(); |
8 |
} |
9 |
|
10 |
var id_cache = Array(); |
11 |
|
12 |
function element_id(id) { |
13 |
if (id_cache[id]) { |
14 |
return id_cache[id]; |
15 |
} else { |
16 |
var el = document.getElementById(id); |
17 |
if (el) { |
18 |
id_cache[id] = el; |
19 |
return el; |
20 |
} else { |
21 |
alert("can't find element id: "+id); |
22 |
} |
23 |
} |
24 |
} |
25 |
|
26 |
// total number of hits |
27 |
var hits = 0; |
28 |
|
29 |
function show_status(status) { |
30 |
var html; |
31 |
if (hits > 0) { |
32 |
html = "shown "+hits+" entries"; |
33 |
} else { |
34 |
html = "no results"; |
35 |
} |
36 |
if (! status) { |
37 |
html = "Enter "+min_len+" letter"+(min_len == 1 ? '' : 's')+" to filter entries"; |
38 |
status = ""; |
39 |
} |
40 |
|
41 |
var el = element_id("status"); |
42 |
el.innerHTML = html+status+"\n"; |
43 |
} |
44 |
|
45 |
var wait = 0; |
46 |
|
47 |
function results(html,clean) { |
48 |
|
49 |
if (! html) { html = ""; } |
50 |
|
51 |
// results_div.style.cursor = 'wait'; // 'auto' |
52 |
var results_div = element_id("results"); |
53 |
if (clean) { |
54 |
results_div.innerHTML = html+"\n"; |
55 |
} else { |
56 |
results_div.innerHTML += html+"\n"; |
57 |
} |
58 |
} |
59 |
|
60 |
|
61 |
function bfilter(document, id, find, arr) { |
62 |
|
63 |
var debug = 0; |
64 |
|
65 |
results('',1); |
66 |
hits = 0; |
67 |
|
68 |
if (find.length < min_len) { |
69 |
show_status(); |
70 |
return; |
71 |
} |
72 |
|
73 |
if (debug) { results("filter: '"+find+"'<br>"); } |
74 |
|
75 |
var find_lc = find.toLowerCase(); |
76 |
|
77 |
var part = find_lc.substr(0,min_len); |
78 |
|
79 |
// no part found |
80 |
if (! arr[part]) { |
81 |
show_status(" for <em>"+find+"</em><br>"); |
82 |
return; |
83 |
} |
84 |
|
85 |
// start anim icon |
86 |
//results("<img src=\"pie.gif\"> Please wait, filtering...\n",1); |
87 |
|
88 |
// full part? (optimization) |
89 |
if (find.length == min_len) { |
90 |
var html = ''; |
91 |
for (var i = 0; i < arr[part].length; i++) { |
92 |
html += "<li>"+arr[part][i]+"</li>\n"; |
93 |
hits++; |
94 |
} |
95 |
results(html); |
96 |
} else { |
97 |
|
98 |
for (var i = 0; i < arr[part].length; i++) { |
99 |
|
100 |
var text = arr[part][i].toLowerCase(); |
101 |
|
102 |
if (debug) { results(part+" "+i+": "+text); } |
103 |
|
104 |
// get li element by ID |
105 |
|
106 |
if (debug) { results("cmp: "+text.substring(0,find.length)+" "+find+" ["+text+"]<br>\n"); } |
107 |
|
108 |
if (text.substring(0,find.length) == find_lc) { |
109 |
results("<li>"+text+"</li>\n"); |
110 |
hits++; |
111 |
} |
112 |
|
113 |
} |
114 |
|
115 |
} |
116 |
|
117 |
// clean clock if no results |
118 |
if (hits == 0) { results("",1); } |
119 |
show_status(" for <em>"+find+"</em>"); |
120 |
|
121 |
} |
122 |
|