Perform form history suggestions case insensitive

A couple of workarounds for IE plus some unused functions are
also removed.
This commit is contained in:
Alexander Butenko 2009-12-04 23:59:26 +01:00 committed by Christian Dywan
parent c6112c9296
commit 0518844eee

View file

@ -1,9 +1,9 @@
/** /**
* An autosuggest textbox control. * An autosuggest textbox control.
* from Nicholas C. Zakas (Author) example: http://www.nczonline.net/ * from Nicholas C. Zakas (Author) example: http://www.nczonline.net/
* @class * Adopted for Midori by Alexander V. Butenko <a.butenka@gmail.com>
* @scope public
*/ */
function AutoSuggestControl(oTextbox /*:HTMLInputElement*/, function AutoSuggestControl(oTextbox /*:HTMLInputElement*/,
oProvider /*:SuggestionProvider*/) { oProvider /*:SuggestionProvider*/) {
/** /**
@ -28,7 +28,6 @@ function AutoSuggestControl(oTextbox /*:HTMLInputElement*/,
this.textbox /*:HTMLInputElement*/ = oTextbox; this.textbox /*:HTMLInputElement*/ = oTextbox;
//initialize the control //initialize the control
this.init(); this.init();
} }
/** /**
@ -38,13 +37,9 @@ function AutoSuggestControl(oTextbox /*:HTMLInputElement*/,
* @param aSuggestions An array of suggestion strings. * @param aSuggestions An array of suggestion strings.
* @param bTypeAhead If the control should provide a type ahead suggestion. * @param bTypeAhead If the control should provide a type ahead suggestion.
*/ */
AutoSuggestControl.prototype.autosuggest = function (aSuggestions /*:Array*/, AutoSuggestControl.prototype.autosuggest = function (aSuggestions /*:Array*/) {
bTypeAhead /*:boolean*/) {
//make sure theres at least one suggestion //make sure theres at least one suggestion
if (aSuggestions.length > 0) { if (aSuggestions.length > 0) {
if (bTypeAhead) {
this.typeAhead(aSuggestions[0]);
}
this.showSuggestions(aSuggestions); this.showSuggestions(aSuggestions);
} else { } else {
this.hideSuggestions(); this.hideSuggestions();
@ -56,7 +51,6 @@ AutoSuggestControl.prototype.autosuggest = function (aSuggestions /*:Array*/,
* @scope private * @scope private
*/ */
AutoSuggestControl.prototype.createDropDown = function () { AutoSuggestControl.prototype.createDropDown = function () {
var oThis = this; var oThis = this;
//create the layer and assign styles //create the layer and assign styles
@ -65,8 +59,6 @@ AutoSuggestControl.prototype.createDropDown = function () {
this.layer.style.visibility = "hidden"; this.layer.style.visibility = "hidden";
this.layer.style.width = this.textbox.offsetWidth; this.layer.style.width = this.textbox.offsetWidth;
//when the user clicks on the a suggestion, get the text (innerHTML)
//and place it into a textbox
this.layer.onmousedown = this.layer.onmousedown =
this.layer.onmouseup = this.layer.onmouseup =
this.layer.onmouseover = function (oEvent) { this.layer.onmouseover = function (oEvent) {
@ -82,8 +74,6 @@ AutoSuggestControl.prototype.createDropDown = function () {
oThis.textbox.focus(); oThis.textbox.focus();
} }
}; };
document.body.appendChild(this.layer); document.body.appendChild(this.layer);
}; };
@ -93,7 +83,6 @@ AutoSuggestControl.prototype.createDropDown = function () {
* @return The left coordinate of the textbox in pixels. * @return The left coordinate of the textbox in pixels.
*/ */
AutoSuggestControl.prototype.getLeft = function () /*:int*/ { AutoSuggestControl.prototype.getLeft = function () /*:int*/ {
var oNode = this.textbox; var oNode = this.textbox;
var iLeft = 0; var iLeft = 0;
@ -148,8 +137,7 @@ AutoSuggestControl.prototype.handleKeyUp = function (oEvent /*:Event*/) {
var iKeyCode = oEvent.keyCode; var iKeyCode = oEvent.keyCode;
//for backspace (8) and delete (46), shows suggestions without typeahead //for backspace (8) and delete (46), shows suggestions without typeahead
if (iKeyCode == 8 || iKeyCode == 46) { if (iKeyCode == 8 || iKeyCode == 46) {
this.provider.requestSuggestions(this, false); this.provider.requestSuggestions(this);
//make sure not to interfere with non-character keys //make sure not to interfere with non-character keys
} else if (iKeyCode < 32 || (iKeyCode >= 33 && iKeyCode < 46) || (iKeyCode >= 112 && iKeyCode <= 123) ) { } else if (iKeyCode < 32 || (iKeyCode >= 33 && iKeyCode < 46) || (iKeyCode >= 112 && iKeyCode <= 123) ) {
//ignore //ignore
@ -158,11 +146,8 @@ AutoSuggestControl.prototype.handleKeyUp = function (oEvent /*:Event*/) {
this.hideSuggestions(); this.hideSuggestions();
} else { } else {
//request suggestions from the suggestion provider with typeahead //request suggestions from the suggestion provider with typeahead
this.provider.requestSuggestions(this, false); this.provider.requestSuggestions(this);
} }
}; };
/** /**
@ -195,44 +180,35 @@ AutoSuggestControl.prototype.highlightSuggestion = function (oSuggestionNode) {
* @scope private * @scope private
*/ */
AutoSuggestControl.prototype.init = function () { AutoSuggestControl.prototype.init = function () {
//save a reference to this object //save a reference to this object
var oThis = this; var oThis = this;
//assign the onkeyup event handler //assign the onkeyup event handler
this.textbox.onkeyup = function (oEvent) { this.textbox.onkeyup = function (oEvent) {
//check for the proper location of the event object //check for the proper location of the event object
if (!oEvent) { if (!oEvent) {
oEvent = window.event; oEvent = window.event;
} }
//call the handleKeyUp() method with the event object //call the handleKeyUp() method with the event object
oThis.handleKeyUp(oEvent); oThis.handleKeyUp(oEvent);
}; };
//assign onkeydown event handler //assign onkeydown event handler
this.textbox.onkeydown = function (oEvent) { this.textbox.onkeydown = function (oEvent) {
//check for the proper location of the event object //check for the proper location of the event object
if (!oEvent) { if (!oEvent) {
oEvent = window.event; oEvent = window.event;
} }
//call the handleKeyDown() method with the event object //call the handleKeyDown() method with the event object
oThis.handleKeyDown(oEvent); oThis.handleKeyDown(oEvent);
}; };
//assign onblur event handler (hides suggestions) //assign onblur event handler (hides suggestions)
this.textbox.onblur = function () { this.textbox.onblur =
oThis.hideSuggestions();
};
this.textbox.onclick = function () { this.textbox.onclick = function () {
oThis.hideSuggestions(); oThis.hideSuggestions();
}; };
//create the suggestions dropdown //create the suggestions dropdown
this.createDropDown(); this.createDropDown();
}; };
@ -274,20 +250,9 @@ AutoSuggestControl.prototype.previousSuggestion = function () {
* @param iLength The number of characters to select. * @param iLength The number of characters to select.
*/ */
AutoSuggestControl.prototype.selectRange = function (iStart /*:int*/, iLength /*:int*/) { AutoSuggestControl.prototype.selectRange = function (iStart /*:int*/, iLength /*:int*/) {
if (this.textbox.setSelectionRange) {
//use text ranges for Internet Explorer
if (this.textbox.createTextRange) {
var oRange = this.textbox.createTextRange();
oRange.moveStart("character", iStart);
oRange.moveEnd("character", iLength - this.textbox.value.length);
oRange.select();
//use setSelectionRange() for Mozilla
} else if (this.textbox.setSelectionRange) {
this.textbox.setSelectionRange(iStart, iLength); this.textbox.setSelectionRange(iStart, iLength);
} }
//set focus back to the textbox
this.textbox.focus(); this.textbox.focus();
}; };
@ -298,7 +263,6 @@ AutoSuggestControl.prototype.selectRange = function (iStart /*:int*/, iLength /*
* @param aSuggestions An array of suggestions for the control. * @param aSuggestions An array of suggestions for the control.
*/ */
AutoSuggestControl.prototype.showSuggestions = function (aSuggestions /*:Array*/) { AutoSuggestControl.prototype.showSuggestions = function (aSuggestions /*:Array*/) {
var oDiv = null; var oDiv = null;
this.layer.innerHTML = ""; //clear contents of the layer this.layer.innerHTML = ""; //clear contents of the layer
@ -311,59 +275,41 @@ AutoSuggestControl.prototype.showSuggestions = function (aSuggestions /*:Array*/
this.layer.style.left = this.getLeft() + "px"; this.layer.style.left = this.getLeft() + "px";
this.layer.style.top = (this.getTop()+this.textbox.offsetHeight) + "px"; this.layer.style.top = (this.getTop()+this.textbox.offsetHeight) + "px";
this.layer.style.visibility = "visible"; this.layer.style.visibility = "visible";
}; };
/**
* Inserts a suggestion into the textbox, highlighting the
* suggested part of the text.
* @scope private
* @param sSuggestion The suggestion for the textbox.
*/
AutoSuggestControl.prototype.typeAhead = function (sSuggestion /*:String*/) {
//check for support of typeahead functionality
if (this.textbox.createTextRange || this.textbox.setSelectionRange){
var iLen = this.textbox.value.length;
this.textbox.value = sSuggestion;
this.selectRange(iLen, sSuggestion.length);
}
};
/** /**
* Request suggestions for the given autosuggest control. * Request suggestions for the given autosuggest control.
* @scope protected * @scope protected
* @param oAutoSuggestControl The autosuggest control to provide suggestions for. * @param oAutoSuggestControl The autosuggest control to provide suggestions for.
*/ */
FormSuggestions.prototype.requestSuggestions = function (oAutoSuggestControl /*:AutoSuggestControl*/, FormSuggestions.prototype.requestSuggestions = function (oAutoSuggestControl /*:AutoSuggestControl*/) {
bTypeAhead /*:boolean*/) {
var aSuggestions = []; var aSuggestions = [];
var sTextboxValue = oAutoSuggestControl.textbox.value; var sTextboxValue = oAutoSuggestControl.textbox.value.toLowerCase();
if (!this.suggestions) if (!this.suggestions)
return; return;
if (!sTextboxValue.length)
return;
if (sTextboxValue.length > 0){ //search for matching suggestions
//search for matching suggestions for (var i=0; i < this.suggestions.length; i++) {
for (var i=0; i < this.suggestions.length; i++) { if (this.suggestions[i].toLowerCase().indexOf(sTextboxValue) == 0) {
if (this.suggestions[i].indexOf(sTextboxValue) == 0) { aSuggestions.push(this.suggestions[i]);
aSuggestions.push(this.suggestions[i]); }
}
}
} }
//provide suggestions to the control //provide suggestions to the control
oAutoSuggestControl.autosuggest(aSuggestions, bTypeAhead); oAutoSuggestControl.autosuggest(aSuggestions);
}; };
function initSuggestions () { function initSuggestions () {
var inputs = document.getElementsByTagName("input"); var inputs = document.getElementsByTagName("input");
for (i=0;i<inputs.length;i++) for (i=0;i<inputs.length;i++)
{ {
var ename = inputs[i].getAttribute("name"); var ename = inputs[i].getAttribute("name");
var eid = inputs[i].getAttribute("id"); var eid = inputs[i].getAttribute("id");
if (!ename && eid) if (!ename && eid)
ename=eid; ename=eid;
if (inputs[i].type == "text") if (inputs[i].type == "text")
var smth = new AutoSuggestControl(inputs[i], new FormSuggestions(ename)); var smth = new AutoSuggestControl(inputs[i], new FormSuggestions(ename));
} }