735b368b2c9100235331b8ded3f207b4a8f1c1b5
6 // Form completion utils
9 var TextInputCompletion
;
15 TextInputCompletion = function(input
, url
) {
18 this.previousValue
= this.input
.value
;
20 var thisManager
= this;
21 addListener(this.input
, 'keyup', function(evt
) { thisManager
._inputComplete(evt
); });
22 addListener(this.input
, 'blur', function(evt
) {thisManager
._inputQuit(evt
);});
23 this.input
.cancelNextSubmit
= true;
26 for (var i
=0 ; i
<UID_ATTEMPT
;i
++) {
27 uid
= Math
.random().toString().slice(2);
28 uid
= 'completions' + uid
;
29 if (!document
.getElementById(uid
))
33 var completions
= document
.createElement('div');
35 completions
.style
.position
= 'relative';
36 completions
.style
.top
= getObjectHeight(input
) + 'px';
38 var parent
= input
.parentNode
;
39 parent
.insertBefore(completions
, input
);
41 this.completions
= completions
;
42 addListener(this.completions
, 'click',
43 function(evt
){thisManager
.selectCompletion(getEventObject(evt
));}
45 this.selectedCompletion
= null;
49 TextInputCompletion
.prototype._inputComplete = function(evt
) {
50 var currentValue
= this.input
.value
;
51 if (currentValue
== this.previousValue
)
52 this.selectCompletion(getEventObject(evt
));
54 var url
= this.url
+ '?value=' + encodeURIComponent(currentValue
);
55 url
= url
+ '&uid=' + this.completions
.id
;
56 var thisManager
= this;
57 var fi
= new FragmentImporter(url
, function(){thisManager
.completeIfOneResult() })
62 TextInputCompletion
.prototype._inputQuit = function(evt
) {
63 if (this.selectedCompletion
) {
65 this.copyCompletion(this.selectedCompletion
);
69 this.selectedCompletion
= null;
71 setTimeout(function(){thisCompleter
.completions
.innerHTML
='';}, 200);
74 TextInputCompletion
.prototype.selectCompletion = function(evt
) {
75 var res
= this.completions
.getElementsByTagName('li');
78 if (evt
.type
== 'keyup') {
79 var newSelection
= false;
82 if (!this.selectedCompletion
) {
83 this.selectedCompletion
= res
[0];
86 else if (this.selectedCompletion
.nextSibling
) {
87 this.selectedCompletion
.className
= '';
88 this.selectedCompletion
= this.selectedCompletion
.nextSibling
;
93 if (this.selectedCompletion
&& this.selectedCompletion
.previousSibling
) {
94 this.selectedCompletion
.className
= '';
95 this.selectedCompletion
= this.selectedCompletion
.previousSibling
;
100 if (this.selectedCompletion
) {
101 this.copyCompletion(this.selectedCompletion
);
102 this.completions
.innerHTML
= '';
103 this.input
.cancelNextSubmit
= true;
109 this.selectedCompletion
.className
= 'selected';
110 this.copyCompletion(this.selectedCompletion
);
113 else if (evt
.type
=='click') {
114 this.selectedCompletion
= getTargetedObject(evt
);
115 this.copyCompletion(this.selectedCompletion
);
119 TextInputCompletion
.prototype.copyCompletion = function(completion
) {
120 this.input
.value
= this.previousValue
= completion
.firstChild
.nodeValue
;
123 TextInputCompletion
.prototype.completeIfOneResult = function() {
124 this.selectedCompletion
= null;
125 var currentValue
= this.input
.value
;
127 if (this.previousValue
.length
< currentValue
.length
) {
128 var res
= this.completions
.getElementsByTagName('li');
129 if (res
.length
== 1) {
130 var completion
= res
[0];
131 this.copyCompletion(completion
);
132 this.completions
.innerHTML
= '';
136 this.previousValue
= currentValue
;