Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
10582 lgm 1
function actb(obj,ca){
2
 
3
	//alert();
4
	/* ---- Public Variables ---- */
5
	this.actb_timeOut = -1; // Autocomplete Timeout in ms (-1: autocomplete never time out)
6
	this.actb_lim = 10;    // Number of elements autocomplete can show (-1: no limit)
7
	this.actb_firstText = false; // should the auto complete be limited to the beginning of keyword?
8
	this.actb_mouse = true; // Enable Mouse Support
9
	this.actb_delimiter = new Array(';',',');  // Delimiter for multiple autocomplete. Set it to empty array for single autocomplete
10
	this.actb_startcheck = 1; // Show widget only after this number of characters is typed in.
11
	/* ---- Public Variables ---- */
12
 
13
	/* --- Styles --- */
14
	this.actb_bgColor = '#FFFFFF';
15
	this.actb_textColor = '#000000';
16
	this.actb_hColor = '#E6E6E6';
17
	this.actb_fFamily = 'Verdana';
18
	this.actb_fSize = '14px';
19
	this.actb_hStyle = 'font-weight: bolder; text-decoration:underline;';
20
	this.lineheight = '22px';
21
 
22
	/* --- Styles --- */
23
 
24
	/* ---- Private Variables ---- */
25
	var actb_delimwords = new Array();
26
	var actb_cdelimword = 0;
27
	var actb_delimchar = new Array();
28
	var actb_display = false;
29
	var actb_pos = 0;
30
	var actb_total = 0;
31
	var actb_curr = null;
32
	var actb_rangeu = 0;
33
	var actb_ranged = 0;
34
	var actb_bool = new Array();
35
	var actb_pre = 0;
36
	var actb_toid;
37
	var actb_tomake = false;
38
	var actb_getpre = "";
39
	var actb_mouse_on_list = 1;
40
	var actb_kwcount = 0;
41
	var actb_caretmove = false;
42
	this.actb_keywords = new Array();
43
	/* ---- Private Variables---- */
44
 
45
	this.actb_keywords = ca;
46
	var actb_self = this;
47
 
48
	actb_curr = obj;
49
 
50
	addEvent(actb_curr,"focus",actb_setup);
51
	function actb_setup(){
52
		addEvent(document,"keydown",actb_checkkey);
53
		addEvent(actb_curr,"blur",actb_clear);
54
		addEvent(document,"keypress",actb_keypress);
55
	}
56
 
57
	function actb_clear(evt){
58
		if (!evt) evt = event;
59
		removeEvent(document,"keydown",actb_checkkey);
60
		removeEvent(actb_curr,"blur",actb_clear);
61
		removeEvent(document,"keypress",actb_keypress);
62
		actb_removedisp();
63
	}
64
	function actb_parse(n){
65
		if (actb_self.actb_delimiter.length > 0){
66
			var t = actb_delimwords[actb_cdelimword].trim().addslashes();
67
			var plen = actb_delimwords[actb_cdelimword].trim().length;
68
		}else{
69
			var t = actb_curr.value.addslashes();
70
			var plen = actb_curr.value.length;
71
		}
72
		var tobuild = '';
73
		var i;
74
 
75
		if (actb_self.actb_firstText){
76
			var re = new RegExp("^" + t, "i");
77
		}else{
78
			var re = new RegExp(t, "i");
79
		}
80
		var p = n.search(re);
81
 
82
		for (i=0;i<p;i++){
83
			tobuild += n.substr(i,1);
84
		}
85
		tobuild += "<font style='"+(actb_self.actb_hStyle)+"'>"
86
		for (i=p;i<plen+p;i++){
87
			tobuild += n.substr(i,1);
88
		}
89
		tobuild += "</font>";
90
			for (i=plen+p;i<n.length;i++){
91
			tobuild += n.substr(i,1);
92
		}
93
		return tobuild;
94
	}
95
	function actb_generate(){
96
		if (document.getElementById('tat_table')){ actb_display = false;document.body.removeChild(document.getElementById('tat_table')); } 
97
		if (actb_kwcount == 0){
98
			actb_display = false;
99
			return;
100
		}
101
		a = document.createElement('table');
102
		a.cellSpacing='1px';
103
		a.cellPadding='2px';
104
		a.style.position='absolute';
105
		a.style.top = eval(curTop(actb_curr) + actb_curr.offsetHeight) + "px";
106
		a.style.left = curLeft(actb_curr) + "px";
107
		a.style.backgroundColor=actb_self.actb_bgColor;
108
		a.style.width = '191px';
109
		a.id = 'tat_table';
110
		document.body.appendChild(a);
111
		var i;
112
		var first = true;
113
		var j = 1;
114
		if (actb_self.actb_mouse){
115
			a.onmouseout = actb_table_unfocus;
116
			a.onmouseover = actb_table_focus;
117
		}
118
		var counter = 0;
119
		for (i=0;i<actb_self.actb_keywords.length;i++){
120
			if (actb_bool[i]){
121
				counter++;
122
				r = a.insertRow(-1);
123
				if (first && !actb_tomake){
124
					r.style.backgroundColor = actb_self.actb_hColor;
125
					 r.style.height = '22px';
126
					first = false;
127
					actb_pos = counter;
128
				}else if(actb_pre == i){
129
					r.style.backgroundColor = actb_self.actb_hColor;
130
					 r.style.height = '22px';
131
					first = false;
132
					actb_pos = counter;
133
				}else{
134
					r.style.backgroundColor = actb_self.actb_bgColor;
135
					 r.style.height = '22px';
136
				}
137
				r.id = 'tat_tr'+(j);
138
				c = r.insertCell(-1);
139
				c.style.color = actb_self.actb_textColor;
140
				c.style.fontFamily = actb_self.actb_fFamily;
141
				c.style.fontSize = actb_self.actb_fSize;
142
				c.innerHTML = actb_parse(actb_self.actb_keywords[i]);
143
				c.id = 'tat_td'+(j);
144
				c.setAttribute('pos',j);
145
				if (actb_self.actb_mouse){
146
					c.style.cursor = 'pointer';
147
					c.onclick=actb_mouseclick;
148
					c.onmouseover = actb_table_highlight;
149
				}
150
				j++;
151
			}
152
			if (j - 1 == actb_self.actb_lim && j < actb_total){
153
				r = a.insertRow(-1);
154
				r.style.backgroundColor = actb_self.actb_bgColor;
155
				c = r.insertCell(-1);
156
				c.style.color = actb_self.actb_textColor;
157
				c.style.fontFamily = 'arial narrow';
158
 
159
				c.style.fontSize = actb_self.actb_fSize;
160
				c.align='center';
161
				replaceHTML(c,'\\/');
162
				if (actb_self.actb_mouse){
163
					c.style.cursor = 'pointer';
164
					c.onclick = actb_mouse_down;
165
				}
166
				break;
167
			}
168
		}
169
		actb_rangeu = 1;
170
		actb_ranged = j-1;
171
		actb_display = true;
172
		if (actb_pos <= 0) actb_pos = 1;
173
	}
174
	function actb_remake(){
175
		document.body.removeChild(document.getElementById('tat_table'));
176
		a = document.createElement('table');
177
		a.cellSpacing='1px';
178
		a.cellPadding='2px';
179
		a.style.position='absolute';
180
		a.style.top = eval(curTop(actb_curr) + actb_curr.offsetHeight) + "px";
181
		a.style.left = curLeft(actb_curr) + "px";
182
		a.style.backgroundColor=actb_self.actb_bgColor;
183
		a.style.width = '191px';
184
		a.id = 'tat_table';
185
		if (actb_self.actb_mouse){
186
			a.onmouseout= actb_table_unfocus;
187
			a.onmouseover=actb_table_focus;
188
		}
189
		document.body.appendChild(a);
190
		var i;
191
		var first = true;
192
		var j = 1;
193
		if (actb_rangeu > 1){
194
			r = a.insertRow(-1);
195
			r.style.backgroundColor = actb_self.actb_bgColor;
196
			c = r.insertCell(-1);
197
			c.style.color = actb_self.actb_textColor;
198
			c.style.fontFamily = 'arial narrow';
199
			c.style.fontSize = actb_self.actb_fSize;
200
			c.align='center';
201
			replaceHTML(c,'/\\');
202
			if (actb_self.actb_mouse){
203
				c.style.cursor = 'pointer';
204
				c.onclick = actb_mouse_up;
205
			}
206
		}
207
		for (i=0;i<actb_self.actb_keywords.length;i++){
208
			if (actb_bool[i]){
209
				if (j >= actb_rangeu && j <= actb_ranged){
210
					r = a.insertRow(-1);
211
					r.style.backgroundColor = actb_self.actb_bgColor;
212
					r.id = 'tat_tr'+(j);
213
					c = r.insertCell(-1);
214
					c.style.color = actb_self.actb_textColor;
215
					c.style.fontFamily = actb_self.actb_fFamily;
216
					c.style.fontSize = actb_self.actb_fSize;
217
					c.innerHTML = actb_parse(actb_self.actb_keywords[i]);
218
					c.id = 'tat_td'+(j);
219
					c.setAttribute('pos',j);
220
					if (actb_self.actb_mouse){
221
						c.style.cursor = 'pointer';
222
						c.onclick=actb_mouseclick;
223
						c.onmouseover = actb_table_highlight;
224
					}
225
					j++;
226
				}else{
227
					j++;
228
				}
229
			}
230
			if (j > actb_ranged) break;
231
		}
232
		if (j-1 < actb_total){
233
			r = a.insertRow(-1);
234
			r.style.backgroundColor = actb_self.actb_bgColor;
235
			c = r.insertCell(-1);
236
			c.style.color = actb_self.actb_textColor;
237
			c.style.fontFamily = 'arial narrow';
238
			c.style.fontSize = actb_self.actb_fSize;
239
			c.align='center';
240
			replaceHTML(c,'\\/');
241
			if (actb_self.actb_mouse){
242
				c.style.cursor = 'pointer';
243
				c.onclick = actb_mouse_down;
244
			}
245
		}
246
	}
247
	function actb_goup(){
248
		if (!actb_display) return;
249
		if (actb_pos == 1) return;
250
		document.getElementById('tat_tr'+actb_pos).style.backgroundColor = actb_self.actb_bgColor;
251
		actb_pos--;
252
		if (actb_pos < actb_rangeu) actb_moveup();
253
		document.getElementById('tat_tr'+actb_pos).style.backgroundColor = actb_self.actb_hColor;
254
		if (actb_toid) clearTimeout(actb_toid);
255
		if (actb_self.actb_timeOut > 0) actb_toid = setTimeout(function(){actb_mouse_on_list=0;actb_removedisp();},actb_self.actb_timeOut);
256
	}
257
	function actb_godown(){
258
		if (!actb_display) return;
259
		if (actb_pos == actb_total) return;
260
		document.getElementById('tat_tr'+actb_pos).style.backgroundColor = actb_self.actb_bgColor;
261
		actb_pos++;
262
		if (actb_pos > actb_ranged) actb_movedown();
263
		document.getElementById('tat_tr'+actb_pos).style.backgroundColor = actb_self.actb_hColor;
264
		if (actb_toid) clearTimeout(actb_toid);
265
		if (actb_self.actb_timeOut > 0) actb_toid = setTimeout(function(){actb_mouse_on_list=0;actb_removedisp();},actb_self.actb_timeOut);
266
	}
267
	function actb_movedown(){
268
		actb_rangeu++;
269
		actb_ranged++;
270
		actb_remake();
271
	}
272
	function actb_moveup(){
273
		actb_rangeu--;
274
		actb_ranged--;
275
		actb_remake();
276
	}
277
 
278
	/* Mouse */
279
	function actb_mouse_down(){
280
		document.getElementById('tat_tr'+actb_pos).style.backgroundColor = actb_self.actb_bgColor;
281
		actb_pos++;
282
		actb_movedown();
283
		document.getElementById('tat_tr'+actb_pos).style.backgroundColor = actb_self.actb_hColor;
284
		actb_curr.focus();
285
		actb_mouse_on_list = 0;
286
		if (actb_toid) clearTimeout(actb_toid);
287
		if (actb_self.actb_timeOut > 0) actb_toid = setTimeout(function(){actb_mouse_on_list=0;actb_removedisp();},actb_self.actb_timeOut);
288
	}
289
	function actb_mouse_up(evt){
290
		if (!evt) evt = event;
291
		if (evt.stopPropagation){
292
			evt.stopPropagation();
293
		}else{
294
			evt.cancelBubble = true;
295
		}
296
		document.getElementById('tat_tr'+actb_pos).style.backgroundColor = actb_self.actb_bgColor;
297
		actb_pos--;
298
		actb_moveup();
299
		document.getElementById('tat_tr'+actb_pos).style.backgroundColor = actb_self.actb_hColor;
300
		actb_curr.focus();
301
		actb_mouse_on_list = 0;
302
		if (actb_toid) clearTimeout(actb_toid);
303
		if (actb_self.actb_timeOut > 0) actb_toid = setTimeout(function(){actb_mouse_on_list=0;actb_removedisp();},actb_self.actb_timeOut);
304
	}
305
	function actb_mouseclick(evt){
306
		if (!evt) evt = event;
307
		if (!actb_display) return;
308
		actb_mouse_on_list = 0;
309
		actb_pos = this.getAttribute('pos');
310
		actb_penter();
311
	}
312
	function actb_table_focus(){
313
		actb_mouse_on_list = 1;
314
	}
315
	function actb_table_unfocus(){
316
		actb_mouse_on_list = 0;
317
		if (actb_toid) clearTimeout(actb_toid);
318
		if (actb_self.actb_timeOut > 0) actb_toid = setTimeout(function(){actb_mouse_on_list = 0;actb_removedisp();},actb_self.actb_timeOut);
319
	}
320
	function actb_table_highlight(){
321
		actb_mouse_on_list = 1;
322
		document.getElementById('tat_tr'+actb_pos).style.backgroundColor = actb_self.actb_bgColor;
323
		actb_pos = this.getAttribute('pos');
324
		while (actb_pos < actb_rangeu) actb_moveup();
325
		while (actb_pos > actb_ranged) actb_movedown();
326
		document.getElementById('tat_tr'+actb_pos).style.backgroundColor = actb_self.actb_hColor;
327
		if (actb_toid) clearTimeout(actb_toid);
328
		if (actb_self.actb_timeOut > 0) actb_toid = setTimeout(function(){actb_mouse_on_list = 0;actb_removedisp();},actb_self.actb_timeOut);
329
	}
330
	/* ---- */
331
 
332
	function actb_insertword(a){
333
		if (actb_self.actb_delimiter.length > 0){
334
			str = '';
335
			l=0;
336
			for (i=0;i<actb_delimwords.length;i++){
337
				if (actb_cdelimword == i){
338
					prespace = postspace = '';
339
					gotbreak = false;
340
					for (j=0;j<actb_delimwords[i].length;++j){
341
						if (actb_delimwords[i].charAt(j) != ' '){
342
							gotbreak = true;
343
							break;
344
						}
345
						prespace += ' ';
346
					}
347
					for (j=actb_delimwords[i].length-1;j>=0;--j){
348
						if (actb_delimwords[i].charAt(j) != ' ') break;
349
						postspace += ' ';
350
					}
351
					str += prespace;
352
					str += a;
353
					l = str.length;
354
					if (gotbreak) str += postspace;
355
				}else{
356
					str += actb_delimwords[i];
357
				}
358
				if (i != actb_delimwords.length - 1){
359
					str += actb_delimchar[i];
360
				}
361
			}
362
			actb_curr.value = str;
363
			setCaret(actb_curr,l);
364
		}else{
365
			actb_curr.value = a;
366
		}
367
		actb_mouse_on_list = 0;
368
		actb_removedisp();
369
	}
370
	function actb_penter(){
371
		if (!actb_display) return;
372
		actb_display = false;
373
		var word = '';
374
		var c = 0;
375
		for (var i=0;i<=actb_self.actb_keywords.length;i++){
376
			if (actb_bool[i]) c++;
377
			if (c == actb_pos){
378
				word = actb_self.actb_keywords[i];
379
				break;
380
			}
381
		}
382
		actb_insertword(word);
383
		l = getCaretStart(actb_curr);
384
	}
385
	function actb_removedisp(){
386
		if (actb_mouse_on_list==0){
387
			actb_display = 0;
388
			if (document.getElementById('tat_table')){ document.body.removeChild(document.getElementById('tat_table')); }
389
			if (actb_toid) clearTimeout(actb_toid);
390
		}
391
	}
392
	function actb_keypress(e){
393
		if (actb_caretmove) stopEvent(e);
394
		return !actb_caretmove;
395
	}
396
	function actb_checkkey(evt){
397
		if (!evt) evt = event;
398
		a = evt.keyCode;
399
		caret_pos_start = getCaretStart(actb_curr);
400
		actb_caretmove = 0;
401
		switch (a){
402
			case 38:
403
				actb_goup();
404
				actb_caretmove = 1;
405
				return false;
406
				break;
407
			case 40:
408
				actb_godown();
409
				actb_caretmove = 1;
410
				return false;
411
				break;
412
			case 13: case 9:
413
				if (actb_display){
414
					actb_caretmove = 1;
415
					actb_penter();
416
					return false;
417
				}else{
418
					return true;
419
				}
420
				break;
421
			default:
422
				setTimeout(function(){actb_tocomplete(a)},50);
423
				break;
424
		}
425
	}
426
 
427
	function actb_tocomplete(kc){
428
		if (kc == 38 || kc == 40 || kc == 13) return;
429
		var i;
430
		if (actb_display){ 
431
			var word = 0;
432
			var c = 0;
433
			for (var i=0;i<=actb_self.actb_keywords.length;i++){
434
				if (actb_bool[i]) c++;
435
				if (c == actb_pos){
436
					word = i;
437
					break;
438
				}
439
			}
440
			actb_pre = word;
441
		}else{ actb_pre = -1};
442
 
443
		if (actb_curr.value == ''){
444
			actb_mouse_on_list = 0;
445
			actb_removedisp();
446
			return;
447
		}
448
		if (actb_self.actb_delimiter.length > 0){
449
			caret_pos_start = getCaretStart(actb_curr);
450
			caret_pos_end = getCaretEnd(actb_curr);
451
 
452
			delim_split = '';
453
			for (i=0;i<actb_self.actb_delimiter.length;i++){
454
				delim_split += actb_self.actb_delimiter[i];
455
			}
456
			delim_split = delim_split.addslashes();
457
			delim_split_rx = new RegExp("(["+delim_split+"])");
458
			c = 0;
459
			actb_delimwords = new Array();
460
			actb_delimwords[0] = '';
461
			for (i=0,j=actb_curr.value.length;i<actb_curr.value.length;i++,j--){
462
				if (actb_curr.value.substr(i,j).search(delim_split_rx) == 0){
463
					ma = actb_curr.value.substr(i,j).match(delim_split_rx);
464
					actb_delimchar[c] = ma[1];
465
					c++;
466
					actb_delimwords[c] = '';
467
				}else{
468
					actb_delimwords[c] += actb_curr.value.charAt(i);
469
				}
470
			}
471
 
472
			var l = 0;
473
			actb_cdelimword = -1;
474
			for (i=0;i<actb_delimwords.length;i++){
475
				if (caret_pos_end >= l && caret_pos_end <= l + actb_delimwords[i].length){
476
					actb_cdelimword = i;
477
				}
478
				l+=actb_delimwords[i].length + 1;
479
			}
480
			var ot = actb_delimwords[actb_cdelimword].trim(); 
481
			var t = actb_delimwords[actb_cdelimword].addslashes().trim();
482
		}else{
483
			var ot = actb_curr.value;
484
			var t = actb_curr.value.addslashes();
485
		}
486
		if (ot.length == 0){
487
			actb_mouse_on_list = 0;
488
			actb_removedisp();
489
		}
490
		if (ot.length < actb_self.actb_startcheck) return this;
491
		if (actb_self.actb_firstText){
492
			var re = new RegExp("^" + t, "i");
493
		}else{
494
			var re = new RegExp(t, "i");
495
		}
496
 
497
		actb_total = 0;
498
		actb_tomake = false;
499
		actb_kwcount = 0;
500
		for (i=0;i<actb_self.actb_keywords.length;i++){
501
			actb_bool[i] = false;
502
			if (re.test(actb_self.actb_keywords[i])){
503
				actb_total++;
504
				actb_bool[i] = true;
505
				actb_kwcount++;
506
				if (actb_pre == i) actb_tomake = true;
507
			}
508
		}
509
 
510
		if (actb_toid) clearTimeout(actb_toid);
511
		if (actb_self.actb_timeOut > 0) actb_toid = setTimeout(function(){actb_mouse_on_list = 0;actb_removedisp();},actb_self.actb_timeOut);
512
		actb_generate();
513
	}
514
	return this;
515
}