📄 autocomplete.js
字号:
/*Copyright (c) 2007, Yahoo! Inc. All rights reserved.Code licensed under the BSD License:http://developer.yahoo.net/yui/license.txtversion: 2.2.2*/ /** * The AutoComplete control provides the front-end logic for text-entry suggestion and * completion functionality. * * @module autocomplete * @requires yahoo, dom, event, datasource * @optional animation, connection * @namespace YAHOO.widget * @title AutoComplete Widget *//****************************************************************************//****************************************************************************//****************************************************************************//** * The AutoComplete class provides the customizable functionality of a plug-and-play DHTML * auto completion 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> * * @class AutoComplete * @constructor * @param elInput {HTMLElement} DOM element reference of an input field. * @param elInput {String} String ID of an input field. * @param elContainer {HTMLElement} DOM element reference of an existing DIV. * @param elContainer {String} String ID of an existing DIV. * @param oDataSource {YAHOO.widget.DataSource} DataSource instance. * @param oConfigs {Object} (optional) Object literal of configuration params. */YAHOO.widget.AutoComplete = function(elInput,elContainer,oDataSource,oConfigs) { if(elInput && elContainer && oDataSource) { // Validate DataSource if(oDataSource instanceof YAHOO.widget.DataSource) { this.dataSource = oDataSource; } else { return; } // Validate input element if(YAHOO.util.Dom.inDocument(elInput)) { if(YAHOO.lang.isString(elInput)) { this._sName = "instance" + YAHOO.widget.AutoComplete._nIndex + " " + elInput; this._oTextbox = document.getElementById(elInput); } else { this._sName = (elInput.id) ? "instance" + YAHOO.widget.AutoComplete._nIndex + " " + elInput.id: "instance" + YAHOO.widget.AutoComplete._nIndex; this._oTextbox = elInput; } } else { return; } // Validate container element if(YAHOO.util.Dom.inDocument(elContainer)) { if(YAHOO.lang.isString(elContainer)) { this._oContainer = document.getElementById(elContainer); } else { this._oContainer = elContainer; } if(this._oContainer.style.display == "none") { } } else { return; } // Set any config params passed in to override defaults if(oConfigs && (oConfigs.constructor == 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,"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); } YAHOO.util.Event.addListener(oTextbox,"keypress",oSelf._onTextboxKeyPress,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 DataSource object that encapsulates the data used for auto completion. * This object should be an inherited object from YAHOO.widget.DataSource. * * @property dataSource * @type YAHOO.widget.DataSource */YAHOO.widget.AutoComplete.prototype.dataSource = null;/** * Number of characters that must be entered before querying for results. A negative value * effectively turns off the widget. A value of 0 allows queries of null or empty string * values. * * @property minQueryLength * @type Number * @default 1 */YAHOO.widget.AutoComplete.prototype.minQueryLength = 1;/** * Maximum number of results to display in results container. * * @property maxResultsDisplayed * @type Number * @default 10 */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. * * @property queryDelay * @type Number * @default 0.5 */YAHOO.widget.AutoComplete.prototype.queryDelay = 0.5;/** * Class name of a highlighted item within results container. * * @property highlightClassName * @type String * @default "yui-ac-highlight" */YAHOO.widget.AutoComplete.prototype.highlightClassName = "yui-ac-highlight";/** * Class name of a pre-highlighted item within results container. * * @property prehighlightClassName * @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. * * @property delimChar * @type String | String[] */YAHOO.widget.AutoComplete.prototype.delimChar = null;/** * Whether or not the first item in results container should be automatically highlighted * on expand. * * @property autoHighlight * @type Boolean * @default true */YAHOO.widget.AutoComplete.prototype.autoHighlight = true;/** * Whether or not the 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. * * @property typeAhead * @type Boolean * @default false */YAHOO.widget.AutoComplete.prototype.typeAhead = false;/** * Whether or not to animate the expansion/collapse of the results container in the * horizontal direction. * * @property animHoriz * @type Boolean * @default false */YAHOO.widget.AutoComplete.prototype.animHoriz = false;/** * Whether or not to animate the expansion/collapse of the results container in the * vertical direction. * * @property animVert * @type Boolean * @default true */YAHOO.widget.AutoComplete.prototype.animVert = true;/** * Speed of container expand/collapse animation, in seconds.. * * @property animSpeed * @type Number * @default 0.3 */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 input field into a * <select> field. This feature is not recommended with delimiter character(s) * defined. * * @property forceSelection * @type Boolean * @default false */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 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. * * @property allowBrowserAutocomplete * @type Boolean * @default true */YAHOO.widget.AutoComplete.prototype.allowBrowserAutocomplete = true;/** * Whether or not the results container should always be displayed. * Enabling this feature displays the container when the widget is instantiated * and prevents the toggling of the container to a collapsed state. * * @property alwaysShowContainer * @type Boolean * @default false */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 results 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). * * @property useIFrame * @type Boolean * @default false */YAHOO.widget.AutoComplete.prototype.useIFrame = false;/** * Whether or not the results container should have a shadow. * * @property useShadow * @type Boolean * @default false */YAHOO.widget.AutoComplete.prototype.useShadow = false;///////////////////////////////////////////////////////////////////////////////// Public methods/////////////////////////////////////////////////////////////////////////////// /** * Public accessor to the unique name of the AutoComplete instance. * * @method toString * @return {String} Unique name of the AutoComplete instance. */YAHOO.widget.AutoComplete.prototype.toString = function() { return "AutoComplete " + this._sName;}; /** * Returns true if container is in an expanded state, false otherwise. * * @method isContainerOpen * @return {Boolean} Returns true if container is in an expanded state, false otherwise. */YAHOO.widget.AutoComplete.prototype.isContainerOpen = function() { return this._bContainerOpen;};/** * Public accessor to the internal array of DOM <li> elements that * display query results within the results container. * * @method getListItems * @return {HTMLElement[]} Array of <li> elements within the results container. */YAHOO.widget.AutoComplete.prototype.getListItems = function() { return this._aListItems;};/** * Public accessor to the data held in an <li> element of the * results container. * * @method getListItemData * @return {Object | Object[]} 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 results container header. This markup will be * inserted within a <div> tag with a class of "yui-ac-hd". * * @method setHeader * @param sHeader {String} HTML markup for results 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 results container footer. This markup will be * inserted within a <div> tag with a class of "yui-ac-ft". * * @method setFooter * @param sFooter {String} HTML markup for results 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"; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -