/*
* AutoSuggestController based on Nicholas C.Zackas code
*/
function AutoSuggestController(oTextBox, oProvider) {
this.provider = oProvider;
this.textbox = oTextBox;
this.init();
}
var _p = AutoSuggestController.prototype;
_p.init = function () {
var oThis = this;
this.textbox.onkeyup = function (e) { oThis.handleKeyUp(e); }
}
_p.selectRange = function (iStart, iLength) {
if (this.textbox.createTextRange) {
var oRange = this.textbox.createTextRange();
oRange.moveStart("character", iStart);
oRange.moveEnd("character", iLength - this.textbox.value.length);
oRange.select();
} else if (this.textbox.setSelectionRange)
this.textbox.setSelectionRange(iStart, iLength);
this.textbox.focus();
}
_p.typeAhead = function (sSuggestion) {
if (this.textbox.createTextRange || this.textbox.setSelectionRange) {
var iLength = this.textbox.value.length;
this.textbox.value = sSuggestion;
this.selectRange(iLength, sSuggestion.length);
}
}
_p.autoSuggest = function (aSuggestions) {
if (aSuggestions.length > 0)
this.typeAhead(aSuggestions[0]);
}
_p.handleKeyUp = function (e) {
var oEvent = window.event || e;
var code = oEvent.keyCode;
if (!(code < 32 || (code >= 33 && code <= 46) || (code >= 112 && code <= 123)))
this.provider.requestSuggestions(this);
}
|