/src/main/resources/org/apache/struts2/static/autocomplete/autocomplete.js

http://struts2yuiplugin.googlecode.com/ · JavaScript · 2865 lines · 1300 code · 279 blank · 1286 comment · 372 complexity · 38888510ef9533e2b379cf5360fb4bdf MD5 · raw file

Large files are truncated click here to view the full file

  1. /*
  2. Copyright (c) 2009, Yahoo! Inc. All rights reserved.
  3. Code licensed under the BSD License:
  4. http://developer.yahoo.net/yui/license.txt
  5. version: 2.7.0
  6. */
  7. /////////////////////////////////////////////////////////////////////////////
  8. //
  9. // YAHOO.widget.DataSource Backwards Compatibility
  10. //
  11. /////////////////////////////////////////////////////////////////////////////
  12. YAHOO.widget.DS_JSArray = YAHOO.util.LocalDataSource;
  13. YAHOO.widget.DS_JSFunction = YAHOO.util.FunctionDataSource;
  14. YAHOO.widget.DS_XHR = function(sScriptURI, aSchema, oConfigs) {
  15. var DS = new YAHOO.util.XHRDataSource(sScriptURI, oConfigs);
  16. DS._aDeprecatedSchema = aSchema;
  17. return DS;
  18. };
  19. YAHOO.widget.DS_ScriptNode = function(sScriptURI, aSchema, oConfigs) {
  20. var DS = new YAHOO.util.ScriptNodeDataSource(sScriptURI, oConfigs);
  21. DS._aDeprecatedSchema = aSchema;
  22. return DS;
  23. };
  24. YAHOO.widget.DS_XHR.TYPE_JSON = YAHOO.util.DataSourceBase.TYPE_JSON;
  25. YAHOO.widget.DS_XHR.TYPE_XML = YAHOO.util.DataSourceBase.TYPE_XML;
  26. YAHOO.widget.DS_XHR.TYPE_FLAT = YAHOO.util.DataSourceBase.TYPE_TEXT;
  27. // TODO: widget.DS_ScriptNode.scriptCallbackParam
  28. /**
  29. * The AutoComplete control provides the front-end logic for text-entry suggestion and
  30. * completion functionality.
  31. *
  32. * @module autocomplete
  33. * @requires yahoo, dom, event, datasource
  34. * @optional animation
  35. * @namespace YAHOO.widget
  36. * @title AutoComplete Widget
  37. */
  38. /****************************************************************************/
  39. /****************************************************************************/
  40. /****************************************************************************/
  41. /**
  42. * The AutoComplete class provides the customizable functionality of a plug-and-play DHTML
  43. * auto completion widget. Some key features:
  44. * <ul>
  45. * <li>Navigate with up/down arrow keys and/or mouse to pick a selection</li>
  46. * <li>The drop down container can "roll down" or "fly out" via configurable
  47. * animation</li>
  48. * <li>UI look-and-feel customizable through CSS, including container
  49. * attributes, borders, position, fonts, etc</li>
  50. * </ul>
  51. *
  52. * @class AutoComplete
  53. * @constructor
  54. * @param elInput {HTMLElement} DOM element reference of an input field.
  55. * @param elInput {String} String ID of an input field.
  56. * @param elContainer {HTMLElement} DOM element reference of an existing DIV.
  57. * @param elContainer {String} String ID of an existing DIV.
  58. * @param oDataSource {YAHOO.widget.DataSource} DataSource instance.
  59. * @param oConfigs {Object} (optional) Object literal of configuration params.
  60. */
  61. YAHOO.widget.AutoComplete = function(elInput,elContainer,oDataSource,oConfigs) {
  62. if(elInput && elContainer && oDataSource) {
  63. // Validate DataSource
  64. if(oDataSource instanceof YAHOO.util.DataSourceBase) {
  65. this.dataSource = oDataSource;
  66. }
  67. else {
  68. return;
  69. }
  70. // YAHOO.widget.DataSource schema backwards compatibility
  71. // Converted deprecated schema into supported schema
  72. // First assume key data is held in position 0 of results array
  73. this.key = 0;
  74. var schema = oDataSource.responseSchema;
  75. // An old school schema has been defined in the deprecated DataSource constructor
  76. if(oDataSource._aDeprecatedSchema) {
  77. var aDeprecatedSchema = oDataSource._aDeprecatedSchema;
  78. if(YAHOO.lang.isArray(aDeprecatedSchema)) {
  79. if((oDataSource.responseType === YAHOO.util.DataSourceBase.TYPE_JSON) ||
  80. (oDataSource.responseType === YAHOO.util.DataSourceBase.TYPE_UNKNOWN)) { // Used to default to unknown
  81. // Store the resultsList
  82. schema.resultsList = aDeprecatedSchema[0];
  83. // Store the key
  84. this.key = aDeprecatedSchema[1];
  85. // Only resultsList and key are defined, so grab all the data
  86. schema.fields = (aDeprecatedSchema.length < 3) ? null : aDeprecatedSchema.slice(1);
  87. }
  88. else if(oDataSource.responseType === YAHOO.util.DataSourceBase.TYPE_XML) {
  89. schema.resultNode = aDeprecatedSchema[0];
  90. this.key = aDeprecatedSchema[1];
  91. schema.fields = aDeprecatedSchema.slice(1);
  92. }
  93. else if(oDataSource.responseType === YAHOO.util.DataSourceBase.TYPE_TEXT) {
  94. schema.recordDelim = aDeprecatedSchema[0];
  95. schema.fieldDelim = aDeprecatedSchema[1];
  96. }
  97. oDataSource.responseSchema = schema;
  98. }
  99. }
  100. // Validate input element
  101. if(YAHOO.util.Dom.inDocument(elInput)) {
  102. if(YAHOO.lang.isString(elInput)) {
  103. this._sName = "instance" + YAHOO.widget.AutoComplete._nIndex + " " + elInput;
  104. this._elTextbox = document.getElementById(elInput);
  105. }
  106. else {
  107. this._sName = (elInput.id) ?
  108. "instance" + YAHOO.widget.AutoComplete._nIndex + " " + elInput.id:
  109. "instance" + YAHOO.widget.AutoComplete._nIndex;
  110. this._elTextbox = elInput;
  111. }
  112. YAHOO.util.Dom.addClass(this._elTextbox, "yui-ac-input");
  113. }
  114. else {
  115. return;
  116. }
  117. // Validate container element
  118. if(YAHOO.util.Dom.inDocument(elContainer)) {
  119. if(YAHOO.lang.isString(elContainer)) {
  120. this._elContainer = document.getElementById(elContainer);
  121. }
  122. else {
  123. this._elContainer = elContainer;
  124. }
  125. if(this._elContainer.style.display == "none") {
  126. }
  127. // For skinning
  128. var elParent = this._elContainer.parentNode;
  129. var elTag = elParent.tagName.toLowerCase();
  130. if(elTag == "div") {
  131. YAHOO.util.Dom.addClass(elParent, "yui-ac");
  132. }
  133. else {
  134. }
  135. }
  136. else {
  137. return;
  138. }
  139. // Default applyLocalFilter setting is to enable for local sources
  140. if(this.dataSource.dataType === YAHOO.util.DataSourceBase.TYPE_LOCAL) {
  141. this.applyLocalFilter = true;
  142. }
  143. // Set any config params passed in to override defaults
  144. if(oConfigs && (oConfigs.constructor == Object)) {
  145. for(var sConfig in oConfigs) {
  146. if(sConfig) {
  147. this[sConfig] = oConfigs[sConfig];
  148. }
  149. }
  150. }
  151. // Initialization sequence
  152. this._initContainerEl();
  153. this._initProps();
  154. this._initListEl();
  155. this._initContainerHelperEls();
  156. // Set up events
  157. var oSelf = this;
  158. var elTextbox = this._elTextbox;
  159. // Dom events
  160. YAHOO.util.Event.addListener(elTextbox,"keyup",oSelf._onTextboxKeyUp,oSelf);
  161. YAHOO.util.Event.addListener(elTextbox,"keydown",oSelf._onTextboxKeyDown,oSelf);
  162. YAHOO.util.Event.addListener(elTextbox,"focus",oSelf._onTextboxFocus,oSelf);
  163. YAHOO.util.Event.addListener(elTextbox,"blur",oSelf._onTextboxBlur,oSelf);
  164. YAHOO.util.Event.addListener(elContainer,"mouseover",oSelf._onContainerMouseover,oSelf);
  165. YAHOO.util.Event.addListener(elContainer,"mouseout",oSelf._onContainerMouseout,oSelf);
  166. YAHOO.util.Event.addListener(elContainer,"click",oSelf._onContainerClick,oSelf);
  167. YAHOO.util.Event.addListener(elContainer,"scroll",oSelf._onContainerScroll,oSelf);
  168. YAHOO.util.Event.addListener(elContainer,"resize",oSelf._onContainerResize,oSelf);
  169. YAHOO.util.Event.addListener(elTextbox,"keypress",oSelf._onTextboxKeyPress,oSelf);
  170. YAHOO.util.Event.addListener(window,"unload",oSelf._onWindowUnload,oSelf);
  171. // Custom events
  172. this.textboxFocusEvent = new YAHOO.util.CustomEvent("textboxFocus", this);
  173. this.textboxKeyEvent = new YAHOO.util.CustomEvent("textboxKey", this);
  174. this.dataRequestEvent = new YAHOO.util.CustomEvent("dataRequest", this);
  175. this.dataReturnEvent = new YAHOO.util.CustomEvent("dataReturn", this);
  176. this.dataErrorEvent = new YAHOO.util.CustomEvent("dataError", this);
  177. this.containerPopulateEvent = new YAHOO.util.CustomEvent("containerPopulate", this);
  178. this.containerExpandEvent = new YAHOO.util.CustomEvent("containerExpand", this);
  179. this.typeAheadEvent = new YAHOO.util.CustomEvent("typeAhead", this);
  180. this.itemMouseOverEvent = new YAHOO.util.CustomEvent("itemMouseOver", this);
  181. this.itemMouseOutEvent = new YAHOO.util.CustomEvent("itemMouseOut", this);
  182. this.itemArrowToEvent = new YAHOO.util.CustomEvent("itemArrowTo", this);
  183. this.itemArrowFromEvent = new YAHOO.util.CustomEvent("itemArrowFrom", this);
  184. this.itemSelectEvent = new YAHOO.util.CustomEvent("itemSelect", this);
  185. this.unmatchedItemSelectEvent = new YAHOO.util.CustomEvent("unmatchedItemSelect", this);
  186. this.selectionEnforceEvent = new YAHOO.util.CustomEvent("selectionEnforce", this);
  187. this.containerCollapseEvent = new YAHOO.util.CustomEvent("containerCollapse", this);
  188. this.textboxBlurEvent = new YAHOO.util.CustomEvent("textboxBlur", this);
  189. this.textboxChangeEvent = new YAHOO.util.CustomEvent("textboxChange", this);
  190. // Finish up
  191. elTextbox.setAttribute("autocomplete","off");
  192. YAHOO.widget.AutoComplete._nIndex++;
  193. }
  194. // Required arguments were not found
  195. else {
  196. }
  197. };
  198. /////////////////////////////////////////////////////////////////////////////
  199. //
  200. // Public member variables
  201. //
  202. /////////////////////////////////////////////////////////////////////////////
  203. /**
  204. * The DataSource object that encapsulates the data used for auto completion.
  205. * This object should be an inherited object from YAHOO.widget.DataSource.
  206. *
  207. * @property dataSource
  208. * @type YAHOO.widget.DataSource
  209. */
  210. YAHOO.widget.AutoComplete.prototype.dataSource = null;
  211. /**
  212. * By default, results from local DataSources will pass through the filterResults
  213. * method to apply a client-side matching algorithm.
  214. *
  215. * @property applyLocalFilter
  216. * @type Boolean
  217. * @default true for local arrays and json, otherwise false
  218. */
  219. YAHOO.widget.AutoComplete.prototype.applyLocalFilter = null;
  220. /**
  221. * When applyLocalFilter is true, the local filtering algorthim can have case sensitivity
  222. * enabled.
  223. *
  224. * @property queryMatchCase
  225. * @type Boolean
  226. * @default false
  227. */
  228. YAHOO.widget.AutoComplete.prototype.queryMatchCase = false;
  229. /**
  230. * When applyLocalFilter is true, results can be locally filtered to return
  231. * matching strings that "contain" the query string rather than simply "start with"
  232. * the query string.
  233. *
  234. * @property queryMatchContains
  235. * @type Boolean
  236. * @default false
  237. */
  238. YAHOO.widget.AutoComplete.prototype.queryMatchContains = false;
  239. /**
  240. * Enables query subset matching. When the DataSource's cache is enabled and queryMatchSubset is
  241. * true, substrings of queries will return matching cached results. For
  242. * instance, if the first query is for "abc" susequent queries that start with
  243. * "abc", like "abcd", will be queried against the cache, and not the live data
  244. * source. Recommended only for DataSources that return comprehensive results
  245. * for queries with very few characters.
  246. *
  247. * @property queryMatchSubset
  248. * @type Boolean
  249. * @default false
  250. *
  251. */
  252. YAHOO.widget.AutoComplete.prototype.queryMatchSubset = false;
  253. /**
  254. * Number of characters that must be entered before querying for results. A negative value
  255. * effectively turns off the widget. A value of 0 allows queries of null or empty string
  256. * values.
  257. *
  258. * @property minQueryLength
  259. * @type Number
  260. * @default 1
  261. */
  262. YAHOO.widget.AutoComplete.prototype.minQueryLength = 1;
  263. /**
  264. * Maximum number of results to display in results container.
  265. *
  266. * @property maxResultsDisplayed
  267. * @type Number
  268. * @default 10
  269. */
  270. YAHOO.widget.AutoComplete.prototype.maxResultsDisplayed = 10;
  271. /**
  272. * Number of seconds to delay before submitting a query request. If a query
  273. * request is received before a previous one has completed its delay, the
  274. * previous request is cancelled and the new request is set to the delay. If
  275. * typeAhead is also enabled, this value must always be less than the typeAheadDelay
  276. * in order to avoid certain race conditions.
  277. *
  278. * @property queryDelay
  279. * @type Number
  280. * @default 0.2
  281. */
  282. YAHOO.widget.AutoComplete.prototype.queryDelay = 0.2;
  283. /**
  284. * If typeAhead is true, number of seconds to delay before updating input with
  285. * typeAhead value. In order to prevent certain race conditions, this value must
  286. * always be greater than the queryDelay.
  287. *
  288. * @property typeAheadDelay
  289. * @type Number
  290. * @default 0.5
  291. */
  292. YAHOO.widget.AutoComplete.prototype.typeAheadDelay = 0.5;
  293. /**
  294. * When IME usage is detected, AutoComplete will switch to querying the input
  295. * value at the given interval rather than per key event.
  296. *
  297. * @property queryInterval
  298. * @type Number
  299. * @default 500
  300. */
  301. YAHOO.widget.AutoComplete.prototype.queryInterval = 500;
  302. /**
  303. * Class name of a highlighted item within results container.
  304. *
  305. * @property highlightClassName
  306. * @type String
  307. * @default "yui-ac-highlight"
  308. */
  309. YAHOO.widget.AutoComplete.prototype.highlightClassName = "yui-ac-highlight";
  310. /**
  311. * Class name of a pre-highlighted item within results container.
  312. *
  313. * @property prehighlightClassName
  314. * @type String
  315. */
  316. YAHOO.widget.AutoComplete.prototype.prehighlightClassName = null;
  317. /**
  318. * Query delimiter. A single character separator for multiple delimited
  319. * selections. Multiple delimiter characteres may be defined as an array of
  320. * strings. A null value or empty string indicates that query results cannot
  321. * be delimited. This feature is not recommended if you need forceSelection to
  322. * be true.
  323. *
  324. * @property delimChar
  325. * @type String | String[]
  326. */
  327. YAHOO.widget.AutoComplete.prototype.delimChar = null;
  328. /**
  329. * Whether or not the first item in results container should be automatically highlighted
  330. * on expand.
  331. *
  332. * @property autoHighlight
  333. * @type Boolean
  334. * @default true
  335. */
  336. YAHOO.widget.AutoComplete.prototype.autoHighlight = true;
  337. /**
  338. * If autohighlight is enabled, whether or not the input field should be automatically updated
  339. * with the first query result as the user types, auto-selecting the substring portion
  340. * of the first result that the user has not yet typed.
  341. *
  342. * @property typeAhead
  343. * @type Boolean
  344. * @default false
  345. */
  346. YAHOO.widget.AutoComplete.prototype.typeAhead = false;
  347. /**
  348. * Whether or not to animate the expansion/collapse of the results container in the
  349. * horizontal direction.
  350. *
  351. * @property animHoriz
  352. * @type Boolean
  353. * @default false
  354. */
  355. YAHOO.widget.AutoComplete.prototype.animHoriz = false;
  356. /**
  357. * Whether or not to animate the expansion/collapse of the results container in the
  358. * vertical direction.
  359. *
  360. * @property animVert
  361. * @type Boolean
  362. * @default true
  363. */
  364. YAHOO.widget.AutoComplete.prototype.animVert = true;
  365. /**
  366. * Speed of container expand/collapse animation, in seconds..
  367. *
  368. * @property animSpeed
  369. * @type Number
  370. * @default 0.3
  371. */
  372. YAHOO.widget.AutoComplete.prototype.animSpeed = 0.3;
  373. /**
  374. * Whether or not to force the user's selection to match one of the query
  375. * results. Enabling this feature essentially transforms the input field into a
  376. * &lt;select&gt; field. This feature is not recommended with delimiter character(s)
  377. * defined.
  378. *
  379. * @property forceSelection
  380. * @type Boolean
  381. * @default false
  382. */
  383. YAHOO.widget.AutoComplete.prototype.forceSelection = false;
  384. /**
  385. * Whether or not to allow browsers to cache user-typed input in the input
  386. * field. Disabling this feature will prevent the widget from setting the
  387. * autocomplete="off" on the input field. When autocomplete="off"
  388. * and users click the back button after form submission, user-typed input can
  389. * be prefilled by the browser from its cache. This caching of user input may
  390. * not be desired for sensitive data, such as credit card numbers, in which
  391. * case, implementers should consider setting allowBrowserAutocomplete to false.
  392. *
  393. * @property allowBrowserAutocomplete
  394. * @type Boolean
  395. * @default true
  396. */
  397. YAHOO.widget.AutoComplete.prototype.allowBrowserAutocomplete = true;
  398. /**
  399. * Enabling this feature prevents the toggling of the container to a collapsed state.
  400. * Setting to true does not automatically trigger the opening of the container.
  401. * Implementers are advised to pre-load the container with an explicit "sendQuery()" call.
  402. *
  403. * @property alwaysShowContainer
  404. * @type Boolean
  405. * @default false
  406. */
  407. YAHOO.widget.AutoComplete.prototype.alwaysShowContainer = false;
  408. /**
  409. * Whether or not to use an iFrame to layer over Windows form elements in
  410. * IE. Set to true only when the results container will be on top of a
  411. * &lt;select&gt; field in IE and thus exposed to the IE z-index bug (i.e.,
  412. * 5.5 < IE < 7).
  413. *
  414. * @property useIFrame
  415. * @type Boolean
  416. * @default false
  417. */
  418. YAHOO.widget.AutoComplete.prototype.useIFrame = false;
  419. /**
  420. * Whether or not the results container should have a shadow.
  421. *
  422. * @property useShadow
  423. * @type Boolean
  424. * @default false
  425. */
  426. YAHOO.widget.AutoComplete.prototype.useShadow = false;
  427. /**
  428. * Whether or not the input field should be updated with selections.
  429. *
  430. * @property suppressInputUpdate
  431. * @type Boolean
  432. * @default false
  433. */
  434. YAHOO.widget.AutoComplete.prototype.suppressInputUpdate = false;
  435. /**
  436. * For backward compatibility to pre-2.6.0 formatResults() signatures, setting
  437. * resultsTypeList to true will take each object literal result returned by
  438. * DataSource and flatten into an array.
  439. *
  440. * @property resultTypeList
  441. * @type Boolean
  442. * @default true
  443. */
  444. YAHOO.widget.AutoComplete.prototype.resultTypeList = true;
  445. /**
  446. * For XHR DataSources, AutoComplete will automatically insert a "?" between the server URI and
  447. * the "query" param/value pair. To prevent this behavior, implementers should
  448. * set this value to false. To more fully customize the query syntax, implementers
  449. * should override the generateRequest() method.
  450. *
  451. * @property queryQuestionMark
  452. * @type Boolean
  453. * @default true
  454. */
  455. YAHOO.widget.AutoComplete.prototype.queryQuestionMark = true;
  456. /////////////////////////////////////////////////////////////////////////////
  457. //
  458. // Public methods
  459. //
  460. /////////////////////////////////////////////////////////////////////////////
  461. /**
  462. * Public accessor to the unique name of the AutoComplete instance.
  463. *
  464. * @method toString
  465. * @return {String} Unique name of the AutoComplete instance.
  466. */
  467. YAHOO.widget.AutoComplete.prototype.toString = function() {
  468. return "AutoComplete " + this._sName;
  469. };
  470. /**
  471. * Returns DOM reference to input element.
  472. *
  473. * @method getInputEl
  474. * @return {HTMLELement} DOM reference to input element.
  475. */
  476. YAHOO.widget.AutoComplete.prototype.getInputEl = function() {
  477. return this._elTextbox;
  478. };
  479. /**
  480. * Returns DOM reference to container element.
  481. *
  482. * @method getContainerEl
  483. * @return {HTMLELement} DOM reference to container element.
  484. */
  485. YAHOO.widget.AutoComplete.prototype.getContainerEl = function() {
  486. return this._elContainer;
  487. };
  488. /**
  489. * Returns true if widget instance is currently focused.
  490. *
  491. * @method isFocused
  492. * @return {Boolean} Returns true if widget instance is currently focused.
  493. */
  494. YAHOO.widget.AutoComplete.prototype.isFocused = function() {
  495. return (this._bFocused === null) ? false : this._bFocused;
  496. };
  497. /**
  498. * Returns true if container is in an expanded state, false otherwise.
  499. *
  500. * @method isContainerOpen
  501. * @return {Boolean} Returns true if container is in an expanded state, false otherwise.
  502. */
  503. YAHOO.widget.AutoComplete.prototype.isContainerOpen = function() {
  504. return this._bContainerOpen;
  505. };
  506. /**
  507. * Public accessor to the &lt;ul&gt; element that displays query results within the results container.
  508. *
  509. * @method getListEl
  510. * @return {HTMLElement[]} Reference to &lt;ul&gt; element within the results container.
  511. */
  512. YAHOO.widget.AutoComplete.prototype.getListEl = function() {
  513. return this._elList;
  514. };
  515. /**
  516. * Public accessor to the matching string associated with a given &lt;li&gt; result.
  517. *
  518. * @method getListItemMatch
  519. * @param elListItem {HTMLElement} Reference to &lt;LI&gt; element.
  520. * @return {String} Matching string.
  521. */
  522. YAHOO.widget.AutoComplete.prototype.getListItemMatch = function(elListItem) {
  523. if(elListItem._sResultMatch) {
  524. return elListItem._sResultMatch;
  525. }
  526. else {
  527. return null;
  528. }
  529. };
  530. /**
  531. * Public accessor to the result data associated with a given &lt;li&gt; result.
  532. *
  533. * @method getListItemData
  534. * @param elListItem {HTMLElement} Reference to &lt;LI&gt; element.
  535. * @return {Object} Result data.
  536. */
  537. YAHOO.widget.AutoComplete.prototype.getListItemData = function(elListItem) {
  538. if(elListItem._oResultData) {
  539. return elListItem._oResultData;
  540. }
  541. else {
  542. return null;
  543. }
  544. };
  545. /**
  546. * Public accessor to the index of the associated with a given &lt;li&gt; result.
  547. *
  548. * @method getListItemIndex
  549. * @param elListItem {HTMLElement} Reference to &lt;LI&gt; element.
  550. * @return {Number} Index.
  551. */
  552. YAHOO.widget.AutoComplete.prototype.getListItemIndex = function(elListItem) {
  553. if(YAHOO.lang.isNumber(elListItem._nItemIndex)) {
  554. return elListItem._nItemIndex;
  555. }
  556. else {
  557. return null;
  558. }
  559. };
  560. /**
  561. * Sets HTML markup for the results container header. This markup will be
  562. * inserted within a &lt;div&gt; tag with a class of "yui-ac-hd".
  563. *
  564. * @method setHeader
  565. * @param sHeader {String} HTML markup for results container header.
  566. */
  567. YAHOO.widget.AutoComplete.prototype.setHeader = function(sHeader) {
  568. if(this._elHeader) {
  569. var elHeader = this._elHeader;
  570. if(sHeader) {
  571. elHeader.innerHTML = sHeader;
  572. elHeader.style.display = "block";
  573. }
  574. else {
  575. elHeader.innerHTML = "";
  576. elHeader.style.display = "none";
  577. }
  578. }
  579. };
  580. /**
  581. * Sets HTML markup for the results container footer. This markup will be
  582. * inserted within a &lt;div&gt; tag with a class of "yui-ac-ft".
  583. *
  584. * @method setFooter
  585. * @param sFooter {String} HTML markup for results container footer.
  586. */
  587. YAHOO.widget.AutoComplete.prototype.setFooter = function(sFooter) {
  588. if(this._elFooter) {
  589. var elFooter = this._elFooter;
  590. if(sFooter) {
  591. elFooter.innerHTML = sFooter;
  592. elFooter.style.display = "block";
  593. }
  594. else {
  595. elFooter.innerHTML = "";
  596. elFooter.style.display = "none";
  597. }
  598. }
  599. };
  600. /**
  601. * Sets HTML markup for the results container body. This markup will be
  602. * inserted within a &lt;div&gt; tag with a class of "yui-ac-bd".
  603. *
  604. * @method setBody
  605. * @param sBody {String} HTML markup for results container body.
  606. */
  607. YAHOO.widget.AutoComplete.prototype.setBody = function(sBody) {
  608. if(this._elBody) {
  609. var elBody = this._elBody;
  610. YAHOO.util.Event.purgeElement(elBody, true);
  611. if(sBody) {
  612. elBody.innerHTML = sBody;
  613. elBody.style.display = "block";
  614. }
  615. else {
  616. elBody.innerHTML = "";
  617. elBody.style.display = "none";
  618. }
  619. this._elList = null;
  620. }
  621. };
  622. /**
  623. * A function that converts an AutoComplete query into a request value which is then
  624. * passed to the DataSource's sendRequest method in order to retrieve data for
  625. * the query. By default, returns a String with the syntax: "query={query}"
  626. * Implementers can customize this method for custom request syntaxes.
  627. *
  628. * @method generateRequest
  629. * @param sQuery {String} Query string
  630. * @return {MIXED} Request
  631. */
  632. YAHOO.widget.AutoComplete.prototype.generateRequest = function(sQuery) {
  633. var dataType = this.dataSource.dataType;
  634. // Transform query string in to a request for remote data
  635. // By default, local data doesn't need a transformation, just passes along the query as is.
  636. if(dataType === YAHOO.util.DataSourceBase.TYPE_XHR) {
  637. // By default, XHR GET requests look like "{scriptURI}?{scriptQueryParam}={sQuery}&{scriptQueryAppend}"
  638. if(!this.dataSource.connMethodPost) {
  639. sQuery = (this.queryQuestionMark ? "?" : "") + (this.dataSource.scriptQueryParam || "query") + "=" + sQuery +
  640. (this.dataSource.scriptQueryAppend ? ("&" + this.dataSource.scriptQueryAppend) : "");
  641. }
  642. // By default, XHR POST bodies are sent to the {scriptURI} like "{scriptQueryParam}={sQuery}&{scriptQueryAppend}"
  643. else {
  644. sQuery = (this.dataSource.scriptQueryParam || "query") + "=" + sQuery +
  645. (this.dataSource.scriptQueryAppend ? ("&" + this.dataSource.scriptQueryAppend) : "");
  646. }
  647. }
  648. // By default, remote script node requests look like "{scriptURI}&{scriptCallbackParam}={callbackString}&{scriptQueryParam}={sQuery}&{scriptQueryAppend}"
  649. else if(dataType === YAHOO.util.DataSourceBase.TYPE_SCRIPTNODE) {
  650. sQuery = "&" + (this.dataSource.scriptQueryParam || "query") + "=" + sQuery +
  651. (this.dataSource.scriptQueryAppend ? ("&" + this.dataSource.scriptQueryAppend) : "");
  652. }
  653. return sQuery;
  654. };
  655. /**
  656. * Makes query request to the DataSource.
  657. *
  658. * @method sendQuery
  659. * @param sQuery {String} Query string.
  660. */
  661. YAHOO.widget.AutoComplete.prototype.sendQuery = function(sQuery) {
  662. // Reset focus for a new interaction
  663. this._bFocused = null;
  664. // Adjust programatically sent queries to look like they were input by user
  665. // when delimiters are enabled
  666. var newQuery = (this.delimChar) ? this._elTextbox.value + sQuery : sQuery;
  667. this._sendQuery(newQuery);
  668. };
  669. /**
  670. * Collapses container.
  671. *
  672. * @method collapseContainer
  673. */
  674. YAHOO.widget.AutoComplete.prototype.collapseContainer = function() {
  675. this._toggleContainer(false);
  676. };
  677. /**
  678. * Handles subset matching for when queryMatchSubset is enabled.
  679. *
  680. * @method getSubsetMatches
  681. * @param sQuery {String} Query string.
  682. * @return {Object} oParsedResponse or null.
  683. */
  684. YAHOO.widget.AutoComplete.prototype.getSubsetMatches = function(sQuery) {
  685. var subQuery, oCachedResponse, subRequest;
  686. // Loop through substrings of each cached element's query property...
  687. for(var i = sQuery.length; i >= this.minQueryLength ; i--) {
  688. subRequest = this.generateRequest(sQuery.substr(0,i));
  689. this.dataRequestEvent.fire(this, subQuery, subRequest);
  690. // If a substring of the query is found in the cache
  691. oCachedResponse = this.dataSource.getCachedResponse(subRequest);
  692. if(oCachedResponse) {
  693. return this.filterResults.apply(this.dataSource, [sQuery, oCachedResponse, oCachedResponse, {scope:this}]);
  694. }
  695. }
  696. return null;
  697. };
  698. /**
  699. * Executed by DataSource (within DataSource scope via doBeforeParseData()) to
  700. * handle responseStripAfter cleanup.
  701. *
  702. * @method preparseRawResponse
  703. * @param sQuery {String} Query string.
  704. * @return {Object} oParsedResponse or null.
  705. */
  706. YAHOO.widget.AutoComplete.prototype.preparseRawResponse = function(oRequest, oFullResponse, oCallback) {
  707. var nEnd = ((this.responseStripAfter !== "") && (oFullResponse.indexOf)) ?
  708. oFullResponse.indexOf(this.responseStripAfter) : -1;
  709. if(nEnd != -1) {
  710. oFullResponse = oFullResponse.substring(0,nEnd);
  711. }
  712. return oFullResponse;
  713. };
  714. /**
  715. * Executed by DataSource (within DataSource scope via doBeforeCallback()) to
  716. * filter results through a simple client-side matching algorithm.
  717. *
  718. * @method filterResults
  719. * @param sQuery {String} Original request.
  720. * @param oFullResponse {Object} Full response object.
  721. * @param oParsedResponse {Object} Parsed response object.
  722. * @param oCallback {Object} Callback object.
  723. * @return {Object} Filtered response object.
  724. */
  725. YAHOO.widget.AutoComplete.prototype.filterResults = function(sQuery, oFullResponse, oParsedResponse, oCallback) {
  726. // If AC has passed a query string value back to itself, grab it
  727. if(oCallback && oCallback.argument && oCallback.argument.query) {
  728. sQuery = oCallback.argument.query;
  729. }
  730. // Only if a query string is available to match against
  731. if(sQuery && sQuery !== "") {
  732. // First make a copy of the oParseResponse
  733. oParsedResponse = YAHOO.widget.AutoComplete._cloneObject(oParsedResponse);
  734. var oAC = oCallback.scope,
  735. oDS = this,
  736. allResults = oParsedResponse.results, // the array of results
  737. filteredResults = [], // container for filtered results
  738. bMatchFound = false,
  739. bMatchCase = (oDS.queryMatchCase || oAC.queryMatchCase), // backward compat
  740. bMatchContains = (oDS.queryMatchContains || oAC.queryMatchContains); // backward compat
  741. // Loop through each result object...
  742. for(var i = allResults.length-1; i >= 0; i--) {
  743. var oResult = allResults[i];
  744. // Grab the data to match against from the result object...
  745. var sResult = null;
  746. // Result object is a simple string already
  747. if(YAHOO.lang.isString(oResult)) {
  748. sResult = oResult;
  749. }
  750. // Result object is an array of strings
  751. else if(YAHOO.lang.isArray(oResult)) {
  752. sResult = oResult[0];
  753. }
  754. // Result object is an object literal of strings
  755. else if(this.responseSchema.fields) {
  756. var key = this.responseSchema.fields[0].key || this.responseSchema.fields[0];
  757. sResult = oResult[key];
  758. }
  759. // Backwards compatibility
  760. else if(this.key) {
  761. sResult = oResult[this.key];
  762. }
  763. if(YAHOO.lang.isString(sResult)) {
  764. var sKeyIndex = (bMatchCase) ?
  765. sResult.indexOf(decodeURIComponent(sQuery)) :
  766. sResult.toLowerCase().indexOf(decodeURIComponent(sQuery).toLowerCase());
  767. // A STARTSWITH match is when the query is found at the beginning of the key string...
  768. if((!bMatchContains && (sKeyIndex === 0)) ||
  769. // A CONTAINS match is when the query is found anywhere within the key string...
  770. (bMatchContains && (sKeyIndex > -1))) {
  771. // Stash the match
  772. filteredResults.unshift(oResult);
  773. }
  774. }
  775. }
  776. oParsedResponse.results = filteredResults;
  777. }
  778. else {
  779. }
  780. return oParsedResponse;
  781. };
  782. /**
  783. * Handles response for display. This is the callback function method passed to
  784. * YAHOO.util.DataSourceBase#sendRequest so results from the DataSource are
  785. * returned to the AutoComplete instance.
  786. *
  787. * @method handleResponse
  788. * @param sQuery {String} Original request.
  789. * @param oResponse {Object} Response object.
  790. * @param oPayload {MIXED} (optional) Additional argument(s)
  791. */
  792. YAHOO.widget.AutoComplete.prototype.handleResponse = function(sQuery, oResponse, oPayload) {
  793. if((this instanceof YAHOO.widget.AutoComplete) && this._sName) {
  794. this._populateList(sQuery, oResponse, oPayload);
  795. }
  796. };
  797. /**
  798. * Overridable method called before container is loaded with result data.
  799. *
  800. * @method doBeforeLoadData
  801. * @param sQuery {String} Original request.
  802. * @param oResponse {Object} Response object.
  803. * @param oPayload {MIXED} (optional) Additional argument(s)
  804. * @return {Boolean} Return true to continue loading data, false to cancel.
  805. */
  806. YAHOO.widget.AutoComplete.prototype.doBeforeLoadData = function(sQuery, oResponse, oPayload) {
  807. return true;
  808. };
  809. /**
  810. * Overridable method that returns HTML markup for one result to be populated
  811. * as innerHTML of an &lt;LI&gt; element.
  812. *
  813. * @method formatResult
  814. * @param oResultData {Object} Result data object.
  815. * @param sQuery {String} The corresponding query string.
  816. * @param sResultMatch {HTMLElement} The current query string.
  817. * @return {String} HTML markup of formatted result data.
  818. */
  819. YAHOO.widget.AutoComplete.prototype.formatResult = function(oResultData, sQuery, sResultMatch) {
  820. var sMarkup = (sResultMatch) ? sResultMatch : "";
  821. return sMarkup;
  822. };
  823. /**
  824. * Overridable method called before container expands allows implementers to access data
  825. * and DOM elements.
  826. *
  827. * @method doBeforeExpandContainer
  828. * @param elTextbox {HTMLElement} The text input box.
  829. * @param elContainer {HTMLElement} The container element.
  830. * @param sQuery {String} The query string.
  831. * @param aResults {Object[]} An array of query results.
  832. * @return {Boolean} Return true to continue expanding container, false to cancel the expand.
  833. */
  834. YAHOO.widget.AutoComplete.prototype.doBeforeExpandContainer = function(elTextbox, elContainer, sQuery, aResults) {
  835. return true;
  836. };
  837. /**
  838. * Nulls out the entire AutoComplete instance and related objects, removes attached
  839. * event listeners, and clears out DOM elements inside the container. After
  840. * calling this method, the instance reference should be expliclitly nulled by
  841. * implementer, as in myAutoComplete = null. Use with caution!
  842. *
  843. * @method destroy
  844. */
  845. YAHOO.widget.AutoComplete.prototype.destroy = function() {
  846. var instanceName = this.toString();
  847. var elInput = this._elTextbox;
  848. var elContainer = this._elContainer;
  849. // Unhook custom events
  850. this.textboxFocusEvent.unsubscribeAll();
  851. this.textboxKeyEvent.unsubscribeAll();
  852. this.dataRequestEvent.unsubscribeAll();
  853. this.dataReturnEvent.unsubscribeAll();
  854. this.dataErrorEvent.unsubscribeAll();
  855. this.containerPopulateEvent.unsubscribeAll();
  856. this.containerExpandEvent.unsubscribeAll();
  857. this.typeAheadEvent.unsubscribeAll();
  858. this.itemMouseOverEvent.unsubscribeAll();
  859. this.itemMouseOutEvent.unsubscribeAll();
  860. this.itemArrowToEvent.unsubscribeAll();
  861. this.itemArrowFromEvent.unsubscribeAll();
  862. this.itemSelectEvent.unsubscribeAll();
  863. this.unmatchedItemSelectEvent.unsubscribeAll();
  864. this.selectionEnforceEvent.unsubscribeAll();
  865. this.containerCollapseEvent.unsubscribeAll();
  866. this.textboxBlurEvent.unsubscribeAll();
  867. this.textboxChangeEvent.unsubscribeAll();
  868. // Unhook DOM events
  869. YAHOO.util.Event.purgeElement(elInput, true);
  870. YAHOO.util.Event.purgeElement(elContainer, true);
  871. // Remove DOM elements
  872. elContainer.innerHTML = "";
  873. // Null out objects
  874. for(var key in this) {
  875. if(YAHOO.lang.hasOwnProperty(this, key)) {
  876. this[key] = null;
  877. }
  878. }
  879. };
  880. /////////////////////////////////////////////////////////////////////////////
  881. //
  882. // Public events
  883. //
  884. /////////////////////////////////////////////////////////////////////////////
  885. /**
  886. * Fired when the input field receives focus.
  887. *
  888. * @event textboxFocusEvent
  889. * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
  890. */
  891. YAHOO.widget.AutoComplete.prototype.textboxFocusEvent = null;
  892. /**
  893. * Fired when the input field receives key input.
  894. *
  895. * @event textboxKeyEvent
  896. * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
  897. * @param nKeycode {Number} The keycode number.
  898. */
  899. YAHOO.widget.AutoComplete.prototype.textboxKeyEvent = null;
  900. /**
  901. * Fired when the AutoComplete instance makes a request to the DataSource.
  902. *
  903. * @event dataRequestEvent
  904. * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
  905. * @param sQuery {String} The query string.
  906. * @param oRequest {Object} The request.
  907. */
  908. YAHOO.widget.AutoComplete.prototype.dataRequestEvent = null;
  909. /**
  910. * Fired when the AutoComplete instance receives query results from the data
  911. * source.
  912. *
  913. * @event dataReturnEvent
  914. * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
  915. * @param sQuery {String} The query string.
  916. * @param aResults {Object[]} Results array.
  917. */
  918. YAHOO.widget.AutoComplete.prototype.dataReturnEvent = null;
  919. /**
  920. * Fired when the AutoComplete instance does not receive query results from the
  921. * DataSource due to an error.
  922. *
  923. * @event dataErrorEvent
  924. * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
  925. * @param sQuery {String} The query string.
  926. */
  927. YAHOO.widget.AutoComplete.prototype.dataErrorEvent = null;
  928. /**
  929. * Fired when the results container is populated.
  930. *
  931. * @event containerPopulateEvent
  932. * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
  933. */
  934. YAHOO.widget.AutoComplete.prototype.containerPopulateEvent = null;
  935. /**
  936. * Fired when the results container is expanded.
  937. *
  938. * @event containerExpandEvent
  939. * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
  940. */
  941. YAHOO.widget.AutoComplete.prototype.containerExpandEvent = null;
  942. /**
  943. * Fired when the input field has been prefilled by the type-ahead
  944. * feature.
  945. *
  946. * @event typeAheadEvent
  947. * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
  948. * @param sQuery {String} The query string.
  949. * @param sPrefill {String} The prefill string.
  950. */
  951. YAHOO.widget.AutoComplete.prototype.typeAheadEvent = null;
  952. /**
  953. * Fired when result item has been moused over.
  954. *
  955. * @event itemMouseOverEvent
  956. * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
  957. * @param elItem {HTMLElement} The &lt;li&gt element item moused to.
  958. */
  959. YAHOO.widget.AutoComplete.prototype.itemMouseOverEvent = null;
  960. /**
  961. * Fired when result item has been moused out.
  962. *
  963. * @event itemMouseOutEvent
  964. * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
  965. * @param elItem {HTMLElement} The &lt;li&gt; element item moused from.
  966. */
  967. YAHOO.widget.AutoComplete.prototype.itemMouseOutEvent = null;
  968. /**
  969. * Fired when result item has been arrowed to.
  970. *
  971. * @event itemArrowToEvent
  972. * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
  973. * @param elItem {HTMLElement} The &lt;li&gt; element item arrowed to.
  974. */
  975. YAHOO.widget.AutoComplete.prototype.itemArrowToEvent = null;
  976. /**
  977. * Fired when result item has been arrowed away from.
  978. *
  979. * @event itemArrowFromEvent
  980. * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
  981. * @param elItem {HTMLElement} The &lt;li&gt; element item arrowed from.
  982. */
  983. YAHOO.widget.AutoComplete.prototype.itemArrowFromEvent = null;
  984. /**
  985. * Fired when an item is selected via mouse click, ENTER key, or TAB key.
  986. *
  987. * @event itemSelectEvent
  988. * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
  989. * @param elItem {HTMLElement} The selected &lt;li&gt; element item.
  990. * @param oData {Object} The data returned for the item, either as an object,
  991. * or mapped from the schema into an array.
  992. */
  993. YAHOO.widget.AutoComplete.prototype.itemSelectEvent = null;
  994. /**
  995. * Fired when a user selection does not match any of the displayed result items.
  996. *
  997. * @event unmatchedItemSelectEvent
  998. * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
  999. * @param sSelection {String} The selected string.
  1000. */
  1001. YAHOO.widget.AutoComplete.prototype.unmatchedItemSelectEvent = null;
  1002. /**
  1003. * Fired if forceSelection is enabled and the user's input has been cleared
  1004. * because it did not match one of the returned query results.
  1005. *
  1006. * @event selectionEnforceEvent
  1007. * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
  1008. * @param sClearedValue {String} The cleared value (including delimiters if applicable).
  1009. */
  1010. YAHOO.widget.AutoComplete.prototype.selectionEnforceEvent = null;
  1011. /**
  1012. * Fired when the results container is collapsed.
  1013. *
  1014. * @event containerCollapseEvent
  1015. * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
  1016. */
  1017. YAHOO.widget.AutoComplete.prototype.containerCollapseEvent = null;
  1018. /**
  1019. * Fired when the input field loses focus.
  1020. *
  1021. * @event textboxBlurEvent
  1022. * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
  1023. */
  1024. YAHOO.widget.AutoComplete.prototype.textboxBlurEvent = null;
  1025. /**
  1026. * Fired when the input field value has changed when it loses focus.
  1027. *
  1028. * @event textboxChangeEvent
  1029. * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
  1030. */
  1031. YAHOO.widget.AutoComplete.prototype.textboxChangeEvent = null;
  1032. /////////////////////////////////////////////////////////////////////////////
  1033. //
  1034. // Private member variables
  1035. //
  1036. /////////////////////////////////////////////////////////////////////////////
  1037. /**
  1038. * Internal class variable to index multiple AutoComplete instances.
  1039. *
  1040. * @property _nIndex
  1041. * @type Number
  1042. * @default 0
  1043. * @private
  1044. */
  1045. YAHOO.widget.AutoComplete._nIndex = 0;
  1046. /**
  1047. * Name of AutoComplete instance.
  1048. *
  1049. * @property _sName
  1050. * @type String
  1051. * @private
  1052. */
  1053. YAHOO.widget.AutoComplete.prototype._sName = null;
  1054. /**
  1055. * Text input field DOM element.
  1056. *
  1057. * @property _elTextbox
  1058. * @type HTMLElement
  1059. * @private
  1060. */
  1061. YAHOO.widget.AutoComplete.prototype._elTextbox = null;
  1062. /**
  1063. * Container DOM element.
  1064. *
  1065. * @property _elContainer
  1066. * @type HTMLElement
  1067. * @private
  1068. */
  1069. YAHOO.widget.AutoComplete.prototype._elContainer = null;
  1070. /**
  1071. * Reference to content element within container element.
  1072. *
  1073. * @property _elContent
  1074. * @type HTMLElement
  1075. * @private
  1076. */
  1077. YAHOO.widget.AutoComplete.prototype._elContent = null;
  1078. /**
  1079. * Reference to header element within content element.
  1080. *
  1081. * @property _elHeader
  1082. * @type HTMLElement
  1083. * @private
  1084. */
  1085. YAHOO.widget.AutoComplete.prototype._elHeader = null;
  1086. /**
  1087. * Reference to body element within content element.
  1088. *
  1089. * @property _elBody
  1090. * @type HTMLElement
  1091. * @private
  1092. */
  1093. YAHOO.widget.AutoComplete.prototype._elBody = null;
  1094. /**
  1095. * Reference to footer element within content element.
  1096. *
  1097. * @property _elFooter
  1098. * @type HTMLElement
  1099. * @private
  1100. */
  1101. YAHOO.widget.AutoComplete.prototype._elFooter = null;
  1102. /**
  1103. * Reference to shadow element within container element.
  1104. *
  1105. * @property _elShadow
  1106. * @type HTMLElement
  1107. * @private
  1108. */
  1109. YAHOO.widget.AutoComplete.prototype._elShadow = null;
  1110. /**
  1111. * Reference to iframe element within container element.
  1112. *
  1113. * @property _elIFrame
  1114. * @type HTMLElement
  1115. * @private
  1116. */
  1117. YAHOO.widget.AutoComplete.prototype._elIFrame = null;
  1118. /**
  1119. * Whether or not the input field is currently in focus. If query results come back
  1120. * but the user has already moved on, do not proceed with auto complete behavior.
  1121. *
  1122. * @property _bFocused
  1123. * @type Boolean
  1124. * @private
  1125. */
  1126. YAHOO.widget.AutoComplete.prototype._bFocused = null;
  1127. /**
  1128. * Animation instance for container expand/collapse.
  1129. *
  1130. * @property _oAnim
  1131. * @type Boolean
  1132. * @private
  1133. */
  1134. YAHOO.widget.AutoComplete.prototype._oAnim = null;
  1135. /**
  1136. * Whether or not the results container is currently open.
  1137. *
  1138. * @property _bContainerOpen
  1139. * @type Boolean
  1140. * @private
  1141. */
  1142. YAHOO.widget.AutoComplete.prototype._bContainerOpen = false;
  1143. /**
  1144. * Whether or not the mouse is currently over the results
  1145. * container. This is necessary in order to prevent clicks on container items
  1146. * from being text input field blur events.
  1147. *
  1148. * @property _bOverContainer
  1149. * @type Boolean
  1150. * @private
  1151. */
  1152. YAHOO.widget.AutoComplete.prototype._bOverContainer = false;
  1153. /**
  1154. * Internal reference to &lt;ul&gt; elements that contains query results within the
  1155. * results container.
  1156. *
  1157. * @property _elList
  1158. * @type HTMLElement
  1159. * @private
  1160. */
  1161. YAHOO.widget.AutoComplete.prototype._elList = null;
  1162. /*
  1163. * Array of &lt;li&gt; elements references that contain query results within the
  1164. * results container.
  1165. *
  1166. * @property _aListItemEls
  1167. * @type HTMLElement[]
  1168. * @private
  1169. */
  1170. //YAHOO.widget.AutoComplete.prototype._aListItemEls = null;
  1171. /**
  1172. * Number of &lt;li&gt; elements currently displayed in results container.
  1173. *
  1174. * @property _nDisplayedItems
  1175. * @type Number
  1176. * @private
  1177. */
  1178. YAHOO.widget.AutoComplete.prototype._nDisplayedItems = 0;
  1179. /*
  1180. * Internal count of &lt;li&gt; elements displayed and hidden in results container.
  1181. *
  1182. * @property _maxResultsDisplayed
  1183. * @type Number
  1184. * @private
  1185. */
  1186. //YAHOO.widget.AutoComplete.prototype._maxResultsDisplayed = 0;
  1187. /**
  1188. * Current query string
  1189. *
  1190. * @property _sCurQuery
  1191. * @type String
  1192. * @private
  1193. */
  1194. YAHOO.widget.AutoComplete.prototype._sCurQuery = null;
  1195. /**
  1196. * Selections from previous queries (for saving delimited queries).
  1197. *
  1198. * @property _sPastSelections
  1199. * @type String
  1200. * @default ""
  1201. * @private
  1202. */
  1203. YAHOO.widget.AutoComplete.prototype._sPastSelections = "";
  1204. /**
  1205. * Stores initial input value used to determine if textboxChangeEvent should be fired.
  1206. *
  1207. * @property _sInitInputValue
  1208. * @type String
  1209. * @private
  1210. */
  1211. YAHOO.widget.AutoComplete.prototype._sInitInputValue = null;
  1212. /**
  1213. * Pointer to the currently highlighted &lt;li&gt; element in the container.
  1214. *
  1215. * @property _elCurListItem
  1216. * @type HTMLElement
  1217. * @private
  1218. */
  1219. YAHOO.widget.AutoComplete.prototype._elCurListItem = null;
  1220. /**
  1221. * Whether or not an item has been selected since the container was populated
  1222. * with results. Reset to false by _populateList, and set to true when item is
  1223. * selected.
  1224. *
  1225. * @property _bItemSelected
  1226. * @type Boolean
  1227. * @private
  1228. */
  1229. YAHOO.widget.AutoComplete.prototype._bItemSelected = false;
  1230. /**
  1231. * Key code of the last key pressed in textbox.
  1232. *
  1233. * @property _nKeyCode
  1234. * @type Number
  1235. * @private
  1236. */
  1237. YAHOO.widget.AutoComplete.prototype._nKeyCode = null;
  1238. /**
  1239. * Delay timeout ID.
  1240. *
  1241. * @property _nDelayID
  1242. * @type Number
  1243. * @private
  1244. */
  1245. YAHOO.widget.AutoComplete.prototype._nDelayID = -1;
  1246. /**
  1247. * TypeAhead delay timeout ID.
  1248. *
  1249. * @property _nTypeAheadDelayID
  1250. * @type Number
  1251. * @private
  1252. */
  1253. YAHOO.widget.AutoComplete.prototype._nTypeAheadDelayID = -1;
  1254. /**
  1255. * Src to iFrame used when useIFrame = true. Supports implementations over SSL
  1256. * as well.
  1257. *
  1258. * @property _iFrameSrc
  1259. * @type String
  1260. * @private
  1261. */
  1262. YAHOO.widget.AutoComplete.prototype._iFrameSrc = "javascript:false;";
  1263. /**
  1264. * For users typing via certain IMEs, queries must be triggered by intervals,
  1265. * since key events yet supported across all browsers for all IMEs.
  1266. *
  1267. * @property _queryInterval
  1268. * @type Object
  1269. * @private
  1270. */
  1271. YAHOO.widget.AutoComplete.prototype._queryInterval = null;
  1272. /**
  1273. * Internal tracker to last known textbox value, used to determine whether or not
  1274. * to trigger a query via interval for certain IME users.
  1275. *
  1276. * @event _sLastTextboxValue
  1277. * @type String
  1278. * @private
  1279. */
  1280. YAHOO.widget.AutoComplete.prototype._sLastTextboxValue = null;
  1281. /////////////////////////////////////////////////////////////////////////////
  1282. //
  1283. // Private methods
  1284. //
  1285. /////////////////////////////////////////////////////////////////////////////
  1286. /**
  1287. * Updates and validates latest public config properties.
  1288. *
  1289. * @method __initProps
  1290. * @private
  1291. */
  1292. YAHOO.widget.AutoComplete.prototype._initProps = function() {
  1293. // Correct any invalid values
  1294. var minQueryLength = this.minQueryLength;
  1295. if(!YAHOO.lang.isNumber(minQueryLength)) {
  1296. this.minQueryLength = 1;
  1297. }
  1298. var maxResultsDisplayed = this.maxResultsDisplayed;
  1299. if(!YAHOO.lang.isNumber(maxResultsDisplayed) || (maxResultsDisplayed < 1)) {
  1300. this.maxResultsDisplayed = 10;
  1301. }
  1302. var queryDelay = this.queryDelay;
  1303. if(!YAHOO.lang.isNumber(queryDelay) || (queryDelay < 0)) {
  1304. this.queryDelay = 0.2;
  1305. }
  1306. var typeAheadDelay = this.typeAheadDelay;
  1307. if(!YAHOO.lang.isNumber(typeAheadDelay) || (typeAheadDelay < 0)) {
  1308. this.typeAheadDelay = 0.2;
  1309. }
  1310. var delimChar = this.delimChar;
  1311. if(YAHOO.lang.isString(delimChar) && (delimChar.length > 0)) {
  1312. this.delimChar = [delimChar];
  1313. }
  1314. else if(!YAHOO.lang.isArray(delimChar)) {
  1315. this.delimChar = null;
  1316. }
  1317. var animSpeed = this.animSpeed;
  1318. if((this.animHoriz || this.animVert) && YAHOO.util.Anim) {
  1319. if(!YAHOO.lang.isNumber(animSpeed) || (animSpeed < 0)) {
  1320. this.animSpeed = 0.3;
  1321. }
  1322. if(!this._oAnim ) {
  1323. this._oAnim = new YAHOO.util.Anim(this._elContent, {}, this.animSpeed);
  1324. }
  1325. else {
  1326. this._oAnim.duration = this.animSpeed;
  1327. }
  1328. }
  1329. if(this.forceSelection && delimChar) {
  1330. }
  1331. };
  1332. /**
  1333. * Initializes the results container helpers if they are enabled and do
  1334. * not exist
  1335. *
  1336. * @method _initContainerHelperEls
  1337. * @private
  1338. */
  1339. YAHOO.widget.AutoComplete.prototype._initContainerHelperEls = function() {
  1340. if(this.useShadow && !this._elShadow) {
  1341. var elShadow = document.createElement("div");
  1342. elShadow.className = "yui-ac-shadow";
  1343. elShadow.style.width = 0;
  1344. elShadow.style.height = 0;
  1345. this._elShadow = this._elContainer.appendChild(elShadow);
  1346. }
  1347. if(this.useIFrame && !this._elIFrame) {
  1348. var elIFrame = document.createElement("iframe");
  1349. elIFrame.src = this._iFrameSrc;
  1350. elIFrame.frameBorder = 0;
  1351. elIFrame.scrolling = "no";
  1352. elIFrame.style.position = "absolute";
  1353. elIFrame.style.width = 0;
  1354. elIFrame.style.height = 0;
  1355. elIFrame.tabIndex = -1;
  1356. elIFrame.style.padding = 0;
  1357. this._elIFrame = this._elContainer.appendChild(elIFrame);
  1358. }
  1359. };
  1360. /**
  1361. * Initializes the results container once at object creation
  1362. *
  1363. * @method _initContainerEl
  1364. * @private
  1365. */
  1366. YAHOO.widget.AutoComplete.prototype._initContainerEl = function() {
  1367. YAHOO.util.Dom.addClass(this._elContainer, "yui-ac-container");
  1368. if(!this._elContent) {
  1369. // The elContent div is assigned DOM listeners and
  1370. // helps size the iframe and shadow properly
  1371. var elContent = document.createElement("div");
  1372. elContent.className = "yui-ac-content";
  1373. elContent.style.display = "none";
  1374. this._elContent = this._elContainer.appendChild(elContent);
  1375. var elHeader = document.createElement("div");
  1376. elHeader.className = "yui-ac-hd";
  1377. elHeader.style.display = "none";
  1378. this._elHeader = this._elContent.appendChild(elHeader);
  1379. var elBody = document.createElement("div");
  1380. elBody.className = "yui-ac-bd";
  1381. this._elBody = this._elContent.appendChild(elBody);
  1382. var elFooter = document.createElement("div");
  1383. elFooter.className = "yui-ac-ft";
  1384. elFooter.style.display = "none";
  1385. this._elFooter = this._elContent.appendChild(elFooter);
  1386. }
  1387. else {
  1388. }
  1389. };
  1390. /**
  1391. * Clears out contents of container body an…