-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchField.js
More file actions
322 lines (242 loc) · 9.11 KB
/
SearchField.js
File metadata and controls
322 lines (242 loc) · 9.11 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
if(!javaxt) var javaxt={};
if(!javaxt.express) javaxt.express={};
//******************************************************************************
//** Search Field
//******************************************************************************
/**
* Form field used to initiate a search via web sockets
*
******************************************************************************/
javaxt.express.SearchField = function(parent, config) {
this.className = "javaxt.express.SearchField";
var me = this;
//Create default style
var defaultStyle = {};
if (javaxt.dhtml.style){
if (javaxt.dhtml.style.default){
var style = javaxt.dhtml.style.default.combobox;
if (style) defaultStyle = style;
}
}
var defaultConfig = {
/** Relative path the to websocket endpoint (e.g. "/search"). You do not
* need to specify a full url with a "ws://" or "wss://" prefix.
*/
url: '/search',
/** Delay between the last user input and start of a new search.
*/
searchDelay: 500,
/** Style for individual elements within the component. Note that you can
* provide CSS class names instead of individual style definitions.
*/
style: defaultStyle,
/** Function to call when the user clicks on the search button.
*/
onButtonClick: function(){
},
debugr: null
};
var input;
var socket;
var socketStatus = 0; //0 (Closed), 1 (Opening), 2 (Open), 3 (Ready)
//**************************************************************************
//** Constructor
//**************************************************************************
var init = function(){
if (typeof parent === "string"){
parent = document.getElementById(parent);
}
if (!parent) return;
//Clone the config so we don't modify the original config object
var clone = {};
merge(clone, config);
//Merge clone with default config
merge(clone, defaultConfig);
config = clone;
//Update event handlers
for (var key in config) {
if (config.hasOwnProperty(key)){
if (typeof config[key] == "function") {
if (me[key] && typeof me[key] == "function"){
me[key] = config[key];
}
}
}
}
//Create ComboBox
input = new javaxt.dhtml.ComboBox(parent, {
maxVisibleRows: 6,
showMenuOnFocus: false,
style: config.style
});
input.filter = function(){}; //<--override native filter
input.getButton().onclick = config.onButtonClick;
//Watch for changes
var timer;
input.onChange = function(label, value){
if (value) me.onSelect();
if (timer) clearTimeout(timer);
timer = setTimeout(function(){
if (!value) search();
}, config.searchDelay);
};
//Get reference to the input element
var el = me.el = input.el;
//Add event listener to the input
el.addEventListener("focusout", function(){
if (timer) clearTimeout(timer);
if (socket) socket.close();
socketStatus = 0;
});
};
//**************************************************************************
//** getValue
//**************************************************************************
/** Used to retrieve the value for the key/id associated with the selected
* search result
*/
this.getValue = function(){
return input.getValue();
};
//**************************************************************************
//** setValue
//**************************************************************************
this.setValue = function(value, silent){
input.setValue(value, silent);
};
//**************************************************************************
//** onSelect
//**************************************************************************
/** Called whenever a user selects an item from the drop-down menu
*/
this.onSelect = function(){};
//**************************************************************************
//** clear
//**************************************************************************
this.clear = function(){
input.clear();
};
//**************************************************************************
//** getInput
//**************************************************************************
this.getInput = function(){
return input.getInput();
};
//**************************************************************************
//** createFilter
//**************************************************************************
this.createFilter = function(text){
return text;
};
//**************************************************************************
//** parseResponse
//**************************************************************************
/** Default method used to parse responses from the server. Should return
* an array of key/value pairs to insert into the combobox. Example:
* [["Bob","12/30"],["Jim","10/28"]]
*/
this.parseResponse = function(text){
return [];
};
//**************************************************************************
//** sendQuery
//**************************************************************************
/** Used to send a query to the remote server.
*/
var sendQuery = function(){
log("sendQuery");
var text = input.getText().trim();
if (text.length===0) return;
var filter = me.createFilter(text);
if (filter) socket.send(filter);
};
//**************************************************************************
//** search
//**************************************************************************
/** Used to update the combo box and initiate a search.
*/
var search = function(){
log("search");
//Clear menu options
input.removeAll();
//Create socket connection as needed
if (!socket || socketStatus===0){
openSocket();
}
else{
if (socketStatus===3){
sendQuery();
}
}
};
//**************************************************************************
//** openSocket
//**************************************************************************
/** Used to create a socket connection to the server.
*/
var openSocket = function(){
socketStatus = 1;
//Set URL
var protocol = window.location.protocol.toLowerCase();
if (protocol.indexOf("https")===0) protocol = "wss";
else protocol = "ws";
var url = protocol + '://' + window.location.host;
if (config.url.indexOf("/")!=0) url += "/";
url += config.url;
log(url);
socket = new WebSocket(url);
socket.onopen = function(event){
log("onopen");
socketStatus = 2;
};
socket.onclose = function(event){
log("onclose", event.code, event.reason);
socketStatus = 0;
};
socket.onerror = function(error){
log("onerror");
};
socket.onmessage = function(event) {
var text = event.data;
log(text);
if (text==="Ready!"){
socketStatus = 3;
sendQuery();
}
else{
var arr = config.parseResponse(text);
try{
arr.forEach((entry)=>{
if (isArray(entry)){
var label = entry[0];
var value = entry[1];
input.add(label, value);
}
});
var options = input.getOptions();
log(options);
if (options.length>0){
input.showMenu();
}
}
catch(e){}
}
};
};
//**************************************************************************
//** log
//**************************************************************************
var log = function(msg){
var debugr = config.debugr;
if (debugr && debugr.append){
debugr.append(msg);
}
//console.log(msg);
};
//**************************************************************************
//** Utils
//**************************************************************************
var merge = javaxt.dhtml.utils.merge;
var isArray = javaxt.dhtml.utils.isArray;
init();
};