2 * author: Timothy Groves - http://www.brandspankingnew.net
3 * version: 1.0 - 2006-08-03
14 if (typeof(bsn) == "undefined")
24 if (typeof(_bsn.DOM) == "undefined")
34 _bsn.AutoSuggest = function (fldID, param)
36 if (!document.getElementById)
39 this.fld = _bsn.DOM.getElement(fldID);
46 this.aSuggestions = [];
47 this.iHighlighted = 0;
51 this.oP = (param) ? param : {};
53 if (!this.oP.minchars) this.oP.minchars = 1;
54 if (!this.oP.method) this.oP.meth = "get";
55 if (!this.oP.varname) this.oP.varname = "input";
56 if (!this.oP.className) this.oP.className = "autosuggest";
57 if (!this.oP.timeout) this.oP.timeout = 2500;
58 if (!this.oP.delay) this.oP.delay = 500;
59 if (!this.oP.maxheight && this.oP.maxheight !== 0) this.oP.maxheight = 250;
60 if (!this.oP.cache) this.oP.cache = true;
64 this.fld.onkeyup = function () { pointer.getSuggestions( this.value ) };
65 this.fld.setAttribute("autocomplete","off");
70 _bsn.AutoSuggest.prototype.getSuggestions = function (val)
73 if (val.length == this.nInputChars)
76 if (val.length < this.oP.minchars)
78 this.nInputChars = val.length;
79 this.aSuggestions = [];
80 this.clearSuggestions();
85 if (val.length>this.nInputChars && this.aSuggestions.length && this.oP.cache)
89 for (var i=0;i<this.aSuggestions.length;i++)
91 if (this.aSuggestions[i].substr(0,val.length).toLowerCase() == val.toLowerCase())
92 arr.push( this.aSuggestions[i] );
95 this.nInputChars = val.length;
96 this.aSuggestions = arr;
99 this.createList( this.aSuggestions );
105 this.nInputChars = val.length;
108 clearTimeout(this.ajID);
109 this.ajID = setTimeout( function() { pointer.doAjaxRequest() }, this.oP.delay );
119 _bsn.AutoSuggest.prototype.doAjaxRequest = function ()
123 // create ajax request
124 var url = this.oP.script+this.oP.varname+"="+this.fld.value;
125 var meth = this.oP.meth;
127 var onSuccessFunc = function (req) { pointer.setSuggestions(req) };
128 var onErrorFunc = function (status) { alert("AJAX error: "+status); };
130 var myAjax = new _bsn.Ajax;
131 myAjax.makeRequest( url, meth, onSuccessFunc, onErrorFunc );
138 _bsn.AutoSuggest.prototype.setSuggestions = function (req)
141 var xml = req.responseXML;
145 this.aSuggestions = [];
146 var results = xml.getElementsByTagName('results')[0].childNodes;
148 for (var i=0;i<results.length;i++)
150 if (results[i].hasChildNodes())
151 this.aSuggestions.push( results[i].childNodes[0].nodeValue );
155 this.idAs = "as_"+this.fld.id;
158 this.createList(this.aSuggestions);
166 _bsn.AutoSuggest.prototype.createList = function(arr)
168 // clear previous list
169 this.clearSuggestions();
171 // create and populate ul
172 var ul = _bsn.DOM.createElement("ul", {id:this.idAs, className:this.oP.className});
176 for (var i=0;i<arr.length;i++)
178 var a = _bsn.DOM.createElement("a", { href:"#" }, arr[i]);
179 a.onclick = function () { pointer.setValue( this.childNodes[0].nodeValue ); return false; }
180 var li = _bsn.DOM.createElement( "li", {className:this.oP.className}, a );
181 ul.appendChild( li );
184 var pos = _bsn.DOM.getPos(this.fld);
186 ul.style.left = pos.x + "px";
187 ul.style.top = ( pos.y + this.fld.offsetHeight ) + "px";
188 ul.style.width = this.fld.offsetWidth+"px";
189 ul.onmouseover = function(){ pointer.killTimeout() }
190 ul.onmouseout = function(){ pointer.resetTimeout() }
193 document.getElementsByTagName("body")[0].appendChild(ul);
195 if (ul.offsetHeight > this.oP.maxheight && this.oP.maxheight != 0)
197 ul.style['height'] = this.oP.maxheight;
209 this.fld.onkeydown = function(ev)
211 var key = (window.event) ? window.event.keyCode : ev.keyCode;
216 pointer.setHighlightedValue();
220 pointer.clearSuggestions();
224 pointer.changeHighlight(key);
229 pointer.changeHighlight(key);
236 this.iHighlighted = 0;
239 // remove autosuggest after an interval
241 clearTimeout(this.toID);
243 this.toID = setTimeout(function () { pointer.clearSuggestions() }, this.oP.timeout);
254 _bsn.AutoSuggest.prototype.changeHighlight = function(key)
256 var list = _bsn.DOM.getElement(this.idAs);
261 if (this.iHighlighted > 0)
262 list.childNodes[this.iHighlighted-1].className = "";
265 this.iHighlighted ++;
267 this.iHighlighted --;
270 if (this.iHighlighted > list.childNodes.length)
271 this.iHighlighted = list.childNodes.length;
272 if (this.iHighlighted < 1)
273 this.iHighlighted = 1;
275 list.childNodes[this.iHighlighted-1].className = "highlight";
277 //alert( list.childNodes[this.iHighlighted-1].firstChild.firstChild.nodeValue );
289 _bsn.AutoSuggest.prototype.killTimeout = function()
291 clearTimeout(this.toID);
294 _bsn.AutoSuggest.prototype.resetTimeout = function()
296 clearTimeout(this.toID);
298 this.toID = setTimeout(function () { pointer.clearSuggestions() }, 1000);
307 _bsn.AutoSuggest.prototype.clearSuggestions = function ()
309 if (document.getElementById(this.idAs))
310 _bsn.DOM.removeElement(this.idAs);
311 this.fld.onkeydown = null;
320 _bsn.AutoSuggest.prototype.setHighlightedValue = function ()
322 if (this.iHighlighted)
324 this.fld.value = document.getElementById(this.idAs).childNodes[this.iHighlighted-1].firstChild.firstChild.nodeValue;
326 this.clearSuggestions();
332 _bsn.AutoSuggest.prototype.setValue = function (val)
334 this.fld.value = val;