2 Copyright (c) 2008, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.net/yui/license.txt
8 * The AutoComplete control provides the front-end logic for text-entry suggestion and
9 * completion functionality.
11 * @module autocomplete
12 * @requires yahoo, dom, event, datasource
13 * @optional animation, connection, get
14 * @namespace YAHOO.widget
15 * @title AutoComplete Widget
18 /****************************************************************************/
19 /****************************************************************************/
20 /****************************************************************************/
23 * The AutoComplete class provides the customizable functionality of a plug-and-play DHTML
24 * auto completion widget. Some key features:
26 * <li>Navigate with up/down arrow keys and/or mouse to pick a selection</li>
27 * <li>The drop down container can "roll down" or "fly out" via configurable
29 * <li>UI look-and-feel customizable through CSS, including container
30 * attributes, borders, position, fonts, etc</li>
35 * @param elInput {HTMLElement} DOM element reference of an input field.
36 * @param elInput {String} String ID of an input field.
37 * @param elContainer {HTMLElement} DOM element reference of an existing DIV.
38 * @param elContainer {String} String ID of an existing DIV.
39 * @param oDataSource {YAHOO.widget.DataSource} DataSource instance.
40 * @param oConfigs {Object} (optional) Object literal of configuration params.
42 YAHOO.widget.AutoComplete = function(elInput,elContainer,oDataSource,oConfigs) {
43 if(elInput && elContainer && oDataSource) {
44 // Validate DataSource
45 if(oDataSource instanceof YAHOO.widget.DataSource) {
46 this.dataSource = oDataSource;
49 YAHOO.log("Could not instantiate AutoComplete due to an invalid DataSource", "error", this.toString());
53 // Validate input element
54 if(YAHOO.util.Dom.inDocument(elInput)) {
55 if(YAHOO.lang.isString(elInput)) {
56 this._sName = "instance" + YAHOO.widget.AutoComplete._nIndex + " " + elInput;
57 this._elTextbox = document.getElementById(elInput);
60 this._sName = (elInput.id) ?
61 "instance" + YAHOO.widget.AutoComplete._nIndex + " " + elInput.id:
62 "instance" + YAHOO.widget.AutoComplete._nIndex;
63 this._elTextbox = elInput;
65 YAHOO.util.Dom.addClass(this._elTextbox, "yui-ac-input");
68 YAHOO.log("Could not instantiate AutoComplete due to an invalid input element", "error", this.toString());
72 // Validate container element
73 if(YAHOO.util.Dom.inDocument(elContainer)) {
74 if(YAHOO.lang.isString(elContainer)) {
75 this._elContainer = document.getElementById(elContainer);
78 this._elContainer = elContainer;
80 if(this._elContainer.style.display == "none") {
81 YAHOO.log("The container may not display properly if display is set to \"none\" in CSS", "warn", this.toString());
85 var elParent = this._elContainer.parentNode;
86 var elTag = elParent.tagName.toLowerCase();
88 YAHOO.util.Dom.addClass(elParent, "yui-ac");
91 YAHOO.log("Could not find the wrapper element for skinning", "warn", this.toString());
95 YAHOO.log("Could not instantiate AutoComplete due to an invalid container element", "error", this.toString());
99 // Set any config params passed in to override defaults
100 if(oConfigs && (oConfigs.constructor == Object)) {
101 for(var sConfig in oConfigs) {
103 this[sConfig] = oConfigs[sConfig];
108 // Initialization sequence
109 this._initContainer();
112 this._initContainerHelpers();
116 var elTextbox = this._elTextbox;
117 // Events are actually for the content module within the container
118 var elContent = this._elContent;
121 YAHOO.util.Event.addListener(elTextbox,"keyup",oSelf._onTextboxKeyUp,oSelf);
122 YAHOO.util.Event.addListener(elTextbox,"keydown",oSelf._onTextboxKeyDown,oSelf);
123 YAHOO.util.Event.addListener(elTextbox,"focus",oSelf._onTextboxFocus,oSelf);
124 YAHOO.util.Event.addListener(elTextbox,"blur",oSelf._onTextboxBlur,oSelf);
125 YAHOO.util.Event.addListener(elContent,"mouseover",oSelf._onContainerMouseover,oSelf);
126 YAHOO.util.Event.addListener(elContent,"mouseout",oSelf._onContainerMouseout,oSelf);
127 YAHOO.util.Event.addListener(elContent,"scroll",oSelf._onContainerScroll,oSelf);
128 YAHOO.util.Event.addListener(elContent,"resize",oSelf._onContainerResize,oSelf);
129 YAHOO.util.Event.addListener(elTextbox,"keypress",oSelf._onTextboxKeyPress,oSelf);
130 YAHOO.util.Event.addListener(window,"unload",oSelf._onWindowUnload,oSelf);
133 this.textboxFocusEvent = new YAHOO.util.CustomEvent("textboxFocus", this);
134 this.textboxKeyEvent = new YAHOO.util.CustomEvent("textboxKey", this);
135 this.dataRequestEvent = new YAHOO.util.CustomEvent("dataRequest", this);
136 this.dataReturnEvent = new YAHOO.util.CustomEvent("dataReturn", this);
137 this.dataErrorEvent = new YAHOO.util.CustomEvent("dataError", this);
138 this.containerExpandEvent = new YAHOO.util.CustomEvent("containerExpand", this);
139 this.typeAheadEvent = new YAHOO.util.CustomEvent("typeAhead", this);
140 this.itemMouseOverEvent = new YAHOO.util.CustomEvent("itemMouseOver", this);
141 this.itemMouseOutEvent = new YAHOO.util.CustomEvent("itemMouseOut", this);
142 this.itemArrowToEvent = new YAHOO.util.CustomEvent("itemArrowTo", this);
143 this.itemArrowFromEvent = new YAHOO.util.CustomEvent("itemArrowFrom", this);
144 this.itemSelectEvent = new YAHOO.util.CustomEvent("itemSelect", this);
145 this.unmatchedItemSelectEvent = new YAHOO.util.CustomEvent("unmatchedItemSelect", this);
146 this.selectionEnforceEvent = new YAHOO.util.CustomEvent("selectionEnforce", this);
147 this.containerCollapseEvent = new YAHOO.util.CustomEvent("containerCollapse", this);
148 this.textboxBlurEvent = new YAHOO.util.CustomEvent("textboxBlur", this);
151 elTextbox.setAttribute("autocomplete","off");
152 YAHOO.widget.AutoComplete._nIndex++;
153 YAHOO.log("AutoComplete initialized","info",this.toString());
155 // Required arguments were not found
157 YAHOO.log("Could not instantiate AutoComplete due invalid arguments", "error", this.toString());
161 /////////////////////////////////////////////////////////////////////////////
163 // Public member variables
165 /////////////////////////////////////////////////////////////////////////////
168 * The DataSource object that encapsulates the data used for auto completion.
169 * This object should be an inherited object from YAHOO.widget.DataSource.
171 * @property dataSource
172 * @type YAHOO.widget.DataSource
174 YAHOO.widget.AutoComplete.prototype.dataSource = null;
177 * Number of characters that must be entered before querying for results. A negative value
178 * effectively turns off the widget. A value of 0 allows queries of null or empty string
181 * @property minQueryLength
185 YAHOO.widget.AutoComplete.prototype.minQueryLength = 1;
188 * Maximum number of results to display in results container.
190 * @property maxResultsDisplayed
194 YAHOO.widget.AutoComplete.prototype.maxResultsDisplayed = 10;
197 * Number of seconds to delay before submitting a query request. If a query
198 * request is received before a previous one has completed its delay, the
199 * previous request is cancelled and the new request is set to the delay.
200 * Implementers should take care when setting this value very low (i.e., less
201 * than 0.2) with low latency DataSources and the typeAhead feature enabled, as
202 * fast typers may see unexpected behavior.
204 * @property queryDelay
208 YAHOO.widget.AutoComplete.prototype.queryDelay = 0.2;
211 * Class name of a highlighted item within results container.
213 * @property highlightClassName
215 * @default "yui-ac-highlight"
217 YAHOO.widget.AutoComplete.prototype.highlightClassName = "yui-ac-highlight";
220 * Class name of a pre-highlighted item within results container.
222 * @property prehighlightClassName
225 YAHOO.widget.AutoComplete.prototype.prehighlightClassName = null;
228 * Query delimiter. A single character separator for multiple delimited
229 * selections. Multiple delimiter characteres may be defined as an array of
230 * strings. A null value or empty string indicates that query results cannot
231 * be delimited. This feature is not recommended if you need forceSelection to
234 * @property delimChar
235 * @type String | String[]
237 YAHOO.widget.AutoComplete.prototype.delimChar = null;
240 * Whether or not the first item in results container should be automatically highlighted
243 * @property autoHighlight
247 YAHOO.widget.AutoComplete.prototype.autoHighlight = true;
250 * Whether or not the input field should be automatically updated
251 * with the first query result as the user types, auto-selecting the substring
252 * that the user has not typed.
254 * @property typeAhead
258 YAHOO.widget.AutoComplete.prototype.typeAhead = false;
261 * Whether or not to animate the expansion/collapse of the results container in the
262 * horizontal direction.
264 * @property animHoriz
268 YAHOO.widget.AutoComplete.prototype.animHoriz = false;
271 * Whether or not to animate the expansion/collapse of the results container in the
272 * vertical direction.
278 YAHOO.widget.AutoComplete.prototype.animVert = true;
281 * Speed of container expand/collapse animation, in seconds..
283 * @property animSpeed
287 YAHOO.widget.AutoComplete.prototype.animSpeed = 0.3;
290 * Whether or not to force the user's selection to match one of the query
291 * results. Enabling this feature essentially transforms the input field into a
292 * <select> field. This feature is not recommended with delimiter character(s)
295 * @property forceSelection
299 YAHOO.widget.AutoComplete.prototype.forceSelection = false;
302 * Whether or not to allow browsers to cache user-typed input in the input
303 * field. Disabling this feature will prevent the widget from setting the
304 * autocomplete="off" on the input field. When autocomplete="off"
305 * and users click the back button after form submission, user-typed input can
306 * be prefilled by the browser from its cache. This caching of user input may
307 * not be desired for sensitive data, such as credit card numbers, in which
308 * case, implementers should consider setting allowBrowserAutocomplete to false.
310 * @property allowBrowserAutocomplete
314 YAHOO.widget.AutoComplete.prototype.allowBrowserAutocomplete = true;
317 * Whether or not the results container should always be displayed.
318 * Enabling this feature displays the container when the widget is instantiated
319 * and prevents the toggling of the container to a collapsed state.
321 * @property alwaysShowContainer
325 YAHOO.widget.AutoComplete.prototype.alwaysShowContainer = false;
328 * Whether or not to use an iFrame to layer over Windows form elements in
329 * IE. Set to true only when the results container will be on top of a
330 * <select> field in IE and thus exposed to the IE z-index bug (i.e.,
333 * @property useIFrame
337 YAHOO.widget.AutoComplete.prototype.useIFrame = false;
340 * Whether or not the results container should have a shadow.
342 * @property useShadow
346 YAHOO.widget.AutoComplete.prototype.useShadow = false;
348 /////////////////////////////////////////////////////////////////////////////
352 /////////////////////////////////////////////////////////////////////////////
355 * Public accessor to the unique name of the AutoComplete instance.
358 * @return {String} Unique name of the AutoComplete instance.
360 YAHOO.widget.AutoComplete.prototype.toString = function() {
361 return "AutoComplete " + this._sName;
365 * Returns true if container is in an expanded state, false otherwise.
367 * @method isContainerOpen
368 * @return {Boolean} Returns true if container is in an expanded state, false otherwise.
370 YAHOO.widget.AutoComplete.prototype.isContainerOpen = function() {
371 return this._bContainerOpen;
375 * Public accessor to the internal array of DOM <li> elements that
376 * display query results within the results container.
378 * @method getListItems
379 * @return {HTMLElement[]} Array of <li> elements within the results container.
381 YAHOO.widget.AutoComplete.prototype.getListItems = function() {
382 return this._aListItems;
386 * Public accessor to the data held in an <li> element of the
389 * @method getListItemData
390 * @return {Object | Object[]} Object or array of result data or null
392 YAHOO.widget.AutoComplete.prototype.getListItemData = function(oListItem) {
393 if(oListItem._oResultData) {
394 return oListItem._oResultData;
402 * Sets HTML markup for the results container header. This markup will be
403 * inserted within a <div> tag with a class of "yui-ac-hd".
406 * @param sHeader {String} HTML markup for results container header.
408 YAHOO.widget.AutoComplete.prototype.setHeader = function(sHeader) {
410 var elHeader = this._elHeader;
412 elHeader.innerHTML = sHeader;
413 elHeader.style.display = "block";
416 elHeader.innerHTML = "";
417 elHeader.style.display = "none";
423 * Sets HTML markup for the results container footer. This markup will be
424 * inserted within a <div> tag with a class of "yui-ac-ft".
427 * @param sFooter {String} HTML markup for results container footer.
429 YAHOO.widget.AutoComplete.prototype.setFooter = function(sFooter) {
431 var elFooter = this._elFooter;
433 elFooter.innerHTML = sFooter;
434 elFooter.style.display = "block";
437 elFooter.innerHTML = "";
438 elFooter.style.display = "none";
444 * Sets HTML markup for the results container body. This markup will be
445 * inserted within a <div> tag with a class of "yui-ac-bd".
448 * @param sBody {String} HTML markup for results container body.
450 YAHOO.widget.AutoComplete.prototype.setBody = function(sBody) {
452 var elBody = this._elBody;
454 elBody.innerHTML = sBody;
455 elBody.style.display = "block";
456 elBody.style.display = "block";
459 elBody.innerHTML = "";
460 elBody.style.display = "none";
462 this._maxResultsDisplayed = 0;
467 * Overridable method that converts a result item object into HTML markup
468 * for display. Return data values are accessible via the oResultItem object,
469 * and the key return value will always be oResultItem[0]. Markup will be
470 * displayed within <li> element tags in the container.
472 * @method formatResult
473 * @param oResultItem {Object} Result item representing one query result. Data is held in an array.
474 * @param sQuery {String} The current query string.
475 * @return {String} HTML markup of formatted result data.
477 YAHOO.widget.AutoComplete.prototype.formatResult = function(oResultItem, sQuery) {
478 var sResult = oResultItem[0];
488 * Overridable method called before container expands allows implementers to access data
491 * @method doBeforeExpandContainer
492 * @param elTextbox {HTMLElement} The text input box.
493 * @param elContainer {HTMLElement} The container element.
494 * @param sQuery {String} The query string.
495 * @param aResults {Object[]} An array of query results.
496 * @return {Boolean} Return true to continue expanding container, false to cancel the expand.
498 YAHOO.widget.AutoComplete.prototype.doBeforeExpandContainer = function(elTextbox, elContainer, sQuery, aResults) {
503 * Makes query request to the DataSource.
506 * @param sQuery {String} Query string.
508 YAHOO.widget.AutoComplete.prototype.sendQuery = function(sQuery) {
509 this._sendQuery(sQuery);
513 * Overridable method gives implementers access to the query before it gets sent.
515 * @method doBeforeSendQuery
516 * @param sQuery {String} Query string.
517 * @return {String} Query string.
519 YAHOO.widget.AutoComplete.prototype.doBeforeSendQuery = function(sQuery) {
524 * Nulls out the entire AutoComplete instance and related objects, removes attached
525 * event listeners, and clears out DOM elements inside the container. After
526 * calling this method, the instance reference should be expliclitly nulled by
527 * implementer, as in myDataTable = null. Use with caution!
531 YAHOO.widget.AutoComplete.prototype.destroy = function() {
532 var instanceName = this.toString();
533 var elInput = this._elTextbox;
534 var elContainer = this._elContainer;
536 // Unhook custom events
537 this.textboxFocusEvent.unsubscribeAll();
538 this.textboxKeyEvent.unsubscribeAll();
539 this.dataRequestEvent.unsubscribeAll();
540 this.dataReturnEvent.unsubscribeAll();
541 this.dataErrorEvent.unsubscribeAll();
542 this.containerExpandEvent.unsubscribeAll();
543 this.typeAheadEvent.unsubscribeAll();
544 this.itemMouseOverEvent.unsubscribeAll();
545 this.itemMouseOutEvent.unsubscribeAll();
546 this.itemArrowToEvent.unsubscribeAll();
547 this.itemArrowFromEvent.unsubscribeAll();
548 this.itemSelectEvent.unsubscribeAll();
549 this.unmatchedItemSelectEvent.unsubscribeAll();
550 this.selectionEnforceEvent.unsubscribeAll();
551 this.containerCollapseEvent.unsubscribeAll();
552 this.textboxBlurEvent.unsubscribeAll();
555 YAHOO.util.Event.purgeElement(elInput, true);
556 YAHOO.util.Event.purgeElement(elContainer, true);
558 // Remove DOM elements
559 elContainer.innerHTML = "";
562 for(var key in this) {
563 if(YAHOO.lang.hasOwnProperty(this, key)) {
568 YAHOO.log("AutoComplete instance destroyed: " + instanceName);
571 /////////////////////////////////////////////////////////////////////////////
575 /////////////////////////////////////////////////////////////////////////////
578 * Fired when the input field receives focus.
580 * @event textboxFocusEvent
581 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
583 YAHOO.widget.AutoComplete.prototype.textboxFocusEvent = null;
586 * Fired when the input field receives key input.
588 * @event textboxKeyEvent
589 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
590 * @param nKeycode {Number} The keycode number.
592 YAHOO.widget.AutoComplete.prototype.textboxKeyEvent = null;
595 * Fired when the AutoComplete instance makes a query to the DataSource.
597 * @event dataRequestEvent
598 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
599 * @param sQuery {String} The query string.
601 YAHOO.widget.AutoComplete.prototype.dataRequestEvent = null;
604 * Fired when the AutoComplete instance receives query results from the data
607 * @event dataReturnEvent
608 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
609 * @param sQuery {String} The query string.
610 * @param aResults {Object[]} Results array.
612 YAHOO.widget.AutoComplete.prototype.dataReturnEvent = null;
615 * Fired when the AutoComplete instance does not receive query results from the
616 * DataSource due to an error.
618 * @event dataErrorEvent
619 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
620 * @param sQuery {String} The query string.
622 YAHOO.widget.AutoComplete.prototype.dataErrorEvent = null;
625 * Fired when the results container is expanded.
627 * @event containerExpandEvent
628 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
630 YAHOO.widget.AutoComplete.prototype.containerExpandEvent = null;
633 * Fired when the input field has been prefilled by the type-ahead
636 * @event typeAheadEvent
637 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
638 * @param sQuery {String} The query string.
639 * @param sPrefill {String} The prefill string.
641 YAHOO.widget.AutoComplete.prototype.typeAheadEvent = null;
644 * Fired when result item has been moused over.
646 * @event itemMouseOverEvent
647 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
648 * @param elItem {HTMLElement} The <li> element item moused to.
650 YAHOO.widget.AutoComplete.prototype.itemMouseOverEvent = null;
653 * Fired when result item has been moused out.
655 * @event itemMouseOutEvent
656 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
657 * @param elItem {HTMLElement} The <li> element item moused from.
659 YAHOO.widget.AutoComplete.prototype.itemMouseOutEvent = null;
662 * Fired when result item has been arrowed to.
664 * @event itemArrowToEvent
665 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
666 * @param elItem {HTMLElement} The <li> element item arrowed to.
668 YAHOO.widget.AutoComplete.prototype.itemArrowToEvent = null;
671 * Fired when result item has been arrowed away from.
673 * @event itemArrowFromEvent
674 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
675 * @param elItem {HTMLElement} The <li> element item arrowed from.
677 YAHOO.widget.AutoComplete.prototype.itemArrowFromEvent = null;
680 * Fired when an item is selected via mouse click, ENTER key, or TAB key.
682 * @event itemSelectEvent
683 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
684 * @param elItem {HTMLElement} The selected <li> element item.
685 * @param oData {Object} The data returned for the item, either as an object,
686 * or mapped from the schema into an array.
688 YAHOO.widget.AutoComplete.prototype.itemSelectEvent = null;
691 * Fired when a user selection does not match any of the displayed result items.
693 * @event unmatchedItemSelectEvent
694 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
696 YAHOO.widget.AutoComplete.prototype.unmatchedItemSelectEvent = null;
699 * Fired if forceSelection is enabled and the user's input has been cleared
700 * because it did not match one of the returned query results.
702 * @event selectionEnforceEvent
703 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
705 YAHOO.widget.AutoComplete.prototype.selectionEnforceEvent = null;
708 * Fired when the results container is collapsed.
710 * @event containerCollapseEvent
711 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
713 YAHOO.widget.AutoComplete.prototype.containerCollapseEvent = null;
716 * Fired when the input field loses focus.
718 * @event textboxBlurEvent
719 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
721 YAHOO.widget.AutoComplete.prototype.textboxBlurEvent = null;
723 /////////////////////////////////////////////////////////////////////////////
725 // Private member variables
727 /////////////////////////////////////////////////////////////////////////////
730 * Internal class variable to index multiple AutoComplete instances.
737 YAHOO.widget.AutoComplete._nIndex = 0;
740 * Name of AutoComplete instance.
746 YAHOO.widget.AutoComplete.prototype._sName = null;
749 * Text input field DOM element.
751 * @property _elTextbox
755 YAHOO.widget.AutoComplete.prototype._elTextbox = null;
758 * Container DOM element.
760 * @property _elContainer
764 YAHOO.widget.AutoComplete.prototype._elContainer = null;
767 * Reference to content element within container element.
769 * @property _elContent
773 YAHOO.widget.AutoComplete.prototype._elContent = null;
776 * Reference to header element within content element.
778 * @property _elHeader
782 YAHOO.widget.AutoComplete.prototype._elHeader = null;
785 * Reference to body element within content element.
791 YAHOO.widget.AutoComplete.prototype._elBody = null;
794 * Reference to footer element within content element.
796 * @property _elFooter
800 YAHOO.widget.AutoComplete.prototype._elFooter = null;
803 * Reference to shadow element within container element.
805 * @property _elShadow
809 YAHOO.widget.AutoComplete.prototype._elShadow = null;
812 * Reference to iframe element within container element.
814 * @property _elIFrame
818 YAHOO.widget.AutoComplete.prototype._elIFrame = null;
821 * Whether or not the input field is currently in focus. If query results come back
822 * but the user has already moved on, do not proceed with auto complete behavior.
824 * @property _bFocused
828 YAHOO.widget.AutoComplete.prototype._bFocused = true;
831 * Animation instance for container expand/collapse.
837 YAHOO.widget.AutoComplete.prototype._oAnim = null;
840 * Whether or not the results container is currently open.
842 * @property _bContainerOpen
846 YAHOO.widget.AutoComplete.prototype._bContainerOpen = false;
849 * Whether or not the mouse is currently over the results
850 * container. This is necessary in order to prevent clicks on container items
851 * from being text input field blur events.
853 * @property _bOverContainer
857 YAHOO.widget.AutoComplete.prototype._bOverContainer = false;
860 * Array of <li> elements references that contain query results within the
863 * @property _aListItems
864 * @type HTMLElement[]
867 YAHOO.widget.AutoComplete.prototype._aListItems = null;
870 * Number of <li> elements currently displayed in results container.
872 * @property _nDisplayedItems
876 YAHOO.widget.AutoComplete.prototype._nDisplayedItems = 0;
879 * Internal count of <li> elements displayed and hidden in results container.
881 * @property _maxResultsDisplayed
885 YAHOO.widget.AutoComplete.prototype._maxResultsDisplayed = 0;
888 * Current query string
890 * @property _sCurQuery
894 YAHOO.widget.AutoComplete.prototype._sCurQuery = null;
897 * Past queries this session (for saving delimited queries).
899 * @property _sSavedQuery
903 YAHOO.widget.AutoComplete.prototype._sSavedQuery = null;
906 * Pointer to the currently highlighted <li> element in the container.
908 * @property _oCurItem
912 YAHOO.widget.AutoComplete.prototype._oCurItem = null;
915 * Whether or not an item has been selected since the container was populated
916 * with results. Reset to false by _populateList, and set to true when item is
919 * @property _bItemSelected
923 YAHOO.widget.AutoComplete.prototype._bItemSelected = false;
926 * Key code of the last key pressed in textbox.
928 * @property _nKeyCode
932 YAHOO.widget.AutoComplete.prototype._nKeyCode = null;
937 * @property _nDelayID
941 YAHOO.widget.AutoComplete.prototype._nDelayID = -1;
944 * Src to iFrame used when useIFrame = true. Supports implementations over SSL
947 * @property _iFrameSrc
951 YAHOO.widget.AutoComplete.prototype._iFrameSrc = "javascript:false;";
954 * For users typing via certain IMEs, queries must be triggered by intervals,
955 * since key events yet supported across all browsers for all IMEs.
957 * @property _queryInterval
961 YAHOO.widget.AutoComplete.prototype._queryInterval = null;
964 * Internal tracker to last known textbox value, used to determine whether or not
965 * to trigger a query via interval for certain IME users.
967 * @event _sLastTextboxValue
971 YAHOO.widget.AutoComplete.prototype._sLastTextboxValue = null;
973 /////////////////////////////////////////////////////////////////////////////
977 /////////////////////////////////////////////////////////////////////////////
980 * Updates and validates latest public config properties.
982 * @method __initProps
985 YAHOO.widget.AutoComplete.prototype._initProps = function() {
986 // Correct any invalid values
987 var minQueryLength = this.minQueryLength;
988 if(!YAHOO.lang.isNumber(minQueryLength)) {
989 this.minQueryLength = 1;
991 var maxResultsDisplayed = this.maxResultsDisplayed;
992 if(!YAHOO.lang.isNumber(maxResultsDisplayed) || (maxResultsDisplayed < 1)) {
993 this.maxResultsDisplayed = 10;
995 var queryDelay = this.queryDelay;
996 if(!YAHOO.lang.isNumber(queryDelay) || (queryDelay < 0)) {
997 this.queryDelay = 0.2;
999 var delimChar = this.delimChar;
1000 if(YAHOO.lang.isString(delimChar) && (delimChar.length > 0)) {
1001 this.delimChar = [delimChar];
1003 else if(!YAHOO.lang.isArray(delimChar)) {
1004 this.delimChar = null;
1006 var animSpeed = this.animSpeed;
1007 if((this.animHoriz || this.animVert) && YAHOO.util.Anim) {
1008 if(!YAHOO.lang.isNumber(animSpeed) || (animSpeed < 0)) {
1009 this.animSpeed = 0.3;
1012 this._oAnim = new YAHOO.util.Anim(this._elContent, {}, this.animSpeed);
1015 this._oAnim.duration = this.animSpeed;
1018 if(this.forceSelection && delimChar) {
1019 YAHOO.log("The forceSelection feature has been enabled with delimChar defined.","warn", this.toString());
1024 * Initializes the results container helpers if they are enabled and do
1027 * @method _initContainerHelpers
1030 YAHOO.widget.AutoComplete.prototype._initContainerHelpers = function() {
1031 if(this.useShadow && !this._elShadow) {
1032 var elShadow = document.createElement("div");
1033 elShadow.className = "yui-ac-shadow";
1034 this._elShadow = this._elContainer.appendChild(elShadow);
1036 if(this.useIFrame && !this._elIFrame) {
1037 var elIFrame = document.createElement("iframe");
1038 elIFrame.src = this._iFrameSrc;
1039 elIFrame.frameBorder = 0;
1040 elIFrame.scrolling = "no";
1041 elIFrame.style.position = "absolute";
1042 elIFrame.style.width = "100%";
1043 elIFrame.style.height = "100%";
1044 elIFrame.tabIndex = -1;
1045 this._elIFrame = this._elContainer.appendChild(elIFrame);
1050 * Initializes the results container once at object creation
1052 * @method _initContainer
1055 YAHOO.widget.AutoComplete.prototype._initContainer = function() {
1056 YAHOO.util.Dom.addClass(this._elContainer, "yui-ac-container");
1058 if(!this._elContent) {
1059 // The elContent div helps size the iframe and shadow properly
1060 var elContent = document.createElement("div");
1061 elContent.className = "yui-ac-content";
1062 elContent.style.display = "none";
1063 this._elContent = this._elContainer.appendChild(elContent);
1065 var elHeader = document.createElement("div");
1066 elHeader.className = "yui-ac-hd";
1067 elHeader.style.display = "none";
1068 this._elHeader = this._elContent.appendChild(elHeader);
1070 var elBody = document.createElement("div");
1071 elBody.className = "yui-ac-bd";
1072 this._elBody = this._elContent.appendChild(elBody);
1074 var elFooter = document.createElement("div");
1075 elFooter.className = "yui-ac-ft";
1076 elFooter.style.display = "none";
1077 this._elFooter = this._elContent.appendChild(elFooter);
1080 YAHOO.log("Could not initialize the container","warn",this.toString());
1085 * Clears out contents of container body and creates up to
1086 * YAHOO.widget.AutoComplete#maxResultsDisplayed <li> elements in an
1087 * <ul> element.
1092 YAHOO.widget.AutoComplete.prototype._initList = function() {
1093 this._aListItems = [];
1094 while(this._elBody.hasChildNodes()) {
1095 var oldListItems = this.getListItems();
1097 for(var oldi = oldListItems.length-1; oldi >= 0; oldi--) {
1098 oldListItems[oldi] = null;
1101 this._elBody.innerHTML = "";
1104 var oList = document.createElement("ul");
1105 oList = this._elBody.appendChild(oList);
1106 for(var i=0; i<this.maxResultsDisplayed; i++) {
1107 var oItem = document.createElement("li");
1108 oItem = oList.appendChild(oItem);
1109 this._aListItems[i] = oItem;
1110 this._initListItem(oItem, i);
1112 this._maxResultsDisplayed = this.maxResultsDisplayed;
1116 * Initializes each <li> element in the container list.
1118 * @method _initListItem
1119 * @param oItem {HTMLElement} The <li> DOM element.
1120 * @param nItemIndex {Number} The index of the element.
1123 YAHOO.widget.AutoComplete.prototype._initListItem = function(oItem, nItemIndex) {
1125 oItem.style.display = "none";
1126 oItem._nItemIndex = nItemIndex;
1128 oItem.mouseover = oItem.mouseout = oItem.onclick = null;
1129 YAHOO.util.Event.addListener(oItem,"mouseover",oSelf._onItemMouseover,oSelf);
1130 YAHOO.util.Event.addListener(oItem,"mouseout",oSelf._onItemMouseout,oSelf);
1131 YAHOO.util.Event.addListener(oItem,"click",oSelf._onItemMouseclick,oSelf);
1135 * Enables interval detection for Korean IME support.
1137 * @method _onIMEDetected
1138 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
1141 YAHOO.widget.AutoComplete.prototype._onIMEDetected = function(oSelf) {
1142 oSelf._enableIntervalDetection();
1146 * Enables query triggers based on text input detection by intervals (rather
1147 * than by key events).
1149 * @method _enableIntervalDetection
1152 YAHOO.widget.AutoComplete.prototype._enableIntervalDetection = function() {
1153 var currValue = this._elTextbox.value;
1154 var lastValue = this._sLastTextboxValue;
1155 if(currValue != lastValue) {
1156 this._sLastTextboxValue = currValue;
1157 this._sendQuery(currValue);
1163 * Cancels text input detection by intervals.
1165 * @method _cancelIntervalDetection
1166 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
1169 YAHOO.widget.AutoComplete.prototype._cancelIntervalDetection = function(oSelf) {
1170 if(oSelf._queryInterval) {
1171 clearInterval(oSelf._queryInterval);
1177 * Whether or not key is functional or should be ignored. Note that the right
1178 * arrow key is NOT an ignored key since it triggers queries for certain intl
1181 * @method _isIgnoreKey
1182 * @param nKeycode {Number} Code of key pressed.
1183 * @return {Boolean} True if key should be ignored, false otherwise.
1186 YAHOO.widget.AutoComplete.prototype._isIgnoreKey = function(nKeyCode) {
1187 if((nKeyCode == 9) || (nKeyCode == 13) || // tab, enter
1188 (nKeyCode == 16) || (nKeyCode == 17) || // shift, ctl
1189 (nKeyCode >= 18 && nKeyCode <= 20) || // alt,pause/break,caps lock
1190 (nKeyCode == 27) || // esc
1191 (nKeyCode >= 33 && nKeyCode <= 35) || // page up,page down,end
1192 /*(nKeyCode >= 36 && nKeyCode <= 38) || // home,left,up
1193 (nKeyCode == 40) || // down*/
1194 (nKeyCode >= 36 && nKeyCode <= 40) || // home,left,up, right, down
1195 (nKeyCode >= 44 && nKeyCode <= 45)) { // print screen,insert
1202 * Makes query request to the DataSource.
1204 * @method _sendQuery
1205 * @param sQuery {String} Query string.
1208 YAHOO.widget.AutoComplete.prototype._sendQuery = function(sQuery) {
1209 // Widget has been effectively turned off
1210 if(this.minQueryLength == -1) {
1211 this._toggleContainer(false);
1212 YAHOO.log("Property minQueryLength is set to -1", "info", this.toString());
1215 // Delimiter has been enabled
1216 var aDelimChar = (this.delimChar) ? this.delimChar : null;
1218 // Loop through all possible delimiters and find the latest one
1219 // A " " may be a false positive if they are defined as delimiters AND
1220 // are used to separate delimited queries
1221 var nDelimIndex = -1;
1222 for(var i = aDelimChar.length-1; i >= 0; i--) {
1223 var nNewIndex = sQuery.lastIndexOf(aDelimChar[i]);
1224 if(nNewIndex > nDelimIndex) {
1225 nDelimIndex = nNewIndex;
1228 // If we think the last delimiter is a space (" "), make sure it is NOT
1229 // a false positive by also checking the char directly before it
1230 if(aDelimChar[i] == " ") {
1231 for (var j = aDelimChar.length-1; j >= 0; j--) {
1232 if(sQuery[nDelimIndex - 1] == aDelimChar[j]) {
1238 // A delimiter has been found so extract the latest query
1239 if(nDelimIndex > -1) {
1240 var nQueryStart = nDelimIndex + 1;
1241 // Trim any white space from the beginning...
1242 while(sQuery.charAt(nQueryStart) == " ") {
1245 // ...and save the rest of the string for later
1246 this._sSavedQuery = sQuery.substring(0,nQueryStart);
1247 // Here is the query itself
1248 sQuery = sQuery.substr(nQueryStart);
1250 else if(sQuery.indexOf(this._sSavedQuery) < 0){
1251 this._sSavedQuery = null;
1255 // Don't search queries that are too short
1256 if((sQuery && (sQuery.length < this.minQueryLength)) || (!sQuery && this.minQueryLength > 0)) {
1257 if(this._nDelayID != -1) {
1258 clearTimeout(this._nDelayID);
1260 this._toggleContainer(false);
1261 YAHOO.log("Query \"" + sQuery + "\" is too short", "info", this.toString());
1265 sQuery = encodeURIComponent(sQuery);
1266 this._nDelayID = -1; // Reset timeout ID because request has been made
1267 sQuery = this.doBeforeSendQuery(sQuery);
1268 this.dataRequestEvent.fire(this, sQuery);
1269 YAHOO.log("Sending query \"" + sQuery + "\"", "info", this.toString());
1270 this.dataSource.getResults(this._populateList, sQuery, this);
1274 * Populates the array of <li> elements in the container with query
1275 * results. This method is passed to YAHOO.widget.DataSource#getResults as a
1276 * callback function so results from the DataSource instance are returned to the
1277 * AutoComplete instance.
1279 * @method _populateList
1280 * @param sQuery {String} The query string.
1281 * @param aResults {Object[]} An array of query result objects from the DataSource.
1282 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
1285 YAHOO.widget.AutoComplete.prototype._populateList = function(sQuery, aResults, oSelf) {
1286 if(aResults === null) {
1287 oSelf.dataErrorEvent.fire(oSelf, sQuery);
1289 if(!oSelf._bFocused || !aResults) {
1290 YAHOO.log("Could not populate list", "info", oSelf.toString());
1294 var isOpera = (navigator.userAgent.toLowerCase().indexOf("opera") != -1);
1295 var contentStyle = oSelf._elContent.style;
1296 contentStyle.width = (!isOpera) ? null : "";
1297 contentStyle.height = (!isOpera) ? null : "";
1299 var sCurQuery = decodeURIComponent(sQuery);
1300 oSelf._sCurQuery = sCurQuery;
1301 oSelf._bItemSelected = false;
1303 if(oSelf._maxResultsDisplayed != oSelf.maxResultsDisplayed) {
1307 var nItems = Math.min(aResults.length,oSelf.maxResultsDisplayed);
1308 oSelf._nDisplayedItems = nItems;
1310 oSelf._initContainerHelpers();
1311 var aItems = oSelf._aListItems;
1313 // Fill items with data
1314 for(var i = nItems-1; i >= 0; i--) {
1315 var oItemi = aItems[i];
1316 var oResultItemi = aResults[i];
1317 oItemi.innerHTML = oSelf.formatResult(oResultItemi, sCurQuery);
1318 oItemi.style.display = "list-item";
1319 oItemi._sResultKey = oResultItemi[0];
1320 oItemi._oResultData = oResultItemi;
1324 // Empty out remaining items if any
1325 for(var j = aItems.length-1; j >= nItems ; j--) {
1326 var oItemj = aItems[j];
1327 oItemj.innerHTML = null;
1328 oItemj.style.display = "none";
1329 oItemj._sResultKey = null;
1330 oItemj._oResultData = null;
1333 // Expand the container
1334 var ok = oSelf.doBeforeExpandContainer(oSelf._elTextbox, oSelf._elContainer, sQuery, aResults);
1335 oSelf._toggleContainer(ok);
1337 if(oSelf.autoHighlight) {
1338 // Go to the first item
1339 var oFirstItem = aItems[0];
1340 oSelf._toggleHighlight(oFirstItem,"to");
1341 oSelf.itemArrowToEvent.fire(oSelf, oFirstItem);
1342 YAHOO.log("Arrowed to first item", "info", oSelf.toString());
1343 oSelf._typeAhead(oFirstItem,sQuery);
1346 oSelf._oCurItem = null;
1350 oSelf._toggleContainer(false);
1352 oSelf.dataReturnEvent.fire(oSelf, sQuery, aResults);
1353 YAHOO.log("Container populated with list items", "info", oSelf.toString());
1358 * When forceSelection is true and the user attempts
1359 * leave the text input box without selecting an item from the query results,
1360 * the user selection is cleared.
1362 * @method _clearSelection
1365 YAHOO.widget.AutoComplete.prototype._clearSelection = function() {
1366 var sValue = this._elTextbox.value;
1367 var sChar = (this.delimChar) ? this.delimChar[0] : null;
1368 var nIndex = (sChar) ? sValue.lastIndexOf(sChar, sValue.length-2) : -1;
1370 this._elTextbox.value = sValue.substring(0,nIndex);
1373 this._elTextbox.value = "";
1375 this._sSavedQuery = this._elTextbox.value;
1377 // Fire custom event
1378 this.selectionEnforceEvent.fire(this);
1379 YAHOO.log("Selection enforced", "info", this.toString());
1383 * Whether or not user-typed value in the text input box matches any of the
1386 * @method _textMatchesOption
1387 * @return {HTMLElement} Matching list item element if user-input text matches
1388 * a result, null otherwise.
1391 YAHOO.widget.AutoComplete.prototype._textMatchesOption = function() {
1392 var foundMatch = null;
1394 for(var i = this._nDisplayedItems-1; i >= 0 ; i--) {
1395 var oItem = this._aListItems[i];
1396 var sMatch = oItem._sResultKey.toLowerCase();
1397 if(sMatch == this._sCurQuery.toLowerCase()) {
1406 * Updates in the text input box with the first query result as the user types,
1407 * selecting the substring that the user has not typed.
1409 * @method _typeAhead
1410 * @param oItem {HTMLElement} The <li> element item whose data populates the input field.
1411 * @param sQuery {String} Query string.
1414 YAHOO.widget.AutoComplete.prototype._typeAhead = function(oItem, sQuery) {
1415 // Don't update if turned off
1416 if(!this.typeAhead || (this._nKeyCode == 8)) {
1420 var elTextbox = this._elTextbox;
1421 var sValue = this._elTextbox.value; // any saved queries plus what user has typed
1423 // Don't update with type-ahead if text selection is not supported
1424 if(!elTextbox.setSelectionRange && !elTextbox.createTextRange) {
1428 // Select the portion of text that the user has not typed
1429 var nStart = sValue.length;
1430 this._updateValue(oItem);
1431 var nEnd = elTextbox.value.length;
1432 this._selectText(elTextbox,nStart,nEnd);
1433 var sPrefill = elTextbox.value.substr(nStart,nEnd);
1434 this.typeAheadEvent.fire(this,sQuery,sPrefill);
1435 YAHOO.log("Typeahead occured with prefill string \"" + sPrefill + "\"", "info", this.toString());
1439 * Selects text in the input field.
1441 * @method _selectText
1442 * @param elTextbox {HTMLElement} Text input box element in which to select text.
1443 * @param nStart {Number} Starting index of text string to select.
1444 * @param nEnd {Number} Ending index of text selection.
1447 YAHOO.widget.AutoComplete.prototype._selectText = function(elTextbox, nStart, nEnd) {
1448 if(elTextbox.setSelectionRange) { // For Mozilla
1449 elTextbox.setSelectionRange(nStart,nEnd);
1451 else if(elTextbox.createTextRange) { // For IE
1452 var oTextRange = elTextbox.createTextRange();
1453 oTextRange.moveStart("character", nStart);
1454 oTextRange.moveEnd("character", nEnd-elTextbox.value.length);
1455 oTextRange.select();
1463 * Syncs results container with its helpers.
1465 * @method _toggleContainerHelpers
1466 * @param bShow {Boolean} True if container is expanded, false if collapsed
1469 YAHOO.widget.AutoComplete.prototype._toggleContainerHelpers = function(bShow) {
1470 var bFireEvent = false;
1471 var width = this._elContent.offsetWidth + "px";
1472 var height = this._elContent.offsetHeight + "px";
1474 if(this.useIFrame && this._elIFrame) {
1477 this._elIFrame.style.width = width;
1478 this._elIFrame.style.height = height;
1481 this._elIFrame.style.width = 0;
1482 this._elIFrame.style.height = 0;
1485 if(this.useShadow && this._elShadow) {
1488 this._elShadow.style.width = width;
1489 this._elShadow.style.height = height;
1492 this._elShadow.style.width = 0;
1493 this._elShadow.style.height = 0;
1499 * Animates expansion or collapse of the container.
1501 * @method _toggleContainer
1502 * @param bShow {Boolean} True if container should be expanded, false if container should be collapsed
1505 YAHOO.widget.AutoComplete.prototype._toggleContainer = function(bShow) {
1506 var elContainer = this._elContainer;
1508 // Implementer has container always open so don't mess with it
1509 if(this.alwaysShowContainer && this._bContainerOpen) {
1513 // Clear contents of container
1515 this._elContent.scrollTop = 0;
1516 var aItems = this._aListItems;
1518 if(aItems && (aItems.length > 0)) {
1519 for(var i = aItems.length-1; i >= 0 ; i--) {
1520 aItems[i].style.display = "none";
1524 if(this._oCurItem) {
1525 this._toggleHighlight(this._oCurItem,"from");
1528 this._oCurItem = null;
1529 this._nDisplayedItems = 0;
1530 this._sCurQuery = null;
1533 // Container is already closed
1534 if(!bShow && !this._bContainerOpen) {
1535 this._elContent.style.display = "none";
1539 // If animation is enabled...
1540 var oAnim = this._oAnim;
1541 if(oAnim && oAnim.getEl() && (this.animHoriz || this.animVert)) {
1542 // If helpers need to be collapsed, do it right away...
1543 // but if helpers need to be expanded, wait until after the container expands
1545 this._toggleContainerHelpers(bShow);
1548 if(oAnim.isAnimated()) {
1552 // Clone container to grab current size offscreen
1553 var oClone = this._elContent.cloneNode(true);
1554 elContainer.appendChild(oClone);
1555 oClone.style.top = "-9000px";
1556 oClone.style.display = "block";
1558 // Current size of the container is the EXPANDED size
1559 var wExp = oClone.offsetWidth;
1560 var hExp = oClone.offsetHeight;
1562 // Calculate COLLAPSED sizes based on horiz and vert anim
1563 var wColl = (this.animHoriz) ? 0 : wExp;
1564 var hColl = (this.animVert) ? 0 : hExp;
1566 // Set animation sizes
1567 oAnim.attributes = (bShow) ?
1568 {width: { to: wExp }, height: { to: hExp }} :
1569 {width: { to: wColl}, height: { to: hColl }};
1571 // If opening anew, set to a collapsed size...
1572 if(bShow && !this._bContainerOpen) {
1573 this._elContent.style.width = wColl+"px";
1574 this._elContent.style.height = hColl+"px";
1576 // Else, set it to its last known size.
1578 this._elContent.style.width = wExp+"px";
1579 this._elContent.style.height = hExp+"px";
1582 elContainer.removeChild(oClone);
1586 var onAnimComplete = function() {
1587 // Finish the collapse
1588 oAnim.onComplete.unsubscribeAll();
1591 oSelf.containerExpandEvent.fire(oSelf);
1592 YAHOO.log("Container expanded", "info", oSelf.toString());
1595 oSelf._elContent.style.display = "none";
1596 oSelf.containerCollapseEvent.fire(oSelf);
1597 YAHOO.log("Container collapsed", "info", oSelf.toString());
1599 oSelf._toggleContainerHelpers(bShow);
1602 // Display container and animate it
1603 this._elContent.style.display = "block";
1604 oAnim.onComplete.subscribe(onAnimComplete);
1606 this._bContainerOpen = bShow;
1608 // Else don't animate, just show or hide
1611 this._elContent.style.display = "block";
1612 this.containerExpandEvent.fire(this);
1613 YAHOO.log("Container expanded", "info", this.toString());
1616 this._elContent.style.display = "none";
1617 this.containerCollapseEvent.fire(this);
1618 YAHOO.log("Container collapsed", "info", this.toString());
1620 this._toggleContainerHelpers(bShow);
1621 this._bContainerOpen = bShow;
1627 * Toggles the highlight on or off for an item in the container, and also cleans
1628 * up highlighting of any previous item.
1630 * @method _toggleHighlight
1631 * @param oNewItem {HTMLElement} The <li> element item to receive highlight behavior.
1632 * @param sType {String} Type "mouseover" will toggle highlight on, and "mouseout" will toggle highlight off.
1635 YAHOO.widget.AutoComplete.prototype._toggleHighlight = function(oNewItem, sType) {
1636 var sHighlight = this.highlightClassName;
1637 if(this._oCurItem) {
1638 // Remove highlight from old item
1639 YAHOO.util.Dom.removeClass(this._oCurItem, sHighlight);
1642 if((sType == "to") && sHighlight) {
1643 // Apply highlight to new item
1644 YAHOO.util.Dom.addClass(oNewItem, sHighlight);
1645 this._oCurItem = oNewItem;
1650 * Toggles the pre-highlight on or off for an item in the container.
1652 * @method _togglePrehighlight
1653 * @param oNewItem {HTMLElement} The <li> element item to receive highlight behavior.
1654 * @param sType {String} Type "mouseover" will toggle highlight on, and "mouseout" will toggle highlight off.
1657 YAHOO.widget.AutoComplete.prototype._togglePrehighlight = function(oNewItem, sType) {
1658 if(oNewItem == this._oCurItem) {
1662 var sPrehighlight = this.prehighlightClassName;
1663 if((sType == "mouseover") && sPrehighlight) {
1664 // Apply prehighlight to new item
1665 YAHOO.util.Dom.addClass(oNewItem, sPrehighlight);
1668 // Remove prehighlight from old item
1669 YAHOO.util.Dom.removeClass(oNewItem, sPrehighlight);
1674 * Updates the text input box value with selected query result. If a delimiter
1675 * has been defined, then the value gets appended with the delimiter.
1677 * @method _updateValue
1678 * @param oItem {HTMLElement} The <li> element item with which to update the value.
1681 YAHOO.widget.AutoComplete.prototype._updateValue = function(oItem) {
1682 var elTextbox = this._elTextbox;
1683 var sDelimChar = (this.delimChar) ? (this.delimChar[0] || this.delimChar) : null;
1684 var sSavedQuery = this._sSavedQuery;
1685 var sResultKey = oItem._sResultKey;
1688 // First clear text field
1689 elTextbox.value = "";
1690 // Grab data to put into text field
1693 elTextbox.value = sSavedQuery;
1695 elTextbox.value += sResultKey + sDelimChar;
1696 if(sDelimChar != " ") {
1697 elTextbox.value += " ";
1700 else { elTextbox.value = sResultKey; }
1702 // scroll to bottom of textarea if necessary
1703 if(elTextbox.type == "textarea") {
1704 elTextbox.scrollTop = elTextbox.scrollHeight;
1707 // move cursor to end
1708 var end = elTextbox.value.length;
1709 this._selectText(elTextbox,end,end);
1711 this._oCurItem = oItem;
1715 * Selects a result item from the container
1717 * @method _selectItem
1718 * @param oItem {HTMLElement} The selected <li> element item.
1721 YAHOO.widget.AutoComplete.prototype._selectItem = function(oItem) {
1722 this._bItemSelected = true;
1723 this._updateValue(oItem);
1724 this._cancelIntervalDetection(this);
1725 this.itemSelectEvent.fire(this, oItem, oItem._oResultData);
1726 YAHOO.log("Item selected: " + YAHOO.lang.dump(oItem._oResultData), "info", this.toString());
1727 this._toggleContainer(false);
1731 * If an item is highlighted in the container, the right arrow key jumps to the
1732 * end of the textbox and selects the highlighted item, otherwise the container
1735 * @method _jumpSelection
1738 YAHOO.widget.AutoComplete.prototype._jumpSelection = function() {
1739 if(this._oCurItem) {
1740 this._selectItem(this._oCurItem);
1743 this._toggleContainer(false);
1748 * Triggered by up and down arrow keys, changes the current highlighted
1749 * <li> element item. Scrolls container if necessary.
1751 * @method _moveSelection
1752 * @param nKeyCode {Number} Code of key pressed.
1755 YAHOO.widget.AutoComplete.prototype._moveSelection = function(nKeyCode) {
1756 if(this._bContainerOpen) {
1757 // Determine current item's id number
1758 var oCurItem = this._oCurItem;
1759 var nCurItemIndex = -1;
1762 nCurItemIndex = oCurItem._nItemIndex;
1765 var nNewItemIndex = (nKeyCode == 40) ?
1766 (nCurItemIndex + 1) : (nCurItemIndex - 1);
1769 if(nNewItemIndex < -2 || nNewItemIndex >= this._nDisplayedItems) {
1774 // Unhighlight current item
1775 this._toggleHighlight(oCurItem, "from");
1776 this.itemArrowFromEvent.fire(this, oCurItem);
1777 YAHOO.log("Item arrowed from", "info", this.toString());
1779 if(nNewItemIndex == -1) {
1780 // Go back to query (remove type-ahead string)
1781 if(this.delimChar && this._sSavedQuery) {
1782 if(!this._textMatchesOption()) {
1783 this._elTextbox.value = this._sSavedQuery;
1786 this._elTextbox.value = this._sSavedQuery + this._sCurQuery;
1790 this._elTextbox.value = this._sCurQuery;
1792 this._oCurItem = null;
1795 if(nNewItemIndex == -2) {
1797 this._toggleContainer(false);
1801 var oNewItem = this._aListItems[nNewItemIndex];
1803 // Scroll the container if necessary
1804 var elContent = this._elContent;
1805 var scrollOn = ((YAHOO.util.Dom.getStyle(elContent,"overflow") == "auto") ||
1806 (YAHOO.util.Dom.getStyle(elContent,"overflowY") == "auto"));
1807 if(scrollOn && (nNewItemIndex > -1) &&
1808 (nNewItemIndex < this._nDisplayedItems)) {
1809 // User is keying down
1810 if(nKeyCode == 40) {
1811 // Bottom of selected item is below scroll area...
1812 if((oNewItem.offsetTop+oNewItem.offsetHeight) > (elContent.scrollTop + elContent.offsetHeight)) {
1813 // Set bottom of scroll area to bottom of selected item
1814 elContent.scrollTop = (oNewItem.offsetTop+oNewItem.offsetHeight) - elContent.offsetHeight;
1816 // Bottom of selected item is above scroll area...
1817 else if((oNewItem.offsetTop+oNewItem.offsetHeight) < elContent.scrollTop) {
1818 // Set top of selected item to top of scroll area
1819 elContent.scrollTop = oNewItem.offsetTop;
1823 // User is keying up
1825 // Top of selected item is above scroll area
1826 if(oNewItem.offsetTop < elContent.scrollTop) {
1827 // Set top of scroll area to top of selected item
1828 this._elContent.scrollTop = oNewItem.offsetTop;
1830 // Top of selected item is below scroll area
1831 else if(oNewItem.offsetTop > (elContent.scrollTop + elContent.offsetHeight)) {
1832 // Set bottom of selected item to bottom of scroll area
1833 this._elContent.scrollTop = (oNewItem.offsetTop+oNewItem.offsetHeight) - elContent.offsetHeight;
1838 this._toggleHighlight(oNewItem, "to");
1839 this.itemArrowToEvent.fire(this, oNewItem);
1840 YAHOO.log("Item arrowed to", "info", this.toString());
1841 if(this.typeAhead) {
1842 this._updateValue(oNewItem);
1847 /////////////////////////////////////////////////////////////////////////////
1849 // Private event handlers
1851 /////////////////////////////////////////////////////////////////////////////
1854 * Handles <li> element mouseover events in the container.
1856 * @method _onItemMouseover
1857 * @param v {HTMLEvent} The mouseover event.
1858 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
1861 YAHOO.widget.AutoComplete.prototype._onItemMouseover = function(v,oSelf) {
1862 if(oSelf.prehighlightClassName) {
1863 oSelf._togglePrehighlight(this,"mouseover");
1866 oSelf._toggleHighlight(this,"to");
1869 oSelf.itemMouseOverEvent.fire(oSelf, this);
1870 YAHOO.log("Item moused over", "info", oSelf.toString());
1874 * Handles <li> element mouseout events in the container.
1876 * @method _onItemMouseout
1877 * @param v {HTMLEvent} The mouseout event.
1878 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
1881 YAHOO.widget.AutoComplete.prototype._onItemMouseout = function(v,oSelf) {
1882 if(oSelf.prehighlightClassName) {
1883 oSelf._togglePrehighlight(this,"mouseout");
1886 oSelf._toggleHighlight(this,"from");
1889 oSelf.itemMouseOutEvent.fire(oSelf, this);
1890 YAHOO.log("Item moused out", "info", oSelf.toString());
1894 * Handles <li> element click events in the container.
1896 * @method _onItemMouseclick
1897 * @param v {HTMLEvent} The click event.
1898 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
1901 YAHOO.widget.AutoComplete.prototype._onItemMouseclick = function(v,oSelf) {
1902 // In case item has not been moused over
1903 oSelf._toggleHighlight(this,"to");
1904 oSelf._selectItem(this);
1908 * Handles container mouseover events.
1910 * @method _onContainerMouseover
1911 * @param v {HTMLEvent} The mouseover event.
1912 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
1915 YAHOO.widget.AutoComplete.prototype._onContainerMouseover = function(v,oSelf) {
1916 oSelf._bOverContainer = true;
1920 * Handles container mouseout events.
1922 * @method _onContainerMouseout
1923 * @param v {HTMLEvent} The mouseout event.
1924 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
1927 YAHOO.widget.AutoComplete.prototype._onContainerMouseout = function(v,oSelf) {
1928 oSelf._bOverContainer = false;
1929 // If container is still active
1930 if(oSelf._oCurItem) {
1931 oSelf._toggleHighlight(oSelf._oCurItem,"to");
1936 * Handles container scroll events.
1938 * @method _onContainerScroll
1939 * @param v {HTMLEvent} The scroll event.
1940 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
1943 YAHOO.widget.AutoComplete.prototype._onContainerScroll = function(v,oSelf) {
1944 oSelf._elTextbox.focus();
1948 * Handles container resize events.
1950 * @method _onContainerResize
1951 * @param v {HTMLEvent} The resize event.
1952 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
1955 YAHOO.widget.AutoComplete.prototype._onContainerResize = function(v,oSelf) {
1956 oSelf._toggleContainerHelpers(oSelf._bContainerOpen);
1961 * Handles textbox keydown events of functional keys, mainly for UI behavior.
1963 * @method _onTextboxKeyDown
1964 * @param v {HTMLEvent} The keydown event.
1965 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
1968 YAHOO.widget.AutoComplete.prototype._onTextboxKeyDown = function(v,oSelf) {
1969 var nKeyCode = v.keyCode;
1973 if((navigator.userAgent.toLowerCase().indexOf("mac") == -1)) {
1974 // select an item or clear out
1975 if(oSelf._oCurItem) {
1976 if(oSelf.delimChar && (oSelf._nKeyCode != nKeyCode)) {
1977 if(oSelf._bContainerOpen) {
1978 YAHOO.util.Event.stopEvent(v);
1981 oSelf._selectItem(oSelf._oCurItem);
1984 oSelf._toggleContainer(false);
1989 if((navigator.userAgent.toLowerCase().indexOf("mac") == -1)) {
1990 if(oSelf._oCurItem) {
1991 if(oSelf._nKeyCode != nKeyCode) {
1992 if(oSelf._bContainerOpen) {
1993 YAHOO.util.Event.stopEvent(v);
1996 oSelf._selectItem(oSelf._oCurItem);
1999 oSelf._toggleContainer(false);
2004 oSelf._toggleContainer(false);
2007 oSelf._jumpSelection();
2010 YAHOO.util.Event.stopEvent(v);
2011 oSelf._moveSelection(nKeyCode);
2014 YAHOO.util.Event.stopEvent(v);
2015 oSelf._moveSelection(nKeyCode);
2023 * Handles textbox keypress events.
2024 * @method _onTextboxKeyPress
2025 * @param v {HTMLEvent} The keypress event.
2026 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
2029 YAHOO.widget.AutoComplete.prototype._onTextboxKeyPress = function(v,oSelf) {
2030 var nKeyCode = v.keyCode;
2032 //Expose only to Mac browsers, where stopEvent is ineffective on keydown events (bug 790337)
2033 if((navigator.userAgent.toLowerCase().indexOf("mac") != -1)) {
2036 // select an item or clear out
2037 if(oSelf._oCurItem) {
2038 if(oSelf.delimChar && (oSelf._nKeyCode != nKeyCode)) {
2039 if(oSelf._bContainerOpen) {
2040 YAHOO.util.Event.stopEvent(v);
2043 oSelf._selectItem(oSelf._oCurItem);
2046 oSelf._toggleContainer(false);
2050 if(oSelf._oCurItem) {
2051 if(oSelf._nKeyCode != nKeyCode) {
2052 if(oSelf._bContainerOpen) {
2053 YAHOO.util.Event.stopEvent(v);
2056 oSelf._selectItem(oSelf._oCurItem);
2059 oSelf._toggleContainer(false);
2067 //TODO: (?) limit only to non-IE, non-Mac-FF for Korean IME support (bug 811948)
2068 // Korean IME detected
2069 else if(nKeyCode == 229) {
2070 oSelf._queryInterval = setInterval(function() { oSelf._onIMEDetected(oSelf); },500);
2075 * Handles textbox keyup events that trigger queries.
2077 * @method _onTextboxKeyUp
2078 * @param v {HTMLEvent} The keyup event.
2079 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
2082 YAHOO.widget.AutoComplete.prototype._onTextboxKeyUp = function(v,oSelf) {
2083 // Check to see if any of the public properties have been updated
2086 var nKeyCode = v.keyCode;
2088 oSelf._nKeyCode = nKeyCode;
2089 var sText = this.value; //string in textbox
2091 // Filter out chars that don't trigger queries
2092 if(oSelf._isIgnoreKey(nKeyCode) || (sText.toLowerCase() == oSelf._sCurQuery)) {
2096 oSelf._bItemSelected = false;
2097 YAHOO.util.Dom.removeClass(oSelf._oCurItem, oSelf.highlightClassName);
2098 oSelf._oCurItem = null;
2100 oSelf.textboxKeyEvent.fire(oSelf, nKeyCode);
2101 YAHOO.log("Textbox keyed", "info", oSelf.toString());
2104 // Set timeout on the request
2105 if(oSelf.queryDelay > 0) {
2107 setTimeout(function(){oSelf._sendQuery(sText);},(oSelf.queryDelay * 1000));
2109 if(oSelf._nDelayID != -1) {
2110 clearTimeout(oSelf._nDelayID);
2113 oSelf._nDelayID = nDelayID;
2116 // No delay so send request immediately
2117 oSelf._sendQuery(sText);
2122 * Handles text input box receiving focus.
2124 * @method _onTextboxFocus
2125 * @param v {HTMLEvent} The focus event.
2126 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
2129 YAHOO.widget.AutoComplete.prototype._onTextboxFocus = function (v,oSelf) {
2130 oSelf._elTextbox.setAttribute("autocomplete","off");
2131 oSelf._bFocused = true;
2132 if(!oSelf._bItemSelected) {
2133 oSelf.textboxFocusEvent.fire(oSelf);
2134 YAHOO.log("Textbox focused", "info", oSelf.toString());
2139 * Handles text input box losing focus.
2141 * @method _onTextboxBlur
2142 * @param v {HTMLEvent} The focus event.
2143 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
2146 YAHOO.widget.AutoComplete.prototype._onTextboxBlur = function (v,oSelf) {
2147 // Don't treat as a blur if it was a selection via mouse click
2148 if(!oSelf._bOverContainer || (oSelf._nKeyCode == 9)) {
2149 // Current query needs to be validated as a selection
2150 if(!oSelf._bItemSelected) {
2151 var oMatch = oSelf._textMatchesOption();
2152 // Container is closed or current query doesn't match any result
2153 if(!oSelf._bContainerOpen || (oSelf._bContainerOpen && (oMatch === null))) {
2154 // Force selection is enabled so clear the current query
2155 if(oSelf.forceSelection) {
2156 oSelf._clearSelection();
2158 // Treat current query as a valid selection
2160 oSelf.unmatchedItemSelectEvent.fire(oSelf);
2161 YAHOO.log("Unmatched item selected", "info", oSelf.toString());
2164 // Container is open and current query matches a result
2166 // Force a selection when textbox is blurred with a match
2167 if(oSelf.forceSelection) {
2168 oSelf._selectItem(oMatch);
2173 if(oSelf._bContainerOpen) {
2174 oSelf._toggleContainer(false);
2176 oSelf._cancelIntervalDetection(oSelf);
2177 oSelf._bFocused = false;
2178 oSelf.textboxBlurEvent.fire(oSelf);
2179 YAHOO.log("Textbox blurred", "info", oSelf.toString());
2184 * Handles window unload event.
2186 * @method _onWindowUnload
2187 * @param v {HTMLEvent} The unload event.
2188 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
2191 YAHOO.widget.AutoComplete.prototype._onWindowUnload = function(v,oSelf) {
2192 if(oSelf && oSelf._elTextbox && oSelf.allowBrowserAutocomplete) {
2193 oSelf._elTextbox.setAttribute("autocomplete","on");
2197 /****************************************************************************/
2198 /****************************************************************************/
2199 /****************************************************************************/
2202 * The DataSource classes manages sending a request and returning response from a live
2203 * database. Supported data include local JavaScript arrays and objects and databases
2204 * accessible via XHR connections. Supported response formats include JavaScript arrays,
2205 * JSON, XML, and flat-file textual data.
2210 YAHOO.widget.DataSource = function() {
2211 /* abstract class */
2215 /////////////////////////////////////////////////////////////////////////////
2219 /////////////////////////////////////////////////////////////////////////////
2222 * Error message for null data responses.
2224 * @property ERROR_DATANULL
2229 YAHOO.widget.DataSource.ERROR_DATANULL = "Response data was null";
2232 * Error message for data responses with parsing errors.
2234 * @property ERROR_DATAPARSE
2239 YAHOO.widget.DataSource.ERROR_DATAPARSE = "Response data could not be parsed";
2242 /////////////////////////////////////////////////////////////////////////////
2244 // Public member variables
2246 /////////////////////////////////////////////////////////////////////////////
2249 * Max size of the local cache. Set to 0 to turn off caching. Caching is
2250 * useful to reduce the number of server connections. Recommended only for data
2251 * sources that return comprehensive results for queries or when stale data is
2254 * @property maxCacheEntries
2258 YAHOO.widget.DataSource.prototype.maxCacheEntries = 15;
2261 * Use this to fine-tune the matching algorithm used against JS Array types of
2262 * DataSource and DataSource caches. If queryMatchContains is true, then the JS
2263 * Array or cache returns results that "contain" the query string. By default,
2264 * queryMatchContains is set to false, so that only results that "start with"
2265 * the query string are returned.
2267 * @property queryMatchContains
2271 YAHOO.widget.DataSource.prototype.queryMatchContains = false;
2274 * Enables query subset matching. If caching is on and queryMatchSubset is
2275 * true, substrings of queries will return matching cached results. For
2276 * instance, if the first query is for "abc" susequent queries that start with
2277 * "abc", like "abcd", will be queried against the cache, and not the live data
2278 * source. Recommended only for DataSources that return comprehensive results
2279 * for queries with very few characters.
2281 * @property queryMatchSubset
2286 YAHOO.widget.DataSource.prototype.queryMatchSubset = false;
2289 * Enables case-sensitivity in the matching algorithm used against JS Array
2290 * types of DataSources and DataSource caches. If queryMatchCase is true, only
2291 * case-sensitive matches will return.
2293 * @property queryMatchCase
2297 YAHOO.widget.DataSource.prototype.queryMatchCase = false;
2300 /////////////////////////////////////////////////////////////////////////////
2304 /////////////////////////////////////////////////////////////////////////////
2307 * Public accessor to the unique name of the DataSource instance.
2310 * @return {String} Unique name of the DataSource instance
2312 YAHOO.widget.DataSource.prototype.toString = function() {
2313 return "DataSource " + this._sName;
2317 * Retrieves query results, first checking the local cache, then making the
2318 * query request to the live data source as defined by the function doQuery.
2320 * @method getResults
2321 * @param oCallbackFn {HTMLFunction} Callback function defined by oParent object to which to return results.
2322 * @param sQuery {String} Query string.
2323 * @param oParent {Object} The object instance that has requested data.
2325 YAHOO.widget.DataSource.prototype.getResults = function(oCallbackFn, sQuery, oParent) {
2327 // First look in cache
2328 var aResults = this._doQueryCache(oCallbackFn,sQuery,oParent);
2329 // Not in cache, so get results from server
2330 if(aResults.length === 0) {
2331 this.queryEvent.fire(this, oParent, sQuery);
2332 YAHOO.log("Query received \"" + sQuery, "info", this.toString());
2333 this.doQuery(oCallbackFn, sQuery, oParent);
2338 * Abstract method implemented by subclasses to make a query to the live data
2339 * source. Must call the callback function with the response returned from the
2340 * query. Populates cache (if enabled).
2343 * @param oCallbackFn {HTMLFunction} Callback function implemented by oParent to which to return results.
2344 * @param sQuery {String} Query string.
2345 * @param oParent {Object} The object instance that has requested data.
2347 YAHOO.widget.DataSource.prototype.doQuery = function(oCallbackFn, sQuery, oParent) {
2354 * @method flushCache
2356 YAHOO.widget.DataSource.prototype.flushCache = function() {
2360 if(this._aCacheHelper) {
2361 this._aCacheHelper = [];
2363 this.cacheFlushEvent.fire(this);
2364 YAHOO.log("Cache flushed", "info", this.toString());
2368 /////////////////////////////////////////////////////////////////////////////
2372 /////////////////////////////////////////////////////////////////////////////
2375 * Fired when a query is made to the live data source.
2378 * @param oSelf {Object} The DataSource instance.
2379 * @param oParent {Object} The requesting object.
2380 * @param sQuery {String} The query string.
2382 YAHOO.widget.DataSource.prototype.queryEvent = null;
2385 * Fired when a query is made to the local cache.
2387 * @event cacheQueryEvent
2388 * @param oSelf {Object} The DataSource instance.
2389 * @param oParent {Object} The requesting object.
2390 * @param sQuery {String} The query string.
2392 YAHOO.widget.DataSource.prototype.cacheQueryEvent = null;
2395 * Fired when data is retrieved from the live data source.
2397 * @event getResultsEvent
2398 * @param oSelf {Object} The DataSource instance.
2399 * @param oParent {Object} The requesting object.
2400 * @param sQuery {String} The query string.
2401 * @param aResults {Object[]} Array of result objects.
2403 YAHOO.widget.DataSource.prototype.getResultsEvent = null;
2406 * Fired when data is retrieved from the local cache.
2408 * @event getCachedResultsEvent
2409 * @param oSelf {Object} The DataSource instance.
2410 * @param oParent {Object} The requesting object.
2411 * @param sQuery {String} The query string.
2412 * @param aResults {Object[]} Array of result objects.
2414 YAHOO.widget.DataSource.prototype.getCachedResultsEvent = null;
2417 * Fired when an error is encountered with the live data source.
2419 * @event dataErrorEvent
2420 * @param oSelf {Object} The DataSource instance.
2421 * @param oParent {Object} The requesting object.
2422 * @param sQuery {String} The query string.
2423 * @param sMsg {String} Error message string
2425 YAHOO.widget.DataSource.prototype.dataErrorEvent = null;
2428 * Fired when the local cache is flushed.
2430 * @event cacheFlushEvent
2431 * @param oSelf {Object} The DataSource instance
2433 YAHOO.widget.DataSource.prototype.cacheFlushEvent = null;
2435 /////////////////////////////////////////////////////////////////////////////
2437 // Private member variables
2439 /////////////////////////////////////////////////////////////////////////////
2442 * Internal class variable to index multiple DataSource instances.
2449 YAHOO.widget.DataSource._nIndex = 0;
2452 * Name of DataSource instance.
2458 YAHOO.widget.DataSource.prototype._sName = null;
2461 * Local cache of data result objects indexed chronologically.
2467 YAHOO.widget.DataSource.prototype._aCache = null;
2470 /////////////////////////////////////////////////////////////////////////////
2474 /////////////////////////////////////////////////////////////////////////////
2477 * Initializes DataSource instance.
2482 YAHOO.widget.DataSource.prototype._init = function() {
2483 // Validate and initialize public configs
2484 var maxCacheEntries = this.maxCacheEntries;
2485 if(!YAHOO.lang.isNumber(maxCacheEntries) || (maxCacheEntries < 0)) {
2486 maxCacheEntries = 0;
2488 // Initialize local cache
2489 if(maxCacheEntries > 0 && !this._aCache) {
2493 this._sName = "instance" + YAHOO.widget.DataSource._nIndex;
2494 YAHOO.widget.DataSource._nIndex++;
2496 this.queryEvent = new YAHOO.util.CustomEvent("query", this);
2497 this.cacheQueryEvent = new YAHOO.util.CustomEvent("cacheQuery", this);
2498 this.getResultsEvent = new YAHOO.util.CustomEvent("getResults", this);
2499 this.getCachedResultsEvent = new YAHOO.util.CustomEvent("getCachedResults", this);
2500 this.dataErrorEvent = new YAHOO.util.CustomEvent("dataError", this);
2501 this.cacheFlushEvent = new YAHOO.util.CustomEvent("cacheFlush", this);
2505 * Adds a result object to the local cache, evicting the oldest element if the
2506 * cache is full. Newer items will have higher indexes, the oldest item will have
2509 * @method _addCacheElem
2510 * @param oResult {Object} Data result object, including array of results.
2513 YAHOO.widget.DataSource.prototype._addCacheElem = function(oResult) {
2514 var aCache = this._aCache;
2515 // Don't add if anything important is missing.
2516 if(!aCache || !oResult || !oResult.query || !oResult.results) {
2520 // If the cache is full, make room by removing from index=0
2521 if(aCache.length >= this.maxCacheEntries) {
2525 // Add to cache, at the end of the array
2526 aCache.push(oResult);
2530 * Queries the local cache for results. If query has been cached, the callback
2531 * function is called with the results, and the cached is refreshed so that it
2532 * is now the newest element.
2534 * @method _doQueryCache
2535 * @param oCallbackFn {HTMLFunction} Callback function defined by oParent object to which to return results.
2536 * @param sQuery {String} Query string.
2537 * @param oParent {Object} The object instance that has requested data.
2538 * @return aResults {Object[]} Array of results from local cache if found, otherwise null.
2541 YAHOO.widget.DataSource.prototype._doQueryCache = function(oCallbackFn, sQuery, oParent) {
2543 var bMatchFound = false;
2544 var aCache = this._aCache;
2545 var nCacheLength = (aCache) ? aCache.length : 0;
2546 var bMatchContains = this.queryMatchContains;
2549 // If cache is enabled...
2550 if((this.maxCacheEntries > 0) && aCache && (nCacheLength > 0)) {
2551 this.cacheQueryEvent.fire(this, oParent, sQuery);
2552 YAHOO.log("Querying cache: \"" + sQuery + "\"", "info", this.toString());
2553 // If case is unimportant, normalize query now instead of in loops
2554 if(!this.queryMatchCase) {
2555 sOrigQuery = sQuery;
2556 sQuery = sQuery.toLowerCase();
2559 // Loop through each cached element's query property...
2560 for(var i = nCacheLength-1; i >= 0; i--) {
2561 var resultObj = aCache[i];
2562 var aAllResultItems = resultObj.results;
2563 // If case is unimportant, normalize match key for comparison
2564 var matchKey = (!this.queryMatchCase) ?
2565 encodeURIComponent(resultObj.query).toLowerCase():
2566 encodeURIComponent(resultObj.query);
2568 // If a cached match key exactly matches the query...
2569 if(matchKey == sQuery) {
2570 // Stash all result objects into aResult[] and stop looping through the cache.
2572 aResults = aAllResultItems;
2574 // The matching cache element was not the most recent,
2575 // so now we need to refresh the cache.
2576 if(i != nCacheLength-1) {
2577 // Remove element from its original location
2579 // Add element as newest
2580 this._addCacheElem(resultObj);
2584 // Else if this query is not an exact match and subset matching is enabled...
2585 else if(this.queryMatchSubset) {
2586 // Loop through substrings of each cached element's query property...
2587 for(var j = sQuery.length-1; j >= 0 ; j--) {
2588 var subQuery = sQuery.substr(0,j);
2590 // If a substring of a cached sQuery exactly matches the query...
2591 if(matchKey == subQuery) {
2594 // Go through each cached result object to match against the query...
2595 for(var k = aAllResultItems.length-1; k >= 0; k--) {
2596 var aRecord = aAllResultItems[k];
2597 var sKeyIndex = (this.queryMatchCase) ?
2598 encodeURIComponent(aRecord[0]).indexOf(sQuery):
2599 encodeURIComponent(aRecord[0]).toLowerCase().indexOf(sQuery);
2601 // A STARTSWITH match is when the query is found at the beginning of the key string...
2602 if((!bMatchContains && (sKeyIndex === 0)) ||
2603 // A CONTAINS match is when the query is found anywhere within the key string...
2604 (bMatchContains && (sKeyIndex > -1))) {
2605 // Stash a match into aResults[].
2606 aResults.unshift(aRecord);
2610 // Add the subset match result set object as the newest element to cache,
2611 // and stop looping through the cache.
2613 resultObj.query = sQuery;
2614 resultObj.results = aResults;
2615 this._addCacheElem(resultObj);
2625 // If there was a match, send along the results.
2627 this.getCachedResultsEvent.fire(this, oParent, sOrigQuery, aResults);
2628 YAHOO.log("Cached results found for query \"" + sQuery + "\": " +
2629 YAHOO.lang.dump(aResults), "info", this.toString());
2630 oCallbackFn(sOrigQuery, aResults, oParent);
2637 /****************************************************************************/
2638 /****************************************************************************/
2639 /****************************************************************************/
2642 * Implementation of YAHOO.widget.DataSource using XML HTTP requests that return
2646 * @extends YAHOO.widget.DataSource
2647 * @requires connection
2649 * @param sScriptURI {String} Absolute or relative URI to script that returns query
2650 * results as JSON, XML, or delimited flat-file data.
2651 * @param aSchema {String[]} Data schema definition of results.
2652 * @param oConfigs {Object} (optional) Object literal of config params.
2654 YAHOO.widget.DS_XHR = function(sScriptURI, aSchema, oConfigs) {
2655 // Set any config params passed in to override defaults
2656 if(oConfigs && (oConfigs.constructor == Object)) {
2657 for(var sConfig in oConfigs) {
2658 this[sConfig] = oConfigs[sConfig];
2662 // Initialization sequence
2663 if(!YAHOO.lang.isArray(aSchema) || !YAHOO.lang.isString(sScriptURI)) {
2664 YAHOO.log("Could not instantiate XHR DataSource due to invalid arguments", "error", this.toString());
2668 this.schema = aSchema;
2669 this.scriptURI = sScriptURI;
2672 YAHOO.log("XHR DataSource initialized","info",this.toString());
2675 YAHOO.widget.DS_XHR.prototype = new YAHOO.widget.DataSource();
2677 /////////////////////////////////////////////////////////////////////////////
2681 /////////////////////////////////////////////////////////////////////////////
2686 * @property TYPE_JSON
2691 YAHOO.widget.DS_XHR.TYPE_JSON = 0;
2696 * @property TYPE_XML
2701 YAHOO.widget.DS_XHR.TYPE_XML = 1;
2704 * Flat-file data type.
2706 * @property TYPE_FLAT
2711 YAHOO.widget.DS_XHR.TYPE_FLAT = 2;
2714 * Error message for XHR failure.
2716 * @property ERROR_DATAXHR
2721 YAHOO.widget.DS_XHR.ERROR_DATAXHR = "XHR response failed";
2723 /////////////////////////////////////////////////////////////////////////////
2725 // Public member variables
2727 /////////////////////////////////////////////////////////////////////////////
2730 * Alias to YUI Connection Manager, to allow implementers to customize the utility.
2734 * @default YAHOO.util.Connect
2736 YAHOO.widget.DS_XHR.prototype.connMgr = YAHOO.util.Connect;
2739 * Number of milliseconds the XHR connection will wait for a server response. A
2740 * a value of zero indicates the XHR connection will wait forever. Any value
2741 * greater than zero will use the Connection utility's Auto-Abort feature.
2743 * @property connTimeout
2747 YAHOO.widget.DS_XHR.prototype.connTimeout = 0;
2750 * Absolute or relative URI to script that returns query results. For instance,
2751 * queries will be sent to <scriptURI>?<scriptQueryParam>=userinput
2753 * @property scriptURI
2756 YAHOO.widget.DS_XHR.prototype.scriptURI = null;
2759 * Query string parameter name sent to scriptURI. For instance, queries will be
2760 * sent to <scriptURI>?<scriptQueryParam>=userinput
2762 * @property scriptQueryParam
2766 YAHOO.widget.DS_XHR.prototype.scriptQueryParam = "query";
2769 * String of key/value pairs to append to requests made to scriptURI. Define
2770 * this string when you want to send additional query parameters to your script.
2771 * When defined, queries will be sent to
2772 * <scriptURI>?<scriptQueryParam>=userinput&<scriptQueryAppend>
2774 * @property scriptQueryAppend
2778 YAHOO.widget.DS_XHR.prototype.scriptQueryAppend = "";
2781 * XHR response data type. Other types that may be defined are YAHOO.widget.DS_XHR.TYPE_XML
2782 * and YAHOO.widget.DS_XHR.TYPE_FLAT.
2784 * @property responseType
2786 * @default YAHOO.widget.DS_XHR.TYPE_JSON
2788 YAHOO.widget.DS_XHR.prototype.responseType = YAHOO.widget.DS_XHR.TYPE_JSON;
2791 * String after which to strip results. If the results from the XHR are sent
2792 * back as HTML, the gzip HTML comment appears at the end of the data and should
2795 * @property responseStripAfter
2797 * @default "\n<!-"
2799 YAHOO.widget.DS_XHR.prototype.responseStripAfter = "\n<!-";
2801 /////////////////////////////////////////////////////////////////////////////
2805 /////////////////////////////////////////////////////////////////////////////
2808 * Queries the live data source defined by scriptURI for results. Results are
2809 * passed back to a callback function.
2812 * @param oCallbackFn {HTMLFunction} Callback function defined by oParent object to which to return results.
2813 * @param sQuery {String} Query string.
2814 * @param oParent {Object} The object instance that has requested data.
2816 YAHOO.widget.DS_XHR.prototype.doQuery = function(oCallbackFn, sQuery, oParent) {
2817 var isXML = (this.responseType == YAHOO.widget.DS_XHR.TYPE_XML);
2818 var sUri = this.scriptURI+"?"+this.scriptQueryParam+"="+sQuery;
2819 if(this.scriptQueryAppend.length > 0) {
2820 sUri += "&" + this.scriptQueryAppend;
2822 YAHOO.log("DataSource is querying URL " + sUri, "info", this.toString());
2823 var oResponse = null;
2827 * Sets up ajax request callback
2829 * @param {object} oReq HTTPXMLRequest object
2832 var responseSuccess = function(oResp) {
2833 // Response ID does not match last made request ID.
2834 if(!oSelf._oConn || (oResp.tId != oSelf._oConn.tId)) {
2835 oSelf.dataErrorEvent.fire(oSelf, oParent, sQuery, YAHOO.widget.DataSource.ERROR_DATANULL);
2836 YAHOO.log(YAHOO.widget.DataSource.ERROR_DATANULL, "error", oSelf.toString());
2840 /*YAHOO.log(oResp.responseXML.getElementsByTagName("Result"),'warn');
2841 for(var foo in oResp) {
2842 YAHOO.log(foo + ": "+oResp[foo],'warn');
2844 YAHOO.log('responseXML.xml: '+oResp.responseXML.xml,'warn');*/
2846 oResp = oResp.responseText;
2849 oResp = oResp.responseXML;
2851 if(oResp === null) {
2852 oSelf.dataErrorEvent.fire(oSelf, oParent, sQuery, YAHOO.widget.DataSource.ERROR_DATANULL);
2853 YAHOO.log(YAHOO.widget.DataSource.ERROR_DATANULL, "error", oSelf.toString());
2857 var aResults = oSelf.parseResponse(sQuery, oResp, oParent);
2859 resultObj.query = decodeURIComponent(sQuery);
2860 resultObj.results = aResults;
2861 if(aResults === null) {
2862 oSelf.dataErrorEvent.fire(oSelf, oParent, sQuery, YAHOO.widget.DataSource.ERROR_DATAPARSE);
2863 YAHOO.log(YAHOO.widget.DataSource.ERROR_DATAPARSE, "error", oSelf.toString());
2867 oSelf.getResultsEvent.fire(oSelf, oParent, sQuery, aResults);
2868 YAHOO.log("Results returned for query \"" + sQuery + "\": " +
2869 YAHOO.lang.dump(aResults), "info", oSelf.toString());
2870 oSelf._addCacheElem(resultObj);
2872 oCallbackFn(sQuery, aResults, oParent);
2875 var responseFailure = function(oResp) {
2876 oSelf.dataErrorEvent.fire(oSelf, oParent, sQuery, YAHOO.widget.DS_XHR.ERROR_DATAXHR);
2877 YAHOO.log(YAHOO.widget.DS_XHR.ERROR_DATAXHR + ": " + oResp.statusText, "error", oSelf.toString());
2882 success:responseSuccess,
2883 failure:responseFailure
2886 if(YAHOO.lang.isNumber(this.connTimeout) && (this.connTimeout > 0)) {
2887 oCallback.timeout = this.connTimeout;
2891 this.connMgr.abort(this._oConn);
2894 oSelf._oConn = this.connMgr.asyncRequest("GET", sUri, oCallback, null);
2898 * Parses raw response data into an array of result objects. The result data key
2899 * is always stashed in the [0] element of each result object.
2901 * @method parseResponse
2902 * @param sQuery {String} Query string.
2903 * @param oResponse {Object} The raw response data to parse.
2904 * @param oParent {Object} The object instance that has requested data.
2905 * @returns {Object[]} Array of result objects.
2907 YAHOO.widget.DS_XHR.prototype.parseResponse = function(sQuery, oResponse, oParent) {
2908 var aSchema = this.schema;
2912 // Strip out comment at the end of results
2913 var nEnd = ((this.responseStripAfter !== "") && (oResponse.indexOf)) ?
2914 oResponse.indexOf(this.responseStripAfter) : -1;
2916 oResponse = oResponse.substring(0,nEnd);
2919 switch (this.responseType) {
2920 case YAHOO.widget.DS_XHR.TYPE_JSON:
2921 var jsonList, jsonObjParsed;
2922 // Check for YUI JSON
2923 if(YAHOO.lang.JSON) {
2924 // Use the JSON utility if available
2925 jsonObjParsed = YAHOO.lang.JSON.parse(oResponse);
2926 if(!jsonObjParsed) {
2932 // eval is necessary here since aSchema[0] is of unknown depth
2933 jsonList = eval("jsonObjParsed." + aSchema[0]);
2941 // Check for JSON lib
2942 else if(oResponse.parseJSON) {
2943 // Use the new JSON utility if available
2944 jsonObjParsed = oResponse.parseJSON();
2945 if(!jsonObjParsed) {
2950 // eval is necessary here since aSchema[0] is of unknown depth
2951 jsonList = eval("jsonObjParsed." + aSchema[0]);
2959 // Use older JSON lib if available
2960 else if(window.JSON) {
2961 jsonObjParsed = JSON.parse(oResponse);
2962 if(!jsonObjParsed) {
2968 // eval is necessary here since aSchema[0] is of unknown depth
2969 jsonList = eval("jsonObjParsed." + aSchema[0]);
2978 // Parse the JSON response as a string
2980 // Trim leading spaces
2981 while (oResponse.substring(0,1) == " ") {
2982 oResponse = oResponse.substring(1, oResponse.length);
2985 // Invalid JSON response
2986 if(oResponse.indexOf("{") < 0) {
2991 // Empty (but not invalid) JSON response
2992 if(oResponse.indexOf("{}") === 0) {
2996 // Turn the string into an object literal...
2997 // ...eval is necessary here
2998 var jsonObjRaw = eval("(" + oResponse + ")");
3004 // Grab the object member that contains an array of all reponses...
3005 // ...eval is necessary here since aSchema[0] is of unknown depth
3006 jsonList = eval("(jsonObjRaw." + aSchema[0]+")");
3019 if(!YAHOO.lang.isArray(jsonList)) {
3020 jsonList = [jsonList];
3023 // Loop through the array of all responses...
3024 for(var i = jsonList.length-1; i >= 0 ; i--) {
3025 var aResultItem = [];
3026 var jsonResult = jsonList[i];
3027 // ...and loop through each data field value of each response
3028 for(var j = aSchema.length-1; j >= 1 ; j--) {
3029 // ...and capture data into an array mapped according to the schema...
3030 var dataFieldValue = jsonResult[aSchema[j]];
3031 if(!dataFieldValue) {
3032 dataFieldValue = "";
3034 //YAHOO.log("data: " + i + " value:" +j+" = "+dataFieldValue,"debug",this.toString());
3035 aResultItem.unshift(dataFieldValue);
3037 // If schema isn't well defined, pass along the entire result object
3038 if(aResultItem.length == 1) {
3039 aResultItem.push(jsonResult);
3041 // Capture the array of data field values in an array of results
3042 aResults.unshift(aResultItem);
3045 case YAHOO.widget.DS_XHR.TYPE_XML:
3046 // Get the collection of results
3047 var xmlList = oResponse.getElementsByTagName(aSchema[0]);
3052 // Loop through each result
3053 for(var k = xmlList.length-1; k >= 0 ; k--) {
3054 var result = xmlList.item(k);
3055 //YAHOO.log("Result"+k+" is "+result.attributes.item(0).firstChild.nodeValue,"debug",this.toString());
3057 // Loop through each data field in each result using the schema
3058 for(var m = aSchema.length-1; m >= 1 ; m--) {
3059 //YAHOO.log(aSchema[m]+" is "+result.attributes.getNamedItem(aSchema[m]).firstChild.nodeValue);
3061 // Values may be held in an attribute...
3062 var xmlAttr = result.attributes.getNamedItem(aSchema[m]);
3064 sValue = xmlAttr.value;
3065 //YAHOO.log("Attr value is "+sValue,"debug",this.toString());
3069 var xmlNode = result.getElementsByTagName(aSchema[m]);
3070 if(xmlNode && xmlNode.item(0) && xmlNode.item(0).firstChild) {
3071 sValue = xmlNode.item(0).firstChild.nodeValue;
3072 //YAHOO.log("Node value is "+sValue,"debug",this.toString());
3076 //YAHOO.log("Value not found","debug",this.toString());
3079 // Capture the schema-mapped data field values into an array
3080 aFieldSet.unshift(sValue);
3082 // Capture each array of values into an array of results
3083 aResults.unshift(aFieldSet);
3086 case YAHOO.widget.DS_XHR.TYPE_FLAT:
3087 if(oResponse.length > 0) {
3088 // Delete the last line delimiter at the end of the data if it exists
3089 var newLength = oResponse.length-aSchema[0].length;
3090 if(oResponse.substr(newLength) == aSchema[0]) {
3091 oResponse = oResponse.substr(0, newLength);
3093 if(oResponse.length > 0) {
3094 var aRecords = oResponse.split(aSchema[0]);
3095 for(var n = aRecords.length-1; n >= 0; n--) {
3096 if(aRecords[n].length > 0) {
3097 aResults[n] = aRecords[n].split(aSchema[1]);
3117 /////////////////////////////////////////////////////////////////////////////
3119 // Private member variables
3121 /////////////////////////////////////////////////////////////////////////////
3124 * XHR connection object.
3130 YAHOO.widget.DS_XHR.prototype._oConn = null;
3133 /****************************************************************************/
3134 /****************************************************************************/
3135 /****************************************************************************/
3138 * Implementation of YAHOO.widget.DataSource using the Get Utility to generate
3139 * dynamic SCRIPT nodes for data retrieval.
3141 * @class DS_ScriptNode
3143 * @extends YAHOO.widget.DataSource
3144 * @param sUri {String} URI to the script location that will return data.
3145 * @param aSchema {String[]} Data schema definition of results.
3146 * @param oConfigs {Object} (optional) Object literal of config params.
3148 YAHOO.widget.DS_ScriptNode = function(sUri, aSchema, oConfigs) {
3149 // Set any config params passed in to override defaults
3150 if(oConfigs && (oConfigs.constructor == Object)) {
3151 for(var sConfig in oConfigs) {
3152 this[sConfig] = oConfigs[sConfig];
3156 // Initialization sequence
3157 if(!YAHOO.lang.isArray(aSchema) || !YAHOO.lang.isString(sUri)) {
3158 YAHOO.log("Could not instantiate Script Node DataSource due to invalid arguments", "error", this.toString());
3162 this.schema = aSchema;
3163 this.scriptURI = sUri;
3166 YAHOO.log("Script Node DataSource initialized","info",this.toString());
3169 YAHOO.widget.DS_ScriptNode.prototype = new YAHOO.widget.DataSource();
3171 /////////////////////////////////////////////////////////////////////////////
3173 // Public member variables
3175 /////////////////////////////////////////////////////////////////////////////
3178 * Alias to YUI Get Utility. Allows implementers to specify their own
3179 * subclasses of the YUI Get Utility.
3181 * @property getUtility
3183 * @default YAHOO.util.Get
3185 YAHOO.widget.DS_ScriptNode.prototype.getUtility = YAHOO.util.Get;
3188 * URI to the script that returns data.
3190 * @property scriptURI
3193 YAHOO.widget.DS_ScriptNode.prototype.scriptURI = null;
3196 * Query string parameter name sent to scriptURI. For instance, requests will be
3197 * sent to <scriptURI>?<scriptQueryParam>=queryString
3199 * @property scriptQueryParam
3203 YAHOO.widget.DS_ScriptNode.prototype.scriptQueryParam = "query";
3206 * Defines request/response management in the following manner:
3208 * <!--<dt>queueRequests</dt>
3209 * <dd>If a request is already in progress, wait until response is returned before sending the next request.</dd>
3210 * <dt>cancelStaleRequests</dt>
3211 * <dd>If a request is already in progress, cancel it before sending the next request.</dd>-->
3212 * <dt>ignoreStaleResponses</dt>
3213 * <dd>Send all requests, but handle only the response for the most recently sent request.</dd>
3215 * <dd>Send all requests and handle all responses.</dd>
3218 * @property asyncMode
3220 * @default "allowAll"
3222 YAHOO.widget.DS_ScriptNode.prototype.asyncMode = "allowAll";
3225 * Callback string parameter name sent to scriptURI. For instance, requests will be
3226 * sent to <scriptURI>?<scriptCallbackParam>=callbackFunction
3228 * @property scriptCallbackParam
3230 * @default "callback"
3232 YAHOO.widget.DS_ScriptNode.prototype.scriptCallbackParam = "callback";
3235 * Global array of callback functions, one for each request sent.
3237 * @property callbacks
3241 YAHOO.widget.DS_ScriptNode.callbacks = [];
3243 /////////////////////////////////////////////////////////////////////////////
3245 // Private member variables
3247 /////////////////////////////////////////////////////////////////////////////
3250 * Unique ID to track requests.
3257 YAHOO.widget.DS_ScriptNode._nId = 0;
3260 * Counter for pending requests. When this is 0, it is safe to purge callbacks
3263 * @property _nPending
3268 YAHOO.widget.DS_ScriptNode._nPending = 0;
3270 /////////////////////////////////////////////////////////////////////////////
3274 /////////////////////////////////////////////////////////////////////////////
3277 * Queries the live data source. Results are passed back to a callback function.
3280 * @param oCallbackFn {HTMLFunction} Callback function defined by oParent object to which to return results.
3281 * @param sQuery {String} Query string.
3282 * @param oParent {Object} The object instance that has requested data.
3284 YAHOO.widget.DS_ScriptNode.prototype.doQuery = function(oCallbackFn, sQuery, oParent) {
3287 // If there are no global pending requests, it is safe to purge global callback stack and global counter
3288 if(YAHOO.widget.DS_ScriptNode._nPending === 0) {
3289 YAHOO.widget.DS_ScriptNode.callbacks = [];
3290 YAHOO.widget.DS_ScriptNode._nId = 0;
3293 // ID for this request
3294 var id = YAHOO.widget.DS_ScriptNode._nId;
3295 YAHOO.widget.DS_ScriptNode._nId++;
3297 // Dynamically add handler function with a closure to the callback stack
3298 YAHOO.widget.DS_ScriptNode.callbacks[id] = function(oResponse) {
3299 if((oSelf.asyncMode !== "ignoreStaleResponses")||
3300 (id === YAHOO.widget.DS_ScriptNode.callbacks.length-1)) { // Must ignore stale responses
3301 oSelf.handleResponse(oResponse, oCallbackFn, sQuery, oParent);
3304 YAHOO.log("DataSource ignored stale response for " + sQuery, "info", oSelf.toString());
3307 delete YAHOO.widget.DS_ScriptNode.callbacks[id];
3310 // We are now creating a request
3311 YAHOO.widget.DS_ScriptNode._nPending++;
3313 var sUri = this.scriptURI+"&"+ this.scriptQueryParam+"="+sQuery+"&"+
3314 this.scriptCallbackParam+"=YAHOO.widget.DS_ScriptNode.callbacks["+id+"]";
3315 YAHOO.log("DataSource is querying URL " + sUri, "info", this.toString());
3316 this.getUtility.script(sUri,
3318 onsuccess:YAHOO.widget.DS_ScriptNode._bumpPendingDown,
3319 onfail:YAHOO.widget.DS_ScriptNode._bumpPendingDown});
3323 * Parses JSON response data into an array of result objects and passes it to
3324 * the callback function.
3326 * @method handleResponse
3327 * @param oResponse {Object} The raw response data to parse.
3328 * @param oCallbackFn {HTMLFunction} Callback function defined by oParent object to which to return results.
3329 * @param sQuery {String} Query string.
3330 * @param oParent {Object} The object instance that has requested data.
3332 YAHOO.widget.DS_ScriptNode.prototype.handleResponse = function(oResponse, oCallbackFn, sQuery, oParent) {
3333 var aSchema = this.schema;
3337 var jsonList, jsonObjParsed;
3339 // Parse the JSON response as a string
3341 // Grab the object member that contains an array of all reponses...
3342 // ...eval is necessary here since aSchema[0] is of unknown depth
3343 jsonList = eval("(oResponse." + aSchema[0]+")");
3354 else if(!YAHOO.lang.isArray(jsonList)) {
3355 jsonList = [jsonList];
3358 // Loop through the array of all responses...
3359 for(var i = jsonList.length-1; i >= 0 ; i--) {
3360 var aResultItem = [];
3361 var jsonResult = jsonList[i];
3362 // ...and loop through each data field value of each response
3363 for(var j = aSchema.length-1; j >= 1 ; j--) {
3364 // ...and capture data into an array mapped according to the schema...
3365 var dataFieldValue = jsonResult[aSchema[j]];
3366 if(!dataFieldValue) {
3367 dataFieldValue = "";
3369 //YAHOO.log("data: " + i + " value:" +j+" = "+dataFieldValue,"debug",this.toString());
3370 aResultItem.unshift(dataFieldValue);
3372 // If schema isn't well defined, pass along the entire result object
3373 if(aResultItem.length == 1) {
3374 aResultItem.push(jsonResult);
3376 // Capture the array of data field values in an array of results
3377 aResults.unshift(aResultItem);
3384 if(aResults === null) {
3385 this.dataErrorEvent.fire(this, oParent, sQuery, YAHOO.widget.DataSource.ERROR_DATAPARSE);
3386 YAHOO.log(YAHOO.widget.DataSource.ERROR_DATAPARSE, "error", this.toString());
3391 resultObj.query = decodeURIComponent(sQuery);
3392 resultObj.results = aResults;
3393 this._addCacheElem(resultObj);
3395 this.getResultsEvent.fire(this, oParent, sQuery, aResults);
3396 YAHOO.log("Results returned for query \"" + sQuery + "\": " +
3397 YAHOO.lang.dump(aResults), "info", this.toString());
3400 oCallbackFn(sQuery, aResults, oParent);
3403 /////////////////////////////////////////////////////////////////////////////
3407 /////////////////////////////////////////////////////////////////////////////
3410 * Any success/failure response should decrement counter.
3412 * @method _bumpPendingDown
3415 YAHOO.widget.DS_ScriptNode._bumpPendingDown = function() {
3416 YAHOO.widget.DS_ScriptNode._nPending--;
3420 /****************************************************************************/
3421 /****************************************************************************/
3422 /****************************************************************************/
3425 * Implementation of YAHOO.widget.DataSource using a native Javascript function as
3426 * its live data source.
3428 * @class DS_JSFunction
3430 * @extends YAHOO.widget.DataSource
3431 * @param oFunction {HTMLFunction} In-memory Javascript function that returns query results as an array of objects.
3432 * @param oConfigs {Object} (optional) Object literal of config params.
3434 YAHOO.widget.DS_JSFunction = function(oFunction, oConfigs) {
3435 // Set any config params passed in to override defaults
3436 if(oConfigs && (oConfigs.constructor == Object)) {
3437 for(var sConfig in oConfigs) {
3438 this[sConfig] = oConfigs[sConfig];
3442 // Initialization sequence
3443 if(!YAHOO.lang.isFunction(oFunction)) {
3444 YAHOO.log("Could not instantiate JSFunction DataSource due to invalid arguments", "error", this.toString());
3448 this.dataFunction = oFunction;
3450 YAHOO.log("JS Function DataSource initialized","info",this.toString());
3454 YAHOO.widget.DS_JSFunction.prototype = new YAHOO.widget.DataSource();
3456 /////////////////////////////////////////////////////////////////////////////
3458 // Public member variables
3460 /////////////////////////////////////////////////////////////////////////////
3463 * In-memory Javascript function that returns query results.
3465 * @property dataFunction
3466 * @type HTMLFunction
3468 YAHOO.widget.DS_JSFunction.prototype.dataFunction = null;
3470 /////////////////////////////////////////////////////////////////////////////
3474 /////////////////////////////////////////////////////////////////////////////
3477 * Queries the live data source defined by function for results. Results are
3478 * passed back to a callback function.
3481 * @param oCallbackFn {HTMLFunction} Callback function defined by oParent object to which to return results.
3482 * @param sQuery {String} Query string.
3483 * @param oParent {Object} The object instance that has requested data.
3485 YAHOO.widget.DS_JSFunction.prototype.doQuery = function(oCallbackFn, sQuery, oParent) {
3486 var oFunction = this.dataFunction;
3489 aResults = oFunction(sQuery);
3490 if(aResults === null) {
3491 this.dataErrorEvent.fire(this, oParent, sQuery, YAHOO.widget.DataSource.ERROR_DATANULL);
3492 YAHOO.log(YAHOO.widget.DataSource.ERROR_DATANULL, "error", this.toString());
3497 resultObj.query = decodeURIComponent(sQuery);
3498 resultObj.results = aResults;
3499 this._addCacheElem(resultObj);
3501 this.getResultsEvent.fire(this, oParent, sQuery, aResults);
3502 YAHOO.log("Results returned for query \"" + sQuery +
3503 "\": " + YAHOO.lang.dump(aResults), "info", this.toString());
3504 oCallbackFn(sQuery, aResults, oParent);
3509 /****************************************************************************/
3510 /****************************************************************************/
3511 /****************************************************************************/
3514 * Implementation of YAHOO.widget.DataSource using a native Javascript array as
3515 * its live data source.
3519 * @extends YAHOO.widget.DataSource
3520 * @param aData {String[]} In-memory Javascript array of simple string data.
3521 * @param oConfigs {Object} (optional) Object literal of config params.
3523 YAHOO.widget.DS_JSArray = function(aData, oConfigs) {
3524 // Set any config params passed in to override defaults
3525 if(oConfigs && (oConfigs.constructor == Object)) {
3526 for(var sConfig in oConfigs) {
3527 this[sConfig] = oConfigs[sConfig];
3531 // Initialization sequence
3532 if(!YAHOO.lang.isArray(aData)) {
3533 YAHOO.log("Could not instantiate JSArray DataSource due to invalid arguments", "error", this.toString());
3539 YAHOO.log("JS Array DataSource initialized","info",this.toString());
3543 YAHOO.widget.DS_JSArray.prototype = new YAHOO.widget.DataSource();
3545 /////////////////////////////////////////////////////////////////////////////
3547 // Public member variables
3549 /////////////////////////////////////////////////////////////////////////////
3552 * In-memory Javascript array of strings.
3557 YAHOO.widget.DS_JSArray.prototype.data = null;
3559 /////////////////////////////////////////////////////////////////////////////
3563 /////////////////////////////////////////////////////////////////////////////
3566 * Queries the live data source defined by data for results. Results are passed
3567 * back to a callback function.
3570 * @param oCallbackFn {HTMLFunction} Callback function defined by oParent object to which to return results.
3571 * @param sQuery {String} Query string.
3572 * @param oParent {Object} The object instance that has requested data.
3574 YAHOO.widget.DS_JSArray.prototype.doQuery = function(oCallbackFn, sQuery, oParent) {
3576 var aData = this.data; // the array
3577 var aResults = []; // container for results
3578 var bMatchFound = false;
3579 var bMatchContains = this.queryMatchContains;
3581 if(!this.queryMatchCase) {
3582 sQuery = sQuery.toLowerCase();
3585 // Loop through each element of the array...
3586 // which can be a string or an array of strings
3587 for(i = aData.length-1; i >= 0; i--) {
3590 if(YAHOO.lang.isString(aData[i])) {
3591 aDataset[0] = aData[i];
3593 else if(YAHOO.lang.isArray(aData[i])) {
3594 aDataset = aData[i];
3597 if(YAHOO.lang.isString(aDataset[0])) {
3598 var sKeyIndex = (this.queryMatchCase) ?
3599 encodeURIComponent(aDataset[0]).indexOf(sQuery):
3600 encodeURIComponent(aDataset[0]).toLowerCase().indexOf(sQuery);
3602 // A STARTSWITH match is when the query is found at the beginning of the key string...
3603 if((!bMatchContains && (sKeyIndex === 0)) ||
3604 // A CONTAINS match is when the query is found anywhere within the key string...
3605 (bMatchContains && (sKeyIndex > -1))) {
3606 // Stash a match into aResults[].
3607 aResults.unshift(aDataset);
3613 for(i = aData.length-1; i >= 0; i--) {
3614 if(YAHOO.lang.isString(aData[i])) {
3615 aResults.unshift([aData[i]]);
3617 else if(YAHOO.lang.isArray(aData[i])) {
3618 aResults.unshift(aData[i]);
3623 this.getResultsEvent.fire(this, oParent, sQuery, aResults);
3624 YAHOO.log("Results returned for query \"" + sQuery +
3625 "\": " + YAHOO.lang.dump(aResults), "info", this.toString());
3626 oCallbackFn(sQuery, aResults, oParent);
3629 YAHOO.register("autocomplete", YAHOO.widget.AutoComplete, {version: "2.5.2", build: "1076"});