2 Copyright (c) 2009, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.net/yui/license.txt
7 /////////////////////////////////////////////////////////////////////////////
9 // YAHOO.widget.DataSource Backwards Compatibility
11 /////////////////////////////////////////////////////////////////////////////
13 YAHOO.widget.DS_JSArray = YAHOO.util.LocalDataSource;
15 YAHOO.widget.DS_JSFunction = YAHOO.util.FunctionDataSource;
17 YAHOO.widget.DS_XHR = function(sScriptURI, aSchema, oConfigs) {
18 var DS = new YAHOO.util.XHRDataSource(sScriptURI, oConfigs);
19 DS._aDeprecatedSchema = aSchema;
23 YAHOO.widget.DS_ScriptNode = function(sScriptURI, aSchema, oConfigs) {
24 var DS = new YAHOO.util.ScriptNodeDataSource(sScriptURI, oConfigs);
25 DS._aDeprecatedSchema = aSchema;
29 YAHOO.widget.DS_XHR.TYPE_JSON = YAHOO.util.DataSourceBase.TYPE_JSON;
30 YAHOO.widget.DS_XHR.TYPE_XML = YAHOO.util.DataSourceBase.TYPE_XML;
31 YAHOO.widget.DS_XHR.TYPE_FLAT = YAHOO.util.DataSourceBase.TYPE_TEXT;
33 // TODO: widget.DS_ScriptNode.scriptCallbackParam
38 * The AutoComplete control provides the front-end logic for text-entry suggestion and
39 * completion functionality.
41 * @module autocomplete
42 * @requires yahoo, dom, event, datasource
44 * @namespace YAHOO.widget
45 * @title AutoComplete Widget
48 /****************************************************************************/
49 /****************************************************************************/
50 /****************************************************************************/
53 * The AutoComplete class provides the customizable functionality of a plug-and-play DHTML
54 * auto completion widget. Some key features:
56 * <li>Navigate with up/down arrow keys and/or mouse to pick a selection</li>
57 * <li>The drop down container can "roll down" or "fly out" via configurable
59 * <li>UI look-and-feel customizable through CSS, including container
60 * attributes, borders, position, fonts, etc</li>
65 * @param elInput {HTMLElement} DOM element reference of an input field.
66 * @param elInput {String} String ID of an input field.
67 * @param elContainer {HTMLElement} DOM element reference of an existing DIV.
68 * @param elContainer {String} String ID of an existing DIV.
69 * @param oDataSource {YAHOO.widget.DataSource} DataSource instance.
70 * @param oConfigs {Object} (optional) Object literal of configuration params.
72 YAHOO.widget.AutoComplete = function(elInput,elContainer,oDataSource,oConfigs) {
73 if(elInput && elContainer && oDataSource) {
74 // Validate DataSource
75 if(oDataSource && YAHOO.lang.isFunction(oDataSource.sendRequest)) {
76 this.dataSource = oDataSource;
82 // YAHOO.widget.DataSource schema backwards compatibility
83 // Converted deprecated schema into supported schema
84 // First assume key data is held in position 0 of results array
86 var schema = oDataSource.responseSchema;
87 // An old school schema has been defined in the deprecated DataSource constructor
88 if(oDataSource._aDeprecatedSchema) {
89 var aDeprecatedSchema = oDataSource._aDeprecatedSchema;
90 if(YAHOO.lang.isArray(aDeprecatedSchema)) {
92 if((oDataSource.responseType === YAHOO.util.DataSourceBase.TYPE_JSON) ||
93 (oDataSource.responseType === YAHOO.util.DataSourceBase.TYPE_UNKNOWN)) { // Used to default to unknown
94 // Store the resultsList
95 schema.resultsList = aDeprecatedSchema[0];
97 this.key = aDeprecatedSchema[1];
98 // Only resultsList and key are defined, so grab all the data
99 schema.fields = (aDeprecatedSchema.length < 3) ? null : aDeprecatedSchema.slice(1);
101 else if(oDataSource.responseType === YAHOO.util.DataSourceBase.TYPE_XML) {
102 schema.resultNode = aDeprecatedSchema[0];
103 this.key = aDeprecatedSchema[1];
104 schema.fields = aDeprecatedSchema.slice(1);
106 else if(oDataSource.responseType === YAHOO.util.DataSourceBase.TYPE_TEXT) {
107 schema.recordDelim = aDeprecatedSchema[0];
108 schema.fieldDelim = aDeprecatedSchema[1];
110 oDataSource.responseSchema = schema;
114 // Validate input element
115 if(YAHOO.util.Dom.inDocument(elInput)) {
116 if(YAHOO.lang.isString(elInput)) {
117 this._sName = "instance" + YAHOO.widget.AutoComplete._nIndex + " " + elInput;
118 this._elTextbox = document.getElementById(elInput);
121 this._sName = (elInput.id) ?
122 "instance" + YAHOO.widget.AutoComplete._nIndex + " " + elInput.id:
123 "instance" + YAHOO.widget.AutoComplete._nIndex;
124 this._elTextbox = elInput;
126 YAHOO.util.Dom.addClass(this._elTextbox, "yui-ac-input");
132 // Validate container element
133 if(YAHOO.util.Dom.inDocument(elContainer)) {
134 if(YAHOO.lang.isString(elContainer)) {
135 this._elContainer = document.getElementById(elContainer);
138 this._elContainer = elContainer;
140 if(this._elContainer.style.display == "none") {
144 var elParent = this._elContainer.parentNode;
145 var elTag = elParent.tagName.toLowerCase();
147 YAHOO.util.Dom.addClass(elParent, "yui-ac");
156 // Default applyLocalFilter setting is to enable for local sources
157 if(this.dataSource.dataType === YAHOO.util.DataSourceBase.TYPE_LOCAL) {
158 this.applyLocalFilter = true;
161 // Set any config params passed in to override defaults
162 if(oConfigs && (oConfigs.constructor == Object)) {
163 for(var sConfig in oConfigs) {
165 this[sConfig] = oConfigs[sConfig];
170 // Initialization sequence
171 this._initContainerEl();
174 this._initContainerHelperEls();
178 var elTextbox = this._elTextbox;
181 YAHOO.util.Event.addListener(elTextbox,"keyup",oSelf._onTextboxKeyUp,oSelf);
182 YAHOO.util.Event.addListener(elTextbox,"keydown",oSelf._onTextboxKeyDown,oSelf);
183 YAHOO.util.Event.addListener(elTextbox,"focus",oSelf._onTextboxFocus,oSelf);
184 YAHOO.util.Event.addListener(elTextbox,"blur",oSelf._onTextboxBlur,oSelf);
185 YAHOO.util.Event.addListener(elContainer,"mouseover",oSelf._onContainerMouseover,oSelf);
186 YAHOO.util.Event.addListener(elContainer,"mouseout",oSelf._onContainerMouseout,oSelf);
187 YAHOO.util.Event.addListener(elContainer,"click",oSelf._onContainerClick,oSelf);
188 YAHOO.util.Event.addListener(elContainer,"scroll",oSelf._onContainerScroll,oSelf);
189 YAHOO.util.Event.addListener(elContainer,"resize",oSelf._onContainerResize,oSelf);
190 YAHOO.util.Event.addListener(elTextbox,"keypress",oSelf._onTextboxKeyPress,oSelf);
191 YAHOO.util.Event.addListener(window,"unload",oSelf._onWindowUnload,oSelf);
194 this.textboxFocusEvent = new YAHOO.util.CustomEvent("textboxFocus", this);
195 this.textboxKeyEvent = new YAHOO.util.CustomEvent("textboxKey", this);
196 this.dataRequestEvent = new YAHOO.util.CustomEvent("dataRequest", this);
197 this.dataReturnEvent = new YAHOO.util.CustomEvent("dataReturn", this);
198 this.dataErrorEvent = new YAHOO.util.CustomEvent("dataError", this);
199 this.containerPopulateEvent = new YAHOO.util.CustomEvent("containerPopulate", this);
200 this.containerExpandEvent = new YAHOO.util.CustomEvent("containerExpand", this);
201 this.typeAheadEvent = new YAHOO.util.CustomEvent("typeAhead", this);
202 this.itemMouseOverEvent = new YAHOO.util.CustomEvent("itemMouseOver", this);
203 this.itemMouseOutEvent = new YAHOO.util.CustomEvent("itemMouseOut", this);
204 this.itemArrowToEvent = new YAHOO.util.CustomEvent("itemArrowTo", this);
205 this.itemArrowFromEvent = new YAHOO.util.CustomEvent("itemArrowFrom", this);
206 this.itemSelectEvent = new YAHOO.util.CustomEvent("itemSelect", this);
207 this.unmatchedItemSelectEvent = new YAHOO.util.CustomEvent("unmatchedItemSelect", this);
208 this.selectionEnforceEvent = new YAHOO.util.CustomEvent("selectionEnforce", this);
209 this.containerCollapseEvent = new YAHOO.util.CustomEvent("containerCollapse", this);
210 this.textboxBlurEvent = new YAHOO.util.CustomEvent("textboxBlur", this);
211 this.textboxChangeEvent = new YAHOO.util.CustomEvent("textboxChange", this);
214 elTextbox.setAttribute("autocomplete","off");
215 YAHOO.widget.AutoComplete._nIndex++;
217 // Required arguments were not found
222 /////////////////////////////////////////////////////////////////////////////
224 // Public member variables
226 /////////////////////////////////////////////////////////////////////////////
229 * The DataSource object that encapsulates the data used for auto completion.
230 * This object should be an inherited object from YAHOO.widget.DataSource.
232 * @property dataSource
233 * @type YAHOO.widget.DataSource
235 YAHOO.widget.AutoComplete.prototype.dataSource = null;
238 * By default, results from local DataSources will pass through the filterResults
239 * method to apply a client-side matching algorithm.
241 * @property applyLocalFilter
243 * @default true for local arrays and json, otherwise false
245 YAHOO.widget.AutoComplete.prototype.applyLocalFilter = null;
248 * When applyLocalFilter is true, the local filtering algorthim can have case sensitivity
251 * @property queryMatchCase
255 YAHOO.widget.AutoComplete.prototype.queryMatchCase = false;
258 * When applyLocalFilter is true, results can be locally filtered to return
259 * matching strings that "contain" the query string rather than simply "start with"
262 * @property queryMatchContains
266 YAHOO.widget.AutoComplete.prototype.queryMatchContains = false;
269 * Enables query subset matching. When the DataSource's cache is enabled and queryMatchSubset is
270 * true, substrings of queries will return matching cached results. For
271 * instance, if the first query is for "abc" susequent queries that start with
272 * "abc", like "abcd", will be queried against the cache, and not the live data
273 * source. Recommended only for DataSources that return comprehensive results
274 * for queries with very few characters.
276 * @property queryMatchSubset
281 YAHOO.widget.AutoComplete.prototype.queryMatchSubset = false;
284 * Number of characters that must be entered before querying for results. A negative value
285 * effectively turns off the widget. A value of 0 allows queries of null or empty string
288 * @property minQueryLength
292 YAHOO.widget.AutoComplete.prototype.minQueryLength = 1;
295 * Maximum number of results to display in results container.
297 * @property maxResultsDisplayed
301 YAHOO.widget.AutoComplete.prototype.maxResultsDisplayed = 10;
304 * Number of seconds to delay before submitting a query request. If a query
305 * request is received before a previous one has completed its delay, the
306 * previous request is cancelled and the new request is set to the delay. If
307 * typeAhead is also enabled, this value must always be less than the typeAheadDelay
308 * in order to avoid certain race conditions.
310 * @property queryDelay
314 YAHOO.widget.AutoComplete.prototype.queryDelay = 0.2;
317 * If typeAhead is true, number of seconds to delay before updating input with
318 * typeAhead value. In order to prevent certain race conditions, this value must
319 * always be greater than the queryDelay.
321 * @property typeAheadDelay
325 YAHOO.widget.AutoComplete.prototype.typeAheadDelay = 0.5;
328 * When IME usage is detected or interval detection is explicitly enabled,
329 * AutoComplete will detect the input value at the given interval and send a
330 * query if the value has changed.
332 * @property queryInterval
336 YAHOO.widget.AutoComplete.prototype.queryInterval = 500;
339 * Class name of a highlighted item within results container.
341 * @property highlightClassName
343 * @default "yui-ac-highlight"
345 YAHOO.widget.AutoComplete.prototype.highlightClassName = "yui-ac-highlight";
348 * Class name of a pre-highlighted item within results container.
350 * @property prehighlightClassName
353 YAHOO.widget.AutoComplete.prototype.prehighlightClassName = null;
356 * Query delimiter. A single character separator for multiple delimited
357 * selections. Multiple delimiter characteres may be defined as an array of
358 * strings. A null value or empty string indicates that query results cannot
359 * be delimited. This feature is not recommended if you need forceSelection to
362 * @property delimChar
363 * @type String | String[]
365 YAHOO.widget.AutoComplete.prototype.delimChar = null;
368 * Whether or not the first item in results container should be automatically highlighted
371 * @property autoHighlight
375 YAHOO.widget.AutoComplete.prototype.autoHighlight = true;
378 * If autohighlight is enabled, whether or not the input field should be automatically updated
379 * with the first query result as the user types, auto-selecting the substring portion
380 * of the first result that the user has not yet typed.
382 * @property typeAhead
386 YAHOO.widget.AutoComplete.prototype.typeAhead = false;
389 * Whether or not to animate the expansion/collapse of the results container in the
390 * horizontal direction.
392 * @property animHoriz
396 YAHOO.widget.AutoComplete.prototype.animHoriz = false;
399 * Whether or not to animate the expansion/collapse of the results container in the
400 * vertical direction.
406 YAHOO.widget.AutoComplete.prototype.animVert = true;
409 * Speed of container expand/collapse animation, in seconds..
411 * @property animSpeed
415 YAHOO.widget.AutoComplete.prototype.animSpeed = 0.3;
418 * Whether or not to force the user's selection to match one of the query
419 * results. Enabling this feature essentially transforms the input field into a
420 * <select> field. This feature is not recommended with delimiter character(s)
423 * @property forceSelection
427 YAHOO.widget.AutoComplete.prototype.forceSelection = false;
430 * Whether or not to allow browsers to cache user-typed input in the input
431 * field. Disabling this feature will prevent the widget from setting the
432 * autocomplete="off" on the input field. When autocomplete="off"
433 * and users click the back button after form submission, user-typed input can
434 * be prefilled by the browser from its cache. This caching of user input may
435 * not be desired for sensitive data, such as credit card numbers, in which
436 * case, implementers should consider setting allowBrowserAutocomplete to false.
438 * @property allowBrowserAutocomplete
442 YAHOO.widget.AutoComplete.prototype.allowBrowserAutocomplete = true;
445 * Enabling this feature prevents the toggling of the container to a collapsed state.
446 * Setting to true does not automatically trigger the opening of the container.
447 * Implementers are advised to pre-load the container with an explicit "sendQuery()" call.
449 * @property alwaysShowContainer
453 YAHOO.widget.AutoComplete.prototype.alwaysShowContainer = false;
456 * Whether or not to use an iFrame to layer over Windows form elements in
457 * IE. Set to true only when the results container will be on top of a
458 * <select> field in IE and thus exposed to the IE z-index bug (i.e.,
461 * @property useIFrame
465 YAHOO.widget.AutoComplete.prototype.useIFrame = false;
468 * Whether or not the results container should have a shadow.
470 * @property useShadow
474 YAHOO.widget.AutoComplete.prototype.useShadow = false;
477 * Whether or not the input field should be updated with selections.
479 * @property suppressInputUpdate
483 YAHOO.widget.AutoComplete.prototype.suppressInputUpdate = false;
486 * For backward compatibility to pre-2.6.0 formatResults() signatures, setting
487 * resultsTypeList to true will take each object literal result returned by
488 * DataSource and flatten into an array.
490 * @property resultTypeList
494 YAHOO.widget.AutoComplete.prototype.resultTypeList = true;
497 * For XHR DataSources, AutoComplete will automatically insert a "?" between the server URI and
498 * the "query" param/value pair. To prevent this behavior, implementers should
499 * set this value to false. To more fully customize the query syntax, implementers
500 * should override the generateRequest() method.
502 * @property queryQuestionMark
506 YAHOO.widget.AutoComplete.prototype.queryQuestionMark = true;
509 * If true, before each time the container expands, the container element will be
510 * positioned to snap to the bottom-left corner of the input element. If
511 * autoSnapContainer is set to false, this positioning will not be done.
513 * @property autoSnapContainer
517 YAHOO.widget.AutoComplete.prototype.autoSnapContainer = true;
519 /////////////////////////////////////////////////////////////////////////////
523 /////////////////////////////////////////////////////////////////////////////
526 * Public accessor to the unique name of the AutoComplete instance.
529 * @return {String} Unique name of the AutoComplete instance.
531 YAHOO.widget.AutoComplete.prototype.toString = function() {
532 return "AutoComplete " + this._sName;
536 * Returns DOM reference to input element.
539 * @return {HTMLELement} DOM reference to input element.
541 YAHOO.widget.AutoComplete.prototype.getInputEl = function() {
542 return this._elTextbox;
546 * Returns DOM reference to container element.
548 * @method getContainerEl
549 * @return {HTMLELement} DOM reference to container element.
551 YAHOO.widget.AutoComplete.prototype.getContainerEl = function() {
552 return this._elContainer;
556 * Returns true if widget instance is currently active.
559 * @return {Boolean} Returns true if widget instance is currently active.
561 YAHOO.widget.AutoComplete.prototype.isFocused = function() {
562 return this._bFocused;
566 * Returns true if container is in an expanded state, false otherwise.
568 * @method isContainerOpen
569 * @return {Boolean} Returns true if container is in an expanded state, false otherwise.
571 YAHOO.widget.AutoComplete.prototype.isContainerOpen = function() {
572 return this._bContainerOpen;
576 * Public accessor to the <ul> element that displays query results within the results container.
579 * @return {HTMLElement[]} Reference to <ul> element within the results container.
581 YAHOO.widget.AutoComplete.prototype.getListEl = function() {
586 * Public accessor to the matching string associated with a given <li> result.
588 * @method getListItemMatch
589 * @param elListItem {HTMLElement} Reference to <LI> element.
590 * @return {String} Matching string.
592 YAHOO.widget.AutoComplete.prototype.getListItemMatch = function(elListItem) {
593 if(elListItem._sResultMatch) {
594 return elListItem._sResultMatch;
602 * Public accessor to the result data associated with a given <li> result.
604 * @method getListItemData
605 * @param elListItem {HTMLElement} Reference to <LI> element.
606 * @return {Object} Result data.
608 YAHOO.widget.AutoComplete.prototype.getListItemData = function(elListItem) {
609 if(elListItem._oResultData) {
610 return elListItem._oResultData;
618 * Public accessor to the index of the associated with a given <li> result.
620 * @method getListItemIndex
621 * @param elListItem {HTMLElement} Reference to <LI> element.
622 * @return {Number} Index.
624 YAHOO.widget.AutoComplete.prototype.getListItemIndex = function(elListItem) {
625 if(YAHOO.lang.isNumber(elListItem._nItemIndex)) {
626 return elListItem._nItemIndex;
634 * Sets HTML markup for the results container header. This markup will be
635 * inserted within a <div> tag with a class of "yui-ac-hd".
638 * @param sHeader {String} HTML markup for results container header.
640 YAHOO.widget.AutoComplete.prototype.setHeader = function(sHeader) {
642 var elHeader = this._elHeader;
644 elHeader.innerHTML = sHeader;
645 elHeader.style.display = "";
648 elHeader.innerHTML = "";
649 elHeader.style.display = "none";
655 * Sets HTML markup for the results container footer. This markup will be
656 * inserted within a <div> tag with a class of "yui-ac-ft".
659 * @param sFooter {String} HTML markup for results container footer.
661 YAHOO.widget.AutoComplete.prototype.setFooter = function(sFooter) {
663 var elFooter = this._elFooter;
665 elFooter.innerHTML = sFooter;
666 elFooter.style.display = "";
669 elFooter.innerHTML = "";
670 elFooter.style.display = "none";
676 * Sets HTML markup for the results container body. This markup will be
677 * inserted within a <div> tag with a class of "yui-ac-bd".
680 * @param sBody {String} HTML markup for results container body.
682 YAHOO.widget.AutoComplete.prototype.setBody = function(sBody) {
684 var elBody = this._elBody;
685 YAHOO.util.Event.purgeElement(elBody, true);
687 elBody.innerHTML = sBody;
688 elBody.style.display = "";
691 elBody.innerHTML = "";
692 elBody.style.display = "none";
699 * A function that converts an AutoComplete query into a request value which is then
700 * passed to the DataSource's sendRequest method in order to retrieve data for
701 * the query. By default, returns a String with the syntax: "query={query}"
702 * Implementers can customize this method for custom request syntaxes.
704 * @method generateRequest
705 * @param sQuery {String} Query string
706 * @return {MIXED} Request
708 YAHOO.widget.AutoComplete.prototype.generateRequest = function(sQuery) {
709 var dataType = this.dataSource.dataType;
711 // Transform query string in to a request for remote data
712 // By default, local data doesn't need a transformation, just passes along the query as is.
713 if(dataType === YAHOO.util.DataSourceBase.TYPE_XHR) {
714 // By default, XHR GET requests look like "{scriptURI}?{scriptQueryParam}={sQuery}&{scriptQueryAppend}"
715 if(!this.dataSource.connMethodPost) {
716 sQuery = (this.queryQuestionMark ? "?" : "") + (this.dataSource.scriptQueryParam || "query") + "=" + sQuery +
717 (this.dataSource.scriptQueryAppend ? ("&" + this.dataSource.scriptQueryAppend) : "");
719 // By default, XHR POST bodies are sent to the {scriptURI} like "{scriptQueryParam}={sQuery}&{scriptQueryAppend}"
721 sQuery = (this.dataSource.scriptQueryParam || "query") + "=" + sQuery +
722 (this.dataSource.scriptQueryAppend ? ("&" + this.dataSource.scriptQueryAppend) : "");
725 // By default, remote script node requests look like "{scriptURI}&{scriptCallbackParam}={callbackString}&{scriptQueryParam}={sQuery}&{scriptQueryAppend}"
726 else if(dataType === YAHOO.util.DataSourceBase.TYPE_SCRIPTNODE) {
727 sQuery = "&" + (this.dataSource.scriptQueryParam || "query") + "=" + sQuery +
728 (this.dataSource.scriptQueryAppend ? ("&" + this.dataSource.scriptQueryAppend) : "");
735 * Makes query request to the DataSource.
738 * @param sQuery {String} Query string.
740 YAHOO.widget.AutoComplete.prototype.sendQuery = function(sQuery) {
741 // Activate focus for a new interaction
742 this._bFocused = true;
744 // Adjust programatically sent queries to look like they were input by user
745 // when delimiters are enabled
746 var newQuery = (this.delimChar) ? this._elTextbox.value + sQuery : sQuery;
747 this._sendQuery(newQuery);
751 * Snaps container to bottom-left corner of input element
753 * @method snapContainer
755 YAHOO.widget.AutoComplete.prototype.snapContainer = function() {
756 var oTextbox = this._elTextbox,
757 pos = YAHOO.util.Dom.getXY(oTextbox);
758 pos[1] += YAHOO.util.Dom.get(oTextbox).offsetHeight + 2;
759 YAHOO.util.Dom.setXY(this._elContainer,pos);
765 * @method expandContainer
767 YAHOO.widget.AutoComplete.prototype.expandContainer = function() {
768 this._toggleContainer(true);
772 * Collapses container.
774 * @method collapseContainer
776 YAHOO.widget.AutoComplete.prototype.collapseContainer = function() {
777 this._toggleContainer(false);
781 * Clears entire list of suggestions.
785 YAHOO.widget.AutoComplete.prototype.clearList = function() {
786 var allItems = this._elList.childNodes,
789 allItems[i].style.display = "none";
794 * Handles subset matching for when queryMatchSubset is enabled.
796 * @method getSubsetMatches
797 * @param sQuery {String} Query string.
798 * @return {Object} oParsedResponse or null.
800 YAHOO.widget.AutoComplete.prototype.getSubsetMatches = function(sQuery) {
801 var subQuery, oCachedResponse, subRequest;
802 // Loop through substrings of each cached element's query property...
803 for(var i = sQuery.length; i >= this.minQueryLength ; i--) {
804 subRequest = this.generateRequest(sQuery.substr(0,i));
805 this.dataRequestEvent.fire(this, subQuery, subRequest);
807 // If a substring of the query is found in the cache
808 oCachedResponse = this.dataSource.getCachedResponse(subRequest);
809 if(oCachedResponse) {
810 return this.filterResults.apply(this.dataSource, [sQuery, oCachedResponse, oCachedResponse, {scope:this}]);
817 * Executed by DataSource (within DataSource scope via doBeforeParseData()) to
818 * handle responseStripAfter cleanup.
820 * @method preparseRawResponse
821 * @param sQuery {String} Query string.
822 * @return {Object} oParsedResponse or null.
824 YAHOO.widget.AutoComplete.prototype.preparseRawResponse = function(oRequest, oFullResponse, oCallback) {
825 var nEnd = ((this.responseStripAfter !== "") && (oFullResponse.indexOf)) ?
826 oFullResponse.indexOf(this.responseStripAfter) : -1;
828 oFullResponse = oFullResponse.substring(0,nEnd);
830 return oFullResponse;
834 * Executed by DataSource (within DataSource scope via doBeforeCallback()) to
835 * filter results through a simple client-side matching algorithm.
837 * @method filterResults
838 * @param sQuery {String} Original request.
839 * @param oFullResponse {Object} Full response object.
840 * @param oParsedResponse {Object} Parsed response object.
841 * @param oCallback {Object} Callback object.
842 * @return {Object} Filtered response object.
845 YAHOO.widget.AutoComplete.prototype.filterResults = function(sQuery, oFullResponse, oParsedResponse, oCallback) {
846 // If AC has passed a query string value back to itself, grab it
847 if(oCallback && oCallback.argument && oCallback.argument.query) {
848 sQuery = oCallback.argument.query;
851 // Only if a query string is available to match against
852 if(sQuery && sQuery !== "") {
853 // First make a copy of the oParseResponse
854 oParsedResponse = YAHOO.widget.AutoComplete._cloneObject(oParsedResponse);
856 var oAC = oCallback.scope,
858 allResults = oParsedResponse.results, // the array of results
859 filteredResults = [], // container for filtered results,
860 nMax = oAC.maxResultsDisplayed, // max to find
861 bMatchCase = (oDS.queryMatchCase || oAC.queryMatchCase), // backward compat
862 bMatchContains = (oDS.queryMatchContains || oAC.queryMatchContains); // backward compat
864 // Loop through each result object...
865 for(var i=0, len=allResults.length; i<len; i++) {
866 var oResult = allResults[i];
868 // Grab the data to match against from the result object...
871 // Result object is a simple string already
872 if(YAHOO.lang.isString(oResult)) {
875 // Result object is an array of strings
876 else if(YAHOO.lang.isArray(oResult)) {
877 sResult = oResult[0];
880 // Result object is an object literal of strings
881 else if(this.responseSchema.fields) {
882 var key = this.responseSchema.fields[0].key || this.responseSchema.fields[0];
883 sResult = oResult[key];
885 // Backwards compatibility
887 sResult = oResult[this.key];
890 if(YAHOO.lang.isString(sResult)) {
892 var sKeyIndex = (bMatchCase) ?
893 sResult.indexOf(decodeURIComponent(sQuery)) :
894 sResult.toLowerCase().indexOf(decodeURIComponent(sQuery).toLowerCase());
896 // A STARTSWITH match is when the query is found at the beginning of the key string...
897 if((!bMatchContains && (sKeyIndex === 0)) ||
898 // A CONTAINS match is when the query is found anywhere within the key string...
899 (bMatchContains && (sKeyIndex > -1))) {
901 filteredResults.push(oResult);
905 // Filter no more if maxResultsDisplayed is reached
906 if(len>nMax && filteredResults.length===nMax) {
910 oParsedResponse.results = filteredResults;
915 return oParsedResponse;
919 * Handles response for display. This is the callback function method passed to
920 * YAHOO.util.DataSourceBase#sendRequest so results from the DataSource are
921 * returned to the AutoComplete instance.
923 * @method handleResponse
924 * @param sQuery {String} Original request.
925 * @param oResponse {Object} Response object.
926 * @param oPayload {MIXED} (optional) Additional argument(s)
928 YAHOO.widget.AutoComplete.prototype.handleResponse = function(sQuery, oResponse, oPayload) {
929 if((this instanceof YAHOO.widget.AutoComplete) && this._sName) {
930 this._populateList(sQuery, oResponse, oPayload);
935 * Overridable method called before container is loaded with result data.
937 * @method doBeforeLoadData
938 * @param sQuery {String} Original request.
939 * @param oResponse {Object} Response object.
940 * @param oPayload {MIXED} (optional) Additional argument(s)
941 * @return {Boolean} Return true to continue loading data, false to cancel.
943 YAHOO.widget.AutoComplete.prototype.doBeforeLoadData = function(sQuery, oResponse, oPayload) {
948 * Overridable method that returns HTML markup for one result to be populated
949 * as innerHTML of an <LI> element.
951 * @method formatResult
952 * @param oResultData {Object} Result data object.
953 * @param sQuery {String} The corresponding query string.
954 * @param sResultMatch {HTMLElement} The current query string.
955 * @return {String} HTML markup of formatted result data.
957 YAHOO.widget.AutoComplete.prototype.formatResult = function(oResultData, sQuery, sResultMatch) {
958 var sMarkup = (sResultMatch) ? sResultMatch : "";
963 * Overridable method called before container expands allows implementers to access data
966 * @method doBeforeExpandContainer
967 * @param elTextbox {HTMLElement} The text input box.
968 * @param elContainer {HTMLElement} The container element.
969 * @param sQuery {String} The query string.
970 * @param aResults {Object[]} An array of query results.
971 * @return {Boolean} Return true to continue expanding container, false to cancel the expand.
973 YAHOO.widget.AutoComplete.prototype.doBeforeExpandContainer = function(elTextbox, elContainer, sQuery, aResults) {
979 * Nulls out the entire AutoComplete instance and related objects, removes attached
980 * event listeners, and clears out DOM elements inside the container. After
981 * calling this method, the instance reference should be expliclitly nulled by
982 * implementer, as in myAutoComplete = null. Use with caution!
986 YAHOO.widget.AutoComplete.prototype.destroy = function() {
987 var instanceName = this.toString();
988 var elInput = this._elTextbox;
989 var elContainer = this._elContainer;
991 // Unhook custom events
992 this.textboxFocusEvent.unsubscribeAll();
993 this.textboxKeyEvent.unsubscribeAll();
994 this.dataRequestEvent.unsubscribeAll();
995 this.dataReturnEvent.unsubscribeAll();
996 this.dataErrorEvent.unsubscribeAll();
997 this.containerPopulateEvent.unsubscribeAll();
998 this.containerExpandEvent.unsubscribeAll();
999 this.typeAheadEvent.unsubscribeAll();
1000 this.itemMouseOverEvent.unsubscribeAll();
1001 this.itemMouseOutEvent.unsubscribeAll();
1002 this.itemArrowToEvent.unsubscribeAll();
1003 this.itemArrowFromEvent.unsubscribeAll();
1004 this.itemSelectEvent.unsubscribeAll();
1005 this.unmatchedItemSelectEvent.unsubscribeAll();
1006 this.selectionEnforceEvent.unsubscribeAll();
1007 this.containerCollapseEvent.unsubscribeAll();
1008 this.textboxBlurEvent.unsubscribeAll();
1009 this.textboxChangeEvent.unsubscribeAll();
1011 // Unhook DOM events
1012 YAHOO.util.Event.purgeElement(elInput, true);
1013 YAHOO.util.Event.purgeElement(elContainer, true);
1015 // Remove DOM elements
1016 elContainer.innerHTML = "";
1019 for(var key in this) {
1020 if(YAHOO.lang.hasOwnProperty(this, key)) {
1027 /////////////////////////////////////////////////////////////////////////////
1031 /////////////////////////////////////////////////////////////////////////////
1034 * Fired when the input field receives focus.
1036 * @event textboxFocusEvent
1037 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
1039 YAHOO.widget.AutoComplete.prototype.textboxFocusEvent = null;
1042 * Fired when the input field receives key input.
1044 * @event textboxKeyEvent
1045 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
1046 * @param nKeycode {Number} The keycode number.
1048 YAHOO.widget.AutoComplete.prototype.textboxKeyEvent = null;
1051 * Fired when the AutoComplete instance makes a request to the DataSource.
1053 * @event dataRequestEvent
1054 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
1055 * @param sQuery {String} The query string.
1056 * @param oRequest {Object} The request.
1058 YAHOO.widget.AutoComplete.prototype.dataRequestEvent = null;
1061 * Fired when the AutoComplete instance receives query results from the data
1064 * @event dataReturnEvent
1065 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
1066 * @param sQuery {String} The query string.
1067 * @param aResults {Object[]} Results array.
1069 YAHOO.widget.AutoComplete.prototype.dataReturnEvent = null;
1072 * Fired when the AutoComplete instance does not receive query results from the
1073 * DataSource due to an error.
1075 * @event dataErrorEvent
1076 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
1077 * @param sQuery {String} The query string.
1078 * @param oResponse {Object} The response object, if available.
1080 YAHOO.widget.AutoComplete.prototype.dataErrorEvent = null;
1083 * Fired when the results container is populated.
1085 * @event containerPopulateEvent
1086 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
1088 YAHOO.widget.AutoComplete.prototype.containerPopulateEvent = null;
1091 * Fired when the results container is expanded.
1093 * @event containerExpandEvent
1094 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
1096 YAHOO.widget.AutoComplete.prototype.containerExpandEvent = null;
1099 * Fired when the input field has been prefilled by the type-ahead
1102 * @event typeAheadEvent
1103 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
1104 * @param sQuery {String} The query string.
1105 * @param sPrefill {String} The prefill string.
1107 YAHOO.widget.AutoComplete.prototype.typeAheadEvent = null;
1110 * Fired when result item has been moused over.
1112 * @event itemMouseOverEvent
1113 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
1114 * @param elItem {HTMLElement} The <li> element item moused to.
1116 YAHOO.widget.AutoComplete.prototype.itemMouseOverEvent = null;
1119 * Fired when result item has been moused out.
1121 * @event itemMouseOutEvent
1122 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
1123 * @param elItem {HTMLElement} The <li> element item moused from.
1125 YAHOO.widget.AutoComplete.prototype.itemMouseOutEvent = null;
1128 * Fired when result item has been arrowed to.
1130 * @event itemArrowToEvent
1131 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
1132 * @param elItem {HTMLElement} The <li> element item arrowed to.
1134 YAHOO.widget.AutoComplete.prototype.itemArrowToEvent = null;
1137 * Fired when result item has been arrowed away from.
1139 * @event itemArrowFromEvent
1140 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
1141 * @param elItem {HTMLElement} The <li> element item arrowed from.
1143 YAHOO.widget.AutoComplete.prototype.itemArrowFromEvent = null;
1146 * Fired when an item is selected via mouse click, ENTER key, or TAB key.
1148 * @event itemSelectEvent
1149 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
1150 * @param elItem {HTMLElement} The selected <li> element item.
1151 * @param oData {Object} The data returned for the item, either as an object,
1152 * or mapped from the schema into an array.
1154 YAHOO.widget.AutoComplete.prototype.itemSelectEvent = null;
1157 * Fired when a user selection does not match any of the displayed result items.
1159 * @event unmatchedItemSelectEvent
1160 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
1161 * @param sSelection {String} The selected string.
1163 YAHOO.widget.AutoComplete.prototype.unmatchedItemSelectEvent = null;
1166 * Fired if forceSelection is enabled and the user's input has been cleared
1167 * because it did not match one of the returned query results.
1169 * @event selectionEnforceEvent
1170 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
1171 * @param sClearedValue {String} The cleared value (including delimiters if applicable).
1173 YAHOO.widget.AutoComplete.prototype.selectionEnforceEvent = null;
1176 * Fired when the results container is collapsed.
1178 * @event containerCollapseEvent
1179 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
1181 YAHOO.widget.AutoComplete.prototype.containerCollapseEvent = null;
1184 * Fired when the input field loses focus.
1186 * @event textboxBlurEvent
1187 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
1189 YAHOO.widget.AutoComplete.prototype.textboxBlurEvent = null;
1192 * Fired when the input field value has changed when it loses focus.
1194 * @event textboxChangeEvent
1195 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
1197 YAHOO.widget.AutoComplete.prototype.textboxChangeEvent = null;
1199 /////////////////////////////////////////////////////////////////////////////
1201 // Private member variables
1203 /////////////////////////////////////////////////////////////////////////////
1206 * Internal class variable to index multiple AutoComplete instances.
1213 YAHOO.widget.AutoComplete._nIndex = 0;
1216 * Name of AutoComplete instance.
1222 YAHOO.widget.AutoComplete.prototype._sName = null;
1225 * Text input field DOM element.
1227 * @property _elTextbox
1231 YAHOO.widget.AutoComplete.prototype._elTextbox = null;
1234 * Container DOM element.
1236 * @property _elContainer
1240 YAHOO.widget.AutoComplete.prototype._elContainer = null;
1243 * Reference to content element within container element.
1245 * @property _elContent
1249 YAHOO.widget.AutoComplete.prototype._elContent = null;
1252 * Reference to header element within content element.
1254 * @property _elHeader
1258 YAHOO.widget.AutoComplete.prototype._elHeader = null;
1261 * Reference to body element within content element.
1267 YAHOO.widget.AutoComplete.prototype._elBody = null;
1270 * Reference to footer element within content element.
1272 * @property _elFooter
1276 YAHOO.widget.AutoComplete.prototype._elFooter = null;
1279 * Reference to shadow element within container element.
1281 * @property _elShadow
1285 YAHOO.widget.AutoComplete.prototype._elShadow = null;
1288 * Reference to iframe element within container element.
1290 * @property _elIFrame
1294 YAHOO.widget.AutoComplete.prototype._elIFrame = null;
1297 * Whether or not the widget instance is currently active. If query results come back
1298 * but the user has already moved on, do not proceed with auto complete behavior.
1300 * @property _bFocused
1304 YAHOO.widget.AutoComplete.prototype._bFocused = false;
1307 * Animation instance for container expand/collapse.
1313 YAHOO.widget.AutoComplete.prototype._oAnim = null;
1316 * Whether or not the results container is currently open.
1318 * @property _bContainerOpen
1322 YAHOO.widget.AutoComplete.prototype._bContainerOpen = false;
1325 * Whether or not the mouse is currently over the results
1326 * container. This is necessary in order to prevent clicks on container items
1327 * from being text input field blur events.
1329 * @property _bOverContainer
1333 YAHOO.widget.AutoComplete.prototype._bOverContainer = false;
1336 * Internal reference to <ul> elements that contains query results within the
1337 * results container.
1343 YAHOO.widget.AutoComplete.prototype._elList = null;
1346 * Array of <li> elements references that contain query results within the
1347 * results container.
1349 * @property _aListItemEls
1350 * @type HTMLElement[]
1353 //YAHOO.widget.AutoComplete.prototype._aListItemEls = null;
1356 * Number of <li> elements currently displayed in results container.
1358 * @property _nDisplayedItems
1362 YAHOO.widget.AutoComplete.prototype._nDisplayedItems = 0;
1365 * Internal count of <li> elements displayed and hidden in results container.
1367 * @property _maxResultsDisplayed
1371 //YAHOO.widget.AutoComplete.prototype._maxResultsDisplayed = 0;
1374 * Current query string
1376 * @property _sCurQuery
1380 YAHOO.widget.AutoComplete.prototype._sCurQuery = null;
1383 * Selections from previous queries (for saving delimited queries).
1385 * @property _sPastSelections
1390 YAHOO.widget.AutoComplete.prototype._sPastSelections = "";
1393 * Stores initial input value used to determine if textboxChangeEvent should be fired.
1395 * @property _sInitInputValue
1399 YAHOO.widget.AutoComplete.prototype._sInitInputValue = null;
1402 * Pointer to the currently highlighted <li> element in the container.
1404 * @property _elCurListItem
1408 YAHOO.widget.AutoComplete.prototype._elCurListItem = null;
1411 * Pointer to the currently pre-highlighted <li> element in the container.
1413 * @property _elCurPrehighlightItem
1417 YAHOO.widget.AutoComplete.prototype._elCurPrehighlightItem = null;
1420 * Whether or not an item has been selected since the container was populated
1421 * with results. Reset to false by _populateList, and set to true when item is
1424 * @property _bItemSelected
1428 YAHOO.widget.AutoComplete.prototype._bItemSelected = false;
1431 * Key code of the last key pressed in textbox.
1433 * @property _nKeyCode
1437 YAHOO.widget.AutoComplete.prototype._nKeyCode = null;
1442 * @property _nDelayID
1446 YAHOO.widget.AutoComplete.prototype._nDelayID = -1;
1449 * TypeAhead delay timeout ID.
1451 * @property _nTypeAheadDelayID
1455 YAHOO.widget.AutoComplete.prototype._nTypeAheadDelayID = -1;
1458 * Src to iFrame used when useIFrame = true. Supports implementations over SSL
1461 * @property _iFrameSrc
1465 YAHOO.widget.AutoComplete.prototype._iFrameSrc = "javascript:false;";
1468 * For users typing via certain IMEs, queries must be triggered by intervals,
1469 * since key events yet supported across all browsers for all IMEs.
1471 * @property _queryInterval
1475 YAHOO.widget.AutoComplete.prototype._queryInterval = null;
1478 * Internal tracker to last known textbox value, used to determine whether or not
1479 * to trigger a query via interval for certain IME users.
1481 * @event _sLastTextboxValue
1485 YAHOO.widget.AutoComplete.prototype._sLastTextboxValue = null;
1487 /////////////////////////////////////////////////////////////////////////////
1491 /////////////////////////////////////////////////////////////////////////////
1494 * Updates and validates latest public config properties.
1496 * @method __initProps
1499 YAHOO.widget.AutoComplete.prototype._initProps = function() {
1500 // Correct any invalid values
1501 var minQueryLength = this.minQueryLength;
1502 if(!YAHOO.lang.isNumber(minQueryLength)) {
1503 this.minQueryLength = 1;
1505 var maxResultsDisplayed = this.maxResultsDisplayed;
1506 if(!YAHOO.lang.isNumber(maxResultsDisplayed) || (maxResultsDisplayed < 1)) {
1507 this.maxResultsDisplayed = 10;
1509 var queryDelay = this.queryDelay;
1510 if(!YAHOO.lang.isNumber(queryDelay) || (queryDelay < 0)) {
1511 this.queryDelay = 0.2;
1513 var typeAheadDelay = this.typeAheadDelay;
1514 if(!YAHOO.lang.isNumber(typeAheadDelay) || (typeAheadDelay < 0)) {
1515 this.typeAheadDelay = 0.2;
1517 var delimChar = this.delimChar;
1518 if(YAHOO.lang.isString(delimChar) && (delimChar.length > 0)) {
1519 this.delimChar = [delimChar];
1521 else if(!YAHOO.lang.isArray(delimChar)) {
1522 this.delimChar = null;
1524 var animSpeed = this.animSpeed;
1525 if((this.animHoriz || this.animVert) && YAHOO.util.Anim) {
1526 if(!YAHOO.lang.isNumber(animSpeed) || (animSpeed < 0)) {
1527 this.animSpeed = 0.3;
1530 this._oAnim = new YAHOO.util.Anim(this._elContent, {}, this.animSpeed);
1533 this._oAnim.duration = this.animSpeed;
1536 if(this.forceSelection && delimChar) {
1541 * Initializes the results container helpers if they are enabled and do
1544 * @method _initContainerHelperEls
1547 YAHOO.widget.AutoComplete.prototype._initContainerHelperEls = function() {
1548 if(this.useShadow && !this._elShadow) {
1549 var elShadow = document.createElement("div");
1550 elShadow.className = "yui-ac-shadow";
1551 elShadow.style.width = 0;
1552 elShadow.style.height = 0;
1553 this._elShadow = this._elContainer.appendChild(elShadow);
1555 if(this.useIFrame && !this._elIFrame) {
1556 var elIFrame = document.createElement("iframe");
1557 elIFrame.src = this._iFrameSrc;
1558 elIFrame.frameBorder = 0;
1559 elIFrame.scrolling = "no";
1560 elIFrame.style.position = "absolute";
1561 elIFrame.style.width = 0;
1562 elIFrame.style.height = 0;
1563 elIFrame.style.padding = 0;
1564 elIFrame.tabIndex = -1;
1565 elIFrame.role = "presentation";
1566 elIFrame.title = "Presentational iframe shim";
1567 this._elIFrame = this._elContainer.appendChild(elIFrame);
1572 * Initializes the results container once at object creation
1574 * @method _initContainerEl
1577 YAHOO.widget.AutoComplete.prototype._initContainerEl = function() {
1578 YAHOO.util.Dom.addClass(this._elContainer, "yui-ac-container");
1580 if(!this._elContent) {
1581 // The elContent div is assigned DOM listeners and
1582 // helps size the iframe and shadow properly
1583 var elContent = document.createElement("div");
1584 elContent.className = "yui-ac-content";
1585 elContent.style.display = "none";
1587 this._elContent = this._elContainer.appendChild(elContent);
1589 var elHeader = document.createElement("div");
1590 elHeader.className = "yui-ac-hd";
1591 elHeader.style.display = "none";
1592 this._elHeader = this._elContent.appendChild(elHeader);
1594 var elBody = document.createElement("div");
1595 elBody.className = "yui-ac-bd";
1596 this._elBody = this._elContent.appendChild(elBody);
1598 var elFooter = document.createElement("div");
1599 elFooter.className = "yui-ac-ft";
1600 elFooter.style.display = "none";
1601 this._elFooter = this._elContent.appendChild(elFooter);
1608 * Clears out contents of container body and creates up to
1609 * YAHOO.widget.AutoComplete#maxResultsDisplayed <li> elements in an
1610 * <ul> element.
1612 * @method _initListEl
1615 YAHOO.widget.AutoComplete.prototype._initListEl = function() {
1616 var nListLength = this.maxResultsDisplayed,
1617 elList = this._elList || document.createElement("ul"),
1620 while(elList.childNodes.length < nListLength) {
1621 elListItem = document.createElement("li");
1622 elListItem.style.display = "none";
1623 elListItem._nItemIndex = elList.childNodes.length;
1624 elList.appendChild(elListItem);
1627 var elBody = this._elBody;
1628 YAHOO.util.Event.purgeElement(elBody, true);
1629 elBody.innerHTML = "";
1630 this._elList = elBody.appendChild(elList);
1633 this._elBody.style.display = "";
1637 * Focuses input field.
1642 YAHOO.widget.AutoComplete.prototype._focus = function() {
1643 // http://developer.mozilla.org/en/docs/index.php?title=Key-navigable_custom_DHTML_widgets
1645 setTimeout(function() {
1647 oSelf._elTextbox.focus();
1655 * Enables interval detection for IME support.
1657 * @method _enableIntervalDetection
1660 YAHOO.widget.AutoComplete.prototype._enableIntervalDetection = function() {
1662 if(!oSelf._queryInterval && oSelf.queryInterval) {
1663 oSelf._queryInterval = setInterval(function() { oSelf._onInterval(); }, oSelf.queryInterval);
1668 * Enables interval detection for a less performant but brute force mechanism to
1669 * detect input values at an interval set by queryInterval and send queries if
1670 * input value has changed. Needed to support right-click+paste or shift+insert
1671 * edge cases. Please note that intervals are cleared at the end of each interaction,
1672 * so enableIntervalDetection must be called for each new interaction. The
1673 * recommended approach is to call it in response to textboxFocusEvent.
1675 * @method enableIntervalDetection
1677 YAHOO.widget.AutoComplete.prototype.enableIntervalDetection =
1678 YAHOO.widget.AutoComplete.prototype._enableIntervalDetection;
1681 * Enables query triggers based on text input detection by intervals (rather
1682 * than by key events).
1684 * @method _onInterval
1687 YAHOO.widget.AutoComplete.prototype._onInterval = function() {
1688 var currValue = this._elTextbox.value;
1689 var lastValue = this._sLastTextboxValue;
1690 if(currValue != lastValue) {
1691 this._sLastTextboxValue = currValue;
1692 this._sendQuery(currValue);
1697 * Cancels text input detection by intervals.
1699 * @method _clearInterval
1700 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
1703 YAHOO.widget.AutoComplete.prototype._clearInterval = function() {
1704 if(this._queryInterval) {
1705 clearInterval(this._queryInterval);
1706 this._queryInterval = null;
1711 * Whether or not key is functional or should be ignored. Note that the right
1712 * arrow key is NOT an ignored key since it triggers queries for certain intl
1715 * @method _isIgnoreKey
1716 * @param nKeycode {Number} Code of key pressed.
1717 * @return {Boolean} True if key should be ignored, false otherwise.
1720 YAHOO.widget.AutoComplete.prototype._isIgnoreKey = function(nKeyCode) {
1721 if((nKeyCode == 9) || (nKeyCode == 13) || // tab, enter
1722 (nKeyCode == 16) || (nKeyCode == 17) || // shift, ctl
1723 (nKeyCode >= 18 && nKeyCode <= 20) || // alt, pause/break,caps lock
1724 (nKeyCode == 27) || // esc
1725 (nKeyCode >= 33 && nKeyCode <= 35) || // page up,page down,end
1726 /*(nKeyCode >= 36 && nKeyCode <= 38) || // home,left,up
1727 (nKeyCode == 40) || // down*/
1728 (nKeyCode >= 36 && nKeyCode <= 40) || // home,left,up, right, down
1729 (nKeyCode >= 44 && nKeyCode <= 45) || // print screen,insert
1730 (nKeyCode == 229) // Bug 2041973: Korean XP fires 2 keyup events, the key and 229
1738 * Makes query request to the DataSource.
1740 * @method _sendQuery
1741 * @param sQuery {String} Query string.
1744 YAHOO.widget.AutoComplete.prototype._sendQuery = function(sQuery) {
1745 // Widget has been effectively turned off
1746 if(this.minQueryLength < 0) {
1747 this._toggleContainer(false);
1750 // Delimiter has been enabled
1751 if(this.delimChar) {
1752 var extraction = this._extractQuery(sQuery);
1753 // Here is the query itself
1754 sQuery = extraction.query;
1755 // ...and save the rest of the string for later
1756 this._sPastSelections = extraction.previous;
1759 // Don't search queries that are too short
1760 if((sQuery && (sQuery.length < this.minQueryLength)) || (!sQuery && this.minQueryLength > 0)) {
1761 if(this._nDelayID != -1) {
1762 clearTimeout(this._nDelayID);
1764 this._toggleContainer(false);
1768 sQuery = encodeURIComponent(sQuery);
1769 this._nDelayID = -1; // Reset timeout ID because request is being made
1772 if(this.dataSource.queryMatchSubset || this.queryMatchSubset) { // backward compat
1773 var oResponse = this.getSubsetMatches(sQuery);
1775 this.handleResponse(sQuery, oResponse, {query: sQuery});
1780 if(this.dataSource.responseStripAfter) {
1781 this.dataSource.doBeforeParseData = this.preparseRawResponse;
1783 if(this.applyLocalFilter) {
1784 this.dataSource.doBeforeCallback = this.filterResults;
1787 var sRequest = this.generateRequest(sQuery);
1788 this.dataRequestEvent.fire(this, sQuery, sRequest);
1790 this.dataSource.sendRequest(sRequest, {
1791 success : this.handleResponse,
1792 failure : this.handleResponse,
1801 * Populates the given <li> element with return value from formatResult().
1803 * @method _populateListItem
1804 * @param elListItem {HTMLElement} The LI element.
1805 * @param oResult {Object} The result object.
1806 * @param sCurQuery {String} The query string.
1809 YAHOO.widget.AutoComplete.prototype._populateListItem = function(elListItem, oResult, sQuery) {
1810 elListItem.innerHTML = this.formatResult(oResult, sQuery, elListItem._sResultMatch);
1814 * Populates the array of <li> elements in the container with query
1817 * @method _populateList
1818 * @param sQuery {String} Original request.
1819 * @param oResponse {Object} Response object.
1820 * @param oPayload {MIXED} (optional) Additional argument(s)
1823 YAHOO.widget.AutoComplete.prototype._populateList = function(sQuery, oResponse, oPayload) {
1824 // Clear previous timeout
1825 if(this._nTypeAheadDelayID != -1) {
1826 clearTimeout(this._nTypeAheadDelayID);
1829 sQuery = (oPayload && oPayload.query) ? oPayload.query : sQuery;
1831 // Pass data through abstract method for any transformations
1832 var ok = this.doBeforeLoadData(sQuery, oResponse, oPayload);
1835 if(ok && !oResponse.error) {
1836 this.dataReturnEvent.fire(this, sQuery, oResponse.results);
1838 // Continue only if instance is still active (i.e., user hasn't already moved on)
1839 if(this._bFocused) {
1840 // Store state for this interaction
1841 var sCurQuery = decodeURIComponent(sQuery);
1842 this._sCurQuery = sCurQuery;
1843 this._bItemSelected = false;
1845 var allResults = oResponse.results,
1846 nItemsToShow = Math.min(allResults.length,this.maxResultsDisplayed),
1847 sMatchKey = (this.dataSource.responseSchema.fields) ?
1848 (this.dataSource.responseSchema.fields[0].key || this.dataSource.responseSchema.fields[0]) : 0;
1850 if(nItemsToShow > 0) {
1851 // Make sure container and helpers are ready to go
1852 if(!this._elList || (this._elList.childNodes.length < nItemsToShow)) {
1855 this._initContainerHelperEls();
1857 var allListItemEls = this._elList.childNodes;
1858 // Fill items with data from the bottom up
1859 for(var i = nItemsToShow-1; i >= 0; i--) {
1860 var elListItem = allListItemEls[i],
1861 oResult = allResults[i];
1863 // Backward compatibility
1864 if(this.resultTypeList) {
1865 // Results need to be converted back to an array
1867 // Match key is first
1868 aResult[0] = (YAHOO.lang.isString(oResult)) ? oResult : oResult[sMatchKey] || oResult[this.key];
1869 // Add additional data to the result array
1870 var fields = this.dataSource.responseSchema.fields;
1871 if(YAHOO.lang.isArray(fields) && (fields.length > 1)) {
1872 for(var k=1, len=fields.length; k<len; k++) {
1873 aResult[aResult.length] = oResult[fields[k].key || fields[k]];
1876 // No specific fields defined, so pass along entire data object
1879 if(YAHOO.lang.isArray(oResult)) {
1883 else if(YAHOO.lang.isString(oResult)) {
1884 aResult = [oResult];
1888 aResult[1] = oResult;
1894 // The matching value, including backward compatibility for array format and safety net
1895 elListItem._sResultMatch = (YAHOO.lang.isString(oResult)) ? oResult : (YAHOO.lang.isArray(oResult)) ? oResult[0] : (oResult[sMatchKey] || "");
1896 elListItem._oResultData = oResult; // Additional data
1897 this._populateListItem(elListItem, oResult, sCurQuery);
1898 elListItem.style.display = "";
1901 // Clear out extraneous items
1902 if(nItemsToShow < allListItemEls.length) {
1904 for(var j = allListItemEls.length-1; j >= nItemsToShow; j--) {
1905 extraListItem = allListItemEls[j];
1906 extraListItem.style.display = "none";
1910 this._nDisplayedItems = nItemsToShow;
1912 this.containerPopulateEvent.fire(this, sQuery, allResults);
1914 // Highlight the first item
1915 if(this.autoHighlight) {
1916 var elFirstListItem = this._elList.firstChild;
1917 this._toggleHighlight(elFirstListItem,"to");
1918 this.itemArrowToEvent.fire(this, elFirstListItem);
1919 this._typeAhead(elFirstListItem,sQuery);
1921 // Unhighlight any previous time
1923 this._toggleHighlight(this._elCurListItem,"from");
1926 // Pre-expansion stuff
1927 ok = this._doBeforeExpandContainer(this._elTextbox, this._elContainer, sQuery, allResults);
1929 // Expand the container
1930 this._toggleContainer(ok);
1933 this._toggleContainer(false);
1941 this.dataErrorEvent.fire(this, sQuery, oResponse);
1947 * Called before container expands, by default snaps container to the
1948 * bottom-left corner of the input element, then calls public overrideable method.
1950 * @method _doBeforeExpandContainer
1951 * @param elTextbox {HTMLElement} The text input box.
1952 * @param elContainer {HTMLElement} The container element.
1953 * @param sQuery {String} The query string.
1954 * @param aResults {Object[]} An array of query results.
1955 * @return {Boolean} Return true to continue expanding container, false to cancel the expand.
1958 YAHOO.widget.AutoComplete.prototype._doBeforeExpandContainer = function(elTextbox, elContainer, sQuery, aResults) {
1959 if(this.autoSnapContainer) {
1960 this.snapContainer();
1963 return this.doBeforeExpandContainer(elTextbox, elContainer, sQuery, aResults);
1967 * When forceSelection is true and the user attempts
1968 * leave the text input box without selecting an item from the query results,
1969 * the user selection is cleared.
1971 * @method _clearSelection
1974 YAHOO.widget.AutoComplete.prototype._clearSelection = function() {
1975 var extraction = (this.delimChar) ? this._extractQuery(this._elTextbox.value) :
1976 {previous:"",query:this._elTextbox.value};
1977 this._elTextbox.value = extraction.previous;
1978 this.selectionEnforceEvent.fire(this, extraction.query);
1982 * Whether or not user-typed value in the text input box matches any of the
1985 * @method _textMatchesOption
1986 * @return {HTMLElement} Matching list item element if user-input text matches
1987 * a result, null otherwise.
1990 YAHOO.widget.AutoComplete.prototype._textMatchesOption = function() {
1993 for(var i=0; i<this._nDisplayedItems; i++) {
1994 var elListItem = this._elList.childNodes[i];
1995 var sMatch = ("" + elListItem._sResultMatch).toLowerCase();
1996 if(sMatch == this._sCurQuery.toLowerCase()) {
1997 elMatch = elListItem;
2005 * Updates in the text input box with the first query result as the user types,
2006 * selecting the substring that the user has not typed.
2008 * @method _typeAhead
2009 * @param elListItem {HTMLElement} The <li> element item whose data populates the input field.
2010 * @param sQuery {String} Query string.
2013 YAHOO.widget.AutoComplete.prototype._typeAhead = function(elListItem, sQuery) {
2014 // Don't typeAhead if turned off or is backspace
2015 if(!this.typeAhead || (this._nKeyCode == 8)) {
2020 elTextbox = this._elTextbox;
2022 // Only if text selection is supported
2023 if(elTextbox.setSelectionRange || elTextbox.createTextRange) {
2024 // Set and store timeout for this typeahead
2025 this._nTypeAheadDelayID = setTimeout(function() {
2026 // Select the portion of text that the user has not typed
2027 var nStart = elTextbox.value.length; // any saved queries plus what user has typed
2028 oSelf._updateValue(elListItem);
2029 var nEnd = elTextbox.value.length;
2030 oSelf._selectText(elTextbox,nStart,nEnd);
2031 var sPrefill = elTextbox.value.substr(nStart,nEnd);
2032 oSelf.typeAheadEvent.fire(oSelf,sQuery,sPrefill);
2033 },(this.typeAheadDelay*1000));
2038 * Selects text in the input field.
2040 * @method _selectText
2041 * @param elTextbox {HTMLElement} Text input box element in which to select text.
2042 * @param nStart {Number} Starting index of text string to select.
2043 * @param nEnd {Number} Ending index of text selection.
2046 YAHOO.widget.AutoComplete.prototype._selectText = function(elTextbox, nStart, nEnd) {
2047 if(elTextbox.setSelectionRange) { // For Mozilla
2048 elTextbox.setSelectionRange(nStart,nEnd);
2050 else if(elTextbox.createTextRange) { // For IE
2051 var oTextRange = elTextbox.createTextRange();
2052 oTextRange.moveStart("character", nStart);
2053 oTextRange.moveEnd("character", nEnd-elTextbox.value.length);
2054 oTextRange.select();
2062 * Extracts rightmost query from delimited string.
2064 * @method _extractQuery
2065 * @param sQuery {String} String to parse
2066 * @return {Object} Object literal containing properties "query" and "previous".
2069 YAHOO.widget.AutoComplete.prototype._extractQuery = function(sQuery) {
2070 var aDelimChar = this.delimChar,
2072 nNewIndex, nQueryStart,
2073 i = aDelimChar.length-1,
2076 // Loop through all possible delimiters and find the rightmost one in the query
2077 // A " " may be a false positive if they are defined as delimiters AND
2078 // are used to separate delimited queries
2079 for(; i >= 0; i--) {
2080 nNewIndex = sQuery.lastIndexOf(aDelimChar[i]);
2081 if(nNewIndex > nDelimIndex) {
2082 nDelimIndex = nNewIndex;
2085 // If we think the last delimiter is a space (" "), make sure it is NOT
2086 // a false positive by also checking the char directly before it
2087 if(aDelimChar[i] == " ") {
2088 for (var j = aDelimChar.length-1; j >= 0; j--) {
2089 if(sQuery[nDelimIndex - 1] == aDelimChar[j]) {
2095 // A delimiter has been found in the query so extract the latest query from past selections
2096 if(nDelimIndex > -1) {
2097 nQueryStart = nDelimIndex + 1;
2098 // Trim any white space from the beginning...
2099 while(sQuery.charAt(nQueryStart) == " ") {
2102 // ...and save the rest of the string for later
2103 sPrevious = sQuery.substring(0,nQueryStart);
2104 // Here is the query itself
2105 sQuery = sQuery.substr(nQueryStart);
2107 // No delimiter found in the query, so there are no selections from past queries
2113 previous: sPrevious,
2119 * Syncs results container with its helpers.
2121 * @method _toggleContainerHelpers
2122 * @param bShow {Boolean} True if container is expanded, false if collapsed
2125 YAHOO.widget.AutoComplete.prototype._toggleContainerHelpers = function(bShow) {
2126 var width = this._elContent.offsetWidth + "px";
2127 var height = this._elContent.offsetHeight + "px";
2129 if(this.useIFrame && this._elIFrame) {
2130 var elIFrame = this._elIFrame;
2132 elIFrame.style.width = width;
2133 elIFrame.style.height = height;
2134 elIFrame.style.padding = "";
2137 elIFrame.style.width = 0;
2138 elIFrame.style.height = 0;
2139 elIFrame.style.padding = 0;
2142 if(this.useShadow && this._elShadow) {
2143 var elShadow = this._elShadow;
2145 elShadow.style.width = width;
2146 elShadow.style.height = height;
2149 elShadow.style.width = 0;
2150 elShadow.style.height = 0;
2156 * Animates expansion or collapse of the container.
2158 * @method _toggleContainer
2159 * @param bShow {Boolean} True if container should be expanded, false if container should be collapsed
2162 YAHOO.widget.AutoComplete.prototype._toggleContainer = function(bShow) {
2164 var elContainer = this._elContainer;
2166 // If implementer has container always open and it's already open, don't mess with it
2167 // Container is initialized with display "none" so it may need to be shown first time through
2168 if(this.alwaysShowContainer && this._bContainerOpen) {
2174 this._toggleHighlight(this._elCurListItem,"from");
2175 this._nDisplayedItems = 0;
2176 this._sCurQuery = null;
2178 // Container is already closed, so don't bother with changing the UI
2179 if(this._elContent.style.display == "none") {
2184 // If animation is enabled...
2185 var oAnim = this._oAnim;
2186 if(oAnim && oAnim.getEl() && (this.animHoriz || this.animVert)) {
2187 if(oAnim.isAnimated()) {
2191 // Clone container to grab current size offscreen
2192 var oClone = this._elContent.cloneNode(true);
2193 elContainer.appendChild(oClone);
2194 oClone.style.top = "-9000px";
2195 oClone.style.width = "";
2196 oClone.style.height = "";
2197 oClone.style.display = "";
2199 // Current size of the container is the EXPANDED size
2200 var wExp = oClone.offsetWidth;
2201 var hExp = oClone.offsetHeight;
2203 // Calculate COLLAPSED sizes based on horiz and vert anim
2204 var wColl = (this.animHoriz) ? 0 : wExp;
2205 var hColl = (this.animVert) ? 0 : hExp;
2207 // Set animation sizes
2208 oAnim.attributes = (bShow) ?
2209 {width: { to: wExp }, height: { to: hExp }} :
2210 {width: { to: wColl}, height: { to: hColl }};
2212 // If opening anew, set to a collapsed size...
2213 if(bShow && !this._bContainerOpen) {
2214 this._elContent.style.width = wColl+"px";
2215 this._elContent.style.height = hColl+"px";
2217 // Else, set it to its last known size.
2219 this._elContent.style.width = wExp+"px";
2220 this._elContent.style.height = hExp+"px";
2223 elContainer.removeChild(oClone);
2227 var onAnimComplete = function() {
2228 // Finish the collapse
2229 oAnim.onComplete.unsubscribeAll();
2232 oSelf._toggleContainerHelpers(true);
2233 oSelf._bContainerOpen = bShow;
2234 oSelf.containerExpandEvent.fire(oSelf);
2237 oSelf._elContent.style.display = "none";
2238 oSelf._bContainerOpen = bShow;
2239 oSelf.containerCollapseEvent.fire(oSelf);
2243 // Display container and animate it
2244 this._toggleContainerHelpers(false); // Bug 1424486: Be early to hide, late to show;
2245 this._elContent.style.display = "";
2246 oAnim.onComplete.subscribe(onAnimComplete);
2249 // Else don't animate, just show or hide
2252 this._elContent.style.display = "";
2253 this._toggleContainerHelpers(true);
2254 this._bContainerOpen = bShow;
2255 this.containerExpandEvent.fire(this);
2258 this._toggleContainerHelpers(false);
2259 this._elContent.style.display = "none";
2260 this._bContainerOpen = bShow;
2261 this.containerCollapseEvent.fire(this);
2268 * Toggles the highlight on or off for an item in the container, and also cleans
2269 * up highlighting of any previous item.
2271 * @method _toggleHighlight
2272 * @param elNewListItem {HTMLElement} The <li> element item to receive highlight behavior.
2273 * @param sType {String} Type "mouseover" will toggle highlight on, and "mouseout" will toggle highlight off.
2276 YAHOO.widget.AutoComplete.prototype._toggleHighlight = function(elNewListItem, sType) {
2278 var sHighlight = this.highlightClassName;
2279 if(this._elCurListItem) {
2280 // Remove highlight from old item
2281 YAHOO.util.Dom.removeClass(this._elCurListItem, sHighlight);
2282 this._elCurListItem = null;
2285 if((sType == "to") && sHighlight) {
2286 // Apply highlight to new item
2287 YAHOO.util.Dom.addClass(elNewListItem, sHighlight);
2288 this._elCurListItem = elNewListItem;
2294 * Toggles the pre-highlight on or off for an item in the container, and also cleans
2295 * up pre-highlighting of any previous item.
2297 * @method _togglePrehighlight
2298 * @param elNewListItem {HTMLElement} The <li> element item to receive highlight behavior.
2299 * @param sType {String} Type "mouseover" will toggle highlight on, and "mouseout" will toggle highlight off.
2302 YAHOO.widget.AutoComplete.prototype._togglePrehighlight = function(elNewListItem, sType) {
2303 var sPrehighlight = this.prehighlightClassName;
2305 if(this._elCurPrehighlightItem) {
2306 YAHOO.util.Dom.removeClass(this._elCurPrehighlightItem, sPrehighlight);
2308 if(elNewListItem == this._elCurListItem) {
2312 if((sType == "mouseover") && sPrehighlight) {
2313 // Apply prehighlight to new item
2314 YAHOO.util.Dom.addClass(elNewListItem, sPrehighlight);
2315 this._elCurPrehighlightItem = elNewListItem;
2318 // Remove prehighlight from old item
2319 YAHOO.util.Dom.removeClass(elNewListItem, sPrehighlight);
2324 * Updates the text input box value with selected query result. If a delimiter
2325 * has been defined, then the value gets appended with the delimiter.
2327 * @method _updateValue
2328 * @param elListItem {HTMLElement} The <li> element item with which to update the value.
2331 YAHOO.widget.AutoComplete.prototype._updateValue = function(elListItem) {
2332 if(!this.suppressInputUpdate) {
2333 var elTextbox = this._elTextbox;
2334 var sDelimChar = (this.delimChar) ? (this.delimChar[0] || this.delimChar) : null;
2335 var sResultMatch = elListItem._sResultMatch;
2337 // Calculate the new value
2340 // Preserve selections from past queries
2341 sNewValue = this._sPastSelections;
2342 // Add new selection plus delimiter
2343 sNewValue += sResultMatch + sDelimChar;
2344 if(sDelimChar != " ") {
2349 sNewValue = sResultMatch;
2352 // Update input field
2353 elTextbox.value = sNewValue;
2355 // Scroll to bottom of textarea if necessary
2356 if(elTextbox.type == "textarea") {
2357 elTextbox.scrollTop = elTextbox.scrollHeight;
2360 // Move cursor to end
2361 var end = elTextbox.value.length;
2362 this._selectText(elTextbox,end,end);
2364 this._elCurListItem = elListItem;
2369 * Selects a result item from the container
2371 * @method _selectItem
2372 * @param elListItem {HTMLElement} The selected <li> element item.
2375 YAHOO.widget.AutoComplete.prototype._selectItem = function(elListItem) {
2376 this._bItemSelected = true;
2377 this._updateValue(elListItem);
2378 this._sPastSelections = this._elTextbox.value;
2379 this._clearInterval();
2380 this.itemSelectEvent.fire(this, elListItem, elListItem._oResultData);
2381 this._toggleContainer(false);
2385 * If an item is highlighted in the container, the right arrow key jumps to the
2386 * end of the textbox and selects the highlighted item, otherwise the container
2389 * @method _jumpSelection
2392 YAHOO.widget.AutoComplete.prototype._jumpSelection = function() {
2393 if(this._elCurListItem) {
2394 this._selectItem(this._elCurListItem);
2397 this._toggleContainer(false);
2402 * Triggered by up and down arrow keys, changes the current highlighted
2403 * <li> element item. Scrolls container if necessary.
2405 * @method _moveSelection
2406 * @param nKeyCode {Number} Code of key pressed.
2409 YAHOO.widget.AutoComplete.prototype._moveSelection = function(nKeyCode) {
2410 if(this._bContainerOpen) {
2411 // Determine current item's id number
2412 var elCurListItem = this._elCurListItem,
2416 nCurItemIndex = elCurListItem._nItemIndex;
2419 var nNewItemIndex = (nKeyCode == 40) ?
2420 (nCurItemIndex + 1) : (nCurItemIndex - 1);
2423 if(nNewItemIndex < -2 || nNewItemIndex >= this._nDisplayedItems) {
2428 // Unhighlight current item
2429 this._toggleHighlight(elCurListItem, "from");
2430 this.itemArrowFromEvent.fire(this, elCurListItem);
2432 if(nNewItemIndex == -1) {
2433 // Go back to query (remove type-ahead string)
2434 if(this.delimChar) {
2435 this._elTextbox.value = this._sPastSelections + this._sCurQuery;
2438 this._elTextbox.value = this._sCurQuery;
2442 if(nNewItemIndex == -2) {
2444 this._toggleContainer(false);
2448 var elNewListItem = this._elList.childNodes[nNewItemIndex],
2450 // Scroll the container if necessary
2451 elContent = this._elContent,
2452 sOF = YAHOO.util.Dom.getStyle(elContent,"overflow"),
2453 sOFY = YAHOO.util.Dom.getStyle(elContent,"overflowY"),
2454 scrollOn = ((sOF == "auto") || (sOF == "scroll") || (sOFY == "auto") || (sOFY == "scroll"));
2455 if(scrollOn && (nNewItemIndex > -1) &&
2456 (nNewItemIndex < this._nDisplayedItems)) {
2457 // User is keying down
2458 if(nKeyCode == 40) {
2459 // Bottom of selected item is below scroll area...
2460 if((elNewListItem.offsetTop+elNewListItem.offsetHeight) > (elContent.scrollTop + elContent.offsetHeight)) {
2461 // Set bottom of scroll area to bottom of selected item
2462 elContent.scrollTop = (elNewListItem.offsetTop+elNewListItem.offsetHeight) - elContent.offsetHeight;
2464 // Bottom of selected item is above scroll area...
2465 else if((elNewListItem.offsetTop+elNewListItem.offsetHeight) < elContent.scrollTop) {
2466 // Set top of selected item to top of scroll area
2467 elContent.scrollTop = elNewListItem.offsetTop;
2471 // User is keying up
2473 // Top of selected item is above scroll area
2474 if(elNewListItem.offsetTop < elContent.scrollTop) {
2475 // Set top of scroll area to top of selected item
2476 this._elContent.scrollTop = elNewListItem.offsetTop;
2478 // Top of selected item is below scroll area
2479 else if(elNewListItem.offsetTop > (elContent.scrollTop + elContent.offsetHeight)) {
2480 // Set bottom of selected item to bottom of scroll area
2481 this._elContent.scrollTop = (elNewListItem.offsetTop+elNewListItem.offsetHeight) - elContent.offsetHeight;
2486 this._toggleHighlight(elNewListItem, "to");
2487 this.itemArrowToEvent.fire(this, elNewListItem);
2488 if(this.typeAhead) {
2489 this._updateValue(elNewListItem);
2494 /////////////////////////////////////////////////////////////////////////////
2496 // Private event handlers
2498 /////////////////////////////////////////////////////////////////////////////
2501 * Handles container mouseover events.
2503 * @method _onContainerMouseover
2504 * @param v {HTMLEvent} The mouseover event.
2505 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
2508 YAHOO.widget.AutoComplete.prototype._onContainerMouseover = function(v,oSelf) {
2509 var elTarget = YAHOO.util.Event.getTarget(v);
2510 var elTag = elTarget.nodeName.toLowerCase();
2511 while(elTarget && (elTag != "table")) {
2516 if(oSelf.prehighlightClassName) {
2517 oSelf._togglePrehighlight(elTarget,"mouseover");
2520 oSelf._toggleHighlight(elTarget,"to");
2523 oSelf.itemMouseOverEvent.fire(oSelf, elTarget);
2526 if(YAHOO.util.Dom.hasClass(elTarget,"yui-ac-container")) {
2527 oSelf._bOverContainer = true;
2535 elTarget = elTarget.parentNode;
2537 elTag = elTarget.nodeName.toLowerCase();
2543 * Handles container mouseout events.
2545 * @method _onContainerMouseout
2546 * @param v {HTMLEvent} The mouseout event.
2547 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
2550 YAHOO.widget.AutoComplete.prototype._onContainerMouseout = function(v,oSelf) {
2551 var elTarget = YAHOO.util.Event.getTarget(v);
2552 var elTag = elTarget.nodeName.toLowerCase();
2553 while(elTarget && (elTag != "table")) {
2558 if(oSelf.prehighlightClassName) {
2559 oSelf._togglePrehighlight(elTarget,"mouseout");
2562 oSelf._toggleHighlight(elTarget,"from");
2565 oSelf.itemMouseOutEvent.fire(oSelf, elTarget);
2568 oSelf._toggleHighlight(oSelf._elCurListItem,"to");
2571 if(YAHOO.util.Dom.hasClass(elTarget,"yui-ac-container")) {
2572 oSelf._bOverContainer = false;
2580 elTarget = elTarget.parentNode;
2582 elTag = elTarget.nodeName.toLowerCase();
2588 * Handles container click events.
2590 * @method _onContainerClick
2591 * @param v {HTMLEvent} The click event.
2592 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
2595 YAHOO.widget.AutoComplete.prototype._onContainerClick = function(v,oSelf) {
2596 var elTarget = YAHOO.util.Event.getTarget(v);
2597 var elTag = elTarget.nodeName.toLowerCase();
2598 while(elTarget && (elTag != "table")) {
2603 // In case item has not been moused over
2604 oSelf._toggleHighlight(elTarget,"to");
2605 oSelf._selectItem(elTarget);
2611 elTarget = elTarget.parentNode;
2613 elTag = elTarget.nodeName.toLowerCase();
2620 * Handles container scroll events.
2622 * @method _onContainerScroll
2623 * @param v {HTMLEvent} The scroll event.
2624 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
2627 YAHOO.widget.AutoComplete.prototype._onContainerScroll = function(v,oSelf) {
2632 * Handles container resize events.
2634 * @method _onContainerResize
2635 * @param v {HTMLEvent} The resize event.
2636 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
2639 YAHOO.widget.AutoComplete.prototype._onContainerResize = function(v,oSelf) {
2640 oSelf._toggleContainerHelpers(oSelf._bContainerOpen);
2645 * Handles textbox keydown events of functional keys, mainly for UI behavior.
2647 * @method _onTextboxKeyDown
2648 * @param v {HTMLEvent} The keydown event.
2649 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
2652 YAHOO.widget.AutoComplete.prototype._onTextboxKeyDown = function(v,oSelf) {
2653 var nKeyCode = v.keyCode;
2656 if(oSelf._nTypeAheadDelayID != -1) {
2657 clearTimeout(oSelf._nTypeAheadDelayID);
2662 if(!YAHOO.env.ua.opera && (navigator.userAgent.toLowerCase().indexOf("mac") == -1) || (YAHOO.env.ua.webkit>420)) {
2663 // select an item or clear out
2664 if(oSelf._elCurListItem) {
2665 if(oSelf.delimChar && (oSelf._nKeyCode != nKeyCode)) {
2666 if(oSelf._bContainerOpen) {
2667 YAHOO.util.Event.stopEvent(v);
2670 oSelf._selectItem(oSelf._elCurListItem);
2673 oSelf._toggleContainer(false);
2678 if(!YAHOO.env.ua.opera && (navigator.userAgent.toLowerCase().indexOf("mac") == -1) || (YAHOO.env.ua.webkit>420)) {
2679 if(oSelf._elCurListItem) {
2680 if(oSelf._nKeyCode != nKeyCode) {
2681 if(oSelf._bContainerOpen) {
2682 YAHOO.util.Event.stopEvent(v);
2685 oSelf._selectItem(oSelf._elCurListItem);
2688 oSelf._toggleContainer(false);
2693 oSelf._toggleContainer(false);
2696 oSelf._jumpSelection();
2699 if(oSelf._bContainerOpen) {
2700 YAHOO.util.Event.stopEvent(v);
2701 oSelf._moveSelection(nKeyCode);
2705 if(oSelf._bContainerOpen) {
2706 YAHOO.util.Event.stopEvent(v);
2707 oSelf._moveSelection(nKeyCode);
2711 oSelf._bItemSelected = false;
2712 oSelf._toggleHighlight(oSelf._elCurListItem, "from");
2714 oSelf.textboxKeyEvent.fire(oSelf, nKeyCode);
2718 if(nKeyCode === 18){
2719 oSelf._enableIntervalDetection();
2721 oSelf._nKeyCode = nKeyCode;
2725 * Handles textbox keypress events.
2726 * @method _onTextboxKeyPress
2727 * @param v {HTMLEvent} The keypress event.
2728 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
2731 YAHOO.widget.AutoComplete.prototype._onTextboxKeyPress = function(v,oSelf) {
2732 var nKeyCode = v.keyCode;
2734 // Expose only to non SF3 (bug 1978549) Mac browsers (bug 790337) and Opera browsers (bug 583531),
2735 // where stopEvent is ineffective on keydown events
2736 if(YAHOO.env.ua.opera || (navigator.userAgent.toLowerCase().indexOf("mac") != -1) && (YAHOO.env.ua.webkit < 420)) {
2739 // select an item or clear out
2740 if(oSelf._bContainerOpen) {
2741 if(oSelf.delimChar) {
2742 YAHOO.util.Event.stopEvent(v);
2744 if(oSelf._elCurListItem) {
2745 oSelf._selectItem(oSelf._elCurListItem);
2748 oSelf._toggleContainer(false);
2753 if(oSelf._bContainerOpen) {
2754 YAHOO.util.Event.stopEvent(v);
2755 if(oSelf._elCurListItem) {
2756 oSelf._selectItem(oSelf._elCurListItem);
2759 oSelf._toggleContainer(false);
2768 //TODO: (?) limit only to non-IE, non-Mac-FF for Korean IME support (bug 811948)
2769 // Korean IME detected
2770 else if(nKeyCode == 229) {
2771 oSelf._enableIntervalDetection();
2776 * Handles textbox keyup events to trigger queries.
2778 * @method _onTextboxKeyUp
2779 * @param v {HTMLEvent} The keyup event.
2780 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
2783 YAHOO.widget.AutoComplete.prototype._onTextboxKeyUp = function(v,oSelf) {
2784 var sText = this.value; //string in textbox
2786 // Check to see if any of the public properties have been updated
2789 // Filter out chars that don't trigger queries
2790 var nKeyCode = v.keyCode;
2791 if(oSelf._isIgnoreKey(nKeyCode)) {
2795 // Clear previous timeout
2796 if(oSelf._nDelayID != -1) {
2797 clearTimeout(oSelf._nDelayID);
2801 oSelf._nDelayID = setTimeout(function(){
2802 oSelf._sendQuery(sText);
2803 },(oSelf.queryDelay * 1000));
2807 * Handles text input box receiving focus.
2809 * @method _onTextboxFocus
2810 * @param v {HTMLEvent} The focus event.
2811 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
2814 YAHOO.widget.AutoComplete.prototype._onTextboxFocus = function (v,oSelf) {
2815 // Start of a new interaction
2816 if(!oSelf._bFocused) {
2817 oSelf._elTextbox.setAttribute("autocomplete","off");
2818 oSelf._bFocused = true;
2819 oSelf._sInitInputValue = oSelf._elTextbox.value;
2820 oSelf.textboxFocusEvent.fire(oSelf);
2825 * Handles text input box losing focus.
2827 * @method _onTextboxBlur
2828 * @param v {HTMLEvent} The focus event.
2829 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
2832 YAHOO.widget.AutoComplete.prototype._onTextboxBlur = function (v,oSelf) {
2834 if(!oSelf._bOverContainer || (oSelf._nKeyCode == 9)) {
2835 // Current query needs to be validated as a selection
2836 if(!oSelf._bItemSelected) {
2837 var elMatchListItem = oSelf._textMatchesOption();
2838 // Container is closed or current query doesn't match any result
2839 if(!oSelf._bContainerOpen || (oSelf._bContainerOpen && (elMatchListItem === null))) {
2840 // Force selection is enabled so clear the current query
2841 if(oSelf.forceSelection) {
2842 oSelf._clearSelection();
2844 // Treat current query as a valid selection
2846 oSelf.unmatchedItemSelectEvent.fire(oSelf, oSelf._sCurQuery);
2849 // Container is open and current query matches a result
2851 // Force a selection when textbox is blurred with a match
2852 if(oSelf.forceSelection) {
2853 oSelf._selectItem(elMatchListItem);
2858 oSelf._clearInterval();
2859 oSelf._bFocused = false;
2860 if(oSelf._sInitInputValue !== oSelf._elTextbox.value) {
2861 oSelf.textboxChangeEvent.fire(oSelf);
2863 oSelf.textboxBlurEvent.fire(oSelf);
2865 oSelf._toggleContainer(false);
2867 // Not a true blur if it was a selection via mouse click
2874 * Handles window unload event.
2876 * @method _onWindowUnload
2877 * @param v {HTMLEvent} The unload event.
2878 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
2881 YAHOO.widget.AutoComplete.prototype._onWindowUnload = function(v,oSelf) {
2882 if(oSelf && oSelf._elTextbox && oSelf.allowBrowserAutocomplete) {
2883 oSelf._elTextbox.setAttribute("autocomplete","on");
2887 /////////////////////////////////////////////////////////////////////////////
2889 // Deprecated for Backwards Compatibility
2891 /////////////////////////////////////////////////////////////////////////////
2893 * @method doBeforeSendQuery
2894 * @deprecated Use generateRequest.
2896 YAHOO.widget.AutoComplete.prototype.doBeforeSendQuery = function(sQuery) {
2897 return this.generateRequest(sQuery);
2901 * @method getListItems
2902 * @deprecated Use getListEl().childNodes.
2904 YAHOO.widget.AutoComplete.prototype.getListItems = function() {
2905 var allListItemEls = [],
2906 els = this._elList.childNodes;
2907 for(var i=els.length-1; i>=0; i--) {
2908 allListItemEls[i] = els[i];
2910 return allListItemEls;
2913 /////////////////////////////////////////////////////////////////////////
2915 // Private static methods
2917 /////////////////////////////////////////////////////////////////////////
2920 * Clones object literal or array of object literals.
2922 * @method AutoComplete._cloneObject
2923 * @param o {Object} Object.
2927 YAHOO.widget.AutoComplete._cloneObject = function(o) {
2928 if(!YAHOO.lang.isValue(o)) {
2934 if(YAHOO.lang.isFunction(o)) {
2937 else if(YAHOO.lang.isArray(o)) {
2939 for(var i=0,len=o.length;i<len;i++) {
2940 array[i] = YAHOO.widget.AutoComplete._cloneObject(o[i]);
2944 else if(YAHOO.lang.isObject(o)) {
2946 if(YAHOO.lang.hasOwnProperty(o, x)) {
2947 if(YAHOO.lang.isValue(o[x]) && YAHOO.lang.isObject(o[x]) || YAHOO.lang.isArray(o[x])) {
2948 copy[x] = YAHOO.widget.AutoComplete._cloneObject(o[x]);
2966 YAHOO.register("autocomplete", YAHOO.widget.AutoComplete, {version: "2.8.0r4", build: "2449"});