-
Notifications
You must be signed in to change notification settings - Fork 52
/
livesearch.js
249 lines (195 loc) · 4.93 KB
/
livesearch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
var xmlhttp;
var loadedat=0;
var ajax_highlight = new Array();
function setLoaded()
{
var d = new Date();
loadedat = d.getTime();
}
function hideit(delayed, command)
{
if (delayed == 1)
{
return setTimeout("hideit(0, '"+command+"')", 1000);
}
document.getElementById("livesearch").innerHTML="";
document.getElementById("livesearch").style.border="0px;";
document.getElementById("livecontainer").style.visibility="hidden";
if (command != '')
ajax_highlight[command] = -1;
return;
}
function is_visible(id)
{
if (document.getElementById(id).style.visibility == "visible")
{
return 1;
}
return 0;
}
function handle_key_press(key_pressed, command)
{
if (ajax_highlight[command] === undefined)
ajax_highlight[command] = -1;
var selected = ajax_highlight[command];
if (key_pressed == 9 || key_pressed == 13 || key_pressed == 32)
{
if (is_visible("livecontainer") && selected >= 0)
{
//9 = tab
//13 enter
//32 space
var id_name = command+'-'+selected;
var item_element = document.getElementById(id_name);
if (item_element != null)
{
if (typeof item_element.onclick == 'function')
{
item_element.onclick();
}
else if (typeof item_element.click == 'function')
{
item_element.click();
}
hideit(0, command);
}
}
return;
}
if (key_pressed == 38 || key_pressed == 40)
{
var old_selected = selected;
if (key_pressed == 38) //up
{
selected = (+selected) - 1;
}
if (key_pressed == 40) //down
{
selected = (+selected) + 1;
}
if (selected == -2)
selected = -1;
//alert("new selected will be "+selected);
if (selected >= 0)
{
var id_name = command+'-'+selected;
var item_element = document.getElementById(id_name);
if (item_element == null)
{
return; //nope, doesn't exist. Can't go here.
}
if (old_selected >= 0)
{
var old_id_name = command+'-'+old_selected;
var old_item_element = document.getElementById(old_id_name);
if (old_item_element != null)
{
outof(old_item_element, 0);
}
}
over(item_element,0);
}
}
}
function showResult(str, command)
{
var d = new Date();
if (is_visible("livecontainer"))
{
var key_pressed = document.layers ? event.which : document.all ? event.keyCode : document.getElementById ? event.keyCode : 0;
if (key_pressed == 38 || key_pressed == 40 || key_pressed == 9 || key_pressed == 13 || key_pressed == 32)
{
handle_key_press(key_pressed, command);
return;
}
}
//we need to ensure at least 200 milliseconds has passed because keyup from form submission can happen after the next page has loaded.
if (str.length < 2 || d.getTime() - loadedat < 200 || loadedat == 0)
{
return hideit(0, command);
}
xmlhttp=GetXmlHttpObject()
if (xmlhttp==null)
{
alert ("Your browser does not support XML HTTP Request");
return;
}
str = str.replace(/&/, '%26');
var url=command;
url=url+"?query="+str;
url=url+"&sid="+Math.random();
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
function stateChanged()
{
if (xmlhttp.readyState==4)
{
document.getElementById("livesearch").innerHTML=xmlhttp.responseText;
document.getElementById("livesearch").style.border="1px solid #A5ACB2";
//new
if (document.getElementById("livesearch").innerHTML == "")
{
hideit(0, '');
}
else
{
document.getElementById("livecontainer").style.visibility="visible";
}
}
}
function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
return new XMLHttpRequest();
}
if (window.ActiveXObject)
{
// code for IE6, IE5
return new ActiveXObject("Microsoft.XMLHTTP");
}
return null;
}
function get_ajax_name(id_str)
{
if (id_str == "")
return 'default';
var id_split = id_str.split('-', 2);
var ajax_name = id_split[0];
if (ajax_name != "")
return ajax_name;
return 'default';
}
function get_ajax_number(id_str)
{
if (id_str == "")
return 0;
var id_split = id_str.split('-', 2);
var ajax_number = id_split[1];
if (ajax_number != "")
return ajax_number ;
return 0;
}
function over(div, is_mouse)
{
div.className='hoverin';
var ajax_name = get_ajax_name(div.id);
var ajax_number = get_ajax_number(div.id);
if (ajax_highlight[ajax_name] === undefined)
ajax_highlight[ajax_name] = -1;
var old_selected = ajax_highlight[ajax_name];
ajax_highlight[ajax_name] = ajax_number;
if (is_mouse == 0) return;
if (old_selected == -1) return;
var old_name = ajax_name+'-'+old_selected;
var old_element = document.getElementById(old_name);
if (old_element == null) return;
old_element.className='hoverout';
}
function outof(div, is_mouse)
{
div.className='hoverout';
}