/**
* An autosuggest textbox control.
* from Nicholas C. Zakas (Author) example: http://www.nczonline.net/
- * @class
- * @scope public
+ * Adopted for Midori by Alexander V. Butenko <a.butenka@gmail.com>
*/
+
function AutoSuggestControl(oTextbox /*:HTMLInputElement*/,
oProvider /*:SuggestionProvider*/) {
/**
this.textbox /*:HTMLInputElement*/ = oTextbox;
//initialize the control
this.init();
-
}
/**
* @param aSuggestions An array of suggestion strings.
* @param bTypeAhead If the control should provide a type ahead suggestion.
*/
-AutoSuggestControl.prototype.autosuggest = function (aSuggestions /*:Array*/,
- bTypeAhead /*:boolean*/) {
+AutoSuggestControl.prototype.autosuggest = function (aSuggestions /*:Array*/) {
//make sure theres at least one suggestion
if (aSuggestions.length > 0) {
- if (bTypeAhead) {
- this.typeAhead(aSuggestions[0]);
- }
this.showSuggestions(aSuggestions);
} else {
this.hideSuggestions();
* @scope private
*/
AutoSuggestControl.prototype.createDropDown = function () {
-
var oThis = this;
//create the layer and assign styles
this.layer.style.visibility = "hidden";
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.onmouseup =
this.layer.onmouseover = function (oEvent) {
oThis.textbox.focus();
}
};
-
-
document.body.appendChild(this.layer);
};
* @return The left coordinate of the textbox in pixels.
*/
AutoSuggestControl.prototype.getLeft = function () /*:int*/ {
-
var oNode = this.textbox;
var iLeft = 0;
var iKeyCode = oEvent.keyCode;
//for backspace (8) and delete (46), shows suggestions without typeahead
if (iKeyCode == 8 || iKeyCode == 46) {
- this.provider.requestSuggestions(this, false);
-
+ this.provider.requestSuggestions(this);
//make sure not to interfere with non-character keys
} else if (iKeyCode < 32 || (iKeyCode >= 33 && iKeyCode < 46) || (iKeyCode >= 112 && iKeyCode <= 123) ) {
//ignore
this.hideSuggestions();
} else {
//request suggestions from the suggestion provider with typeahead
- this.provider.requestSuggestions(this, false);
+ this.provider.requestSuggestions(this);
}
-
-
-
};
/**
* @scope private
*/
AutoSuggestControl.prototype.init = function () {
-
//save a reference to this object
var oThis = this;
//assign the onkeyup event handler
this.textbox.onkeyup = function (oEvent) {
-
//check for the proper location of the event object
if (!oEvent) {
oEvent = window.event;
}
-
//call the handleKeyUp() method with the event object
oThis.handleKeyUp(oEvent);
};
//assign onkeydown event handler
this.textbox.onkeydown = function (oEvent) {
-
//check for the proper location of the event object
if (!oEvent) {
oEvent = window.event;
}
-
//call the handleKeyDown() method with the event object
oThis.handleKeyDown(oEvent);
};
//assign onblur event handler (hides suggestions)
- this.textbox.onblur = function () {
- oThis.hideSuggestions();
- };
-
+ this.textbox.onblur =
this.textbox.onclick = function () {
oThis.hideSuggestions();
};
-
//create the suggestions dropdown
this.createDropDown();
};
* @param iLength The number of characters to select.
*/
AutoSuggestControl.prototype.selectRange = function (iStart /*:int*/, iLength /*:int*/) {
-
- //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) {
+ if (this.textbox.setSelectionRange) {
this.textbox.setSelectionRange(iStart, iLength);
}
-
- //set focus back to the textbox
this.textbox.focus();
};
* @param aSuggestions An array of suggestions for the control.
*/
AutoSuggestControl.prototype.showSuggestions = function (aSuggestions /*:Array*/) {
-
var oDiv = null;
this.layer.innerHTML = ""; //clear contents of the layer
this.layer.style.left = this.getLeft() + "px";
this.layer.style.top = (this.getTop()+this.textbox.offsetHeight) + "px";
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.
* @scope protected
* @param oAutoSuggestControl The autosuggest control to provide suggestions for.
*/
-FormSuggestions.prototype.requestSuggestions = function (oAutoSuggestControl /*:AutoSuggestControl*/,
- bTypeAhead /*:boolean*/) {
+FormSuggestions.prototype.requestSuggestions = function (oAutoSuggestControl /*:AutoSuggestControl*/) {
var aSuggestions = [];
- var sTextboxValue = oAutoSuggestControl.textbox.value;
+ var sTextboxValue = oAutoSuggestControl.textbox.value.toLowerCase();
if (!this.suggestions)
return;
+ if (!sTextboxValue.length)
+ return;
- if (sTextboxValue.length > 0){
- //search for matching suggestions
- for (var i=0; i < this.suggestions.length; i++) {
- if (this.suggestions[i].indexOf(sTextboxValue) == 0) {
- aSuggestions.push(this.suggestions[i]);
- }
- }
+ //search for matching suggestions
+ for (var i=0; i < this.suggestions.length; i++) {
+ if (this.suggestions[i].toLowerCase().indexOf(sTextboxValue) == 0) {
+ aSuggestions.push(this.suggestions[i]);
+ }
}
//provide suggestions to the control
- oAutoSuggestControl.autosuggest(aSuggestions, bTypeAhead);
+ oAutoSuggestControl.autosuggest(aSuggestions);
};
function initSuggestions () {
var inputs = document.getElementsByTagName("input");
+
for (i=0;i<inputs.length;i++)
{
var ename = inputs[i].getAttribute("name");
var eid = inputs[i].getAttribute("id");
if (!ename && eid)
- ename=eid;
+ ename=eid;
if (inputs[i].type == "text")
var smth = new AutoSuggestControl(inputs[i], new FormSuggestions(ename));
}