📄 autocomplete.js
字号:
/*
Copyright (c) 2006, Yahoo! Inc. All rights reserved.
Code licensed under the BSD License:
http://developer.yahoo.com/yui/license.txt
version: 0.11.0
*/
/****************************************************************************/
/****************************************************************************/
/****************************************************************************/
/**
* Class providing the customizable functionality of a plug-and-play DHTML
* auto complete widget. Some key features:
* <ul>
* <li>Navigate with up/down arrow keys and/or mouse to pick a selection</li>
* <li>The drop down container can "roll down" or "fly out" via configurable
* animation</li>
* <li>UI look-and-feel customizable through CSS, including container
* attributes, borders, position, fonts, etc</li>
* </ul>
*
* requires YAHOO.util.Dom Dom utility
* requires YAHOO.util.Event Event utility
* requires YAHOO.widget.DataSource Data source class
* see YAHOO.util.Animation Animation utility
* see JSON JSON library
*
* @constructor
* @param {element | string} inputEl DOM element reference or string ID of the auto complete input field
* @param {element | string} containerEl DOM element reference or string ID of the auto complete <div>
* container
* @param {object} oDataSource Instance of YAHOO.widget.DataSource for query/results
* @param {object} oConfigs Optional object literal of config params
*/
YAHOO.widget.AutoComplete = function(inputEl,containerEl,oDataSource,oConfigs) {
if(inputEl && containerEl && oDataSource) {
// Validate data source
if (oDataSource && (oDataSource instanceof YAHOO.widget.DataSource)) {
this.dataSource = oDataSource;
}
else {
return;
}
// Validate input element
if(YAHOO.util.Dom.inDocument(inputEl)) {
if(typeof inputEl == "string") {
this._sName = "instance" + YAHOO.widget.AutoComplete._nIndex + " " + inputEl;
this._oTextbox = document.getElementById(inputEl);
}
else {
this._sName = (inputEl.id) ?
"instance" + YAHOO.widget.AutoComplete._nIndex + " " + inputEl.id:
"instance" + YAHOO.widget.AutoComplete._nIndex;
this._oTextbox = inputEl;
}
}
else {
return;
}
// Validate container element
if(YAHOO.util.Dom.inDocument(containerEl)) {
if(typeof containerEl == "string") {
this._oContainer = document.getElementById(containerEl);
}
else {
this._oContainer = containerEl;
}
if(this._oContainer.style.display == "none") {
}
}
else {
return;
}
// Set any config params passed in to override defaults
if (typeof oConfigs == "object") {
for(var sConfig in oConfigs) {
if (sConfig) {
this[sConfig] = oConfigs[sConfig];
}
}
}
// Initialization sequence
this._initContainer();
this._initProps();
this._initList();
this._initContainerHelpers();
// Set up events
var oSelf = this;
var oTextbox = this._oTextbox;
// Events are actually for the content module within the container
var oContent = this._oContainer._oContent;
// Dom events
YAHOO.util.Event.addListener(oTextbox,"keyup",oSelf._onTextboxKeyUp,oSelf);
YAHOO.util.Event.addListener(oTextbox,"keydown",oSelf._onTextboxKeyDown,oSelf);
YAHOO.util.Event.addListener(oTextbox,"keypress",oSelf._onTextboxKeyPress,oSelf);
YAHOO.util.Event.addListener(oTextbox,"focus",oSelf._onTextboxFocus,oSelf);
YAHOO.util.Event.addListener(oTextbox,"blur",oSelf._onTextboxBlur,oSelf);
YAHOO.util.Event.addListener(oContent,"mouseover",oSelf._onContainerMouseover,oSelf);
YAHOO.util.Event.addListener(oContent,"mouseout",oSelf._onContainerMouseout,oSelf);
YAHOO.util.Event.addListener(oContent,"scroll",oSelf._onContainerScroll,oSelf);
YAHOO.util.Event.addListener(oContent,"resize",oSelf._onContainerResize,oSelf);
if(oTextbox.form) {
YAHOO.util.Event.addListener(oTextbox.form,"submit",oSelf._onFormSubmit,oSelf);
}
// Custom events
this.textboxFocusEvent = new YAHOO.util.CustomEvent("textboxFocus", this);
this.textboxKeyEvent = new YAHOO.util.CustomEvent("textboxKey", this);
this.dataRequestEvent = new YAHOO.util.CustomEvent("dataRequest", this);
this.dataReturnEvent = new YAHOO.util.CustomEvent("dataReturn", this);
this.dataErrorEvent = new YAHOO.util.CustomEvent("dataError", this);
this.containerExpandEvent = new YAHOO.util.CustomEvent("containerExpand", this);
this.typeAheadEvent = new YAHOO.util.CustomEvent("typeAhead", this);
this.itemMouseOverEvent = new YAHOO.util.CustomEvent("itemMouseOver", this);
this.itemMouseOutEvent = new YAHOO.util.CustomEvent("itemMouseOut", this);
this.itemArrowToEvent = new YAHOO.util.CustomEvent("itemArrowTo", this);
this.itemArrowFromEvent = new YAHOO.util.CustomEvent("itemArrowFrom", this);
this.itemSelectEvent = new YAHOO.util.CustomEvent("itemSelect", this);
this.unmatchedItemSelectEvent = new YAHOO.util.CustomEvent("unmatchedItemSelect", this);
this.selectionEnforceEvent = new YAHOO.util.CustomEvent("selectionEnforce", this);
this.containerCollapseEvent = new YAHOO.util.CustomEvent("containerCollapse", this);
this.textboxBlurEvent = new YAHOO.util.CustomEvent("textboxBlur", this);
// Finish up
oTextbox.setAttribute("autocomplete","off");
YAHOO.widget.AutoComplete._nIndex++;
}
// Required arguments were not found
else {
}
};
/***************************************************************************
* Public member variables
***************************************************************************/
/**
* The data source object that encapsulates the data used for auto completion.
* This object should be an inherited object from YAHOO.widget.DataSource.
*
* @type object
*/
YAHOO.widget.AutoComplete.prototype.dataSource = null;
/**
* Number of characters that must be entered before querying for results.
* Default: 1.
*
* @type number
*/
YAHOO.widget.AutoComplete.prototype.minQueryLength = 1;
/**
* Maximum number of results to display in auto complete container. Default: 10.
*
* @type number
*/
YAHOO.widget.AutoComplete.prototype.maxResultsDisplayed = 10;
/**
* Number of seconds to delay before submitting a query request. If a query
* request is received before a previous one has completed its delay, the
* previous request is cancelled and the new request is set to the delay.
* Default: 0.5.
*
* @type number
*/
YAHOO.widget.AutoComplete.prototype.queryDelay = 0.5;
/**
* Class name of a highlighted item within the auto complete container.
* Default: "yui-ac-highlight".
*
* @type string
*/
YAHOO.widget.AutoComplete.prototype.highlightClassName = "yui-ac-highlight";
/**
* Class name of a pre-highlighted item within the auto complete container.
* Default: null.
*
* @type string
*/
YAHOO.widget.AutoComplete.prototype.prehighlightClassName = null;
/**
* Query delimiter. A single character separator for multiple delimited
* selections. Multiple delimiter characteres may be defined as an array of
* strings. A null value or empty string indicates that query results cannot
* be delimited. This feature is not recommended if you need forceSelection to
* be true. Default: null.
*
* @type string or array
*/
YAHOO.widget.AutoComplete.prototype.delimChar = null;
/**
* Whether or not the first item in the auto complete container should be
* automatically highlighted on expand. Default: true.
*
* @type boolean
*/
YAHOO.widget.AutoComplete.prototype.autoHighlight = true;
/**
* Whether or not the auto complete input field should be automatically updated
* with the first query result as the user types, auto-selecting the substring
* that the user has not typed. Default: false.
*
* @type boolean
*/
YAHOO.widget.AutoComplete.prototype.typeAhead = false;
/**
* Whether or not to animate the expansion/collapse of the auto complete
* container in the horizontal direction. Default: false.
*
* @type boolean
*/
YAHOO.widget.AutoComplete.prototype.animHoriz = false;
/**
* Whether or not to animate the expansion/collapse of the auto complete
* container in the vertical direction. Default: true.
*
* @type boolean
*/
YAHOO.widget.AutoComplete.prototype.animVert = true;
/**
* Speed of container expand/collapse animation, in seconds. Default: 0.3.
*
* @type number
*/
YAHOO.widget.AutoComplete.prototype.animSpeed = 0.3;
/**
* Whether or not to force the user's selection to match one of the query
* results. Enabling this feature essentially transforms the auto complete form
* input field into a <select> field. This feature is not recommended
* with delimiter character(s) defined. Default: false.
*
* @type boolean
*/
YAHOO.widget.AutoComplete.prototype.forceSelection = false;
/**
* Whether or not to allow browsers to cache user-typed input in the input
* field. Disabling this feature will prevent the widget from setting the
* autocomplete="off" on the auto complete input field. When autocomplete="off"
* and users click the back button after form submission, user-typed input can
* be prefilled by the browser from its cache. This caching of user input may
* not be desired for sensitive data, such as credit card numbers, in which
* case, implementers should consider setting allowBrowserAutocomplete to false.
* Default: true.
*
* @type boolean
*/
YAHOO.widget.AutoComplete.prototype.allowBrowserAutocomplete = true;
/**
* Whether or not the auto complete container should always be displayed.
* Enabling this feature prevents the toggling of the container to a collapsed
* state. Default: false.
*
* @type boolean
*/
YAHOO.widget.AutoComplete.prototype.alwaysShowContainer = false;
/**
* Whether or not to use an iFrame to layer over Windows form elements in
* IE. Set to true only when the auto complete container will be on top of a
* <select> field in IE and thus exposed to the IE z-index bug (i.e.,
* 5.5 < IE < 7). Default:false.
*
* @type boolean
*/
YAHOO.widget.AutoComplete.prototype.useIFrame = false;
/**
* Configurable iFrame src used when useIFrame = true. Implementations over SSL
* should set this parameter to an appropriate https location in order to avoid
* security-related browser errors. Default:"about:blank".
*
* @type boolean
*/
YAHOO.widget.AutoComplete.prototype.iFrameSrc = "about:blank";
/**
* Whether or not the auto complete container should have a shadow. Default:false.
*
* @type boolean
*/
YAHOO.widget.AutoComplete.prototype.useShadow = false;
/***************************************************************************
* Public methods
***************************************************************************/
/**
* Public accessor to the unique name of the auto complete instance.
*
* @return {string} Unique name of the auto complete instance
*/
YAHOO.widget.AutoComplete.prototype.getName = function() {
return this._sName;
};
/**
* Public accessor to the unique name of the auto complete instance.
*
* @return {string} Unique name of the auto complete instance
*/
YAHOO.widget.AutoComplete.prototype.toString = function() {
return "AutoComplete " + this._sName;
};
/**
* Public accessor to the internal array of DOM <li> elements that
* display query results within the auto complete container.
*
* @return {array} Array of <li> elements within the auto complete
* container
*/
YAHOO.widget.AutoComplete.prototype.getListItems = function() {
return this._aListItems;
};
/**
* Public accessor to the data held in an <li> element of the
* auto complete container.
*
* @return {object or array} Object or array of result data or null
*/
YAHOO.widget.AutoComplete.prototype.getListItemData = function(oListItem) {
if(oListItem._oResultData) {
return oListItem._oResultData;
}
else {
return false;
}
};
/**
* Sets HTML markup for the auto complete container header. This markup will be
* inserted within a <div> tag with a class of "ac_hd".
*
* @param {string} sHeader HTML markup for container header
*/
YAHOO.widget.AutoComplete.prototype.setHeader = function(sHeader) {
if(sHeader) {
if(this._oContainer._oContent._oHeader) {
this._oContainer._oContent._oHeader.innerHTML = sHeader;
this._oContainer._oContent._oHeader.style.display = "block";
}
}
else {
this._oContainer._oContent._oHeader.innerHTML = "";
this._oContainer._oContent._oHeader.style.display = "none";
}
};
/**
* Sets HTML markup for the auto complete container footer. This markup will be
* inserted within a <div> tag with a class of "ac_ft".
*
* @param {string} sFooter HTML markup for container footer
*/
YAHOO.widget.AutoComplete.prototype.setFooter = function(sFooter) {
if(sFooter) {
if(this._oContainer._oContent._oFooter) {
this._oContainer._oContent._oFooter.innerHTML = sFooter;
this._oContainer._oContent._oFooter.style.display = "block";
}
}
else {
this._oContainer._oContent._oFooter.innerHTML = "";
this._oContainer._oContent._oFooter.style.display = "none";
}
};
/**
* Sets HTML markup for the auto complete container body. This markup will be
* inserted within a <div> tag with a class of "ac_bd".
*
* @param {string} sHeader HTML markup for container body
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -