2 Copyright (c) 2007, 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
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._oTextbox
= document
.getElementById(elInput
);
60 this._sName
= (elInput
.id
) ?
61 "instance" + YAHOO
.widget
.AutoComplete
._nIndex
+ " " + elInput
.id
:
62 "instance" + YAHOO
.widget
.AutoComplete
._nIndex
;
63 this._oTextbox
= elInput
;
65 YAHOO
.util
.Dom
.addClass(this._oTextbox
, "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._oContainer
= document
.getElementById(elContainer
);
78 this._oContainer
= elContainer
;
80 if(this._oContainer
.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._oContainer
.parentNode
;
86 var elTag
= elParent
.tagName
.toLowerCase();
87 while(elParent
&& (elParent
!= "document")) {
89 YAHOO
.util
.Dom
.addClass(elParent
, "yui-ac");
93 elParent
= elParent
.parentNode
;
94 elTag
= elParent
.tagName
.toLowerCase();
98 YAHOO
.log("Could not find an appropriate parent container for skinning", "warn", this.toString());
102 YAHOO
.log("Could not instantiate AutoComplete due to an invalid container element", "error", this.toString());
106 // Set any config params passed in to override defaults
107 if(oConfigs
&& (oConfigs
.constructor == Object
)) {
108 for(var sConfig
in oConfigs
) {
110 this[sConfig
] = oConfigs
[sConfig
];
115 // Initialization sequence
116 this._initContainer();
119 this._initContainerHelpers();
123 var oTextbox
= this._oTextbox
;
124 // Events are actually for the content module within the container
125 var oContent
= this._oContainer
._oContent
;
128 YAHOO
.util
.Event
.addListener(oTextbox
,"keyup",oSelf
._onTextboxKeyUp
,oSelf
);
129 YAHOO
.util
.Event
.addListener(oTextbox
,"keydown",oSelf
._onTextboxKeyDown
,oSelf
);
130 YAHOO
.util
.Event
.addListener(oTextbox
,"focus",oSelf
._onTextboxFocus
,oSelf
);
131 YAHOO
.util
.Event
.addListener(oTextbox
,"blur",oSelf
._onTextboxBlur
,oSelf
);
132 YAHOO
.util
.Event
.addListener(oContent
,"mouseover",oSelf
._onContainerMouseover
,oSelf
);
133 YAHOO
.util
.Event
.addListener(oContent
,"mouseout",oSelf
._onContainerMouseout
,oSelf
);
134 YAHOO
.util
.Event
.addListener(oContent
,"scroll",oSelf
._onContainerScroll
,oSelf
);
135 YAHOO
.util
.Event
.addListener(oContent
,"resize",oSelf
._onContainerResize
,oSelf
);
137 YAHOO
.util
.Event
.addListener(oTextbox
.form
,"submit",oSelf
._onFormSubmit
,oSelf
);
139 YAHOO
.util
.Event
.addListener(oTextbox
,"keypress",oSelf
._onTextboxKeyPress
,oSelf
);
142 this.textboxFocusEvent
= new YAHOO
.util
.CustomEvent("textboxFocus", this);
143 this.textboxKeyEvent
= new YAHOO
.util
.CustomEvent("textboxKey", this);
144 this.dataRequestEvent
= new YAHOO
.util
.CustomEvent("dataRequest", this);
145 this.dataReturnEvent
= new YAHOO
.util
.CustomEvent("dataReturn", this);
146 this.dataErrorEvent
= new YAHOO
.util
.CustomEvent("dataError", this);
147 this.containerExpandEvent
= new YAHOO
.util
.CustomEvent("containerExpand", this);
148 this.typeAheadEvent
= new YAHOO
.util
.CustomEvent("typeAhead", this);
149 this.itemMouseOverEvent
= new YAHOO
.util
.CustomEvent("itemMouseOver", this);
150 this.itemMouseOutEvent
= new YAHOO
.util
.CustomEvent("itemMouseOut", this);
151 this.itemArrowToEvent
= new YAHOO
.util
.CustomEvent("itemArrowTo", this);
152 this.itemArrowFromEvent
= new YAHOO
.util
.CustomEvent("itemArrowFrom", this);
153 this.itemSelectEvent
= new YAHOO
.util
.CustomEvent("itemSelect", this);
154 this.unmatchedItemSelectEvent
= new YAHOO
.util
.CustomEvent("unmatchedItemSelect", this);
155 this.selectionEnforceEvent
= new YAHOO
.util
.CustomEvent("selectionEnforce", this);
156 this.containerCollapseEvent
= new YAHOO
.util
.CustomEvent("containerCollapse", this);
157 this.textboxBlurEvent
= new YAHOO
.util
.CustomEvent("textboxBlur", this);
160 oTextbox
.setAttribute("autocomplete","off");
161 YAHOO
.widget
.AutoComplete
._nIndex
++;
162 YAHOO
.log("AutoComplete initialized","info",this.toString());
164 // Required arguments were not found
166 YAHOO
.log("Could not instantiate AutoComplete due invalid arguments", "error", this.toString());
170 /////////////////////////////////////////////////////////////////////////////
172 // Public member variables
174 /////////////////////////////////////////////////////////////////////////////
177 * The DataSource object that encapsulates the data used for auto completion.
178 * This object should be an inherited object from YAHOO.widget.DataSource.
180 * @property dataSource
181 * @type YAHOO.widget.DataSource
183 YAHOO
.widget
.AutoComplete
.prototype.dataSource
= null;
186 * Number of characters that must be entered before querying for results. A negative value
187 * effectively turns off the widget. A value of 0 allows queries of null or empty string
190 * @property minQueryLength
194 YAHOO
.widget
.AutoComplete
.prototype.minQueryLength
= 1;
197 * Maximum number of results to display in results container.
199 * @property maxResultsDisplayed
203 YAHOO
.widget
.AutoComplete
.prototype.maxResultsDisplayed
= 10;
206 * Number of seconds to delay before submitting a query request. If a query
207 * request is received before a previous one has completed its delay, the
208 * previous request is cancelled and the new request is set to the delay.
209 * Implementers should take care when setting this value very low (i.e., less
210 * than 0.2) with low latency DataSources and the typeAhead feature enabled, as
211 * fast typers may see unexpected behavior.
213 * @property queryDelay
217 YAHOO
.widget
.AutoComplete
.prototype.queryDelay
= 0.2;
220 * Class name of a highlighted item within results container.
222 * @property highlightClassName
224 * @default "yui-ac-highlight"
226 YAHOO
.widget
.AutoComplete
.prototype.highlightClassName
= "yui-ac-highlight";
229 * Class name of a pre-highlighted item within results container.
231 * @property prehighlightClassName
234 YAHOO
.widget
.AutoComplete
.prototype.prehighlightClassName
= null;
237 * Query delimiter. A single character separator for multiple delimited
238 * selections. Multiple delimiter characteres may be defined as an array of
239 * strings. A null value or empty string indicates that query results cannot
240 * be delimited. This feature is not recommended if you need forceSelection to
243 * @property delimChar
244 * @type String | String[]
246 YAHOO
.widget
.AutoComplete
.prototype.delimChar
= null;
249 * Whether or not the first item in results container should be automatically highlighted
252 * @property autoHighlight
256 YAHOO
.widget
.AutoComplete
.prototype.autoHighlight
= true;
259 * Whether or not the input field should be automatically updated
260 * with the first query result as the user types, auto-selecting the substring
261 * that the user has not typed.
263 * @property typeAhead
267 YAHOO
.widget
.AutoComplete
.prototype.typeAhead
= false;
270 * Whether or not to animate the expansion/collapse of the results container in the
271 * horizontal direction.
273 * @property animHoriz
277 YAHOO
.widget
.AutoComplete
.prototype.animHoriz
= false;
280 * Whether or not to animate the expansion/collapse of the results container in the
281 * vertical direction.
287 YAHOO
.widget
.AutoComplete
.prototype.animVert
= true;
290 * Speed of container expand/collapse animation, in seconds..
292 * @property animSpeed
296 YAHOO
.widget
.AutoComplete
.prototype.animSpeed
= 0.3;
299 * Whether or not to force the user's selection to match one of the query
300 * results. Enabling this feature essentially transforms the input field into a
301 * <select> field. This feature is not recommended with delimiter character(s)
304 * @property forceSelection
308 YAHOO
.widget
.AutoComplete
.prototype.forceSelection
= false;
311 * Whether or not to allow browsers to cache user-typed input in the input
312 * field. Disabling this feature will prevent the widget from setting the
313 * autocomplete="off" on the input field. When autocomplete="off"
314 * and users click the back button after form submission, user-typed input can
315 * be prefilled by the browser from its cache. This caching of user input may
316 * not be desired for sensitive data, such as credit card numbers, in which
317 * case, implementers should consider setting allowBrowserAutocomplete to false.
319 * @property allowBrowserAutocomplete
323 YAHOO
.widget
.AutoComplete
.prototype.allowBrowserAutocomplete
= true;
326 * Whether or not the results container should always be displayed.
327 * Enabling this feature displays the container when the widget is instantiated
328 * and prevents the toggling of the container to a collapsed state.
330 * @property alwaysShowContainer
334 YAHOO
.widget
.AutoComplete
.prototype.alwaysShowContainer
= false;
337 * Whether or not to use an iFrame to layer over Windows form elements in
338 * IE. Set to true only when the results container will be on top of a
339 * <select> field in IE and thus exposed to the IE z-index bug (i.e.,
342 * @property useIFrame
346 YAHOO
.widget
.AutoComplete
.prototype.useIFrame
= false;
349 * Whether or not the results container should have a shadow.
351 * @property useShadow
355 YAHOO
.widget
.AutoComplete
.prototype.useShadow
= false;
357 /////////////////////////////////////////////////////////////////////////////
361 /////////////////////////////////////////////////////////////////////////////
364 * Public accessor to the unique name of the AutoComplete instance.
367 * @return {String} Unique name of the AutoComplete instance.
369 YAHOO
.widget
.AutoComplete
.prototype.toString = function() {
370 return "AutoComplete " + this._sName
;
374 * Returns true if container is in an expanded state, false otherwise.
376 * @method isContainerOpen
377 * @return {Boolean} Returns true if container is in an expanded state, false otherwise.
379 YAHOO
.widget
.AutoComplete
.prototype.isContainerOpen = function() {
380 return this._bContainerOpen
;
384 * Public accessor to the internal array of DOM <li> elements that
385 * display query results within the results container.
387 * @method getListItems
388 * @return {HTMLElement[]} Array of <li> elements within the results container.
390 YAHOO
.widget
.AutoComplete
.prototype.getListItems = function() {
391 return this._aListItems
;
395 * Public accessor to the data held in an <li> element of the
398 * @method getListItemData
399 * @return {Object | Object[]} Object or array of result data or null
401 YAHOO
.widget
.AutoComplete
.prototype.getListItemData = function(oListItem
) {
402 if(oListItem
._oResultData
) {
403 return oListItem
._oResultData
;
411 * Sets HTML markup for the results container header. This markup will be
412 * inserted within a <div> tag with a class of "yui-ac-hd".
415 * @param sHeader {String} HTML markup for results container header.
417 YAHOO
.widget
.AutoComplete
.prototype.setHeader = function(sHeader
) {
419 if(this._oContainer
._oContent
._oHeader
) {
420 this._oContainer
._oContent
._oHeader
.innerHTML
= sHeader
;
421 this._oContainer
._oContent
._oHeader
.style
.display
= "block";
425 this._oContainer
._oContent
._oHeader
.innerHTML
= "";
426 this._oContainer
._oContent
._oHeader
.style
.display
= "none";
431 * Sets HTML markup for the results container footer. This markup will be
432 * inserted within a <div> tag with a class of "yui-ac-ft".
435 * @param sFooter {String} HTML markup for results container footer.
437 YAHOO
.widget
.AutoComplete
.prototype.setFooter = function(sFooter
) {
439 if(this._oContainer
._oContent
._oFooter
) {
440 this._oContainer
._oContent
._oFooter
.innerHTML
= sFooter
;
441 this._oContainer
._oContent
._oFooter
.style
.display
= "block";
445 this._oContainer
._oContent
._oFooter
.innerHTML
= "";
446 this._oContainer
._oContent
._oFooter
.style
.display
= "none";
451 * Sets HTML markup for the results container body. This markup will be
452 * inserted within a <div> tag with a class of "yui-ac-bd".
455 * @param sBody {String} HTML markup for results container body.
457 YAHOO
.widget
.AutoComplete
.prototype.setBody = function(sBody
) {
459 if(this._oContainer
._oContent
._oBody
) {
460 this._oContainer
._oContent
._oBody
.innerHTML
= sBody
;
461 this._oContainer
._oContent
._oBody
.style
.display
= "block";
462 this._oContainer
._oContent
.style
.display
= "block";
466 this._oContainer
._oContent
._oBody
.innerHTML
= "";
467 this._oContainer
._oContent
.style
.display
= "none";
469 this._maxResultsDisplayed
= 0;
473 * Overridable method that converts a result item object into HTML markup
474 * for display. Return data values are accessible via the oResultItem object,
475 * and the key return value will always be oResultItem[0]. Markup will be
476 * displayed within <li> element tags in the container.
478 * @method formatResult
479 * @param oResultItem {Object} Result item representing one query result. Data is held in an array.
480 * @param sQuery {String} The current query string.
481 * @return {String} HTML markup of formatted result data.
483 YAHOO
.widget
.AutoComplete
.prototype.formatResult = function(oResultItem
, sQuery
) {
484 var sResult
= oResultItem
[0];
494 * Overridable method called before container expands allows implementers to access data
497 * @method doBeforeExpandContainer
498 * @param oTextbox {HTMLElement} The text input box.
499 * @param oContainer {HTMLElement} The container element.
500 * @param sQuery {String} The query string.
501 * @param aResults {Object[]} An array of query results.
502 * @return {Boolean} Return true to continue expanding container, false to cancel the expand.
504 YAHOO
.widget
.AutoComplete
.prototype.doBeforeExpandContainer = function(oTextbox
, oContainer
, sQuery
, aResults
) {
509 * Makes query request to the DataSource.
512 * @param sQuery {String} Query string.
514 YAHOO
.widget
.AutoComplete
.prototype.sendQuery = function(sQuery
) {
515 this._sendQuery(sQuery
);
519 * Overridable method gives implementers access to the query before it gets sent.
521 * @method doBeforeSendQuery
522 * @param sQuery {String} Query string.
523 * @return {String} Query string.
525 YAHOO
.widget
.AutoComplete
.prototype.doBeforeSendQuery = function(sQuery
) {
530 * Nulls out the entire AutoComplete instance and related objects, removes attached
531 * event listeners, and clears out DOM elements inside the container. After
532 * calling this method, the instance reference should be expliclitly nulled by
533 * implementer, as in myDataTable = null. Use with caution!
537 YAHOO
.widget
.AutoComplete
.prototype.destroy = function() {
538 var instanceName
= this.toString();
539 var elInput
= this._oTextbox
;
540 var elContainer
= this._oContainer
;
542 // Unhook custom events
543 this.textboxFocusEvent
.unsubscribe();
544 this.textboxKeyEvent
.unsubscribe();
545 this.dataRequestEvent
.unsubscribe();
546 this.dataReturnEvent
.unsubscribe();
547 this.dataErrorEvent
.unsubscribe();
548 this.containerExpandEvent
.unsubscribe();
549 this.typeAheadEvent
.unsubscribe();
550 this.itemMouseOverEvent
.unsubscribe();
551 this.itemMouseOutEvent
.unsubscribe();
552 this.itemArrowToEvent
.unsubscribe();
553 this.itemArrowFromEvent
.unsubscribe();
554 this.itemSelectEvent
.unsubscribe();
555 this.unmatchedItemSelectEvent
.unsubscribe();
556 this.selectionEnforceEvent
.unsubscribe();
557 this.containerCollapseEvent
.unsubscribe();
558 this.textboxBlurEvent
.unsubscribe();
561 YAHOO
.util
.Event
.purgeElement(elInput
, true);
562 YAHOO
.util
.Event
.purgeElement(elContainer
, true);
564 // Remove DOM elements
565 elContainer
.innerHTML
= "";
568 for(var key
in this) {
569 if(this.hasOwnProperty(key
)) {
574 YAHOO
.log("AutoComplete instance destroyed: " + instanceName
);
577 /////////////////////////////////////////////////////////////////////////////
581 /////////////////////////////////////////////////////////////////////////////
584 * Fired when the input field receives focus.
586 * @event textboxFocusEvent
587 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
589 YAHOO
.widget
.AutoComplete
.prototype.textboxFocusEvent
= null;
592 * Fired when the input field receives key input.
594 * @event textboxKeyEvent
595 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
596 * @param nKeycode {Number} The keycode number.
598 YAHOO
.widget
.AutoComplete
.prototype.textboxKeyEvent
= null;
601 * Fired when the AutoComplete instance makes a query to the DataSource.
603 * @event dataRequestEvent
604 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
605 * @param sQuery {String} The query string.
607 YAHOO
.widget
.AutoComplete
.prototype.dataRequestEvent
= null;
610 * Fired when the AutoComplete instance receives query results from the data
613 * @event dataReturnEvent
614 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
615 * @param sQuery {String} The query string.
616 * @param aResults {Object[]} Results array.
618 YAHOO
.widget
.AutoComplete
.prototype.dataReturnEvent
= null;
621 * Fired when the AutoComplete instance does not receive query results from the
622 * DataSource due to an error.
624 * @event dataErrorEvent
625 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
626 * @param sQuery {String} The query string.
628 YAHOO
.widget
.AutoComplete
.prototype.dataErrorEvent
= null;
631 * Fired when the results container is expanded.
633 * @event containerExpandEvent
634 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
636 YAHOO
.widget
.AutoComplete
.prototype.containerExpandEvent
= null;
639 * Fired when the input field has been prefilled by the type-ahead
642 * @event typeAheadEvent
643 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
644 * @param sQuery {String} The query string.
645 * @param sPrefill {String} The prefill string.
647 YAHOO
.widget
.AutoComplete
.prototype.typeAheadEvent
= null;
650 * Fired when result item has been moused over.
652 * @event itemMouseOverEvent
653 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
654 * @param elItem {HTMLElement} The <li> element item moused to.
656 YAHOO
.widget
.AutoComplete
.prototype.itemMouseOverEvent
= null;
659 * Fired when result item has been moused out.
661 * @event itemMouseOutEvent
662 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
663 * @param elItem {HTMLElement} The <li> element item moused from.
665 YAHOO
.widget
.AutoComplete
.prototype.itemMouseOutEvent
= null;
668 * Fired when result item has been arrowed to.
670 * @event itemArrowToEvent
671 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
672 * @param elItem {HTMLElement} The <li> element item arrowed to.
674 YAHOO
.widget
.AutoComplete
.prototype.itemArrowToEvent
= null;
677 * Fired when result item has been arrowed away from.
679 * @event itemArrowFromEvent
680 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
681 * @param elItem {HTMLElement} The <li> element item arrowed from.
683 YAHOO
.widget
.AutoComplete
.prototype.itemArrowFromEvent
= null;
686 * Fired when an item is selected via mouse click, ENTER key, or TAB key.
688 * @event itemSelectEvent
689 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
690 * @param elItem {HTMLElement} The selected <li> element item.
691 * @param oData {Object} The data returned for the item, either as an object,
692 * or mapped from the schema into an array.
694 YAHOO
.widget
.AutoComplete
.prototype.itemSelectEvent
= null;
697 * Fired when a user selection does not match any of the displayed result items.
698 * Note that this event may not behave as expected when delimiter characters
701 * @event unmatchedItemSelectEvent
702 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
703 * @param sQuery {String} The user-typed query string.
705 YAHOO
.widget
.AutoComplete
.prototype.unmatchedItemSelectEvent
= null;
708 * Fired if forceSelection is enabled and the user's input has been cleared
709 * because it did not match one of the returned query results.
711 * @event selectionEnforceEvent
712 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
714 YAHOO
.widget
.AutoComplete
.prototype.selectionEnforceEvent
= null;
717 * Fired when the results container is collapsed.
719 * @event containerCollapseEvent
720 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
722 YAHOO
.widget
.AutoComplete
.prototype.containerCollapseEvent
= null;
725 * Fired when the input field loses focus.
727 * @event textboxBlurEvent
728 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
730 YAHOO
.widget
.AutoComplete
.prototype.textboxBlurEvent
= null;
732 /////////////////////////////////////////////////////////////////////////////
734 // Private member variables
736 /////////////////////////////////////////////////////////////////////////////
739 * Internal class variable to index multiple AutoComplete instances.
746 YAHOO
.widget
.AutoComplete
._nIndex
= 0;
749 * Name of AutoComplete instance.
755 YAHOO
.widget
.AutoComplete
.prototype._sName
= null;
758 * Text input field DOM element.
760 * @property _oTextbox
764 YAHOO
.widget
.AutoComplete
.prototype._oTextbox
= null;
767 * Whether or not the input field is currently in focus. If query results come back
768 * but the user has already moved on, do not proceed with auto complete behavior.
770 * @property _bFocused
774 YAHOO
.widget
.AutoComplete
.prototype._bFocused
= true;
777 * Animation instance for container expand/collapse.
783 YAHOO
.widget
.AutoComplete
.prototype._oAnim
= null;
786 * Container DOM element.
788 * @property _oContainer
792 YAHOO
.widget
.AutoComplete
.prototype._oContainer
= null;
795 * Whether or not the results container is currently open.
797 * @property _bContainerOpen
801 YAHOO
.widget
.AutoComplete
.prototype._bContainerOpen
= false;
804 * Whether or not the mouse is currently over the results
805 * container. This is necessary in order to prevent clicks on container items
806 * from being text input field blur events.
808 * @property _bOverContainer
812 YAHOO
.widget
.AutoComplete
.prototype._bOverContainer
= false;
815 * Array of <li> elements references that contain query results within the
818 * @property _aListItems
819 * @type HTMLElement[]
822 YAHOO
.widget
.AutoComplete
.prototype._aListItems
= null;
825 * Number of <li> elements currently displayed in results container.
827 * @property _nDisplayedItems
831 YAHOO
.widget
.AutoComplete
.prototype._nDisplayedItems
= 0;
834 * Internal count of <li> elements displayed and hidden in results container.
836 * @property _maxResultsDisplayed
840 YAHOO
.widget
.AutoComplete
.prototype._maxResultsDisplayed
= 0;
843 * Current query string
845 * @property _sCurQuery
849 YAHOO
.widget
.AutoComplete
.prototype._sCurQuery
= null;
852 * Past queries this session (for saving delimited queries).
854 * @property _sSavedQuery
858 YAHOO
.widget
.AutoComplete
.prototype._sSavedQuery
= null;
861 * Pointer to the currently highlighted <li> element in the container.
863 * @property _oCurItem
867 YAHOO
.widget
.AutoComplete
.prototype._oCurItem
= null;
870 * Whether or not an item has been selected since the container was populated
871 * with results. Reset to false by _populateList, and set to true when item is
874 * @property _bItemSelected
878 YAHOO
.widget
.AutoComplete
.prototype._bItemSelected
= false;
881 * Key code of the last key pressed in textbox.
883 * @property _nKeyCode
887 YAHOO
.widget
.AutoComplete
.prototype._nKeyCode
= null;
892 * @property _nDelayID
896 YAHOO
.widget
.AutoComplete
.prototype._nDelayID
= -1;
899 * Src to iFrame used when useIFrame = true. Supports implementations over SSL
902 * @property _iFrameSrc
906 YAHOO
.widget
.AutoComplete
.prototype._iFrameSrc
= "javascript:false;";
909 * For users typing via certain IMEs, queries must be triggered by intervals,
910 * since key events yet supported across all browsers for all IMEs.
912 * @property _queryInterval
916 YAHOO
.widget
.AutoComplete
.prototype._queryInterval
= null;
919 * Internal tracker to last known textbox value, used to determine whether or not
920 * to trigger a query via interval for certain IME users.
922 * @event _sLastTextboxValue
926 YAHOO
.widget
.AutoComplete
.prototype._sLastTextboxValue
= null;
928 /////////////////////////////////////////////////////////////////////////////
932 /////////////////////////////////////////////////////////////////////////////
935 * Updates and validates latest public config properties.
937 * @method __initProps
940 YAHOO
.widget
.AutoComplete
.prototype._initProps = function() {
941 // Correct any invalid values
942 var minQueryLength
= this.minQueryLength
;
943 if(!YAHOO
.lang
.isNumber(minQueryLength
)) {
944 this.minQueryLength
= 1;
946 var maxResultsDisplayed
= this.maxResultsDisplayed
;
947 if(!YAHOO
.lang
.isNumber(maxResultsDisplayed
) || (maxResultsDisplayed
< 1)) {
948 this.maxResultsDisplayed
= 10;
950 var queryDelay
= this.queryDelay
;
951 if(!YAHOO
.lang
.isNumber(queryDelay
) || (queryDelay
< 0)) {
952 this.queryDelay
= 0.2;
954 var delimChar
= this.delimChar
;
955 if(YAHOO
.lang
.isString(delimChar
)) {
956 this.delimChar
= [delimChar
];
958 else if(!YAHOO
.lang
.isArray(delimChar
)) {
959 this.delimChar
= null;
961 var animSpeed
= this.animSpeed
;
962 if((this.animHoriz
|| this.animVert
) && YAHOO
.util
.Anim
) {
963 if(!YAHOO
.lang
.isNumber(animSpeed
) || (animSpeed
< 0)) {
964 this.animSpeed
= 0.3;
967 this._oAnim
= new YAHOO
.util
.Anim(this._oContainer
._oContent
, {}, this.animSpeed
);
970 this._oAnim
.duration
= this.animSpeed
;
973 if(this.forceSelection
&& delimChar
) {
974 YAHOO
.log("The forceSelection feature has been enabled with delimChar defined.","warn", this.toString());
979 * Initializes the results container helpers if they are enabled and do
982 * @method _initContainerHelpers
985 YAHOO
.widget
.AutoComplete
.prototype._initContainerHelpers = function() {
986 if(this.useShadow
&& !this._oContainer
._oShadow
) {
987 var oShadow
= document
.createElement("div");
988 oShadow
.className
= "yui-ac-shadow";
989 this._oContainer
._oShadow
= this._oContainer
.appendChild(oShadow
);
991 if(this.useIFrame
&& !this._oContainer
._oIFrame
) {
992 var oIFrame
= document
.createElement("iframe");
993 oIFrame
.src
= this._iFrameSrc
;
994 oIFrame
.frameBorder
= 0;
995 oIFrame
.scrolling
= "no";
996 oIFrame
.style
.position
= "absolute";
997 oIFrame
.style
.width
= "100%";
998 oIFrame
.style
.height
= "100%";
999 oIFrame
.tabIndex
= -1;
1000 this._oContainer
._oIFrame
= this._oContainer
.appendChild(oIFrame
);
1005 * Initializes the results container once at object creation
1007 * @method _initContainer
1010 YAHOO
.widget
.AutoComplete
.prototype._initContainer = function() {
1011 YAHOO
.util
.Dom
.addClass(this._oContainer
, "yui-ac-container");
1013 if(!this._oContainer
._oContent
) {
1014 // The oContent div helps size the iframe and shadow properly
1015 var oContent
= document
.createElement("div");
1016 oContent
.className
= "yui-ac-content";
1017 oContent
.style
.display
= "none";
1018 this._oContainer
._oContent
= this._oContainer
.appendChild(oContent
);
1020 var oHeader
= document
.createElement("div");
1021 oHeader
.className
= "yui-ac-hd";
1022 oHeader
.style
.display
= "none";
1023 this._oContainer
._oContent
._oHeader
= this._oContainer
._oContent
.appendChild(oHeader
);
1025 var oBody
= document
.createElement("div");
1026 oBody
.className
= "yui-ac-bd";
1027 this._oContainer
._oContent
._oBody
= this._oContainer
._oContent
.appendChild(oBody
);
1029 var oFooter
= document
.createElement("div");
1030 oFooter
.className
= "yui-ac-ft";
1031 oFooter
.style
.display
= "none";
1032 this._oContainer
._oContent
._oFooter
= this._oContainer
._oContent
.appendChild(oFooter
);
1035 YAHOO
.log("Could not initialize the container","warn",this.toString());
1040 * Clears out contents of container body and creates up to
1041 * YAHOO.widget.AutoComplete#maxResultsDisplayed <li> elements in an
1042 * <ul> element.
1047 YAHOO
.widget
.AutoComplete
.prototype._initList = function() {
1048 this._aListItems
= [];
1049 while(this._oContainer
._oContent
._oBody
.hasChildNodes()) {
1050 var oldListItems
= this.getListItems();
1052 for(var oldi
= oldListItems
.length
-1; oldi
>= 0; oldi
--) {
1053 oldListItems
[oldi
] = null;
1056 this._oContainer
._oContent
._oBody
.innerHTML
= "";
1059 var oList
= document
.createElement("ul");
1060 oList
= this._oContainer
._oContent
._oBody
.appendChild(oList
);
1061 for(var i
=0; i
<this.maxResultsDisplayed
; i
++) {
1062 var oItem
= document
.createElement("li");
1063 oItem
= oList
.appendChild(oItem
);
1064 this._aListItems
[i
] = oItem
;
1065 this._initListItem(oItem
, i
);
1067 this._maxResultsDisplayed
= this.maxResultsDisplayed
;
1071 * Initializes each <li> element in the container list.
1073 * @method _initListItem
1074 * @param oItem {HTMLElement} The <li> DOM element.
1075 * @param nItemIndex {Number} The index of the element.
1078 YAHOO
.widget
.AutoComplete
.prototype._initListItem = function(oItem
, nItemIndex
) {
1080 oItem
.style
.display
= "none";
1081 oItem
._nItemIndex
= nItemIndex
;
1083 oItem
.mouseover
= oItem
.mouseout
= oItem
.onclick
= null;
1084 YAHOO
.util
.Event
.addListener(oItem
,"mouseover",oSelf
._onItemMouseover
,oSelf
);
1085 YAHOO
.util
.Event
.addListener(oItem
,"mouseout",oSelf
._onItemMouseout
,oSelf
);
1086 YAHOO
.util
.Event
.addListener(oItem
,"click",oSelf
._onItemMouseclick
,oSelf
);
1090 * Enables interval detection for Korean IME support.
1092 * @method _onIMEDetected
1093 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
1096 YAHOO
.widget
.AutoComplete
.prototype._onIMEDetected = function(oSelf
) {
1097 oSelf
._enableIntervalDetection();
1101 * Enables query triggers based on text input detection by intervals (rather
1102 * than by key events).
1104 * @method _enableIntervalDetection
1107 YAHOO
.widget
.AutoComplete
.prototype._enableIntervalDetection = function() {
1108 var currValue
= this._oTextbox
.value
;
1109 var lastValue
= this._sLastTextboxValue
;
1110 if(currValue
!= lastValue
) {
1111 this._sLastTextboxValue
= currValue
;
1112 this._sendQuery(currValue
);
1118 * Cancels text input detection by intervals.
1120 * @method _cancelIntervalDetection
1121 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
1124 YAHOO
.widget
.AutoComplete
.prototype._cancelIntervalDetection = function(oSelf
) {
1125 if(oSelf
._queryInterval
) {
1126 clearInterval(oSelf
._queryInterval
);
1132 * Whether or not key is functional or should be ignored. Note that the right
1133 * arrow key is NOT an ignored key since it triggers queries for certain intl
1136 * @method _isIgnoreKey
1137 * @param nKeycode {Number} Code of key pressed.
1138 * @return {Boolean} True if key should be ignored, false otherwise.
1141 YAHOO
.widget
.AutoComplete
.prototype._isIgnoreKey = function(nKeyCode
) {
1142 if((nKeyCode
== 9) || (nKeyCode
== 13) || // tab, enter
1143 (nKeyCode
== 16) || (nKeyCode
== 17) || // shift, ctl
1144 (nKeyCode
>= 18 && nKeyCode
<= 20) || // alt,pause/break,caps lock
1145 (nKeyCode
== 27) || // esc
1146 (nKeyCode
>= 33 && nKeyCode
<= 35) || // page up,page down,end
1147 /*(nKeyCode >= 36 && nKeyCode <= 38) || // home,left,up
1148 (nKeyCode == 40) || // down*/
1149 (nKeyCode
>= 36 && nKeyCode
<= 40) || // home,left,up, right, down
1150 (nKeyCode
>= 44 && nKeyCode
<= 45)) { // print screen,insert
1157 * Makes query request to the DataSource.
1159 * @method _sendQuery
1160 * @param sQuery {String} Query string.
1163 YAHOO
.widget
.AutoComplete
.prototype._sendQuery = function(sQuery
) {
1164 // Widget has been effectively turned off
1165 if(this.minQueryLength
== -1) {
1166 this._toggleContainer(false);
1167 YAHOO
.log("Property minQueryLength is set to -1", "info", this.toString());
1170 // Delimiter has been enabled
1171 var aDelimChar
= (this.delimChar
) ? this.delimChar
: null;
1173 // Loop through all possible delimiters and find the latest one
1174 // A " " may be a false positive if they are defined as delimiters AND
1175 // are used to separate delimited queries
1176 var nDelimIndex
= -1;
1177 for(var i
= aDelimChar
.length
-1; i
>= 0; i
--) {
1178 var nNewIndex
= sQuery
.lastIndexOf(aDelimChar
[i
]);
1179 if(nNewIndex
> nDelimIndex
) {
1180 nDelimIndex
= nNewIndex
;
1183 // If we think the last delimiter is a space (" "), make sure it is NOT
1184 // a false positive by also checking the char directly before it
1185 if(aDelimChar
[i
] == " ") {
1186 for (var j
= aDelimChar
.length
-1; j
>= 0; j
--) {
1187 if(sQuery
[nDelimIndex
- 1] == aDelimChar
[j
]) {
1193 // A delimiter has been found so extract the latest query
1194 if(nDelimIndex
> -1) {
1195 var nQueryStart
= nDelimIndex
+ 1;
1196 // Trim any white space from the beginning...
1197 while(sQuery
.charAt(nQueryStart
) == " ") {
1200 // ...and save the rest of the string for later
1201 this._sSavedQuery
= sQuery
.substring(0,nQueryStart
);
1202 // Here is the query itself
1203 sQuery
= sQuery
.substr(nQueryStart
);
1205 else if(sQuery
.indexOf(this._sSavedQuery
) < 0){
1206 this._sSavedQuery
= null;
1210 // Don't search queries that are too short
1211 if((sQuery
&& (sQuery
.length
< this.minQueryLength
)) || (!sQuery
&& this.minQueryLength
> 0)) {
1212 if(this._nDelayID
!= -1) {
1213 clearTimeout(this._nDelayID
);
1215 this._toggleContainer(false);
1216 YAHOO
.log("Query \"" + sQuery
+ "\" is too short", "info", this.toString());
1220 sQuery
= encodeURIComponent(sQuery
);
1221 this._nDelayID
= -1; // Reset timeout ID because request has been made
1222 sQuery
= this.doBeforeSendQuery(sQuery
);
1223 this.dataRequestEvent
.fire(this, sQuery
);
1224 YAHOO
.log("Sending query \"" + sQuery
+ "\"", "info", this.toString());
1225 this.dataSource
.getResults(this._populateList
, sQuery
, this);
1229 * Populates the array of <li> elements in the container with query
1230 * results. This method is passed to YAHOO.widget.DataSource#getResults as a
1231 * callback function so results from the DataSource instance are returned to the
1232 * AutoComplete instance.
1234 * @method _populateList
1235 * @param sQuery {String} The query string.
1236 * @param aResults {Object[]} An array of query result objects from the DataSource.
1237 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
1240 YAHOO
.widget
.AutoComplete
.prototype._populateList = function(sQuery
, aResults
, oSelf
) {
1241 if(aResults
=== null) {
1242 oSelf
.dataErrorEvent
.fire(oSelf
, sQuery
);
1244 if(!oSelf
._bFocused
|| !aResults
) {
1245 YAHOO
.log("Could not populate list", "info", oSelf
.toString());
1249 var isOpera
= (navigator
.userAgent
.toLowerCase().indexOf("opera") != -1);
1250 var contentStyle
= oSelf
._oContainer
._oContent
.style
;
1251 contentStyle
.width
= (!isOpera
) ? null : "";
1252 contentStyle
.height
= (!isOpera
) ? null : "";
1254 var sCurQuery
= decodeURIComponent(sQuery
);
1255 oSelf
._sCurQuery
= sCurQuery
;
1256 oSelf
._bItemSelected
= false;
1258 if(oSelf
._maxResultsDisplayed
!= oSelf
.maxResultsDisplayed
) {
1262 var nItems
= Math
.min(aResults
.length
,oSelf
.maxResultsDisplayed
);
1263 oSelf
._nDisplayedItems
= nItems
;
1265 oSelf
._initContainerHelpers();
1266 var aItems
= oSelf
._aListItems
;
1268 // Fill items with data
1269 for(var i
= nItems
-1; i
>= 0; i
--) {
1270 var oItemi
= aItems
[i
];
1271 var oResultItemi
= aResults
[i
];
1272 oItemi
.innerHTML
= oSelf
.formatResult(oResultItemi
, sCurQuery
);
1273 oItemi
.style
.display
= "list-item";
1274 oItemi
._sResultKey
= oResultItemi
[0];
1275 oItemi
._oResultData
= oResultItemi
;
1279 // Empty out remaining items if any
1280 for(var j
= aItems
.length
-1; j
>= nItems
; j
--) {
1281 var oItemj
= aItems
[j
];
1282 oItemj
.innerHTML
= null;
1283 oItemj
.style
.display
= "none";
1284 oItemj
._sResultKey
= null;
1285 oItemj
._oResultData
= null;
1288 // Expand the container
1289 var ok
= oSelf
.doBeforeExpandContainer(oSelf
._oTextbox
, oSelf
._oContainer
, sQuery
, aResults
);
1290 oSelf
._toggleContainer(ok
);
1292 if(oSelf
.autoHighlight
) {
1293 // Go to the first item
1294 var oFirstItem
= aItems
[0];
1295 oSelf
._toggleHighlight(oFirstItem
,"to");
1296 oSelf
.itemArrowToEvent
.fire(oSelf
, oFirstItem
);
1297 YAHOO
.log("Arrowed to first item", "info", oSelf
.toString());
1298 oSelf
._typeAhead(oFirstItem
,sQuery
);
1301 oSelf
._oCurItem
= null;
1305 oSelf
._toggleContainer(false);
1307 oSelf
.dataReturnEvent
.fire(oSelf
, sQuery
, aResults
);
1308 YAHOO
.log("Container populated with list items", "info", oSelf
.toString());
1313 * When forceSelection is true and the user attempts
1314 * leave the text input box without selecting an item from the query results,
1315 * the user selection is cleared.
1317 * @method _clearSelection
1320 YAHOO
.widget
.AutoComplete
.prototype._clearSelection = function() {
1321 var sValue
= this._oTextbox
.value
;
1322 var sChar
= (this.delimChar
) ? this.delimChar
[0] : null;
1323 var nIndex
= (sChar
) ? sValue
.lastIndexOf(sChar
, sValue
.length
-2) : -1;
1325 this._oTextbox
.value
= sValue
.substring(0,nIndex
);
1328 this._oTextbox
.value
= "";
1330 this._sSavedQuery
= this._oTextbox
.value
;
1332 // Fire custom event
1333 this.selectionEnforceEvent
.fire(this);
1334 YAHOO
.log("Selection enforced", "info", this.toString());
1338 * Whether or not user-typed value in the text input box matches any of the
1341 * @method _textMatchesOption
1342 * @return {HTMLElement} Matching list item element if user-input text matches
1343 * a result, null otherwise.
1346 YAHOO
.widget
.AutoComplete
.prototype._textMatchesOption = function() {
1347 var foundMatch
= null;
1349 for(var i
= this._nDisplayedItems
-1; i
>= 0 ; i
--) {
1350 var oItem
= this._aListItems
[i
];
1351 var sMatch
= oItem
._sResultKey
.toLowerCase();
1352 if(sMatch
== this._sCurQuery
.toLowerCase()) {
1361 * Updates in the text input box with the first query result as the user types,
1362 * selecting the substring that the user has not typed.
1364 * @method _typeAhead
1365 * @param oItem {HTMLElement} The <li> element item whose data populates the input field.
1366 * @param sQuery {String} Query string.
1369 YAHOO
.widget
.AutoComplete
.prototype._typeAhead = function(oItem
, sQuery
) {
1370 // Don't update if turned off
1371 if(!this.typeAhead
|| (this._nKeyCode
== 8)) {
1375 var oTextbox
= this._oTextbox
;
1376 var sValue
= this._oTextbox
.value
; // any saved queries plus what user has typed
1378 // Don't update with type-ahead if text selection is not supported
1379 if(!oTextbox
.setSelectionRange
&& !oTextbox
.createTextRange
) {
1383 // Select the portion of text that the user has not typed
1384 var nStart
= sValue
.length
;
1385 this._updateValue(oItem
);
1386 var nEnd
= oTextbox
.value
.length
;
1387 this._selectText(oTextbox
,nStart
,nEnd
);
1388 var sPrefill
= oTextbox
.value
.substr(nStart
,nEnd
);
1389 this.typeAheadEvent
.fire(this,sQuery
,sPrefill
);
1390 YAHOO
.log("Typeahead occured with prefill string \"" + sPrefill
+ "\"", "info", this.toString());
1394 * Selects text in the input field.
1396 * @method _selectText
1397 * @param oTextbox {HTMLElement} Text input box element in which to select text.
1398 * @param nStart {Number} Starting index of text string to select.
1399 * @param nEnd {Number} Ending index of text selection.
1402 YAHOO
.widget
.AutoComplete
.prototype._selectText = function(oTextbox
, nStart
, nEnd
) {
1403 if(oTextbox
.setSelectionRange
) { // For Mozilla
1404 oTextbox
.setSelectionRange(nStart
,nEnd
);
1406 else if(oTextbox
.createTextRange
) { // For IE
1407 var oTextRange
= oTextbox
.createTextRange();
1408 oTextRange
.moveStart("character", nStart
);
1409 oTextRange
.moveEnd("character", nEnd
-oTextbox
.value
.length
);
1410 oTextRange
.select();
1418 * Syncs results container with its helpers.
1420 * @method _toggleContainerHelpers
1421 * @param bShow {Boolean} True if container is expanded, false if collapsed
1424 YAHOO
.widget
.AutoComplete
.prototype._toggleContainerHelpers = function(bShow
) {
1425 var bFireEvent
= false;
1426 var width
= this._oContainer
._oContent
.offsetWidth
+ "px";
1427 var height
= this._oContainer
._oContent
.offsetHeight
+ "px";
1429 if(this.useIFrame
&& this._oContainer
._oIFrame
) {
1432 this._oContainer
._oIFrame
.style
.width
= width
;
1433 this._oContainer
._oIFrame
.style
.height
= height
;
1436 this._oContainer
._oIFrame
.style
.width
= 0;
1437 this._oContainer
._oIFrame
.style
.height
= 0;
1440 if(this.useShadow
&& this._oContainer
._oShadow
) {
1443 this._oContainer
._oShadow
.style
.width
= width
;
1444 this._oContainer
._oShadow
.style
.height
= height
;
1447 this._oContainer
._oShadow
.style
.width
= 0;
1448 this._oContainer
._oShadow
.style
.height
= 0;
1454 * Animates expansion or collapse of the container.
1456 * @method _toggleContainer
1457 * @param bShow {Boolean} True if container should be expanded, false if container should be collapsed
1460 YAHOO
.widget
.AutoComplete
.prototype._toggleContainer = function(bShow
) {
1461 var oContainer
= this._oContainer
;
1463 // Implementer has container always open so don't mess with it
1464 if(this.alwaysShowContainer
&& this._bContainerOpen
) {
1468 // Clear contents of container
1470 this._oContainer
._oContent
.scrollTop
= 0;
1471 var aItems
= this._aListItems
;
1473 if(aItems
&& (aItems
.length
> 0)) {
1474 for(var i
= aItems
.length
-1; i
>= 0 ; i
--) {
1475 aItems
[i
].style
.display
= "none";
1479 if(this._oCurItem
) {
1480 this._toggleHighlight(this._oCurItem
,"from");
1483 this._oCurItem
= null;
1484 this._nDisplayedItems
= 0;
1485 this._sCurQuery
= null;
1488 // Container is already closed
1489 if(!bShow
&& !this._bContainerOpen
) {
1490 oContainer
._oContent
.style
.display
= "none";
1494 // If animation is enabled...
1495 var oAnim
= this._oAnim
;
1496 if(oAnim
&& oAnim
.getEl() && (this.animHoriz
|| this.animVert
)) {
1497 // If helpers need to be collapsed, do it right away...
1498 // but if helpers need to be expanded, wait until after the container expands
1500 this._toggleContainerHelpers(bShow
);
1503 if(oAnim
.isAnimated()) {
1507 // Clone container to grab current size offscreen
1508 var oClone
= oContainer
._oContent
.cloneNode(true);
1509 oContainer
.appendChild(oClone
);
1510 oClone
.style
.top
= "-9000px";
1511 oClone
.style
.display
= "block";
1513 // Current size of the container is the EXPANDED size
1514 var wExp
= oClone
.offsetWidth
;
1515 var hExp
= oClone
.offsetHeight
;
1517 // Calculate COLLAPSED sizes based on horiz and vert anim
1518 var wColl
= (this.animHoriz
) ? 0 : wExp
;
1519 var hColl
= (this.animVert
) ? 0 : hExp
;
1521 // Set animation sizes
1522 oAnim
.attributes
= (bShow
) ?
1523 {width
: { to
: wExp
}, height
: { to
: hExp
}} :
1524 {width
: { to
: wColl
}, height
: { to
: hColl
}};
1526 // If opening anew, set to a collapsed size...
1527 if(bShow
&& !this._bContainerOpen
) {
1528 oContainer
._oContent
.style
.width
= wColl
+"px";
1529 oContainer
._oContent
.style
.height
= hColl
+"px";
1531 // Else, set it to its last known size.
1533 oContainer
._oContent
.style
.width
= wExp
+"px";
1534 oContainer
._oContent
.style
.height
= hExp
+"px";
1537 oContainer
.removeChild(oClone
);
1541 var onAnimComplete = function() {
1542 // Finish the collapse
1543 oAnim
.onComplete
.unsubscribeAll();
1546 oSelf
.containerExpandEvent
.fire(oSelf
);
1547 YAHOO
.log("Container expanded", "info", oSelf
.toString());
1550 oContainer
._oContent
.style
.display
= "none";
1551 oSelf
.containerCollapseEvent
.fire(oSelf
);
1552 YAHOO
.log("Container collapsed", "info", oSelf
.toString());
1554 oSelf
._toggleContainerHelpers(bShow
);
1557 // Display container and animate it
1558 oContainer
._oContent
.style
.display
= "block";
1559 oAnim
.onComplete
.subscribe(onAnimComplete
);
1561 this._bContainerOpen
= bShow
;
1563 // Else don't animate, just show or hide
1566 oContainer
._oContent
.style
.display
= "block";
1567 this.containerExpandEvent
.fire(this);
1568 YAHOO
.log("Container expanded", "info", this.toString());
1571 oContainer
._oContent
.style
.display
= "none";
1572 this.containerCollapseEvent
.fire(this);
1573 YAHOO
.log("Container collapsed", "info", this.toString());
1575 this._toggleContainerHelpers(bShow
);
1576 this._bContainerOpen
= bShow
;
1582 * Toggles the highlight on or off for an item in the container, and also cleans
1583 * up highlighting of any previous item.
1585 * @method _toggleHighlight
1586 * @param oNewItem {HTMLElement} The <li> element item to receive highlight behavior.
1587 * @param sType {String} Type "mouseover" will toggle highlight on, and "mouseout" will toggle highlight off.
1590 YAHOO
.widget
.AutoComplete
.prototype._toggleHighlight = function(oNewItem
, sType
) {
1591 var sHighlight
= this.highlightClassName
;
1592 if(this._oCurItem
) {
1593 // Remove highlight from old item
1594 YAHOO
.util
.Dom
.removeClass(this._oCurItem
, sHighlight
);
1597 if((sType
== "to") && sHighlight
) {
1598 // Apply highlight to new item
1599 YAHOO
.util
.Dom
.addClass(oNewItem
, sHighlight
);
1600 this._oCurItem
= oNewItem
;
1605 * Toggles the pre-highlight on or off for an item in the container.
1607 * @method _togglePrehighlight
1608 * @param oNewItem {HTMLElement} The <li> element item to receive highlight behavior.
1609 * @param sType {String} Type "mouseover" will toggle highlight on, and "mouseout" will toggle highlight off.
1612 YAHOO
.widget
.AutoComplete
.prototype._togglePrehighlight = function(oNewItem
, sType
) {
1613 if(oNewItem
== this._oCurItem
) {
1617 var sPrehighlight
= this.prehighlightClassName
;
1618 if((sType
== "mouseover") && sPrehighlight
) {
1619 // Apply prehighlight to new item
1620 YAHOO
.util
.Dom
.addClass(oNewItem
, sPrehighlight
);
1623 // Remove prehighlight from old item
1624 YAHOO
.util
.Dom
.removeClass(oNewItem
, sPrehighlight
);
1629 * Updates the text input box value with selected query result. If a delimiter
1630 * has been defined, then the value gets appended with the delimiter.
1632 * @method _updateValue
1633 * @param oItem {HTMLElement} The <li> element item with which to update the value.
1636 YAHOO
.widget
.AutoComplete
.prototype._updateValue = function(oItem
) {
1637 var oTextbox
= this._oTextbox
;
1638 var sDelimChar
= (this.delimChar
) ? (this.delimChar
[0] || this.delimChar
) : null;
1639 var sSavedQuery
= this._sSavedQuery
;
1640 var sResultKey
= oItem
._sResultKey
;
1643 // First clear text field
1644 oTextbox
.value
= "";
1645 // Grab data to put into text field
1648 oTextbox
.value
= sSavedQuery
;
1650 oTextbox
.value
+= sResultKey
+ sDelimChar
;
1651 if(sDelimChar
!= " ") {
1652 oTextbox
.value
+= " ";
1655 else { oTextbox
.value
= sResultKey
; }
1657 // scroll to bottom of textarea if necessary
1658 if(oTextbox
.type
== "textarea") {
1659 oTextbox
.scrollTop
= oTextbox
.scrollHeight
;
1662 // move cursor to end
1663 var end
= oTextbox
.value
.length
;
1664 this._selectText(oTextbox
,end
,end
);
1666 this._oCurItem
= oItem
;
1670 * Selects a result item from the container
1672 * @method _selectItem
1673 * @param oItem {HTMLElement} The selected <li> element item.
1676 YAHOO
.widget
.AutoComplete
.prototype._selectItem = function(oItem
) {
1677 this._bItemSelected
= true;
1678 this._updateValue(oItem
);
1679 this._cancelIntervalDetection(this);
1680 this.itemSelectEvent
.fire(this, oItem
, oItem
._oResultData
);
1681 YAHOO
.log("Item selected: " + YAHOO
.lang
.dump(oItem
._oResultData
), "info", this.toString());
1682 this._toggleContainer(false);
1686 * If an item is highlighted in the container, the right arrow key jumps to the
1687 * end of the textbox and selects the highlighted item, otherwise the container
1690 * @method _jumpSelection
1693 YAHOO
.widget
.AutoComplete
.prototype._jumpSelection = function() {
1694 if(this._oCurItem
) {
1695 this._selectItem(this._oCurItem
);
1698 this._toggleContainer(false);
1703 * Triggered by up and down arrow keys, changes the current highlighted
1704 * <li> element item. Scrolls container if necessary.
1706 * @method _moveSelection
1707 * @param nKeyCode {Number} Code of key pressed.
1710 YAHOO
.widget
.AutoComplete
.prototype._moveSelection = function(nKeyCode
) {
1711 if(this._bContainerOpen
) {
1712 // Determine current item's id number
1713 var oCurItem
= this._oCurItem
;
1714 var nCurItemIndex
= -1;
1717 nCurItemIndex
= oCurItem
._nItemIndex
;
1720 var nNewItemIndex
= (nKeyCode
== 40) ?
1721 (nCurItemIndex
+ 1) : (nCurItemIndex
- 1);
1724 if(nNewItemIndex
< -2 || nNewItemIndex
>= this._nDisplayedItems
) {
1729 // Unhighlight current item
1730 this._toggleHighlight(oCurItem
, "from");
1731 this.itemArrowFromEvent
.fire(this, oCurItem
);
1732 YAHOO
.log("Item arrowed from", "info", this.toString());
1734 if(nNewItemIndex
== -1) {
1735 // Go back to query (remove type-ahead string)
1736 if(this.delimChar
&& this._sSavedQuery
) {
1737 if(!this._textMatchesOption()) {
1738 this._oTextbox
.value
= this._sSavedQuery
;
1741 this._oTextbox
.value
= this._sSavedQuery
+ this._sCurQuery
;
1745 this._oTextbox
.value
= this._sCurQuery
;
1747 this._oCurItem
= null;
1750 if(nNewItemIndex
== -2) {
1752 this._toggleContainer(false);
1756 var oNewItem
= this._aListItems
[nNewItemIndex
];
1758 // Scroll the container if necessary
1759 var oContent
= this._oContainer
._oContent
;
1760 var scrollOn
= ((YAHOO
.util
.Dom
.getStyle(oContent
,"overflow") == "auto") ||
1761 (YAHOO
.util
.Dom
.getStyle(oContent
,"overflowY") == "auto"));
1762 if(scrollOn
&& (nNewItemIndex
> -1) &&
1763 (nNewItemIndex
< this._nDisplayedItems
)) {
1764 // User is keying down
1765 if(nKeyCode
== 40) {
1766 // Bottom of selected item is below scroll area...
1767 if((oNewItem
.offsetTop
+oNewItem
.offsetHeight
) > (oContent
.scrollTop
+ oContent
.offsetHeight
)) {
1768 // Set bottom of scroll area to bottom of selected item
1769 oContent
.scrollTop
= (oNewItem
.offsetTop
+oNewItem
.offsetHeight
) - oContent
.offsetHeight
;
1771 // Bottom of selected item is above scroll area...
1772 else if((oNewItem
.offsetTop
+oNewItem
.offsetHeight
) < oContent
.scrollTop
) {
1773 // Set top of selected item to top of scroll area
1774 oContent
.scrollTop
= oNewItem
.offsetTop
;
1778 // User is keying up
1780 // Top of selected item is above scroll area
1781 if(oNewItem
.offsetTop
< oContent
.scrollTop
) {
1782 // Set top of scroll area to top of selected item
1783 this._oContainer
._oContent
.scrollTop
= oNewItem
.offsetTop
;
1785 // Top of selected item is below scroll area
1786 else if(oNewItem
.offsetTop
> (oContent
.scrollTop
+ oContent
.offsetHeight
)) {
1787 // Set bottom of selected item to bottom of scroll area
1788 this._oContainer
._oContent
.scrollTop
= (oNewItem
.offsetTop
+oNewItem
.offsetHeight
) - oContent
.offsetHeight
;
1793 this._toggleHighlight(oNewItem
, "to");
1794 this.itemArrowToEvent
.fire(this, oNewItem
);
1795 YAHOO
.log("Item arrowed to", "info", this.toString());
1796 if(this.typeAhead
) {
1797 this._updateValue(oNewItem
);
1802 /////////////////////////////////////////////////////////////////////////////
1804 // Private event handlers
1806 /////////////////////////////////////////////////////////////////////////////
1809 * Handles <li> element mouseover events in the container.
1811 * @method _onItemMouseover
1812 * @param v {HTMLEvent} The mouseover event.
1813 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
1816 YAHOO
.widget
.AutoComplete
.prototype._onItemMouseover = function(v
,oSelf
) {
1817 if(oSelf
.prehighlightClassName
) {
1818 oSelf
._togglePrehighlight(this,"mouseover");
1821 oSelf
._toggleHighlight(this,"to");
1824 oSelf
.itemMouseOverEvent
.fire(oSelf
, this);
1825 YAHOO
.log("Item moused over", "info", oSelf
.toString());
1829 * Handles <li> element mouseout events in the container.
1831 * @method _onItemMouseout
1832 * @param v {HTMLEvent} The mouseout event.
1833 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
1836 YAHOO
.widget
.AutoComplete
.prototype._onItemMouseout = function(v
,oSelf
) {
1837 if(oSelf
.prehighlightClassName
) {
1838 oSelf
._togglePrehighlight(this,"mouseout");
1841 oSelf
._toggleHighlight(this,"from");
1844 oSelf
.itemMouseOutEvent
.fire(oSelf
, this);
1845 YAHOO
.log("Item moused out", "info", oSelf
.toString());
1849 * Handles <li> element click events in the container.
1851 * @method _onItemMouseclick
1852 * @param v {HTMLEvent} The click event.
1853 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
1856 YAHOO
.widget
.AutoComplete
.prototype._onItemMouseclick = function(v
,oSelf
) {
1857 // In case item has not been moused over
1858 oSelf
._toggleHighlight(this,"to");
1859 oSelf
._selectItem(this);
1863 * Handles container mouseover events.
1865 * @method _onContainerMouseover
1866 * @param v {HTMLEvent} The mouseover event.
1867 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
1870 YAHOO
.widget
.AutoComplete
.prototype._onContainerMouseover = function(v
,oSelf
) {
1871 oSelf
._bOverContainer
= true;
1875 * Handles container mouseout events.
1877 * @method _onContainerMouseout
1878 * @param v {HTMLEvent} The mouseout event.
1879 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
1882 YAHOO
.widget
.AutoComplete
.prototype._onContainerMouseout = function(v
,oSelf
) {
1883 oSelf
._bOverContainer
= false;
1884 // If container is still active
1885 if(oSelf
._oCurItem
) {
1886 oSelf
._toggleHighlight(oSelf
._oCurItem
,"to");
1891 * Handles container scroll events.
1893 * @method _onContainerScroll
1894 * @param v {HTMLEvent} The scroll event.
1895 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
1898 YAHOO
.widget
.AutoComplete
.prototype._onContainerScroll = function(v
,oSelf
) {
1899 oSelf
._oTextbox
.focus();
1903 * Handles container resize events.
1905 * @method _onContainerResize
1906 * @param v {HTMLEvent} The resize event.
1907 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
1910 YAHOO
.widget
.AutoComplete
.prototype._onContainerResize = function(v
,oSelf
) {
1911 oSelf
._toggleContainerHelpers(oSelf
._bContainerOpen
);
1916 * Handles textbox keydown events of functional keys, mainly for UI behavior.
1918 * @method _onTextboxKeyDown
1919 * @param v {HTMLEvent} The keydown event.
1920 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
1923 YAHOO
.widget
.AutoComplete
.prototype._onTextboxKeyDown = function(v
,oSelf
) {
1924 var nKeyCode
= v
.keyCode
;
1928 // select an item or clear out
1929 if(oSelf
._oCurItem
) {
1930 if(oSelf
.delimChar
&& (oSelf
._nKeyCode
!= nKeyCode
)) {
1931 if(oSelf
._bContainerOpen
) {
1932 YAHOO
.util
.Event
.stopEvent(v
);
1935 oSelf
._selectItem(oSelf
._oCurItem
);
1938 oSelf
._toggleContainer(false);
1942 if(oSelf
._oCurItem
) {
1943 if(oSelf
._nKeyCode
!= nKeyCode
) {
1944 if(oSelf
._bContainerOpen
) {
1945 YAHOO
.util
.Event
.stopEvent(v
);
1948 oSelf
._selectItem(oSelf
._oCurItem
);
1951 oSelf
._toggleContainer(false);
1955 oSelf
._toggleContainer(false);
1958 oSelf
._jumpSelection();
1961 YAHOO
.util
.Event
.stopEvent(v
);
1962 oSelf
._moveSelection(nKeyCode
);
1965 YAHOO
.util
.Event
.stopEvent(v
);
1966 oSelf
._moveSelection(nKeyCode
);
1974 * Handles textbox keypress events.
1975 * @method _onTextboxKeyPress
1976 * @param v {HTMLEvent} The keypress event.
1977 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
1980 YAHOO
.widget
.AutoComplete
.prototype._onTextboxKeyPress = function(v
,oSelf
) {
1981 var nKeyCode
= v
.keyCode
;
1983 //Expose only to Mac browsers, where stopEvent is ineffective on keydown events (bug 790337)
1984 var isMac
= (navigator
.userAgent
.toLowerCase().indexOf("mac") != -1);
1988 if(oSelf
.delimChar
&& (oSelf
._nKeyCode
!= nKeyCode
)) {
1989 YAHOO
.util
.Event
.stopEvent(v
);
1993 if(oSelf
._nKeyCode
!= nKeyCode
) {
1994 YAHOO
.util
.Event
.stopEvent(v
);
1999 YAHOO
.util
.Event
.stopEvent(v
);
2006 //TODO: (?) limit only to non-IE, non-Mac-FF for Korean IME support (bug 811948)
2007 // Korean IME detected
2008 else if(nKeyCode
== 229) {
2009 oSelf
._queryInterval
= setInterval(function() { oSelf
._onIMEDetected(oSelf
); },500);
2014 * Handles textbox keyup events that trigger queries.
2016 * @method _onTextboxKeyUp
2017 * @param v {HTMLEvent} The keyup event.
2018 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
2021 YAHOO
.widget
.AutoComplete
.prototype._onTextboxKeyUp = function(v
,oSelf
) {
2022 // Check to see if any of the public properties have been updated
2025 var nKeyCode
= v
.keyCode
;
2026 oSelf
._nKeyCode
= nKeyCode
;
2027 var sText
= this.value
; //string in textbox
2029 // Filter out chars that don't trigger queries
2030 if(oSelf
._isIgnoreKey(nKeyCode
) || (sText
.toLowerCase() == oSelf
._sCurQuery
)) {
2034 oSelf
._bItemSelected
= false;
2035 YAHOO
.util
.Dom
.removeClass(oSelf
._oCurItem
, oSelf
.highlightClassName
);
2036 oSelf
._oCurItem
= null;
2038 oSelf
.textboxKeyEvent
.fire(oSelf
, nKeyCode
);
2039 YAHOO
.log("Textbox keyed", "info", oSelf
.toString());
2042 // Set timeout on the request
2043 if(oSelf
.queryDelay
> 0) {
2045 setTimeout(function(){oSelf
._sendQuery(sText
);},(oSelf
.queryDelay
* 1000));
2047 if(oSelf
._nDelayID
!= -1) {
2048 clearTimeout(oSelf
._nDelayID
);
2051 oSelf
._nDelayID
= nDelayID
;
2054 // No delay so send request immediately
2055 oSelf
._sendQuery(sText
);
2060 * Handles text input box receiving focus.
2062 * @method _onTextboxFocus
2063 * @param v {HTMLEvent} The focus event.
2064 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
2067 YAHOO
.widget
.AutoComplete
.prototype._onTextboxFocus = function (v
,oSelf
) {
2068 oSelf
._oTextbox
.setAttribute("autocomplete","off");
2069 oSelf
._bFocused
= true;
2070 if(!oSelf
._bItemSelected
) {
2071 oSelf
.textboxFocusEvent
.fire(oSelf
);
2072 YAHOO
.log("Textbox focused", "info", oSelf
.toString());
2077 * Handles text input box losing focus.
2079 * @method _onTextboxBlur
2080 * @param v {HTMLEvent} The focus event.
2081 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
2084 YAHOO
.widget
.AutoComplete
.prototype._onTextboxBlur = function (v
,oSelf
) {
2085 // Don't treat as a blur if it was a selection via mouse click
2086 if(!oSelf
._bOverContainer
|| (oSelf
._nKeyCode
== 9)) {
2087 // Current query needs to be validated
2088 if(!oSelf
._bItemSelected
) {
2089 var oMatch
= oSelf
._textMatchesOption();
2090 if(!oSelf
._bContainerOpen
|| (oSelf
._bContainerOpen
&& (oMatch
=== null))) {
2091 if(oSelf
.forceSelection
) {
2092 oSelf
._clearSelection();
2095 oSelf
.unmatchedItemSelectEvent
.fire(oSelf
, oSelf
._sCurQuery
);
2096 YAHOO
.log("Unmatched item selected", "info", oSelf
.toString());
2100 oSelf
._selectItem(oMatch
);
2104 if(oSelf
._bContainerOpen
) {
2105 oSelf
._toggleContainer(false);
2107 oSelf
._cancelIntervalDetection(oSelf
);
2108 oSelf
._bFocused
= false;
2109 oSelf
.textboxBlurEvent
.fire(oSelf
);
2110 YAHOO
.log("Textbox blurred", "info", oSelf
.toString());
2115 * Handles form submission event.
2117 * @method _onFormSubmit
2118 * @param v {HTMLEvent} The submit event.
2119 * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
2122 YAHOO
.widget
.AutoComplete
.prototype._onFormSubmit = function(v
,oSelf
) {
2123 if(oSelf
.allowBrowserAutocomplete
) {
2124 oSelf
._oTextbox
.setAttribute("autocomplete","on");
2127 oSelf
._oTextbox
.setAttribute("autocomplete","off");
2131 /****************************************************************************/
2132 /****************************************************************************/
2133 /****************************************************************************/
2136 * The DataSource classes manages sending a request and returning response from a live
2137 * database. Supported data include local JavaScript arrays and objects and databases
2138 * accessible via XHR connections. Supported response formats include JavaScript arrays,
2139 * JSON, XML, and flat-file textual data.
2144 YAHOO
.widget
.DataSource = function() {
2145 /* abstract class */
2149 /////////////////////////////////////////////////////////////////////////////
2153 /////////////////////////////////////////////////////////////////////////////
2156 * Error message for null data responses.
2158 * @property ERROR_DATANULL
2163 YAHOO
.widget
.DataSource
.ERROR_DATANULL
= "Response data was null";
2166 * Error message for data responses with parsing errors.
2168 * @property ERROR_DATAPARSE
2173 YAHOO
.widget
.DataSource
.ERROR_DATAPARSE
= "Response data could not be parsed";
2176 /////////////////////////////////////////////////////////////////////////////
2178 // Public member variables
2180 /////////////////////////////////////////////////////////////////////////////
2183 * Max size of the local cache. Set to 0 to turn off caching. Caching is
2184 * useful to reduce the number of server connections. Recommended only for data
2185 * sources that return comprehensive results for queries or when stale data is
2188 * @property maxCacheEntries
2192 YAHOO
.widget
.DataSource
.prototype.maxCacheEntries
= 15;
2195 * Use this to fine-tune the matching algorithm used against JS Array types of
2196 * DataSource and DataSource caches. If queryMatchContains is true, then the JS
2197 * Array or cache returns results that "contain" the query string. By default,
2198 * queryMatchContains is set to false, so that only results that "start with"
2199 * the query string are returned.
2201 * @property queryMatchContains
2205 YAHOO
.widget
.DataSource
.prototype.queryMatchContains
= false;
2208 * Enables query subset matching. If caching is on and queryMatchSubset is
2209 * true, substrings of queries will return matching cached results. For
2210 * instance, if the first query is for "abc" susequent queries that start with
2211 * "abc", like "abcd", will be queried against the cache, and not the live data
2212 * source. Recommended only for DataSources that return comprehensive results
2213 * for queries with very few characters.
2215 * @property queryMatchSubset
2220 YAHOO
.widget
.DataSource
.prototype.queryMatchSubset
= false;
2223 * Enables case-sensitivity in the matching algorithm used against JS Array
2224 * types of DataSources and DataSource caches. If queryMatchCase is true, only
2225 * case-sensitive matches will return.
2227 * @property queryMatchCase
2231 YAHOO
.widget
.DataSource
.prototype.queryMatchCase
= false;
2234 /////////////////////////////////////////////////////////////////////////////
2238 /////////////////////////////////////////////////////////////////////////////
2241 * Public accessor to the unique name of the DataSource instance.
2244 * @return {String} Unique name of the DataSource instance
2246 YAHOO
.widget
.DataSource
.prototype.toString = function() {
2247 return "DataSource " + this._sName
;
2251 * Retrieves query results, first checking the local cache, then making the
2252 * query request to the live data source as defined by the function doQuery.
2254 * @method getResults
2255 * @param oCallbackFn {HTMLFunction} Callback function defined by oParent object to which to return results.
2256 * @param sQuery {String} Query string.
2257 * @param oParent {Object} The object instance that has requested data.
2259 YAHOO
.widget
.DataSource
.prototype.getResults = function(oCallbackFn
, sQuery
, oParent
) {
2261 // First look in cache
2262 var aResults
= this._doQueryCache(oCallbackFn
,sQuery
,oParent
);
2263 // Not in cache, so get results from server
2264 if(aResults
.length
=== 0) {
2265 this.queryEvent
.fire(this, oParent
, sQuery
);
2266 YAHOO
.log("Query received \"" + sQuery
, "info", this.toString());
2267 this.doQuery(oCallbackFn
, sQuery
, oParent
);
2272 * Abstract method implemented by subclasses to make a query to the live data
2273 * source. Must call the callback function with the response returned from the
2274 * query. Populates cache (if enabled).
2277 * @param oCallbackFn {HTMLFunction} Callback function implemented by oParent to which to return results.
2278 * @param sQuery {String} Query string.
2279 * @param oParent {Object} The object instance that has requested data.
2281 YAHOO
.widget
.DataSource
.prototype.doQuery = function(oCallbackFn
, sQuery
, oParent
) {
2288 * @method flushCache
2290 YAHOO
.widget
.DataSource
.prototype.flushCache = function() {
2294 if(this._aCacheHelper
) {
2295 this._aCacheHelper
= [];
2297 this.cacheFlushEvent
.fire(this);
2298 YAHOO
.log("Cache flushed", "info", this.toString());
2302 /////////////////////////////////////////////////////////////////////////////
2306 /////////////////////////////////////////////////////////////////////////////
2309 * Fired when a query is made to the live data source.
2312 * @param oSelf {Object} The DataSource instance.
2313 * @param oParent {Object} The requesting object.
2314 * @param sQuery {String} The query string.
2316 YAHOO
.widget
.DataSource
.prototype.queryEvent
= null;
2319 * Fired when a query is made to the local cache.
2321 * @event cacheQueryEvent
2322 * @param oSelf {Object} The DataSource instance.
2323 * @param oParent {Object} The requesting object.
2324 * @param sQuery {String} The query string.
2326 YAHOO
.widget
.DataSource
.prototype.cacheQueryEvent
= null;
2329 * Fired when data is retrieved from the live data source.
2331 * @event getResultsEvent
2332 * @param oSelf {Object} The DataSource instance.
2333 * @param oParent {Object} The requesting object.
2334 * @param sQuery {String} The query string.
2335 * @param aResults {Object[]} Array of result objects.
2337 YAHOO
.widget
.DataSource
.prototype.getResultsEvent
= null;
2340 * Fired when data is retrieved from the local cache.
2342 * @event getCachedResultsEvent
2343 * @param oSelf {Object} The DataSource instance.
2344 * @param oParent {Object} The requesting object.
2345 * @param sQuery {String} The query string.
2346 * @param aResults {Object[]} Array of result objects.
2348 YAHOO
.widget
.DataSource
.prototype.getCachedResultsEvent
= null;
2351 * Fired when an error is encountered with the live data source.
2353 * @event dataErrorEvent
2354 * @param oSelf {Object} The DataSource instance.
2355 * @param oParent {Object} The requesting object.
2356 * @param sQuery {String} The query string.
2357 * @param sMsg {String} Error message string
2359 YAHOO
.widget
.DataSource
.prototype.dataErrorEvent
= null;
2362 * Fired when the local cache is flushed.
2364 * @event cacheFlushEvent
2365 * @param oSelf {Object} The DataSource instance
2367 YAHOO
.widget
.DataSource
.prototype.cacheFlushEvent
= null;
2369 /////////////////////////////////////////////////////////////////////////////
2371 // Private member variables
2373 /////////////////////////////////////////////////////////////////////////////
2376 * Internal class variable to index multiple DataSource instances.
2383 YAHOO
.widget
.DataSource
._nIndex
= 0;
2386 * Name of DataSource instance.
2392 YAHOO
.widget
.DataSource
.prototype._sName
= null;
2395 * Local cache of data result objects indexed chronologically.
2401 YAHOO
.widget
.DataSource
.prototype._aCache
= null;
2404 /////////////////////////////////////////////////////////////////////////////
2408 /////////////////////////////////////////////////////////////////////////////
2411 * Initializes DataSource instance.
2416 YAHOO
.widget
.DataSource
.prototype._init = function() {
2417 // Validate and initialize public configs
2418 var maxCacheEntries
= this.maxCacheEntries
;
2419 if(!YAHOO
.lang
.isNumber(maxCacheEntries
) || (maxCacheEntries
< 0)) {
2420 maxCacheEntries
= 0;
2422 // Initialize local cache
2423 if(maxCacheEntries
> 0 && !this._aCache
) {
2427 this._sName
= "instance" + YAHOO
.widget
.DataSource
._nIndex
;
2428 YAHOO
.widget
.DataSource
._nIndex
++;
2430 this.queryEvent
= new YAHOO
.util
.CustomEvent("query", this);
2431 this.cacheQueryEvent
= new YAHOO
.util
.CustomEvent("cacheQuery", this);
2432 this.getResultsEvent
= new YAHOO
.util
.CustomEvent("getResults", this);
2433 this.getCachedResultsEvent
= new YAHOO
.util
.CustomEvent("getCachedResults", this);
2434 this.dataErrorEvent
= new YAHOO
.util
.CustomEvent("dataError", this);
2435 this.cacheFlushEvent
= new YAHOO
.util
.CustomEvent("cacheFlush", this);
2439 * Adds a result object to the local cache, evicting the oldest element if the
2440 * cache is full. Newer items will have higher indexes, the oldest item will have
2443 * @method _addCacheElem
2444 * @param oResult {Object} Data result object, including array of results.
2447 YAHOO
.widget
.DataSource
.prototype._addCacheElem = function(oResult
) {
2448 var aCache
= this._aCache
;
2449 // Don't add if anything important is missing.
2450 if(!aCache
|| !oResult
|| !oResult
.query
|| !oResult
.results
) {
2454 // If the cache is full, make room by removing from index=0
2455 if(aCache
.length
>= this.maxCacheEntries
) {
2459 // Add to cache, at the end of the array
2460 aCache
.push(oResult
);
2464 * Queries the local cache for results. If query has been cached, the callback
2465 * function is called with the results, and the cached is refreshed so that it
2466 * is now the newest element.
2468 * @method _doQueryCache
2469 * @param oCallbackFn {HTMLFunction} Callback function defined by oParent object to which to return results.
2470 * @param sQuery {String} Query string.
2471 * @param oParent {Object} The object instance that has requested data.
2472 * @return aResults {Object[]} Array of results from local cache if found, otherwise null.
2475 YAHOO
.widget
.DataSource
.prototype._doQueryCache = function(oCallbackFn
, sQuery
, oParent
) {
2477 var bMatchFound
= false;
2478 var aCache
= this._aCache
;
2479 var nCacheLength
= (aCache
) ? aCache
.length
: 0;
2480 var bMatchContains
= this.queryMatchContains
;
2482 // If cache is enabled...
2483 if((this.maxCacheEntries
> 0) && aCache
&& (nCacheLength
> 0)) {
2484 this.cacheQueryEvent
.fire(this, oParent
, sQuery
);
2485 YAHOO
.log("Querying cache: \"" + sQuery
+ "\"", "info", this.toString());
2486 // If case is unimportant, normalize query now instead of in loops
2487 if(!this.queryMatchCase
) {
2488 var sOrigQuery
= sQuery
;
2489 sQuery
= sQuery
.toLowerCase();
2492 // Loop through each cached element's query property...
2493 for(var i
= nCacheLength
-1; i
>= 0; i
--) {
2494 var resultObj
= aCache
[i
];
2495 var aAllResultItems
= resultObj
.results
;
2496 // If case is unimportant, normalize match key for comparison
2497 var matchKey
= (!this.queryMatchCase
) ?
2498 encodeURIComponent(resultObj
.query
).toLowerCase():
2499 encodeURIComponent(resultObj
.query
);
2501 // If a cached match key exactly matches the query...
2502 if(matchKey
== sQuery
) {
2503 // Stash all result objects into aResult[] and stop looping through the cache.
2505 aResults
= aAllResultItems
;
2507 // The matching cache element was not the most recent,
2508 // so now we need to refresh the cache.
2509 if(i
!= nCacheLength
-1) {
2510 // Remove element from its original location
2512 // Add element as newest
2513 this._addCacheElem(resultObj
);
2517 // Else if this query is not an exact match and subset matching is enabled...
2518 else if(this.queryMatchSubset
) {
2519 // Loop through substrings of each cached element's query property...
2520 for(var j
= sQuery
.length
-1; j
>= 0 ; j
--) {
2521 var subQuery
= sQuery
.substr(0,j
);
2523 // If a substring of a cached sQuery exactly matches the query...
2524 if(matchKey
== subQuery
) {
2527 // Go through each cached result object to match against the query...
2528 for(var k
= aAllResultItems
.length
-1; k
>= 0; k
--) {
2529 var aRecord
= aAllResultItems
[k
];
2530 var sKeyIndex
= (this.queryMatchCase
) ?
2531 encodeURIComponent(aRecord
[0]).indexOf(sQuery
):
2532 encodeURIComponent(aRecord
[0]).toLowerCase().indexOf(sQuery
);
2534 // A STARTSWITH match is when the query is found at the beginning of the key string...
2535 if((!bMatchContains
&& (sKeyIndex
=== 0)) ||
2536 // A CONTAINS match is when the query is found anywhere within the key string...
2537 (bMatchContains
&& (sKeyIndex
> -1))) {
2538 // Stash a match into aResults[].
2539 aResults
.unshift(aRecord
);
2543 // Add the subset match result set object as the newest element to cache,
2544 // and stop looping through the cache.
2546 resultObj
.query
= sQuery
;
2547 resultObj
.results
= aResults
;
2548 this._addCacheElem(resultObj
);
2558 // If there was a match, send along the results.
2560 this.getCachedResultsEvent
.fire(this, oParent
, sOrigQuery
, aResults
);
2561 YAHOO
.log("Cached results found for query \"" + sQuery
+ "\": " +
2562 YAHOO
.lang
.dump(aResults
), "info", this.toString());
2563 oCallbackFn(sOrigQuery
, aResults
, oParent
);
2570 /****************************************************************************/
2571 /****************************************************************************/
2572 /****************************************************************************/
2575 * Implementation of YAHOO.widget.DataSource using XML HTTP requests that return
2579 * @extends YAHOO.widget.DataSource
2580 * @requires connection
2582 * @param sScriptURI {String} Absolute or relative URI to script that returns query
2583 * results as JSON, XML, or delimited flat-file data.
2584 * @param aSchema {String[]} Data schema definition of results.
2585 * @param oConfigs {Object} (optional) Object literal of config params.
2587 YAHOO
.widget
.DS_XHR = function(sScriptURI
, aSchema
, oConfigs
) {
2588 // Set any config params passed in to override defaults
2589 if(oConfigs
&& (oConfigs
.constructor == Object
)) {
2590 for(var sConfig
in oConfigs
) {
2591 this[sConfig
] = oConfigs
[sConfig
];
2595 // Initialization sequence
2596 if(!YAHOO
.lang
.isArray(aSchema
) || !YAHOO
.lang
.isString(sScriptURI
)) {
2597 YAHOO
.log("Could not instantiate XHR DataSource due to invalid arguments", "error", this.toString());
2601 this.schema
= aSchema
;
2602 this.scriptURI
= sScriptURI
;
2605 YAHOO
.log("XHR DataSource initialized","info",this.toString());
2608 YAHOO
.widget
.DS_XHR
.prototype = new YAHOO
.widget
.DataSource();
2610 /////////////////////////////////////////////////////////////////////////////
2614 /////////////////////////////////////////////////////////////////////////////
2619 * @property TYPE_JSON
2624 YAHOO
.widget
.DS_XHR
.TYPE_JSON
= 0;
2629 * @property TYPE_XML
2634 YAHOO
.widget
.DS_XHR
.TYPE_XML
= 1;
2637 * Flat-file data type.
2639 * @property TYPE_FLAT
2644 YAHOO
.widget
.DS_XHR
.TYPE_FLAT
= 2;
2647 * Error message for XHR failure.
2649 * @property ERROR_DATAXHR
2654 YAHOO
.widget
.DS_XHR
.ERROR_DATAXHR
= "XHR response failed";
2656 /////////////////////////////////////////////////////////////////////////////
2658 // Public member variables
2660 /////////////////////////////////////////////////////////////////////////////
2663 * Alias to YUI Connection Manager. Allows implementers to specify their own
2664 * subclasses of the YUI Connection Manager utility.
2668 * @default YAHOO.util.Connect
2670 YAHOO
.widget
.DS_XHR
.prototype.connMgr
= YAHOO
.util
.Connect
;
2673 * Number of milliseconds the XHR connection will wait for a server response. A
2674 * a value of zero indicates the XHR connection will wait forever. Any value
2675 * greater than zero will use the Connection utility's Auto-Abort feature.
2677 * @property connTimeout
2681 YAHOO
.widget
.DS_XHR
.prototype.connTimeout
= 0;
2684 * Absolute or relative URI to script that returns query results. For instance,
2685 * queries will be sent to <scriptURI>?<scriptQueryParam>=userinput
2687 * @property scriptURI
2690 YAHOO
.widget
.DS_XHR
.prototype.scriptURI
= null;
2693 * Query string parameter name sent to scriptURI. For instance, queries will be
2694 * sent to <scriptURI>?<scriptQueryParam>=userinput
2696 * @property scriptQueryParam
2700 YAHOO
.widget
.DS_XHR
.prototype.scriptQueryParam
= "query";
2703 * String of key/value pairs to append to requests made to scriptURI. Define
2704 * this string when you want to send additional query parameters to your script.
2705 * When defined, queries will be sent to
2706 * <scriptURI>?<scriptQueryParam>=userinput&<scriptQueryAppend>
2708 * @property scriptQueryAppend
2712 YAHOO
.widget
.DS_XHR
.prototype.scriptQueryAppend
= "";
2715 * XHR response data type. Other types that may be defined are YAHOO.widget.DS_XHR.TYPE_XML
2716 * and YAHOO.widget.DS_XHR.TYPE_FLAT.
2718 * @property responseType
2720 * @default YAHOO.widget.DS_XHR.TYPE_JSON
2722 YAHOO
.widget
.DS_XHR
.prototype.responseType
= YAHOO
.widget
.DS_XHR
.TYPE_JSON
;
2725 * String after which to strip results. If the results from the XHR are sent
2726 * back as HTML, the gzip HTML comment appears at the end of the data and should
2729 * @property responseStripAfter
2731 * @default "\n<!-"
2733 YAHOO
.widget
.DS_XHR
.prototype.responseStripAfter
= "\n<!-";
2735 /////////////////////////////////////////////////////////////////////////////
2739 /////////////////////////////////////////////////////////////////////////////
2742 * Queries the live data source defined by scriptURI for results. Results are
2743 * passed back to a callback function.
2746 * @param oCallbackFn {HTMLFunction} Callback function defined by oParent object to which to return results.
2747 * @param sQuery {String} Query string.
2748 * @param oParent {Object} The object instance that has requested data.
2750 YAHOO
.widget
.DS_XHR
.prototype.doQuery = function(oCallbackFn
, sQuery
, oParent
) {
2751 var isXML
= (this.responseType
== YAHOO
.widget
.DS_XHR
.TYPE_XML
);
2752 var sUri
= this.scriptURI
+"?"+this.scriptQueryParam
+"="+sQuery
;
2753 if(this.scriptQueryAppend
.length
> 0) {
2754 sUri
+= "&" + this.scriptQueryAppend
;
2756 YAHOO
.log("DataSource is querying URL " + sUri
, "info", this.toString());
2757 var oResponse
= null;
2761 * Sets up ajax request callback
2763 * @param {object} oReq HTTPXMLRequest object
2766 var responseSuccess = function(oResp
) {
2767 // Response ID does not match last made request ID.
2768 if(!oSelf
._oConn
|| (oResp
.tId
!= oSelf
._oConn
.tId
)) {
2769 oSelf
.dataErrorEvent
.fire(oSelf
, oParent
, sQuery
, YAHOO
.widget
.DataSource
.ERROR_DATANULL
);
2770 YAHOO
.log(YAHOO
.widget
.DataSource
.ERROR_DATANULL
, "error", oSelf
.toString());
2774 /*YAHOO.log(oResp.responseXML.getElementsByTagName("Result"),'warn');
2775 for(var foo in oResp) {
2776 YAHOO.log(foo + ": "+oResp[foo],'warn');
2778 YAHOO.log('responseXML.xml: '+oResp.responseXML.xml,'warn');*/
2780 oResp
= oResp
.responseText
;
2783 oResp
= oResp
.responseXML
;
2785 if(oResp
=== null) {
2786 oSelf
.dataErrorEvent
.fire(oSelf
, oParent
, sQuery
, YAHOO
.widget
.DataSource
.ERROR_DATANULL
);
2787 YAHOO
.log(YAHOO
.widget
.DataSource
.ERROR_DATANULL
, "error", oSelf
.toString());
2791 var aResults
= oSelf
.parseResponse(sQuery
, oResp
, oParent
);
2793 resultObj
.query
= decodeURIComponent(sQuery
);
2794 resultObj
.results
= aResults
;
2795 if(aResults
=== null) {
2796 oSelf
.dataErrorEvent
.fire(oSelf
, oParent
, sQuery
, YAHOO
.widget
.DataSource
.ERROR_DATAPARSE
);
2797 YAHOO
.log(YAHOO
.widget
.DataSource
.ERROR_DATAPARSE
, "error", oSelf
.toString());
2801 oSelf
.getResultsEvent
.fire(oSelf
, oParent
, sQuery
, aResults
);
2802 YAHOO
.log("Results returned for query \"" + sQuery
+ "\": " +
2803 YAHOO
.lang
.dump(aResults
), "info", oSelf
.toString());
2804 oSelf
._addCacheElem(resultObj
);
2806 oCallbackFn(sQuery
, aResults
, oParent
);
2809 var responseFailure = function(oResp
) {
2810 oSelf
.dataErrorEvent
.fire(oSelf
, oParent
, sQuery
, YAHOO
.widget
.DS_XHR
.ERROR_DATAXHR
);
2811 YAHOO
.log(YAHOO
.widget
.DS_XHR
.ERROR_DATAXHR
+ ": " + oResp
.statusText
, "error", oSelf
.toString());
2816 success
:responseSuccess
,
2817 failure
:responseFailure
2820 if(YAHOO
.lang
.isNumber(this.connTimeout
) && (this.connTimeout
> 0)) {
2821 oCallback
.timeout
= this.connTimeout
;
2825 this.connMgr
.abort(this._oConn
);
2828 oSelf
._oConn
= this.connMgr
.asyncRequest("GET", sUri
, oCallback
, null);
2832 * Parses raw response data into an array of result objects. The result data key
2833 * is always stashed in the [0] element of each result object.
2835 * @method parseResponse
2836 * @param sQuery {String} Query string.
2837 * @param oResponse {Object} The raw response data to parse.
2838 * @param oParent {Object} The object instance that has requested data.
2839 * @returns {Object[]} Array of result objects.
2841 YAHOO
.widget
.DS_XHR
.prototype.parseResponse = function(sQuery
, oResponse
, oParent
) {
2842 var aSchema
= this.schema
;
2846 // Strip out comment at the end of results
2847 var nEnd
= ((this.responseStripAfter
!== "") && (oResponse
.indexOf
)) ?
2848 oResponse
.indexOf(this.responseStripAfter
) : -1;
2850 oResponse
= oResponse
.substring(0,nEnd
);
2853 switch (this.responseType
) {
2854 case YAHOO
.widget
.DS_XHR
.TYPE_JSON
:
2855 var jsonList
, jsonObjParsed
;
2856 // Check for JSON lib but divert KHTML clients
2857 var isNotMac
= (navigator
.userAgent
.toLowerCase().indexOf('khtml')== -1);
2858 if(oResponse
.parseJSON
&& isNotMac
) {
2859 // Use the new JSON utility if available
2860 jsonObjParsed
= oResponse
.parseJSON();
2861 if(!jsonObjParsed
) {
2866 // eval is necessary here since aSchema[0] is of unknown depth
2867 jsonList
= eval("jsonObjParsed." + aSchema
[0]);
2875 else if(window
.JSON
&& isNotMac
) {
2876 // Use older JSON lib if available
2877 jsonObjParsed
= JSON
.parse(oResponse
);
2878 if(!jsonObjParsed
) {
2884 // eval is necessary here since aSchema[0] is of unknown depth
2885 jsonList
= eval("jsonObjParsed." + aSchema
[0]);
2894 // Parse the JSON response as a string
2896 // Trim leading spaces
2897 while (oResponse
.substring(0,1) == " ") {
2898 oResponse
= oResponse
.substring(1, oResponse
.length
);
2901 // Invalid JSON response
2902 if(oResponse
.indexOf("{") < 0) {
2907 // Empty (but not invalid) JSON response
2908 if(oResponse
.indexOf("{}") === 0) {
2912 // Turn the string into an object literal...
2913 // ...eval is necessary here
2914 var jsonObjRaw
= eval("(" + oResponse
+ ")");
2920 // Grab the object member that contains an array of all reponses...
2921 // ...eval is necessary here since aSchema[0] is of unknown depth
2922 jsonList
= eval("(jsonObjRaw." + aSchema
[0]+")");
2935 if(!YAHOO
.lang
.isArray(jsonList
)) {
2936 jsonList
= [jsonList
];
2939 // Loop through the array of all responses...
2940 for(var i
= jsonList
.length
-1; i
>= 0 ; i
--) {
2941 var aResultItem
= [];
2942 var jsonResult
= jsonList
[i
];
2943 // ...and loop through each data field value of each response
2944 for(var j
= aSchema
.length
-1; j
>= 1 ; j
--) {
2945 // ...and capture data into an array mapped according to the schema...
2946 var dataFieldValue
= jsonResult
[aSchema
[j
]];
2947 if(!dataFieldValue
) {
2948 dataFieldValue
= "";
2950 //YAHOO.log("data: " + i + " value:" +j+" = "+dataFieldValue,"debug",this.toString());
2951 aResultItem
.unshift(dataFieldValue
);
2953 // If schema isn't well defined, pass along the entire result object
2954 if(aResultItem
.length
== 1) {
2955 aResultItem
.push(jsonResult
);
2957 // Capture the array of data field values in an array of results
2958 aResults
.unshift(aResultItem
);
2961 case YAHOO
.widget
.DS_XHR
.TYPE_XML
:
2962 // Get the collection of results
2963 var xmlList
= oResponse
.getElementsByTagName(aSchema
[0]);
2968 // Loop through each result
2969 for(var k
= xmlList
.length
-1; k
>= 0 ; k
--) {
2970 var result
= xmlList
.item(k
);
2971 //YAHOO.log("Result"+k+" is "+result.attributes.item(0).firstChild.nodeValue,"debug",this.toString());
2973 // Loop through each data field in each result using the schema
2974 for(var m
= aSchema
.length
-1; m
>= 1 ; m
--) {
2975 //YAHOO.log(aSchema[m]+" is "+result.attributes.getNamedItem(aSchema[m]).firstChild.nodeValue);
2977 // Values may be held in an attribute...
2978 var xmlAttr
= result
.attributes
.getNamedItem(aSchema
[m
]);
2980 sValue
= xmlAttr
.value
;
2981 //YAHOO.log("Attr value is "+sValue,"debug",this.toString());
2985 var xmlNode
= result
.getElementsByTagName(aSchema
[m
]);
2986 if(xmlNode
&& xmlNode
.item(0) && xmlNode
.item(0).firstChild
) {
2987 sValue
= xmlNode
.item(0).firstChild
.nodeValue
;
2988 //YAHOO.log("Node value is "+sValue,"debug",this.toString());
2992 //YAHOO.log("Value not found","debug",this.toString());
2995 // Capture the schema-mapped data field values into an array
2996 aFieldSet
.unshift(sValue
);
2998 // Capture each array of values into an array of results
2999 aResults
.unshift(aFieldSet
);
3002 case YAHOO
.widget
.DS_XHR
.TYPE_FLAT
:
3003 if(oResponse
.length
> 0) {
3004 // Delete the last line delimiter at the end of the data if it exists
3005 var newLength
= oResponse
.length
-aSchema
[0].length
;
3006 if(oResponse
.substr(newLength
) == aSchema
[0]) {
3007 oResponse
= oResponse
.substr(0, newLength
);
3009 var aRecords
= oResponse
.split(aSchema
[0]);
3010 for(var n
= aRecords
.length
-1; n
>= 0; n
--) {
3011 aResults
[n
] = aRecords
[n
].split(aSchema
[1]);
3029 /////////////////////////////////////////////////////////////////////////////
3031 // Private member variables
3033 /////////////////////////////////////////////////////////////////////////////
3036 * XHR connection object.
3042 YAHOO
.widget
.DS_XHR
.prototype._oConn
= null;
3045 /****************************************************************************/
3046 /****************************************************************************/
3047 /****************************************************************************/
3050 * Implementation of YAHOO.widget.DataSource using a native Javascript function as
3051 * its live data source.
3053 * @class DS_JSFunction
3055 * @extends YAHOO.widget.DataSource
3056 * @param oFunction {HTMLFunction} In-memory Javascript function that returns query results as an array of objects.
3057 * @param oConfigs {Object} (optional) Object literal of config params.
3059 YAHOO
.widget
.DS_JSFunction = function(oFunction
, oConfigs
) {
3060 // Set any config params passed in to override defaults
3061 if(oConfigs
&& (oConfigs
.constructor == Object
)) {
3062 for(var sConfig
in oConfigs
) {
3063 this[sConfig
] = oConfigs
[sConfig
];
3067 // Initialization sequence
3068 if(!YAHOO
.lang
.isFunction(oFunction
)) {
3069 YAHOO
.log("Could not instantiate JSFunction DataSource due to invalid arguments", "error", this.toString());
3073 this.dataFunction
= oFunction
;
3075 YAHOO
.log("JS Function DataSource initialized","info",this.toString());
3079 YAHOO
.widget
.DS_JSFunction
.prototype = new YAHOO
.widget
.DataSource();
3081 /////////////////////////////////////////////////////////////////////////////
3083 // Public member variables
3085 /////////////////////////////////////////////////////////////////////////////
3088 * In-memory Javascript function that returns query results.
3090 * @property dataFunction
3091 * @type HTMLFunction
3093 YAHOO
.widget
.DS_JSFunction
.prototype.dataFunction
= null;
3095 /////////////////////////////////////////////////////////////////////////////
3099 /////////////////////////////////////////////////////////////////////////////
3102 * Queries the live data source defined by function for results. Results are
3103 * passed back to a callback function.
3106 * @param oCallbackFn {HTMLFunction} Callback function defined by oParent object to which to return results.
3107 * @param sQuery {String} Query string.
3108 * @param oParent {Object} The object instance that has requested data.
3110 YAHOO
.widget
.DS_JSFunction
.prototype.doQuery = function(oCallbackFn
, sQuery
, oParent
) {
3111 var oFunction
= this.dataFunction
;
3114 aResults
= oFunction(sQuery
);
3115 if(aResults
=== null) {
3116 this.dataErrorEvent
.fire(this, oParent
, sQuery
, YAHOO
.widget
.DataSource
.ERROR_DATANULL
);
3117 YAHOO
.log(YAHOO
.widget
.DataSource
.ERROR_DATANULL
, "error", this.toString());
3122 resultObj
.query
= decodeURIComponent(sQuery
);
3123 resultObj
.results
= aResults
;
3124 this._addCacheElem(resultObj
);
3126 this.getResultsEvent
.fire(this, oParent
, sQuery
, aResults
);
3127 YAHOO
.log("Results returned for query \"" + sQuery
+
3128 "\": " + YAHOO
.lang
.dump(aResults
), "info", this.toString());
3129 oCallbackFn(sQuery
, aResults
, oParent
);
3133 /****************************************************************************/
3134 /****************************************************************************/
3135 /****************************************************************************/
3138 * Implementation of YAHOO.widget.DataSource using a native Javascript array as
3139 * its live data source.
3143 * @extends YAHOO.widget.DataSource
3144 * @param aData {String[]} In-memory Javascript array of simple string data.
3145 * @param oConfigs {Object} (optional) Object literal of config params.
3147 YAHOO
.widget
.DS_JSArray = function(aData
, oConfigs
) {
3148 // Set any config params passed in to override defaults
3149 if(oConfigs
&& (oConfigs
.constructor == Object
)) {
3150 for(var sConfig
in oConfigs
) {
3151 this[sConfig
] = oConfigs
[sConfig
];
3155 // Initialization sequence
3156 if(!YAHOO
.lang
.isArray(aData
)) {
3157 YAHOO
.log("Could not instantiate JSArray DataSource due to invalid arguments", "error", this.toString());
3163 YAHOO
.log("JS Array DataSource initialized","info",this.toString());
3167 YAHOO
.widget
.DS_JSArray
.prototype = new YAHOO
.widget
.DataSource();
3169 /////////////////////////////////////////////////////////////////////////////
3171 // Public member variables
3173 /////////////////////////////////////////////////////////////////////////////
3176 * In-memory Javascript array of strings.
3181 YAHOO
.widget
.DS_JSArray
.prototype.data
= null;
3183 /////////////////////////////////////////////////////////////////////////////
3187 /////////////////////////////////////////////////////////////////////////////
3190 * Queries the live data source defined by data for results. Results are passed
3191 * back to a callback function.
3194 * @param oCallbackFn {HTMLFunction} Callback function defined by oParent object to which to return results.
3195 * @param sQuery {String} Query string.
3196 * @param oParent {Object} The object instance that has requested data.
3198 YAHOO
.widget
.DS_JSArray
.prototype.doQuery = function(oCallbackFn
, sQuery
, oParent
) {
3200 var aData
= this.data
; // the array
3201 var aResults
= []; // container for results
3202 var bMatchFound
= false;
3203 var bMatchContains
= this.queryMatchContains
;
3205 if(!this.queryMatchCase
) {
3206 sQuery
= sQuery
.toLowerCase();
3209 // Loop through each element of the array...
3210 // which can be a string or an array of strings
3211 for(i
= aData
.length
-1; i
>= 0; i
--) {
3214 if(YAHOO
.lang
.isString(aData
[i
])) {
3215 aDataset
[0] = aData
[i
];
3217 else if(YAHOO
.lang
.isArray(aData
[i
])) {
3218 aDataset
= aData
[i
];
3221 if(YAHOO
.lang
.isString(aDataset
[0])) {
3222 var sKeyIndex
= (this.queryMatchCase
) ?
3223 encodeURIComponent(aDataset
[0]).indexOf(sQuery
):
3224 encodeURIComponent(aDataset
[0]).toLowerCase().indexOf(sQuery
);
3226 // A STARTSWITH match is when the query is found at the beginning of the key string...
3227 if((!bMatchContains
&& (sKeyIndex
=== 0)) ||
3228 // A CONTAINS match is when the query is found anywhere within the key string...
3229 (bMatchContains
&& (sKeyIndex
> -1))) {
3230 // Stash a match into aResults[].
3231 aResults
.unshift(aDataset
);
3237 for(i
= aData
.length
-1; i
>= 0; i
--) {
3238 if(YAHOO
.lang
.isString(aData
[i
])) {
3239 aResults
.unshift([aData
[i
]]);
3241 else if(YAHOO
.lang
.isArray(aData
[i
])) {
3242 aResults
.unshift(aData
[i
]);
3247 this.getResultsEvent
.fire(this, oParent
, sQuery
, aResults
);
3248 YAHOO
.log("Results returned for query \"" + sQuery
+
3249 "\": " + YAHOO
.lang
.dump(aResults
), "info", this.toString());
3250 oCallbackFn(sQuery
, aResults
, oParent
);
3253 YAHOO
.register("autocomplete", YAHOO
.widget
.AutoComplete
, {version
: "2.3.0", build
: "442"});