-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdict.js
More file actions
227 lines (202 loc) · 5.85 KB
/
Copy pathdict.js
File metadata and controls
227 lines (202 loc) · 5.85 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
//---------------------------------------------------
//Oroginal version of Julian Robichaux(http://www.nsftools.com/tips/ZipLookupTest.htm)
// Modified by vnpenguin@gmail.com for VnOSS webdict
//---------------------------------------------------
var queryField;
var lookupURL = 'http://vnoss.net/dict/dict.php';
//var lookupURL = 'http://localhost/dict/dict.php';
var max;
var divID;
var dictID='dictName';
var dictTitle;
var xmlHttp;
var cache = new Object();
var globalDiv;
var divFormatted = false;
var DIV_BG_COLOR = '#ffffff';
var DIV_HIGHLIGHT_COLOR = '#3366cc';
var FONT_COLOR = '#000000';
var FONT_HOVER_COLOR = '#ffffff';
function InitQuery (_fID,_dID,_max) {
queryField = document.getElementById(_fID);
queryField.onkeydown = keypressHandler;
queryField.focus();
divID = _dID;
max = _max ? _max : 20;
addToCache("", new Array());
dictTitle = document.getElementById(dictID);
}
function addToCache (queryStr, resArr) {
cache[queryStr] = new Array(resArr);
}
function getDiv(ID) {
if(!globalDiv){
globalDiv = document.getElementById(ID);
if(!divFormatted){
globalDiv.style.backgroundColor = DIV_BG_COLOR;
globalDiv.style.fontFamily = 'Verdana,Arial';
globalDiv.style.fontSize = '12px';
globalDiv.style.visibility = 'hidden';
globalDiv.style.zIndex = 10000;
divFormatted = true;
}
}
return globalDiv;
}
function showACDiv (queryStr, dictName, resArr) {
var div = getDiv(divID);
//var span = document.getElementById(dictID);
dictTitle.innerHTML = ' ⇒ ['+dictName+']';
dictTitle.style.color = 'blue';
while (div.childNodes.length > 0)
div.removeChild(div.childNodes[0]);
for (var i = 0; i < resArr.length; i++) {
// each result will be contained within its own div
var res = document.createElement("div");
res.style.cursor = "pointer";
//res.style.borderBottom = "1px solid #000000";
res.style.padding = "0px 3px 0px 3px";
res.style.backgroundColor = DIV_BG_COLOR;
res.style.color = FONT_COLOR;
res.onmousedown = selRes;
res.onmouseover = hiliRes;
res.onmouseout = unhiliRes;
res.innerHTML = resArr[i];
div.appendChild(res);
}
var isCached = cache[queryStr];
if (!isCached)
addToCache(queryStr, resArr);
showDiv(resArr.length > 0);
}
function selRes() { _selRes(this); }
function _selRes(item) {
queryField.value = item.innerHTML;
queryField.focus();
showDiv(false);
return;
}
function hiliRes() { _hiliRes(this); }
function unhiliRes() { _unhiliRes(this); }
function _hiliRes(item) {
item.style.backgroundColor = DIV_HIGHLIGHT_COLOR;
item.style.color= FONT_HOVER_COLOR;
}
function _unhiliRes(item) {
item.style.backgroundColor = DIV_BG_COLOR;
item.style.color= FONT_COLOR;
}
function showDiv (show) {
var div = getDiv(divID);
if (show){
div.style.visibility = 'visible';
div.className = 'box';
} else {
div.style.visibility = 'hidden';
div.className = 'boxhidden';
}
}
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;
}
function doRemoteQuery (queryStr) {
var _url = lookupURL+'?d='+dict+'&q='+queryStr+'&max='+max+'&fmt=js';
if(xmlHttp && xmlHttp.readyState != 0) { xmlHttp.abort() }
xmlHttp=getXMLHTTP();
if(xmlHttp){
xmlHttp.open("GET",_url, true);
// What do we do when the response comes back?
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.responseText) {
eval(xmlHttp.responseText);
}
};
xmlHttp.send(null);
}
}
function keypressHandler (evt) {
// don't do anything if the div is hidden
var div = getDiv(divID);
//var div = document.getElementById(divID);
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))
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)
_selRes(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)
_hiliRes(selSpan);
}
showDiv(true);
return true;
}
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;
}
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) {
if (i == spanNum) {
_hiliRes(spans[i]);
thisSpan = spans[i];
} else {
_unhiliRes(spans[i]);
}
}
}
return thisSpan;
}
function showDictName(t){
//var dictname = t.options[t.selectedIndex].value;
var dictName = t.options[t.selectedIndex].text;
dict = t.options[t.selectedIndex].value;
//var span = document.getElementById(dictID);
dictTitle.innerHTML = ' ⇒ ['+dictName+']';
dictTitle.style.color = 'blue';
}