XmlHttpLookup.js
13.5 KB
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/**
Dynamic XMLHttp lookups based on Google Suggest XMLRPC code. See:
http://serversideguy.blogspot.com/2004/12/google-suggest-dissected.html
http://www.fastbugtrack.com/misc/google/ac.js
http://www.google.com/webhp?complete=1&hl=en
I stripped out a lot of the cool functionality (like variable timers and
highlighting parts of the search result). That was mainly to make this a little
easier to digest. Feel free to look at the JavaScript code that Chris Justus
reformatted (see the link above) and put back anything that's missing.
In version 1.2 support was added for capturing keypresses of arrow keys,
the enter key, and the tab key while the input field has focus.
You can use these scripts in any way you'd like, just don't pretend like
you wrote them yourself.
version 1.2
January 5, 2005
Julian Robichaux, http://www.nsftools.com
*/
var queryField;
var lookupURL;
var divName;
var ifName;
var lastVal = "";
var val = ""
var xmlHttp;
var cache = new Object();
var searching = false;
var globalDiv;
var divFormatted = false;
var DIV_BG_COLOR = "#eeeeee";
var DIV_HIGHLIGHT_COLOR = "#cccccc";
/**
The InitQueryCode function should be called by the <body onload> event, passing
at least the queryFieldName and lookupURLPrefix parameters, where:
queryFieldName = the name of the form field we're using for lookups
lookupURLPrefix = the URL we'll use to pass the query string back to the server,
which will be immediately proceeded by the query string
For example:
<body onload="InitQueryCode('lookupField', 'http://lookupserver/QueryHandler?q=')">
The above example will monitor the input box called "lookupField" on this page,
and when it changes the contents of the field will be passed to lookupserver like:
http://lookupserver/QueryHandler?q=fieldValue
The http://lookupserver/QueryHandler will be expected to return a text response
with a single line of text that calls the showQueryDiv function, in a format like:
showQueryDiv("smi", new Array("John Smith", "Mary Smith"), new Array("555-1212", "555-1234"));
*/
function InitQueryCode (queryFieldName, lookupURLPrefix, hiddenDivName)
{
queryField = document.getElementsByName(queryFieldName).item(0);
queryField.onblur = hideDiv;
queryField.onfocus = hideDiv;
queryField.onkeydown = keypressHandler;
// for some reason, Firefox 1.0 doesn't allow us to set autocomplete to off
// this way, so you should manually set autocomplete="off" in the input tag
// if you can -- we'll try to set it here in case you forget
queryField.autocomplete = "off";
lookupURL = lookupURLPrefix;
if (hiddenDivName)
divName = hiddenDivName;
else
divName = "querydiv";
ifName = "queryiframe";
// add a blank value to the cache (so we don't try to do a lookup when the
// field is empty) and start checking for changes to the input field
addToCache("", new Array(), new Array());
setTimeout("mainLoop()", 100);
}
/**
This is a helper function that just adds results to our cache, to avoid
repeat lookups.
*/
function addToCache (queryString, resultArray1, resultArray2)
{
cache[queryString] = new Array(resultArray1, resultArray2);
}
/**
This is the function that monitors the queryField, and calls the lookup
functions when the queryField value changes.
*/
mainLoop = function() {
val = escape(queryField.value);
// if the field value has changed and we're not currently waiting for
// a lookup result to be returned, do a lookup (or use the cache, if
// we can)
if(lastVal != val && searching == false){
var cacheResult = cache[val];
if (cacheResult)
showQueryDiv(val, cacheResult[0], cacheResult[1]);
else
doRemoteQuery(val);
lastVal = val;
}
setTimeout("mainLoop()", 100);
return true;
}
;
/**
Get the <DIV> we're using to display the lookup results, and create the
<DIV> if it doesn't already exist.
*/
function getDiv (divID)
{
if (!globalDiv) {
// if the div doesn't exist on the page already, create it
if (!document.getElementById(divID)) {
var newNode = document.createElement("div");
newNode.setAttribute("id", divID);
document.body.appendChild(newNode);
}
// set the globalDiv reference
globalDiv = document.getElementById(divID);
// figure out where the top corner of the div should be, based on the
// bottom left corner of the input field
var x = queryField.offsetLeft;
var y = queryField.offsetTop + queryField.offsetHeight;
var parent = queryField;
while (parent.offsetParent) {
parent = parent.offsetParent;
x += parent.offsetLeft;
y += parent.offsetTop;
}
// add some formatting to the div, if we haven't already
if (!divFormatted) {
globalDiv.style.backgroundColor = DIV_BG_COLOR;
globalDiv.style.fontFamily = "Verdana, Geneva, Arial, Helvetica, sans-serif";
globalDiv.style.padding = "4px";
globalDiv.style.border = "1px solid black";
globalDiv.style.fontSize = "90%";
globalDiv.style.position = "absolute";
globalDiv.style.left = x + "px";
globalDiv.style.top = y + "px";
globalDiv.style.visibility = "hidden";
globalDiv.style.zIndex = 10000;
divFormatted = true;
}
}
return globalDiv;
}
/**
This is the function that should be returned by the XMLHTTP call. It will
format and display the lookup results.
*/
function showQueryDiv (queryString, resultArray1, resultArray2)
{
var div = getDiv(divName);
// remove any results that are already there
while (div.childNodes.length > 0)
div.removeChild(div.childNodes[0]);
// add an entry for each of the results in the resultArray
for (var i = 0; i < resultArray1.length; i++)
{
// each result will be contained within its own div
var result = document.createElement("div");
result.style.cursor = "pointer";
result.style.borderBottom = "1px solid #777777";
result.style.padding = "3px 0px 3px 0px";
_unhighlightResult(result);
result.onmousedown = selectResult;
result.onmouseover = highlightResult;
result.onmouseout = unhighlightResult;
var result1 = document.createElement("span");
result1.className = "result1";
result1.style.textAlign = "left";
result1.style.fontWeight = "bold";
result1.innerHTML = resultArray1[i];
var result2 = document.createElement("span");
result2.className = "result2";
result2.style.textAlign = "right";
result2.style.paddingLeft = "20px";
result2.innerHTML = resultArray2[i];
result.appendChild(result1);
result.appendChild(result2);
div.appendChild(result);
}
// if this resultset isn't already in our cache, add it
var isCached = cache[queryString];
if (!isCached)
addToCache(queryString, resultArray1, resultArray2);
// display the div if we had at least one result
showDiv(resultArray1.length > 0);
}
/**
This is called whenever the user clicks one of the lookup results.
It puts the value of the result in the queryField and hides the
lookup div.
*/
function selectResult()
{
_selectResult(this);
}
/** This actually fills the field with the selected result and hides the div */
function _selectResult(item)
{
//queryField.onKeyUp();
var spans = item.getElementsByTagName("span");
if (spans) {
for (var i = 0; i < spans.length; i++) {
if (spans[i].className == "result1") {
queryField.value = spans[i].innerHTML;
lastVal = val = escape(queryField.value);
searching = false;
mainLoop();
queryField.focus();
showDiv(false);
return;
}
}
}
}
/**
This is called when a user mouses over a lookup result
*/
function highlightResult()
{
_highlightResult(this);
}
/** This actually highlights the selected result */
function _highlightResult(item)
{
item.style.backgroundColor = DIV_HIGHLIGHT_COLOR;
}
/**
This is called when a user mouses away from a lookup result
*/
function unhighlightResult()
{
_unhighlightResult(this);
}
/** This actually unhighlights the selected result */
function _unhighlightResult(item)
{
item.style.backgroundColor = DIV_BG_COLOR;
}
/**
This either shows or hides the lookup div, depending on the value of
the "show" parameter.
*/
function showDiv (show)
{
var div = getDiv(divName);
//usado para campo CEP - se for alterado seu valor via javascript nao exibir o div
if (queryField.value.length == 8 && queryField.name == 'cep')
show = false;
if (show)
div.style.visibility = "visible";
else
div.style.visibility = "hidden";
adjustiFrame();
}
/**
We originally used showDiv as the function that was called by the onBlur
event of the field, but it turns out that Firefox will pass an event as the first
parameter of the function, which would cause the div to always be visible.
So onBlur now calls hideDiv instead.
*/
function hideDiv ()
{
showDiv(false);
}
/**
Use an "iFrame shim" to deal with problems where the lookup div shows up behind
selection list elements, if they're below the queryField. The problem and solution are
described at:
http://dotnetjunkies.com/WebLog/jking/archive/2003/07/21/488.aspx
http://dotnetjunkies.com/WebLog/jking/archive/2003/10/30/2975.aspx
*/
function adjustiFrame()
{
if (!document.getElementById(ifName)) {
var newNode = document.createElement("iFrame");
newNode.setAttribute("id", ifName);
newNode.setAttribute("src", "javascript:false;");
newNode.setAttribute("scrolling", "no");
newNode.setAttribute("frameborder", "0");
document.body.appendChild(newNode);
}
iFrameDiv = document.getElementById(ifName);
var div = getDiv(divName);
try {
iFrameDiv.style.position = "absolute";
iFrameDiv.style.width = div.offsetWidth;
iFrameDiv.style.height = div.offsetHeight;
iFrameDiv.style.top = div.style.top;
iFrameDiv.style.left = div.style.left;
iFrameDiv.style.zIndex = div.style.zIndex - 1;
iFrameDiv.style.visibility = div.style.visibility;
} catch(e) {
}
}
/**
This sets up the XMLHTTP object we're using for the dynamic lookups.
*/
function getXMLHTTP(){
var A = null;
try{
A = new ActiveXObject("Msxml2.XMLHTTP");
}catch(e){
try{
A = new ActiveXObject("Microsoft.XMLHTTP");
} catch(oc){
A = null;
}
}
if(!A && typeof XMLHttpRequest != "undefined") {
A = new XMLHttpRequest();
}
return A;
}
/**
This actually sends the lookup request (as a URL with a query string) to a
server in the background. When a response comes back from the server, the
function attached to the onReadyStateChange event is fired off.
*/
function doRemoteQuery (queryString)
{
searching = true;
if(xmlHttp && xmlHttp.readyState != 0) {
xmlHttp.abort()
}
xmlHttp=getXMLHTTP();
if(xmlHttp){
xmlHttp.open("GET", lookupURL + queryString, true);
// What do we do when the response comes back?
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.responseText && searching) {
eval(xmlHttp.responseText);
searching = false;
}
}
;
xmlHttp.send(null);
}
}
/**
This is the key handler function, for when a user presses the up arrow,
down arrow, tab key, or enter key from the input field.
*/
function keypressHandler (evt)
{
// don't do anything if the div is hidden
var div = getDiv(divName);
if (div.style.visibility == "hidden")
return true;
// make sure we have a valid event variable
if(!evt && window.event) {
evt = window.event;
}
var key = evt.keyCode;
// if this key isn't one of the ones we care about, just return
var KEYUP = 38;
var KEYDOWN = 40;
var KEYENTER = 13;
var KEYTAB = 9;
//if ((key != KEYUP) && (key != KEYDOWN) && (key != KEYENTER) && (key != KEYTAB))
if ((key != KEYUP) && (key != KEYDOWN) && (key != KEYENTER))
return true;
// get the span that's currently selected, and perform an appropriate action
var selNum = getSelectedSpanNum(div);
var selSpan = setSelectedSpan(div, selNum);
if ((key == KEYENTER) || (key == KEYTAB)) {
if (selSpan)
_selectResult(selSpan);
evt.cancelBubble=true;
return false;
} else {
if (key == KEYUP)
selSpan = setSelectedSpan(div, selNum - 1);
if (key == KEYDOWN)
selSpan = setSelectedSpan(div, selNum + 1);
if (selSpan)
_highlightResult(selSpan);
}
showDiv(true);
return true;
}
/**
Get the number of the result that's currently selected/highlighted
(the first result is 0, the second is 1, etc.)
*/
function getSelectedSpanNum (div)
{
var count = -1;
var spans = div.getElementsByTagName("div");
if (spans) {
for (var i = 0; i < spans.length; i++) {
count++;
if (spans[i].style.backgroundColor != div.style.backgroundColor)
return count;
}
}
return -1;
}
/**
Select/highlight the result at the given position
*/
function setSelectedSpan (div, spanNum)
{
var count = -1;
var thisSpan;
var spans = div.getElementsByTagName("div");
if (spans) {
for (var i = 0; i < spans.length; i++) {
if (++count == spanNum) {
_highlightResult(spans[i]);
thisSpan = spans[i];
} else {
_unhighlightResult(spans[i]);
}
}
}
return thisSpan;
}