PageRenderTime 64ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

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

http://struts2yuiplugin.googlecode.com/
JavaScript | 2908 lines | 1343 code | 279 blank | 1286 comment | 372 complexity | 4c9f18b74e0a2ac25136d6e9e77ad62b MD5 | raw 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. YAHOO.log("Could not instantiate AutoComplete due to an invalid DataSource", "error", this.toString());
  69. return;
  70. }
  71. // YAHOO.widget.DataSource schema backwards compatibility
  72. // Converted deprecated schema into supported schema
  73. // First assume key data is held in position 0 of results array
  74. this.key = 0;
  75. var schema = oDataSource.responseSchema;
  76. // An old school schema has been defined in the deprecated DataSource constructor
  77. if(oDataSource._aDeprecatedSchema) {
  78. var aDeprecatedSchema = oDataSource._aDeprecatedSchema;
  79. if(YAHOO.lang.isArray(aDeprecatedSchema)) {
  80. if((oDataSource.responseType === YAHOO.util.DataSourceBase.TYPE_JSON) ||
  81. (oDataSource.responseType === YAHOO.util.DataSourceBase.TYPE_UNKNOWN)) { // Used to default to unknown
  82. // Store the resultsList
  83. schema.resultsList = aDeprecatedSchema[0];
  84. // Store the key
  85. this.key = aDeprecatedSchema[1];
  86. // Only resultsList and key are defined, so grab all the data
  87. schema.fields = (aDeprecatedSchema.length < 3) ? null : aDeprecatedSchema.slice(1);
  88. }
  89. else if(oDataSource.responseType === YAHOO.util.DataSourceBase.TYPE_XML) {
  90. schema.resultNode = aDeprecatedSchema[0];
  91. this.key = aDeprecatedSchema[1];
  92. schema.fields = aDeprecatedSchema.slice(1);
  93. }
  94. else if(oDataSource.responseType === YAHOO.util.DataSourceBase.TYPE_TEXT) {
  95. schema.recordDelim = aDeprecatedSchema[0];
  96. schema.fieldDelim = aDeprecatedSchema[1];
  97. }
  98. oDataSource.responseSchema = schema;
  99. }
  100. }
  101. // Validate input element
  102. if(YAHOO.util.Dom.inDocument(elInput)) {
  103. if(YAHOO.lang.isString(elInput)) {
  104. this._sName = "instance" + YAHOO.widget.AutoComplete._nIndex + " " + elInput;
  105. this._elTextbox = document.getElementById(elInput);
  106. }
  107. else {
  108. this._sName = (elInput.id) ?
  109. "instance" + YAHOO.widget.AutoComplete._nIndex + " " + elInput.id:
  110. "instance" + YAHOO.widget.AutoComplete._nIndex;
  111. this._elTextbox = elInput;
  112. }
  113. YAHOO.util.Dom.addClass(this._elTextbox, "yui-ac-input");
  114. }
  115. else {
  116. YAHOO.log("Could not instantiate AutoComplete due to an invalid input element", "error", this.toString());
  117. return;
  118. }
  119. // Validate container element
  120. if(YAHOO.util.Dom.inDocument(elContainer)) {
  121. if(YAHOO.lang.isString(elContainer)) {
  122. this._elContainer = document.getElementById(elContainer);
  123. }
  124. else {
  125. this._elContainer = elContainer;
  126. }
  127. if(this._elContainer.style.display == "none") {
  128. YAHOO.log("The container may not display properly if display is set to \"none\" in CSS", "warn", this.toString());
  129. }
  130. // For skinning
  131. var elParent = this._elContainer.parentNode;
  132. var elTag = elParent.tagName.toLowerCase();
  133. if(elTag == "div") {
  134. YAHOO.util.Dom.addClass(elParent, "yui-ac");
  135. }
  136. else {
  137. YAHOO.log("Could not find the wrapper element for skinning", "warn", this.toString());
  138. }
  139. }
  140. else {
  141. YAHOO.log("Could not instantiate AutoComplete due to an invalid container element", "error", this.toString());
  142. return;
  143. }
  144. // Default applyLocalFilter setting is to enable for local sources
  145. if(this.dataSource.dataType === YAHOO.util.DataSourceBase.TYPE_LOCAL) {
  146. this.applyLocalFilter = true;
  147. }
  148. // Set any config params passed in to override defaults
  149. if(oConfigs && (oConfigs.constructor == Object)) {
  150. for(var sConfig in oConfigs) {
  151. if(sConfig) {
  152. this[sConfig] = oConfigs[sConfig];
  153. }
  154. }
  155. }
  156. // Initialization sequence
  157. this._initContainerEl();
  158. this._initProps();
  159. this._initListEl();
  160. this._initContainerHelperEls();
  161. // Set up events
  162. var oSelf = this;
  163. var elTextbox = this._elTextbox;
  164. // Dom events
  165. YAHOO.util.Event.addListener(elTextbox,"keyup",oSelf._onTextboxKeyUp,oSelf);
  166. YAHOO.util.Event.addListener(elTextbox,"keydown",oSelf._onTextboxKeyDown,oSelf);
  167. YAHOO.util.Event.addListener(elTextbox,"focus",oSelf._onTextboxFocus,oSelf);
  168. YAHOO.util.Event.addListener(elTextbox,"blur",oSelf._onTextboxBlur,oSelf);
  169. YAHOO.util.Event.addListener(elContainer,"mouseover",oSelf._onContainerMouseover,oSelf);
  170. YAHOO.util.Event.addListener(elContainer,"mouseout",oSelf._onContainerMouseout,oSelf);
  171. YAHOO.util.Event.addListener(elContainer,"click",oSelf._onContainerClick,oSelf);
  172. YAHOO.util.Event.addListener(elContainer,"scroll",oSelf._onContainerScroll,oSelf);
  173. YAHOO.util.Event.addListener(elContainer,"resize",oSelf._onContainerResize,oSelf);
  174. YAHOO.util.Event.addListener(elTextbox,"keypress",oSelf._onTextboxKeyPress,oSelf);
  175. YAHOO.util.Event.addListener(window,"unload",oSelf._onWindowUnload,oSelf);
  176. // Custom events
  177. this.textboxFocusEvent = new YAHOO.util.CustomEvent("textboxFocus", this);
  178. this.textboxKeyEvent = new YAHOO.util.CustomEvent("textboxKey", this);
  179. this.dataRequestEvent = new YAHOO.util.CustomEvent("dataRequest", this);
  180. this.dataReturnEvent = new YAHOO.util.CustomEvent("dataReturn", this);
  181. this.dataErrorEvent = new YAHOO.util.CustomEvent("dataError", this);
  182. this.containerPopulateEvent = new YAHOO.util.CustomEvent("containerPopulate", this);
  183. this.containerExpandEvent = new YAHOO.util.CustomEvent("containerExpand", this);
  184. this.typeAheadEvent = new YAHOO.util.CustomEvent("typeAhead", this);
  185. this.itemMouseOverEvent = new YAHOO.util.CustomEvent("itemMouseOver", this);
  186. this.itemMouseOutEvent = new YAHOO.util.CustomEvent("itemMouseOut", this);
  187. this.itemArrowToEvent = new YAHOO.util.CustomEvent("itemArrowTo", this);
  188. this.itemArrowFromEvent = new YAHOO.util.CustomEvent("itemArrowFrom", this);
  189. this.itemSelectEvent = new YAHOO.util.CustomEvent("itemSelect", this);
  190. this.unmatchedItemSelectEvent = new YAHOO.util.CustomEvent("unmatchedItemSelect", this);
  191. this.selectionEnforceEvent = new YAHOO.util.CustomEvent("selectionEnforce", this);
  192. this.containerCollapseEvent = new YAHOO.util.CustomEvent("containerCollapse", this);
  193. this.textboxBlurEvent = new YAHOO.util.CustomEvent("textboxBlur", this);
  194. this.textboxChangeEvent = new YAHOO.util.CustomEvent("textboxChange", this);
  195. // Finish up
  196. elTextbox.setAttribute("autocomplete","off");
  197. YAHOO.widget.AutoComplete._nIndex++;
  198. YAHOO.log("AutoComplete initialized","info",this.toString());
  199. }
  200. // Required arguments were not found
  201. else {
  202. YAHOO.log("Could not instantiate AutoComplete due invalid arguments", "error", this.toString());
  203. }
  204. };
  205. /////////////////////////////////////////////////////////////////////////////
  206. //
  207. // Public member variables
  208. //
  209. /////////////////////////////////////////////////////////////////////////////
  210. /**
  211. * The DataSource object that encapsulates the data used for auto completion.
  212. * This object should be an inherited object from YAHOO.widget.DataSource.
  213. *
  214. * @property dataSource
  215. * @type YAHOO.widget.DataSource
  216. */
  217. YAHOO.widget.AutoComplete.prototype.dataSource = null;
  218. /**
  219. * By default, results from local DataSources will pass through the filterResults
  220. * method to apply a client-side matching algorithm.
  221. *
  222. * @property applyLocalFilter
  223. * @type Boolean
  224. * @default true for local arrays and json, otherwise false
  225. */
  226. YAHOO.widget.AutoComplete.prototype.applyLocalFilter = null;
  227. /**
  228. * When applyLocalFilter is true, the local filtering algorthim can have case sensitivity
  229. * enabled.
  230. *
  231. * @property queryMatchCase
  232. * @type Boolean
  233. * @default false
  234. */
  235. YAHOO.widget.AutoComplete.prototype.queryMatchCase = false;
  236. /**
  237. * When applyLocalFilter is true, results can be locally filtered to return
  238. * matching strings that "contain" the query string rather than simply "start with"
  239. * the query string.
  240. *
  241. * @property queryMatchContains
  242. * @type Boolean
  243. * @default false
  244. */
  245. YAHOO.widget.AutoComplete.prototype.queryMatchContains = false;
  246. /**
  247. * Enables query subset matching. When the DataSource's cache is enabled and queryMatchSubset is
  248. * true, substrings of queries will return matching cached results. For
  249. * instance, if the first query is for "abc" susequent queries that start with
  250. * "abc", like "abcd", will be queried against the cache, and not the live data
  251. * source. Recommended only for DataSources that return comprehensive results
  252. * for queries with very few characters.
  253. *
  254. * @property queryMatchSubset
  255. * @type Boolean
  256. * @default false
  257. *
  258. */
  259. YAHOO.widget.AutoComplete.prototype.queryMatchSubset = false;
  260. /**
  261. * Number of characters that must be entered before querying for results. A negative value
  262. * effectively turns off the widget. A value of 0 allows queries of null or empty string
  263. * values.
  264. *
  265. * @property minQueryLength
  266. * @type Number
  267. * @default 1
  268. */
  269. YAHOO.widget.AutoComplete.prototype.minQueryLength = 1;
  270. /**
  271. * Maximum number of results to display in results container.
  272. *
  273. * @property maxResultsDisplayed
  274. * @type Number
  275. * @default 10
  276. */
  277. YAHOO.widget.AutoComplete.prototype.maxResultsDisplayed = 10;
  278. /**
  279. * Number of seconds to delay before submitting a query request. If a query
  280. * request is received before a previous one has completed its delay, the
  281. * previous request is cancelled and the new request is set to the delay. If
  282. * typeAhead is also enabled, this value must always be less than the typeAheadDelay
  283. * in order to avoid certain race conditions.
  284. *
  285. * @property queryDelay
  286. * @type Number
  287. * @default 0.2
  288. */
  289. YAHOO.widget.AutoComplete.prototype.queryDelay = 0.2;
  290. /**
  291. * If typeAhead is true, number of seconds to delay before updating input with
  292. * typeAhead value. In order to prevent certain race conditions, this value must
  293. * always be greater than the queryDelay.
  294. *
  295. * @property typeAheadDelay
  296. * @type Number
  297. * @default 0.5
  298. */
  299. YAHOO.widget.AutoComplete.prototype.typeAheadDelay = 0.5;
  300. /**
  301. * When IME usage is detected, AutoComplete will switch to querying the input
  302. * value at the given interval rather than per key event.
  303. *
  304. * @property queryInterval
  305. * @type Number
  306. * @default 500
  307. */
  308. YAHOO.widget.AutoComplete.prototype.queryInterval = 500;
  309. /**
  310. * Class name of a highlighted item within results container.
  311. *
  312. * @property highlightClassName
  313. * @type String
  314. * @default "yui-ac-highlight"
  315. */
  316. YAHOO.widget.AutoComplete.prototype.highlightClassName = "yui-ac-highlight";
  317. /**
  318. * Class name of a pre-highlighted item within results container.
  319. *
  320. * @property prehighlightClassName
  321. * @type String
  322. */
  323. YAHOO.widget.AutoComplete.prototype.prehighlightClassName = null;
  324. /**
  325. * Query delimiter. A single character separator for multiple delimited
  326. * selections. Multiple delimiter characteres may be defined as an array of
  327. * strings. A null value or empty string indicates that query results cannot
  328. * be delimited. This feature is not recommended if you need forceSelection to
  329. * be true.
  330. *
  331. * @property delimChar
  332. * @type String | String[]
  333. */
  334. YAHOO.widget.AutoComplete.prototype.delimChar = null;
  335. /**
  336. * Whether or not the first item in results container should be automatically highlighted
  337. * on expand.
  338. *
  339. * @property autoHighlight
  340. * @type Boolean
  341. * @default true
  342. */
  343. YAHOO.widget.AutoComplete.prototype.autoHighlight = true;
  344. /**
  345. * If autohighlight is enabled, whether or not the input field should be automatically updated
  346. * with the first query result as the user types, auto-selecting the substring portion
  347. * of the first result that the user has not yet typed.
  348. *
  349. * @property typeAhead
  350. * @type Boolean
  351. * @default false
  352. */
  353. YAHOO.widget.AutoComplete.prototype.typeAhead = false;
  354. /**
  355. * Whether or not to animate the expansion/collapse of the results container in the
  356. * horizontal direction.
  357. *
  358. * @property animHoriz
  359. * @type Boolean
  360. * @default false
  361. */
  362. YAHOO.widget.AutoComplete.prototype.animHoriz = false;
  363. /**
  364. * Whether or not to animate the expansion/collapse of the results container in the
  365. * vertical direction.
  366. *
  367. * @property animVert
  368. * @type Boolean
  369. * @default true
  370. */
  371. YAHOO.widget.AutoComplete.prototype.animVert = true;
  372. /**
  373. * Speed of container expand/collapse animation, in seconds..
  374. *
  375. * @property animSpeed
  376. * @type Number
  377. * @default 0.3
  378. */
  379. YAHOO.widget.AutoComplete.prototype.animSpeed = 0.3;
  380. /**
  381. * Whether or not to force the user's selection to match one of the query
  382. * results. Enabling this feature essentially transforms the input field into a
  383. * &lt;select&gt; field. This feature is not recommended with delimiter character(s)
  384. * defined.
  385. *
  386. * @property forceSelection
  387. * @type Boolean
  388. * @default false
  389. */
  390. YAHOO.widget.AutoComplete.prototype.forceSelection = false;
  391. /**
  392. * Whether or not to allow browsers to cache user-typed input in the input
  393. * field. Disabling this feature will prevent the widget from setting the
  394. * autocomplete="off" on the input field. When autocomplete="off"
  395. * and users click the back button after form submission, user-typed input can
  396. * be prefilled by the browser from its cache. This caching of user input may
  397. * not be desired for sensitive data, such as credit card numbers, in which
  398. * case, implementers should consider setting allowBrowserAutocomplete to false.
  399. *
  400. * @property allowBrowserAutocomplete
  401. * @type Boolean
  402. * @default true
  403. */
  404. YAHOO.widget.AutoComplete.prototype.allowBrowserAutocomplete = true;
  405. /**
  406. * Enabling this feature prevents the toggling of the container to a collapsed state.
  407. * Setting to true does not automatically trigger the opening of the container.
  408. * Implementers are advised to pre-load the container with an explicit "sendQuery()" call.
  409. *
  410. * @property alwaysShowContainer
  411. * @type Boolean
  412. * @default false
  413. */
  414. YAHOO.widget.AutoComplete.prototype.alwaysShowContainer = false;
  415. /**
  416. * Whether or not to use an iFrame to layer over Windows form elements in
  417. * IE. Set to true only when the results container will be on top of a
  418. * &lt;select&gt; field in IE and thus exposed to the IE z-index bug (i.e.,
  419. * 5.5 < IE < 7).
  420. *
  421. * @property useIFrame
  422. * @type Boolean
  423. * @default false
  424. */
  425. YAHOO.widget.AutoComplete.prototype.useIFrame = false;
  426. /**
  427. * Whether or not the results container should have a shadow.
  428. *
  429. * @property useShadow
  430. * @type Boolean
  431. * @default false
  432. */
  433. YAHOO.widget.AutoComplete.prototype.useShadow = false;
  434. /**
  435. * Whether or not the input field should be updated with selections.
  436. *
  437. * @property suppressInputUpdate
  438. * @type Boolean
  439. * @default false
  440. */
  441. YAHOO.widget.AutoComplete.prototype.suppressInputUpdate = false;
  442. /**
  443. * For backward compatibility to pre-2.6.0 formatResults() signatures, setting
  444. * resultsTypeList to true will take each object literal result returned by
  445. * DataSource and flatten into an array.
  446. *
  447. * @property resultTypeList
  448. * @type Boolean
  449. * @default true
  450. */
  451. YAHOO.widget.AutoComplete.prototype.resultTypeList = true;
  452. /**
  453. * For XHR DataSources, AutoComplete will automatically insert a "?" between the server URI and
  454. * the "query" param/value pair. To prevent this behavior, implementers should
  455. * set this value to false. To more fully customize the query syntax, implementers
  456. * should override the generateRequest() method.
  457. *
  458. * @property queryQuestionMark
  459. * @type Boolean
  460. * @default true
  461. */
  462. YAHOO.widget.AutoComplete.prototype.queryQuestionMark = true;
  463. /////////////////////////////////////////////////////////////////////////////
  464. //
  465. // Public methods
  466. //
  467. /////////////////////////////////////////////////////////////////////////////
  468. /**
  469. * Public accessor to the unique name of the AutoComplete instance.
  470. *
  471. * @method toString
  472. * @return {String} Unique name of the AutoComplete instance.
  473. */
  474. YAHOO.widget.AutoComplete.prototype.toString = function() {
  475. return "AutoComplete " + this._sName;
  476. };
  477. /**
  478. * Returns DOM reference to input element.
  479. *
  480. * @method getInputEl
  481. * @return {HTMLELement} DOM reference to input element.
  482. */
  483. YAHOO.widget.AutoComplete.prototype.getInputEl = function() {
  484. return this._elTextbox;
  485. };
  486. /**
  487. * Returns DOM reference to container element.
  488. *
  489. * @method getContainerEl
  490. * @return {HTMLELement} DOM reference to container element.
  491. */
  492. YAHOO.widget.AutoComplete.prototype.getContainerEl = function() {
  493. return this._elContainer;
  494. };
  495. /**
  496. * Returns true if widget instance is currently focused.
  497. *
  498. * @method isFocused
  499. * @return {Boolean} Returns true if widget instance is currently focused.
  500. */
  501. YAHOO.widget.AutoComplete.prototype.isFocused = function() {
  502. return (this._bFocused === null) ? false : this._bFocused;
  503. };
  504. /**
  505. * Returns true if container is in an expanded state, false otherwise.
  506. *
  507. * @method isContainerOpen
  508. * @return {Boolean} Returns true if container is in an expanded state, false otherwise.
  509. */
  510. YAHOO.widget.AutoComplete.prototype.isContainerOpen = function() {
  511. return this._bContainerOpen;
  512. };
  513. /**
  514. * Public accessor to the &lt;ul&gt; element that displays query results within the results container.
  515. *
  516. * @method getListEl
  517. * @return {HTMLElement[]} Reference to &lt;ul&gt; element within the results container.
  518. */
  519. YAHOO.widget.AutoComplete.prototype.getListEl = function() {
  520. return this._elList;
  521. };
  522. /**
  523. * Public accessor to the matching string associated with a given &lt;li&gt; result.
  524. *
  525. * @method getListItemMatch
  526. * @param elListItem {HTMLElement} Reference to &lt;LI&gt; element.
  527. * @return {String} Matching string.
  528. */
  529. YAHOO.widget.AutoComplete.prototype.getListItemMatch = function(elListItem) {
  530. if(elListItem._sResultMatch) {
  531. return elListItem._sResultMatch;
  532. }
  533. else {
  534. return null;
  535. }
  536. };
  537. /**
  538. * Public accessor to the result data associated with a given &lt;li&gt; result.
  539. *
  540. * @method getListItemData
  541. * @param elListItem {HTMLElement} Reference to &lt;LI&gt; element.
  542. * @return {Object} Result data.
  543. */
  544. YAHOO.widget.AutoComplete.prototype.getListItemData = function(elListItem) {
  545. if(elListItem._oResultData) {
  546. return elListItem._oResultData;
  547. }
  548. else {
  549. return null;
  550. }
  551. };
  552. /**
  553. * Public accessor to the index of the associated with a given &lt;li&gt; result.
  554. *
  555. * @method getListItemIndex
  556. * @param elListItem {HTMLElement} Reference to &lt;LI&gt; element.
  557. * @return {Number} Index.
  558. */
  559. YAHOO.widget.AutoComplete.prototype.getListItemIndex = function(elListItem) {
  560. if(YAHOO.lang.isNumber(elListItem._nItemIndex)) {
  561. return elListItem._nItemIndex;
  562. }
  563. else {
  564. return null;
  565. }
  566. };
  567. /**
  568. * Sets HTML markup for the results container header. This markup will be
  569. * inserted within a &lt;div&gt; tag with a class of "yui-ac-hd".
  570. *
  571. * @method setHeader
  572. * @param sHeader {String} HTML markup for results container header.
  573. */
  574. YAHOO.widget.AutoComplete.prototype.setHeader = function(sHeader) {
  575. if(this._elHeader) {
  576. var elHeader = this._elHeader;
  577. if(sHeader) {
  578. elHeader.innerHTML = sHeader;
  579. elHeader.style.display = "block";
  580. }
  581. else {
  582. elHeader.innerHTML = "";
  583. elHeader.style.display = "none";
  584. }
  585. }
  586. };
  587. /**
  588. * Sets HTML markup for the results container footer. This markup will be
  589. * inserted within a &lt;div&gt; tag with a class of "yui-ac-ft".
  590. *
  591. * @method setFooter
  592. * @param sFooter {String} HTML markup for results container footer.
  593. */
  594. YAHOO.widget.AutoComplete.prototype.setFooter = function(sFooter) {
  595. if(this._elFooter) {
  596. var elFooter = this._elFooter;
  597. if(sFooter) {
  598. elFooter.innerHTML = sFooter;
  599. elFooter.style.display = "block";
  600. }
  601. else {
  602. elFooter.innerHTML = "";
  603. elFooter.style.display = "none";
  604. }
  605. }
  606. };
  607. /**
  608. * Sets HTML markup for the results container body. This markup will be
  609. * inserted within a &lt;div&gt; tag with a class of "yui-ac-bd".
  610. *
  611. * @method setBody
  612. * @param sBody {String} HTML markup for results container body.
  613. */
  614. YAHOO.widget.AutoComplete.prototype.setBody = function(sBody) {
  615. if(this._elBody) {
  616. var elBody = this._elBody;
  617. YAHOO.util.Event.purgeElement(elBody, true);
  618. if(sBody) {
  619. elBody.innerHTML = sBody;
  620. elBody.style.display = "block";
  621. }
  622. else {
  623. elBody.innerHTML = "";
  624. elBody.style.display = "none";
  625. }
  626. this._elList = null;
  627. }
  628. };
  629. /**
  630. * A function that converts an AutoComplete query into a request value which is then
  631. * passed to the DataSource's sendRequest method in order to retrieve data for
  632. * the query. By default, returns a String with the syntax: "query={query}"
  633. * Implementers can customize this method for custom request syntaxes.
  634. *
  635. * @method generateRequest
  636. * @param sQuery {String} Query string
  637. * @return {MIXED} Request
  638. */
  639. YAHOO.widget.AutoComplete.prototype.generateRequest = function(sQuery) {
  640. var dataType = this.dataSource.dataType;
  641. // Transform query string in to a request for remote data
  642. // By default, local data doesn't need a transformation, just passes along the query as is.
  643. if(dataType === YAHOO.util.DataSourceBase.TYPE_XHR) {
  644. // By default, XHR GET requests look like "{scriptURI}?{scriptQueryParam}={sQuery}&{scriptQueryAppend}"
  645. if(!this.dataSource.connMethodPost) {
  646. sQuery = (this.queryQuestionMark ? "?" : "") + (this.dataSource.scriptQueryParam || "query") + "=" + sQuery +
  647. (this.dataSource.scriptQueryAppend ? ("&" + this.dataSource.scriptQueryAppend) : "");
  648. }
  649. // By default, XHR POST bodies are sent to the {scriptURI} like "{scriptQueryParam}={sQuery}&{scriptQueryAppend}"
  650. else {
  651. sQuery = (this.dataSource.scriptQueryParam || "query") + "=" + sQuery +
  652. (this.dataSource.scriptQueryAppend ? ("&" + this.dataSource.scriptQueryAppend) : "");
  653. }
  654. }
  655. // By default, remote script node requests look like "{scriptURI}&{scriptCallbackParam}={callbackString}&{scriptQueryParam}={sQuery}&{scriptQueryAppend}"
  656. else if(dataType === YAHOO.util.DataSourceBase.TYPE_SCRIPTNODE) {
  657. sQuery = "&" + (this.dataSource.scriptQueryParam || "query") + "=" + sQuery +
  658. (this.dataSource.scriptQueryAppend ? ("&" + this.dataSource.scriptQueryAppend) : "");
  659. }
  660. return sQuery;
  661. };
  662. /**
  663. * Makes query request to the DataSource.
  664. *
  665. * @method sendQuery
  666. * @param sQuery {String} Query string.
  667. */
  668. YAHOO.widget.AutoComplete.prototype.sendQuery = function(sQuery) {
  669. // Reset focus for a new interaction
  670. this._bFocused = null;
  671. // Adjust programatically sent queries to look like they were input by user
  672. // when delimiters are enabled
  673. var newQuery = (this.delimChar) ? this._elTextbox.value + sQuery : sQuery;
  674. this._sendQuery(newQuery);
  675. };
  676. /**
  677. * Collapses container.
  678. *
  679. * @method collapseContainer
  680. */
  681. YAHOO.widget.AutoComplete.prototype.collapseContainer = function() {
  682. this._toggleContainer(false);
  683. };
  684. /**
  685. * Handles subset matching for when queryMatchSubset is enabled.
  686. *
  687. * @method getSubsetMatches
  688. * @param sQuery {String} Query string.
  689. * @return {Object} oParsedResponse or null.
  690. */
  691. YAHOO.widget.AutoComplete.prototype.getSubsetMatches = function(sQuery) {
  692. var subQuery, oCachedResponse, subRequest;
  693. // Loop through substrings of each cached element's query property...
  694. for(var i = sQuery.length; i >= this.minQueryLength ; i--) {
  695. subRequest = this.generateRequest(sQuery.substr(0,i));
  696. this.dataRequestEvent.fire(this, subQuery, subRequest);
  697. YAHOO.log("Searching for query subset \"" + subQuery + "\" in cache", "info", this.toString());
  698. // If a substring of the query is found in the cache
  699. oCachedResponse = this.dataSource.getCachedResponse(subRequest);
  700. if(oCachedResponse) {
  701. YAHOO.log("Found match for query subset \"" + subQuery + "\": " + YAHOO.lang.dump(oCachedResponse), "info", this.toString());
  702. return this.filterResults.apply(this.dataSource, [sQuery, oCachedResponse, oCachedResponse, {scope:this}]);
  703. }
  704. }
  705. YAHOO.log("Did not find subset match for query subset \"" + sQuery + "\"" , "info", this.toString());
  706. return null;
  707. };
  708. /**
  709. * Executed by DataSource (within DataSource scope via doBeforeParseData()) to
  710. * handle responseStripAfter cleanup.
  711. *
  712. * @method preparseRawResponse
  713. * @param sQuery {String} Query string.
  714. * @return {Object} oParsedResponse or null.
  715. */
  716. YAHOO.widget.AutoComplete.prototype.preparseRawResponse = function(oRequest, oFullResponse, oCallback) {
  717. var nEnd = ((this.responseStripAfter !== "") && (oFullResponse.indexOf)) ?
  718. oFullResponse.indexOf(this.responseStripAfter) : -1;
  719. if(nEnd != -1) {
  720. oFullResponse = oFullResponse.substring(0,nEnd);
  721. }
  722. return oFullResponse;
  723. };
  724. /**
  725. * Executed by DataSource (within DataSource scope via doBeforeCallback()) to
  726. * filter results through a simple client-side matching algorithm.
  727. *
  728. * @method filterResults
  729. * @param sQuery {String} Original request.
  730. * @param oFullResponse {Object} Full response object.
  731. * @param oParsedResponse {Object} Parsed response object.
  732. * @param oCallback {Object} Callback object.
  733. * @return {Object} Filtered response object.
  734. */
  735. YAHOO.widget.AutoComplete.prototype.filterResults = function(sQuery, oFullResponse, oParsedResponse, oCallback) {
  736. // If AC has passed a query string value back to itself, grab it
  737. if(oCallback && oCallback.argument && oCallback.argument.query) {
  738. sQuery = oCallback.argument.query;
  739. }
  740. // Only if a query string is available to match against
  741. if(sQuery && sQuery !== "") {
  742. // First make a copy of the oParseResponse
  743. oParsedResponse = YAHOO.widget.AutoComplete._cloneObject(oParsedResponse);
  744. var oAC = oCallback.scope,
  745. oDS = this,
  746. allResults = oParsedResponse.results, // the array of results
  747. filteredResults = [], // container for filtered results
  748. bMatchFound = false,
  749. bMatchCase = (oDS.queryMatchCase || oAC.queryMatchCase), // backward compat
  750. bMatchContains = (oDS.queryMatchContains || oAC.queryMatchContains); // backward compat
  751. // Loop through each result object...
  752. for(var i = allResults.length-1; i >= 0; i--) {
  753. var oResult = allResults[i];
  754. // Grab the data to match against from the result object...
  755. var sResult = null;
  756. // Result object is a simple string already
  757. if(YAHOO.lang.isString(oResult)) {
  758. sResult = oResult;
  759. }
  760. // Result object is an array of strings
  761. else if(YAHOO.lang.isArray(oResult)) {
  762. sResult = oResult[0];
  763. }
  764. // Result object is an object literal of strings
  765. else if(this.responseSchema.fields) {
  766. var key = this.responseSchema.fields[0].key || this.responseSchema.fields[0];
  767. sResult = oResult[key];
  768. }
  769. // Backwards compatibility
  770. else if(this.key) {
  771. sResult = oResult[this.key];
  772. }
  773. if(YAHOO.lang.isString(sResult)) {
  774. var sKeyIndex = (bMatchCase) ?
  775. sResult.indexOf(decodeURIComponent(sQuery)) :
  776. sResult.toLowerCase().indexOf(decodeURIComponent(sQuery).toLowerCase());
  777. // A STARTSWITH match is when the query is found at the beginning of the key string...
  778. if((!bMatchContains && (sKeyIndex === 0)) ||
  779. // A CONTAINS match is when the query is found anywhere within the key string...
  780. (bMatchContains && (sKeyIndex > -1))) {
  781. // Stash the match
  782. filteredResults.unshift(oResult);
  783. }
  784. }
  785. }
  786. oParsedResponse.results = filteredResults;
  787. YAHOO.log("Filtered " + filteredResults.length + " results against query \"" + sQuery + "\": " + YAHOO.lang.dump(filteredResults), "info", this.toString());
  788. }
  789. else {
  790. YAHOO.log("Did not filter results against query", "info", this.toString());
  791. }
  792. return oParsedResponse;
  793. };
  794. /**
  795. * Handles response for display. This is the callback function method passed to
  796. * YAHOO.util.DataSourceBase#sendRequest so results from the DataSource are
  797. * returned to the AutoComplete instance.
  798. *
  799. * @method handleResponse
  800. * @param sQuery {String} Original request.
  801. * @param oResponse {Object} Response object.
  802. * @param oPayload {MIXED} (optional) Additional argument(s)
  803. */
  804. YAHOO.widget.AutoComplete.prototype.handleResponse = function(sQuery, oResponse, oPayload) {
  805. if((this instanceof YAHOO.widget.AutoComplete) && this._sName) {
  806. this._populateList(sQuery, oResponse, oPayload);
  807. }
  808. };
  809. /**
  810. * Overridable method called before container is loaded with result data.
  811. *
  812. * @method doBeforeLoadData
  813. * @param sQuery {String} Original request.
  814. * @param oResponse {Object} Response object.
  815. * @param oPayload {MIXED} (optional) Additional argument(s)
  816. * @return {Boolean} Return true to continue loading data, false to cancel.
  817. */
  818. YAHOO.widget.AutoComplete.prototype.doBeforeLoadData = function(sQuery, oResponse, oPayload) {
  819. return true;
  820. };
  821. /**
  822. * Overridable method that returns HTML markup for one result to be populated
  823. * as innerHTML of an &lt;LI&gt; element.
  824. *
  825. * @method formatResult
  826. * @param oResultData {Object} Result data object.
  827. * @param sQuery {String} The corresponding query string.
  828. * @param sResultMatch {HTMLElement} The current query string.
  829. * @return {String} HTML markup of formatted result data.
  830. */
  831. YAHOO.widget.AutoComplete.prototype.formatResult = function(oResultData, sQuery, sResultMatch) {
  832. var sMarkup = (sResultMatch) ? sResultMatch : "";
  833. return sMarkup;
  834. };
  835. /**
  836. * Overridable method called before container expands allows implementers to access data
  837. * and DOM elements.
  838. *
  839. * @method doBeforeExpandContainer
  840. * @param elTextbox {HTMLElement} The text input box.
  841. * @param elContainer {HTMLElement} The container element.
  842. * @param sQuery {String} The query string.
  843. * @param aResults {Object[]} An array of query results.
  844. * @return {Boolean} Return true to continue expanding container, false to cancel the expand.
  845. */
  846. YAHOO.widget.AutoComplete.prototype.doBeforeExpandContainer = function(elTextbox, elContainer, sQuery, aResults) {
  847. return true;
  848. };
  849. /**
  850. * Nulls out the entire AutoComplete instance and related objects, removes attached
  851. * event listeners, and clears out DOM elements inside the container. After
  852. * calling this method, the instance reference should be expliclitly nulled by
  853. * implementer, as in myAutoComplete = null. Use with caution!
  854. *
  855. * @method destroy
  856. */
  857. YAHOO.widget.AutoComplete.prototype.destroy = function() {
  858. var instanceName = this.toString();
  859. var elInput = this._elTextbox;
  860. var elContainer = this._elContainer;
  861. // Unhook custom events
  862. this.textboxFocusEvent.unsubscribeAll();
  863. this.textboxKeyEvent.unsubscribeAll();
  864. this.dataRequestEvent.unsubscribeAll();
  865. this.dataReturnEvent.unsubscribeAll();
  866. this.dataErrorEvent.unsubscribeAll();
  867. this.containerPopulateEvent.unsubscribeAll();
  868. this.containerExpandEvent.unsubscribeAll();
  869. this.typeAheadEvent.unsubscribeAll();
  870. this.itemMouseOverEvent.unsubscribeAll();
  871. this.itemMouseOutEvent.unsubscribeAll();
  872. this.itemArrowToEvent.unsubscribeAll();
  873. this.itemArrowFromEvent.unsubscribeAll();
  874. this.itemSelectEvent.unsubscribeAll();
  875. this.unmatchedItemSelectEvent.unsubscribeAll();
  876. this.selectionEnforceEvent.unsubscribeAll();
  877. this.containerCollapseEvent.unsubscribeAll();
  878. this.textboxBlurEvent.unsubscribeAll();
  879. this.textboxChangeEvent.unsubscribeAll();
  880. // Unhook DOM events
  881. YAHOO.util.Event.purgeElement(elInput, true);
  882. YAHOO.util.Event.purgeElement(elContainer, true);
  883. // Remove DOM elements
  884. elContainer.innerHTML = "";
  885. // Null out objects
  886. for(var key in this) {
  887. if(YAHOO.lang.hasOwnProperty(this, key)) {
  888. this[key] = null;
  889. }
  890. }
  891. YAHOO.log("AutoComplete instance destroyed: " + instanceName);
  892. };
  893. /////////////////////////////////////////////////////////////////////////////
  894. //
  895. // Public events
  896. //
  897. /////////////////////////////////////////////////////////////////////////////
  898. /**
  899. * Fired when the input field receives focus.
  900. *
  901. * @event textboxFocusEvent
  902. * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
  903. */
  904. YAHOO.widget.AutoComplete.prototype.textboxFocusEvent = null;
  905. /**
  906. * Fired when the input field receives key input.
  907. *
  908. * @event textboxKeyEvent
  909. * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
  910. * @param nKeycode {Number} The keycode number.
  911. */
  912. YAHOO.widget.AutoComplete.prototype.textboxKeyEvent = null;
  913. /**
  914. * Fired when the AutoComplete instance makes a request to the DataSource.
  915. *
  916. * @event dataRequestEvent
  917. * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
  918. * @param sQuery {String} The query string.
  919. * @param oRequest {Object} The request.
  920. */
  921. YAHOO.widget.AutoComplete.prototype.dataRequestEvent = null;
  922. /**
  923. * Fired when the AutoComplete instance receives query results from the data
  924. * source.
  925. *
  926. * @event dataReturnEvent
  927. * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
  928. * @param sQuery {String} The query string.
  929. * @param aResults {Object[]} Results array.
  930. */
  931. YAHOO.widget.AutoComplete.prototype.dataReturnEvent = null;
  932. /**
  933. * Fired when the AutoComplete instance does not receive query results from the
  934. * DataSource due to an error.
  935. *
  936. * @event dataErrorEvent
  937. * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
  938. * @param sQuery {String} The query string.
  939. */
  940. YAHOO.widget.AutoComplete.prototype.dataErrorEvent = null;
  941. /**
  942. * Fired when the results container is populated.
  943. *
  944. * @event containerPopulateEvent
  945. * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
  946. */
  947. YAHOO.widget.AutoComplete.prototype.containerPopulateEvent = null;
  948. /**
  949. * Fired when the results container is expanded.
  950. *
  951. * @event containerExpandEvent
  952. * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
  953. */
  954. YAHOO.widget.AutoComplete.prototype.containerExpandEvent = null;
  955. /**
  956. * Fired when the input field has been prefilled by the type-ahead
  957. * feature.
  958. *
  959. * @event typeAheadEvent
  960. * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
  961. * @param sQuery {String} The query string.
  962. * @param sPrefill {String} The prefill string.
  963. */
  964. YAHOO.widget.AutoComplete.prototype.typeAheadEvent = null;
  965. /**
  966. * Fired when result item has been moused over.
  967. *
  968. * @event itemMouseOverEvent
  969. * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
  970. * @param elItem {HTMLElement} The &lt;li&gt element item moused to.
  971. */
  972. YAHOO.widget.AutoComplete.prototype.itemMouseOverEvent = null;
  973. /**
  974. * Fired when result item has been moused out.
  975. *
  976. * @event itemMouseOutEvent
  977. * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
  978. * @param elItem {HTMLElement} The &lt;li&gt; element item moused from.
  979. */
  980. YAHOO.widget.AutoComplete.prototype.itemMouseOutEvent = null;
  981. /**
  982. * Fired when result item has been arrowed to.
  983. *
  984. * @event itemArrowToEvent
  985. * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
  986. * @param elItem {HTMLElement} The &lt;li&gt; element item arrowed to.
  987. */
  988. YAHOO.widget.AutoComplete.prototype.itemArrowToEvent = null;
  989. /**
  990. * Fired when result item has been arrowed away from.
  991. *
  992. * @event itemArrowFromEvent
  993. * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
  994. * @param elItem {HTMLElement} The &lt;li&gt; element item arrowed from.
  995. */
  996. YAHOO.widget.AutoComplete.prototype.itemArrowFromEvent = null;
  997. /**
  998. * Fired when an item is selected via mouse click, ENTER key, or TAB key.
  999. *
  1000. * @event itemSelectEvent
  1001. * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
  1002. * @param elItem {HTMLElement} The selected &lt;li&gt; element item.
  1003. * @param oData {Object} The data returned for the item, either as an object,
  1004. * or mapped from the schema into an array.
  1005. */
  1006. YAHOO.widget.AutoComplete.prototype.itemSelectEvent = null;
  1007. /**
  1008. * Fired when a user selection does not match any of the displayed result items.
  1009. *
  1010. * @event unmatchedItemSelectEvent
  1011. * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
  1012. * @param sSelection {String} The selected string.
  1013. */
  1014. YAHOO.widget.AutoComplete.prototype.unmatchedItemSelectEvent = null;
  1015. /**
  1016. * Fired if forceSelection is enabled and the user's input has been cleared
  1017. * because it did not match one of the returned query results.
  1018. *
  1019. * @event selectionEnforceEvent
  1020. * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
  1021. * @param sClearedValue {String} The cleared value (including delimiters if applicable).
  1022. */
  1023. YAHOO.widget.AutoComplete.prototype.selectionEnforceEvent = null;
  1024. /**
  1025. * Fired when the results container is collapsed.
  1026. *
  1027. * @event containerCollapseEvent
  1028. * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
  1029. */
  1030. YAHOO.widget.AutoComplete.prototype.containerCollapseEvent = null;
  1031. /**
  1032. * Fired when the input field loses focus.
  1033. *
  1034. * @event textboxBlurEvent
  1035. * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
  1036. */
  1037. YAHOO.widget.AutoComplete.prototype.textboxBlurEvent = null;
  1038. /**
  1039. * Fired when the input field value has changed when it loses focus.
  1040. *
  1041. * @event textboxChangeEvent
  1042. * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
  1043. */
  1044. YAHOO.widget.AutoComplete.prototype.textboxChangeEvent = null;
  1045. /////////////////////////////////////////////////////////////////////////////
  1046. //
  1047. // Private member variables
  1048. //
  1049. /////////////////////////////////////////////////////////////////////////////
  1050. /**
  1051. * Internal class variable to index multiple AutoComplete instances.
  1052. *
  1053. * @property _nIndex
  1054. * @type Number
  1055. * @default 0
  1056. * @private
  1057. */
  1058. YAHOO.widget.AutoComplete._nIndex = 0;
  1059. /**
  1060. * Name of AutoComplete instance.
  1061. *
  1062. * @property _sName
  1063. * @type String
  1064. * @private
  1065. */
  1066. YAHOO.widget.AutoComplete.prototype._sName = null;
  1067. /**
  1068. * Text input field DOM element.
  1069. *
  1070. * @property _elTextbox
  1071. * @type HTMLElement
  1072. * @private
  1073. */
  1074. YAHOO.widget.AutoComplete.prototype._elTextbox = null;
  1075. /**
  1076. * Container DOM element.
  1077. *
  1078. * @property _elContainer
  1079. * @type HTMLElement
  1080. * @private
  1081. */
  1082. YAHOO.widget.AutoComplete.prototype._elContainer = null;
  1083. /**
  1084. * Reference to content element within container element.
  1085. *
  1086. * @property _elContent
  1087. * @type HTMLElement
  1088. * @private
  1089. */
  1090. YAHOO.widget.AutoComplete.prototype._elContent = null;
  1091. /**
  1092. * Reference to header element within content element.
  1093. *
  1094. * @property _elHeader
  1095. * @type HTMLElement
  1096. * @private
  1097. */
  1098. YAHOO.widget.AutoComplete.prototype._elHeader = null;
  1099. /**
  1100. * Reference to body element within content element.
  1101. *
  1102. * @property _elBody
  1103. * @type HTMLElement
  1104. * @private
  1105. */
  1106. YAHOO.widget.AutoComplete.prototype._elBody = null;
  1107. /**
  1108. * Reference to footer element within content element.
  1109. *
  1110. * @property _elFooter
  1111. * @type HTMLElement
  1112. * @private
  1113. */
  1114. YAHOO.widget.AutoComplete.prototype._elFooter = null;
  1115. /**
  1116. * Reference to shadow element within container element.
  1117. *
  1118. * @property _elShadow
  1119. * @type HTMLElement
  1120. * @private
  1121. */
  1122. YAHOO.widget.AutoComplete.prototype._elShadow = null;
  1123. /**
  1124. * Reference to iframe element within container element.
  1125. *
  1126. * @property _elIFrame
  1127. * @type HTMLElement
  1128. * @private
  1129. */
  1130. YAHOO.widget.AutoComplete.prototype._elIFrame = null;
  1131. /**
  1132. * Whether or not the input field is currently in focus. If query results come back
  1133. * but the user has already moved on, do not proceed with auto complete behavior.
  1134. *
  1135. * @property _bFocused
  1136. * @type Boolean
  1137. * @private
  1138. */
  1139. YAHOO.widget.AutoComplete.prototype._bFocused = null;
  1140. /**
  1141. * Animation instance for container expand/collapse.
  1142. *
  1143. * @property _oAnim
  1144. * @type Boolean
  1145. * @private
  1146. */
  1147. YAHOO.widget.AutoComplete.prototype._oAnim = null;
  1148. /**
  1149. * Whether or not the results container is currently open.
  1150. *
  1151. * @property _bContainerOpen
  1152. * @type Boolean
  1153. * @private
  1154. */
  1155. YAHOO.widget.AutoComplete.prototype._bContainerOpen = false;
  1156. /**
  1157. * Whether or not the mouse is currently over the results
  1158. * container. This is necessary in order to prevent clicks on container items
  1159. * from being text input field blur events.
  1160. *
  1161. * @property _bOverContainer
  1162. * @type Boolean
  1163. * @private
  1164. */
  1165. YAHOO.widget.AutoComplete.prototype._bOverContainer = false;
  1166. /**
  1167. * Internal reference to &lt;ul&gt; elements that contains query results within the
  1168. * results container.
  1169. *
  1170. * @property _elList
  1171. * @type HTMLElement
  1172. * @private
  1173. */
  1174. YAHOO.widget.AutoComplete.prototype._elList = null;
  1175. /*
  1176. * Array of &lt;li&gt; elements references that contain query results within the
  1177. * results container.
  1178. *
  1179. * @property _aListItemEls
  1180. * @type HTMLElement[]
  1181. * @private
  1182. */
  1183. //YAHOO.widget.AutoComplete.prototype._aListItemEls = null;
  1184. /**
  1185. * Number of &lt;li&gt; elements currently displayed in results container.
  1186. *
  1187. * @property _nDisplayedItems
  1188. * @type Number
  1189. * @private
  1190. */
  1191. YAHOO.widget.AutoComplete.prototype._nDisplayedItems = 0;
  1192. /*
  1193. * Internal count of &lt;li&gt; elements displayed and hidden in results container.
  1194. *
  1195. * @property _maxResultsDisplayed
  1196. * @type Number
  1197. * @private
  1198. */
  1199. //YAHOO.widget.AutoComplete.prototype._maxResultsDisplayed = 0;
  1200. /**
  1201. * Current query string
  1202. *
  1203. * @property _sCurQuery
  1204. * @type String
  1205. * @private
  1206. */
  1207. YAHOO.widget.AutoComplete.prototype._sCurQuery = null;
  1208. /**
  1209. * Selections from previous queries (for saving delimited queries).
  1210. *
  1211. * @property _sPastSelections
  1212. * @type String
  1213. * @default ""
  1214. * @private
  1215. */
  1216. YAHOO.widget.AutoComplete.prototype._sPastSelections = "";
  1217. /**
  1218. * Stores initial input value used to determine if textboxChangeEvent should be fired.
  1219. *
  1220. * @property _sInitInputValue
  1221. * @type String
  1222. * @private
  1223. */
  1224. YAHOO.widget.AutoComplete.prototype._sInitInputValue = null;
  1225. /**
  1226. * Pointer to the currently highlighted &lt;li&gt; element in the container.
  1227. *
  1228. * @property _elCurListItem
  1229. * @type HTMLElement
  1230. * @private
  1231. */
  1232. YAHOO.widget.AutoComplete.prototype._elCurListItem = null;
  1233. /**
  1234. * Whether or not an item has been selected since the container was populated
  1235. * with results. Reset to false by _populateList, and set to true when item is
  1236. * selected.
  1237. *
  1238. * @property _bItemSelected
  1239. * @type Boolean
  1240. * @private
  1241. */
  1242. YAHOO.widget.AutoComplete.prototype._bItemSelected = false;
  1243. /**
  1244. * Key code of the last key pressed in textbox.
  1245. *
  1246. * @property _nKeyCode
  1247. * @type Number
  1248. * @private
  1249. */
  1250. YAHOO.widget.AutoComplete.prototype._nKeyCode = null;
  1251. /**
  1252. * Delay timeout ID.
  1253. *
  1254. * @property _nDelayID
  1255. * @type Number
  1256. * @private
  1257. */
  1258. YAHOO.widget.AutoComplete.prototype._nDelayID = -1;
  1259. /**
  1260. * TypeAhead delay timeout ID.
  1261. *
  1262. * @property _nTypeAheadDelayID
  1263. * @type Number
  1264. * @private
  1265. */
  1266. YAHOO.widget.AutoComplete.prototype._nTypeAheadDelayID = -1;
  1267. /**
  1268. * Src to iFrame used when useIFrame = true. Supports implementations over SSL
  1269. * as well.
  1270. *
  1271. * @property _iFrameSrc
  1272. * @type String
  1273. * @private
  1274. */
  1275. YAHOO.widget.AutoComplete.prototype._iFrameSrc = "javascript:false;";
  1276. /**
  1277. * For users typing via certain IMEs, queries must be triggered by intervals,
  1278. * since key events yet supported across all browsers for all IMEs.
  1279. *
  1280. * @property _queryInterval
  1281. * @type Object
  1282. * @private
  1283. */
  1284. YAHOO.widget.AutoComplete.prototype._queryInterval = null;
  1285. /**
  1286. * Internal tracker to last known textbox value, used to determine whether or not
  1287. * to trigger a query via interval for certain IME users.
  1288. *
  1289. * @event _sLastTextboxValue
  1290. * @type String
  1291. * @private
  1292. */
  1293. YAHOO.widget.AutoComplete.prototype._sLastTextboxValue = null;
  1294. /////////////////////////////////////////////////////////////////////////////
  1295. //
  1296. // Private methods
  1297. //
  1298. /////////////////////////////////////////////////////////////////////////////
  1299. /**
  1300. * Updates and validates latest public config properties.
  1301. *
  1302. * @method __initProps
  1303. * @private
  1304. */
  1305. YAHOO.widget.AutoComplete.prototype._initProps = function() {
  1306. // Correct any invalid values
  1307. var minQueryLength = this.minQueryLength;
  1308. if(!YAHOO.lang.isNumber(minQueryLength)) {
  1309. this.minQueryLength = 1;
  1310. }
  1311. var maxResultsDisplayed = this.maxResultsDisplayed;
  1312. if(!YAHOO.lang.isNumber(maxResultsDisplayed) || (maxResultsDisplayed < 1)) {
  1313. this.maxResultsDisplayed = 10;
  1314. }
  1315. var queryDelay = this.queryDelay;
  1316. if(!YAHOO.lang.isNumber(queryDelay) || (queryDelay < 0)) {
  1317. this.queryDelay = 0.2;
  1318. }
  1319. var typeAheadDelay = this.typeAheadDelay;
  1320. if(!YAHOO.lang.isNumber(typeAheadDelay) || (typeAheadDelay < 0)) {
  1321. this.typeAheadDelay = 0.2;
  1322. }
  1323. var delimChar = this.delimChar;
  1324. if(YAHOO.lang.isString(delimChar) && (delimChar.length > 0)) {
  1325. this.delimChar = [delimChar];
  1326. }
  1327. else if(!YAHOO.lang.isArray(delimChar)) {
  1328. this.delimChar = null;
  1329. }
  1330. var animSpeed = this.animSpeed;
  1331. if((this.animHoriz || this.animVert) && YAHOO.util.Anim) {
  1332. if(!YAHOO.lang.isNumber(animSpeed) || (animSpeed < 0)) {
  1333. this.animSpeed = 0.3;
  1334. }
  1335. if(!this._oAnim ) {
  1336. this._oAnim = new YAHOO.util.Anim(this._elContent, {}, this.animSpeed);
  1337. }
  1338. else {
  1339. this._oAnim.duration = this.animSpeed;
  1340. }
  1341. }
  1342. if(this.forceSelection && delimChar) {
  1343. YAHOO.log("The forceSelection feature has been enabled with delimChar defined.","warn", this.toString());
  1344. }
  1345. };
  1346. /**
  1347. * Initializes the results container helpers if they are enabled and do
  1348. * not exist
  1349. *
  1350. * @method _initContainerHelperEls
  1351. * @private
  1352. */
  1353. YAHOO.widget.AutoComplete.prototype._initContainerHelperEls = function() {
  1354. if(this.useShadow && !this._elShadow) {
  1355. var elShadow = document.createElement("div");
  1356. elShadow.className = "yui-ac-shadow";
  1357. elShadow.style.width = 0;
  1358. elShadow.style.height = 0;
  1359. this._elShadow = this._elContainer.appendChild(elShadow);
  1360. }
  1361. if(this.useIFrame && !this._elIFrame) {
  1362. var elIFrame = document.createElement("iframe");
  1363. elIFrame.src = this._iFrameSrc;
  1364. elIFrame.frameBorder = 0;
  1365. elIFrame.scrolling = "no";
  1366. elIFrame.style.position = "absolute";
  1367. elIFrame.style.width = 0;
  1368. elIFrame.style.height = 0;
  1369. elIFrame.tabIndex = -1;
  1370. elIFrame.style.padding = 0;
  1371. this._elIFrame = this._elContainer.appendChild(elIFrame);
  1372. }
  1373. };
  1374. /**
  1375. * Initializes the results container once at object creation
  1376. *
  1377. * @method _initContainerEl
  1378. * @private
  1379. */
  1380. YAHOO.widget.AutoComplete.prototype._initContainerEl = function() {
  1381. YAHOO.util.Dom.addClass(this._elContainer, "yui-ac-container");
  1382. if(!this._elContent) {
  1383. // The elContent div is assigned DOM listeners and
  1384. // helps size the iframe and shadow properly
  1385. var elContent = document.createElement("div");
  1386. elContent.className = "yui-ac-content";
  1387. elContent.style.display = "none";
  1388. this._elContent = this._elContainer.appendChild(elContent);
  1389. var elHeader = document.createElement("div");
  1390. elHeader.className = "yui-ac-hd";
  1391. elHeader.style.display = "none";
  1392. this._elHeader = this._elContent.appendChild(elHeader);
  1393. var elBody = document.createElement("div");
  1394. elBody.className = "yui-ac-bd";
  1395. this._elBody = this._elContent.appendChild(elBody);
  1396. var elFooter = document.createElement("div");
  1397. elFooter.className = "yui-ac-ft";
  1398. elFooter.style.display = "none";
  1399. this._elFooter = this._elContent.appendChild(elFooter);
  1400. }
  1401. else {
  1402. YAHOO.log("Could not initialize the container","warn",this.toString());
  1403. }
  1404. };
  1405. /**
  1406. * Clears out contents of container body and creates up to
  1407. * YAHOO.widget.AutoComplete#maxResultsDisplayed &lt;li&gt; elements in an
  1408. * &lt;ul&gt; element.
  1409. *
  1410. * @method _initListEl
  1411. * @private
  1412. */
  1413. YAHOO.widget.AutoComplete.prototype._initListEl = function() {
  1414. var nListLength = this.maxResultsDisplayed;
  1415. var elList = this._elList || document.createElement("ul");
  1416. var elListItem;
  1417. while(elList.childNodes.length < nListLength) {
  1418. elListItem = document.createElement("li");
  1419. elListItem.style.display = "none";
  1420. elListItem._nItemIndex = elList.childNodes.length;
  1421. elList.appendChild(elListItem);
  1422. }
  1423. if(!this._elList) {
  1424. var elBody = this._elBody;
  1425. YAHOO.util.Event.purgeElement(elBody, true);
  1426. elBody.innerHTML = "";
  1427. this._elList = elBody.appendChild(elList);
  1428. }
  1429. };
  1430. /**
  1431. * Focuses input field.
  1432. *
  1433. * @method _focus
  1434. * @private
  1435. */
  1436. YAHOO.widget.AutoComplete.prototype._focus = function() {
  1437. // http://developer.mozilla.org/en/docs/index.php?title=Key-navigable_custom_DHTML_widgets
  1438. var oSelf = this;
  1439. setTimeout(function() {
  1440. try {
  1441. oSelf._elTextbox.focus();
  1442. }
  1443. catch(e) {
  1444. }
  1445. },0);
  1446. };
  1447. /**
  1448. * Enables interval detection for IME support.
  1449. *
  1450. * @method _enableIntervalDetection
  1451. * @private
  1452. */
  1453. YAHOO.widget.AutoComplete.prototype._enableIntervalDetection = function() {
  1454. var oSelf = this;
  1455. if(!oSelf._queryInterval && oSelf.queryInterval) {
  1456. oSelf._queryInterval = setInterval(function() { oSelf._onInterval(); }, oSelf.queryInterval);
  1457. YAHOO.log("Interval set", "info", this.toString());
  1458. }
  1459. };
  1460. /**
  1461. * Enables query triggers based on text input detection by intervals (rather
  1462. * than by key events).
  1463. *
  1464. * @method _onInterval
  1465. * @private
  1466. */
  1467. YAHOO.widget.AutoComplete.prototype._onInterval = function() {
  1468. var currValue = this._elTextbox.value;
  1469. var lastValue = this._sLastTextboxValue;
  1470. if(currValue != lastValue) {
  1471. this._sLastTextboxValue = currValue;
  1472. this._sendQuery(currValue);
  1473. }
  1474. };
  1475. /**
  1476. * Cancels text input detection by intervals.
  1477. *
  1478. * @method _clearInterval
  1479. * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
  1480. * @private
  1481. */
  1482. YAHOO.widget.AutoComplete.prototype._clearInterval = function() {
  1483. if(this._queryInterval) {
  1484. clearInterval(this._queryInterval);
  1485. this._queryInterval = null;
  1486. YAHOO.log("Interval cleared", "info", this.toString());
  1487. }
  1488. };
  1489. /**
  1490. * Whether or not key is functional or should be ignored. Note that the right
  1491. * arrow key is NOT an ignored key since it triggers queries for certain intl
  1492. * charsets.
  1493. *
  1494. * @method _isIgnoreKey
  1495. * @param nKeycode {Number} Code of key pressed.
  1496. * @return {Boolean} True if key should be ignored, false otherwise.
  1497. * @private
  1498. */
  1499. YAHOO.widget.AutoComplete.prototype._isIgnoreKey = function(nKeyCode) {
  1500. if((nKeyCode == 9) || (nKeyCode == 13) || // tab, enter
  1501. (nKeyCode == 16) || (nKeyCode == 17) || // shift, ctl
  1502. (nKeyCode >= 18 && nKeyCode <= 20) || // alt, pause/break,caps lock
  1503. (nKeyCode == 27) || // esc
  1504. (nKeyCode >= 33 && nKeyCode <= 35) || // page up,page down,end
  1505. /*(nKeyCode >= 36 && nKeyCode <= 38) || // home,left,up
  1506. (nKeyCode == 40) || // down*/
  1507. (nKeyCode >= 36 && nKeyCode <= 40) || // home,left,up, right, down
  1508. (nKeyCode >= 44 && nKeyCode <= 45) || // print screen,insert
  1509. (nKeyCode == 229) // Bug 2041973: Korean XP fires 2 keyup events, the key and 229
  1510. ) {
  1511. return true;
  1512. }
  1513. return false;
  1514. };
  1515. /**
  1516. * Makes query request to the DataSource.
  1517. *
  1518. * @method _sendQuery
  1519. * @param sQuery {String} Query string.
  1520. * @private
  1521. */
  1522. YAHOO.widget.AutoComplete.prototype._sendQuery = function(sQuery) {
  1523. // Widget has been effectively turned off
  1524. if(this.minQueryLength < 0) {
  1525. this._toggleContainer(false);
  1526. YAHOO.log("Property minQueryLength is less than 0", "info", this.toString());
  1527. return;
  1528. }
  1529. // Delimiter has been enabled
  1530. if(this.delimChar) {
  1531. var extraction = this._extractQuery(sQuery);
  1532. // Here is the query itself
  1533. sQuery = extraction.query;
  1534. // ...and save the rest of the string for later
  1535. this._sPastSelections = extraction.previous;
  1536. }
  1537. // Don't search queries that are too short
  1538. if((sQuery && (sQuery.length < this.minQueryLength)) || (!sQuery && this.minQueryLength > 0)) {
  1539. if(this._nDelayID != -1) {
  1540. clearTimeout(this._nDelayID);
  1541. }
  1542. this._toggleContainer(false);
  1543. YAHOO.log("Query \"" + sQuery + "\" is too short", "info", this.toString());
  1544. return;
  1545. }
  1546. sQuery = encodeURIComponent(sQuery);
  1547. this._nDelayID = -1; // Reset timeout ID because request is being made
  1548. // Subset matching
  1549. if(this.dataSource.queryMatchSubset || this.queryMatchSubset) { // backward compat
  1550. var oResponse = this.getSubsetMatches(sQuery);
  1551. if(oResponse) {
  1552. this.handleResponse(sQuery, oResponse, {query: sQuery});
  1553. return;
  1554. }
  1555. }
  1556. if(this.responseStripAfter) {
  1557. this.dataSource.doBeforeParseData = this.preparseRawResponse;
  1558. }
  1559. if(this.applyLocalFilter) {
  1560. this.dataSource.doBeforeCallback = this.filterResults;
  1561. }
  1562. var sRequest = this.generateRequest(sQuery);
  1563. this.dataRequestEvent.fire(this, sQuery, sRequest);
  1564. YAHOO.log("Sending query \"" + sRequest + "\"", "info", this.toString());
  1565. this.dataSource.sendRequest(sRequest, {
  1566. success : this.handleResponse,
  1567. failure : this.handleResponse,
  1568. scope : this,
  1569. argument: {
  1570. query: sQuery
  1571. }
  1572. });
  1573. };
  1574. /**
  1575. * Populates the array of &lt;li&gt; elements in the container with query
  1576. * results.
  1577. *
  1578. * @method _populateList
  1579. * @param sQuery {String} Original request.
  1580. * @param oResponse {Object} Response object.
  1581. * @param oPayload {MIXED} (optional) Additional argument(s)
  1582. * @private
  1583. */
  1584. YAHOO.widget.AutoComplete.prototype._populateList = function(sQuery, oResponse, oPayload) {
  1585. // Clear previous timeout
  1586. if(this._nTypeAheadDelayID != -1) {
  1587. clearTimeout(this._nTypeAheadDelayID);
  1588. }
  1589. sQuery = (oPayload && oPayload.query) ? oPayload.query : sQuery;
  1590. // Pass data through abstract method for any transformations
  1591. var ok = this.doBeforeLoadData(sQuery, oResponse, oPayload);
  1592. // Data is ok
  1593. if(ok && !oResponse.error) {
  1594. this.dataReturnEvent.fire(this, sQuery, oResponse.results);
  1595. // Continue only if instance is still focused (i.e., user hasn't already moved on)
  1596. // Null indicates initialized state, which is ok too
  1597. if(this._bFocused || (this._bFocused === null)) {
  1598. //TODO: is this still necessary?
  1599. /*var isOpera = (YAHOO.env.ua.opera);
  1600. var contentStyle = this._elContent.style;
  1601. contentStyle.width = (!isOpera) ? null : "";
  1602. contentStyle.height = (!isOpera) ? null : "";*/
  1603. // Store state for this interaction
  1604. var sCurQuery = decodeURIComponent(sQuery);
  1605. this._sCurQuery = sCurQuery;
  1606. this._bItemSelected = false;
  1607. var allResults = oResponse.results,
  1608. nItemsToShow = Math.min(allResults.length,this.maxResultsDisplayed),
  1609. sMatchKey = (this.dataSource.responseSchema.fields) ?
  1610. (this.dataSource.responseSchema.fields[0].key || this.dataSource.responseSchema.fields[0]) : 0;
  1611. if(nItemsToShow > 0) {
  1612. // Make sure container and helpers are ready to go
  1613. if(!this._elList || (this._elList.childNodes.length < nItemsToShow)) {
  1614. this._initListEl();
  1615. }
  1616. this._initContainerHelperEls();
  1617. var allListItemEls = this._elList.childNodes;
  1618. // Fill items with data from the bottom up
  1619. for(var i = nItemsToShow-1; i >= 0; i--) {
  1620. var elListItem = allListItemEls[i],
  1621. oResult = allResults[i];
  1622. // Backward compatibility
  1623. if(this.resultTypeList) {
  1624. // Results need to be converted back to an array
  1625. var aResult = [];
  1626. // Match key is first
  1627. aResult[0] = (YAHOO.lang.isString(oResult)) ? oResult : oResult[sMatchKey] || oResult[this.key];
  1628. // Add additional data to the result array
  1629. var fields = this.dataSource.responseSchema.fields;
  1630. if(YAHOO.lang.isArray(fields) && (fields.length > 1)) {
  1631. for(var k=1, len=fields.length; k<len; k++) {
  1632. aResult[aResult.length] = oResult[fields[k].key || fields[k]];
  1633. }
  1634. }
  1635. // No specific fields defined, so pass along entire data object
  1636. else {
  1637. // Already an array
  1638. if(YAHOO.lang.isArray(oResult)) {
  1639. aResult = oResult;
  1640. }
  1641. // Simple string
  1642. else if(YAHOO.lang.isString(oResult)) {
  1643. aResult = [oResult];
  1644. }
  1645. // Object
  1646. else {
  1647. aResult[1] = oResult;
  1648. }
  1649. }
  1650. oResult = aResult;
  1651. }
  1652. // The matching value, including backward compatibility for array format and safety net
  1653. elListItem._sResultMatch = (YAHOO.lang.isString(oResult)) ? oResult : (YAHOO.lang.isArray(oResult)) ? oResult[0] : (oResult[sMatchKey] || "");
  1654. elListItem._oResultData = oResult; // Additional data
  1655. elListItem.innerHTML = this.formatResult(oResult, sCurQuery, elListItem._sResultMatch);
  1656. elListItem.style.display = "";
  1657. }
  1658. // Clear out extraneous items
  1659. if(nItemsToShow < allListItemEls.length) {
  1660. var extraListItem;
  1661. for(var j = allListItemEls.length-1; j >= nItemsToShow; j--) {
  1662. extraListItem = allListItemEls[j];
  1663. extraListItem.style.display = "none";
  1664. }
  1665. }
  1666. this._nDisplayedItems = nItemsToShow;
  1667. this.containerPopulateEvent.fire(this, sQuery, allResults);
  1668. // Highlight the first item
  1669. if(this.autoHighlight) {
  1670. var elFirstListItem = this._elList.firstChild;
  1671. this._toggleHighlight(elFirstListItem,"to");
  1672. this.itemArrowToEvent.fire(this, elFirstListItem);
  1673. YAHOO.log("Arrowed to first item", "info", this.toString());
  1674. this._typeAhead(elFirstListItem,sQuery);
  1675. }
  1676. // Unhighlight any previous time
  1677. else {
  1678. this._toggleHighlight(this._elCurListItem,"from");
  1679. }
  1680. // Expand the container
  1681. ok = this.doBeforeExpandContainer(this._elTextbox, this._elContainer, sQuery, allResults);
  1682. this._toggleContainer(ok);
  1683. }
  1684. else {
  1685. this._toggleContainer(false);
  1686. }
  1687. YAHOO.log("Container populated with " + nItemsToShow + " list items", "info", this.toString());
  1688. return;
  1689. }
  1690. }
  1691. // Error
  1692. else {
  1693. this.dataErrorEvent.fire(this, sQuery);
  1694. }
  1695. YAHOO.log("Could not populate list", "info", this.toString());
  1696. };
  1697. /**
  1698. * When forceSelection is true and the user attempts
  1699. * leave the text input box without selecting an item from the query results,
  1700. * the user selection is cleared.
  1701. *
  1702. * @method _clearSelection
  1703. * @private
  1704. */
  1705. YAHOO.widget.AutoComplete.prototype._clearSelection = function() {
  1706. var extraction = (this.delimChar) ? this._extractQuery(this._elTextbox.value) :
  1707. {previous:"",query:this._elTextbox.value};
  1708. this._elTextbox.value = extraction.previous;
  1709. this.selectionEnforceEvent.fire(this, extraction.query);
  1710. YAHOO.log("Selection enforced", "info", this.toString());
  1711. };
  1712. /**
  1713. * Whether or not user-typed value in the text input box matches any of the
  1714. * query results.
  1715. *
  1716. * @method _textMatchesOption
  1717. * @return {HTMLElement} Matching list item element if user-input text matches
  1718. * a result, null otherwise.
  1719. * @private
  1720. */
  1721. YAHOO.widget.AutoComplete.prototype._textMatchesOption = function() {
  1722. var elMatch = null;
  1723. for(var i=0; i<this._nDisplayedItems; i++) {
  1724. var elListItem = this._elList.childNodes[i];
  1725. var sMatch = ("" + elListItem._sResultMatch).toLowerCase();
  1726. if(sMatch == this._sCurQuery.toLowerCase()) {
  1727. elMatch = elListItem;
  1728. break;
  1729. }
  1730. }
  1731. return(elMatch);
  1732. };
  1733. /**
  1734. * Updates in the text input box with the first query result as the user types,
  1735. * selecting the substring that the user has not typed.
  1736. *
  1737. * @method _typeAhead
  1738. * @param elListItem {HTMLElement} The &lt;li&gt; element item whose data populates the input field.
  1739. * @param sQuery {String} Query string.
  1740. * @private
  1741. */
  1742. YAHOO.widget.AutoComplete.prototype._typeAhead = function(elListItem, sQuery) {
  1743. // Don't typeAhead if turned off or is backspace
  1744. if(!this.typeAhead || (this._nKeyCode == 8)) {
  1745. return;
  1746. }
  1747. var oSelf = this,
  1748. elTextbox = this._elTextbox;
  1749. // Only if text selection is supported
  1750. if(elTextbox.setSelectionRange || elTextbox.createTextRange) {
  1751. // Set and store timeout for this typeahead
  1752. this._nTypeAheadDelayID = setTimeout(function() {
  1753. // Select the portion of text that the user has not typed
  1754. var nStart = elTextbox.value.length; // any saved queries plus what user has typed
  1755. oSelf._updateValue(elListItem);
  1756. var nEnd = elTextbox.value.length;
  1757. oSelf._selectText(elTextbox,nStart,nEnd);
  1758. var sPrefill = elTextbox.value.substr(nStart,nEnd);
  1759. oSelf.typeAheadEvent.fire(oSelf,sQuery,sPrefill);
  1760. YAHOO.log("Typeahead occured with prefill string \"" + sPrefill + "\"", "info", oSelf.toString());
  1761. },(this.typeAheadDelay*1000));
  1762. }
  1763. };
  1764. /**
  1765. * Selects text in the input field.
  1766. *
  1767. * @method _selectText
  1768. * @param elTextbox {HTMLElement} Text input box element in which to select text.
  1769. * @param nStart {Number} Starting index of text string to select.
  1770. * @param nEnd {Number} Ending index of text selection.
  1771. * @private
  1772. */
  1773. YAHOO.widget.AutoComplete.prototype._selectText = function(elTextbox, nStart, nEnd) {
  1774. if(elTextbox.setSelectionRange) { // For Mozilla
  1775. elTextbox.setSelectionRange(nStart,nEnd);
  1776. }
  1777. else if(elTextbox.createTextRange) { // For IE
  1778. var oTextRange = elTextbox.createTextRange();
  1779. oTextRange.moveStart("character", nStart);
  1780. oTextRange.moveEnd("character", nEnd-elTextbox.value.length);
  1781. oTextRange.select();
  1782. }
  1783. else {
  1784. elTextbox.select();
  1785. }
  1786. };
  1787. /**
  1788. * Extracts rightmost query from delimited string.
  1789. *
  1790. * @method _extractQuery
  1791. * @param sQuery {String} String to parse
  1792. * @return {Object} Object literal containing properties "query" and "previous".
  1793. * @private
  1794. */
  1795. YAHOO.widget.AutoComplete.prototype._extractQuery = function(sQuery) {
  1796. var aDelimChar = this.delimChar,
  1797. nDelimIndex = -1,
  1798. nNewIndex, nQueryStart,
  1799. i = aDelimChar.length-1,
  1800. sPrevious;
  1801. // Loop through all possible delimiters and find the rightmost one in the query
  1802. // A " " may be a false positive if they are defined as delimiters AND
  1803. // are used to separate delimited queries
  1804. for(; i >= 0; i--) {
  1805. nNewIndex = sQuery.lastIndexOf(aDelimChar[i]);
  1806. if(nNewIndex > nDelimIndex) {
  1807. nDelimIndex = nNewIndex;
  1808. }
  1809. }
  1810. // If we think the last delimiter is a space (" "), make sure it is NOT
  1811. // a false positive by also checking the char directly before it
  1812. if(aDelimChar[i] == " ") {
  1813. for (var j = aDelimChar.length-1; j >= 0; j--) {
  1814. if(sQuery[nDelimIndex - 1] == aDelimChar[j]) {
  1815. nDelimIndex--;
  1816. break;
  1817. }
  1818. }
  1819. }
  1820. // A delimiter has been found in the query so extract the latest query from past selections
  1821. if(nDelimIndex > -1) {
  1822. nQueryStart = nDelimIndex + 1;
  1823. // Trim any white space from the beginning...
  1824. while(sQuery.charAt(nQueryStart) == " ") {
  1825. nQueryStart += 1;
  1826. }
  1827. // ...and save the rest of the string for later
  1828. sPrevious = sQuery.substring(0,nQueryStart);
  1829. // Here is the query itself
  1830. sQuery = sQuery.substr(nQueryStart);
  1831. }
  1832. // No delimiter found in the query, so there are no selections from past queries
  1833. else {
  1834. sPrevious = "";
  1835. }
  1836. return {
  1837. previous: sPrevious,
  1838. query: sQuery
  1839. };
  1840. };
  1841. /**
  1842. * Syncs results container with its helpers.
  1843. *
  1844. * @method _toggleContainerHelpers
  1845. * @param bShow {Boolean} True if container is expanded, false if collapsed
  1846. * @private
  1847. */
  1848. YAHOO.widget.AutoComplete.prototype._toggleContainerHelpers = function(bShow) {
  1849. var width = this._elContent.offsetWidth + "px";
  1850. var height = this._elContent.offsetHeight + "px";
  1851. if(this.useIFrame && this._elIFrame) {
  1852. var elIFrame = this._elIFrame;
  1853. if(bShow) {
  1854. elIFrame.style.width = width;
  1855. elIFrame.style.height = height;
  1856. elIFrame.style.padding = "";
  1857. YAHOO.log("Iframe expanded", "info", this.toString());
  1858. }
  1859. else {
  1860. elIFrame.style.width = 0;
  1861. elIFrame.style.height = 0;
  1862. elIFrame.style.padding = 0;
  1863. YAHOO.log("Iframe collapsed", "info", this.toString());
  1864. }
  1865. }
  1866. if(this.useShadow && this._elShadow) {
  1867. var elShadow = this._elShadow;
  1868. if(bShow) {
  1869. elShadow.style.width = width;
  1870. elShadow.style.height = height;
  1871. YAHOO.log("Shadow expanded", "info", this.toString());
  1872. }
  1873. else {
  1874. elShadow.style.width = 0;
  1875. elShadow.style.height = 0;
  1876. YAHOO.log("Shadow collapsed", "info", this.toString());
  1877. }
  1878. }
  1879. };
  1880. /**
  1881. * Animates expansion or collapse of the container.
  1882. *
  1883. * @method _toggleContainer
  1884. * @param bShow {Boolean} True if container should be expanded, false if container should be collapsed
  1885. * @private
  1886. */
  1887. YAHOO.widget.AutoComplete.prototype._toggleContainer = function(bShow) {
  1888. YAHOO.log("Toggling container " + ((bShow) ? "open" : "closed"), "info", this.toString());
  1889. var elContainer = this._elContainer;
  1890. // If implementer has container always open and it's already open, don't mess with it
  1891. // Container is initialized with display "none" so it may need to be shown first time through
  1892. if(this.alwaysShowContainer && this._bContainerOpen) {
  1893. return;
  1894. }
  1895. // Reset states
  1896. if(!bShow) {
  1897. this._toggleHighlight(this._elCurListItem,"from");
  1898. this._nDisplayedItems = 0;
  1899. this._sCurQuery = null;
  1900. // Container is already closed, so don't bother with changing the UI
  1901. if(this._elContent.style.display == "none") {
  1902. return;
  1903. }
  1904. }
  1905. // If animation is enabled...
  1906. var oAnim = this._oAnim;
  1907. if(oAnim && oAnim.getEl() && (this.animHoriz || this.animVert)) {
  1908. if(oAnim.isAnimated()) {
  1909. oAnim.stop(true);
  1910. }
  1911. // Clone container to grab current size offscreen
  1912. var oClone = this._elContent.cloneNode(true);
  1913. elContainer.appendChild(oClone);
  1914. oClone.style.top = "-9000px";
  1915. oClone.style.width = "";
  1916. oClone.style.height = "";
  1917. oClone.style.display = "";
  1918. // Current size of the container is the EXPANDED size
  1919. var wExp = oClone.offsetWidth;
  1920. var hExp = oClone.offsetHeight;
  1921. // Calculate COLLAPSED sizes based on horiz and vert anim
  1922. var wColl = (this.animHoriz) ? 0 : wExp;
  1923. var hColl = (this.animVert) ? 0 : hExp;
  1924. // Set animation sizes
  1925. oAnim.attributes = (bShow) ?
  1926. {width: { to: wExp }, height: { to: hExp }} :
  1927. {width: { to: wColl}, height: { to: hColl }};
  1928. // If opening anew, set to a collapsed size...
  1929. if(bShow && !this._bContainerOpen) {
  1930. this._elContent.style.width = wColl+"px";
  1931. this._elContent.style.height = hColl+"px";
  1932. }
  1933. // Else, set it to its last known size.
  1934. else {
  1935. this._elContent.style.width = wExp+"px";
  1936. this._elContent.style.height = hExp+"px";
  1937. }
  1938. elContainer.removeChild(oClone);
  1939. oClone = null;
  1940. var oSelf = this;
  1941. var onAnimComplete = function() {
  1942. // Finish the collapse
  1943. oAnim.onComplete.unsubscribeAll();
  1944. if(bShow) {
  1945. oSelf._toggleContainerHelpers(true);
  1946. oSelf._bContainerOpen = bShow;
  1947. oSelf.containerExpandEvent.fire(oSelf);
  1948. YAHOO.log("Container expanded", "info", oSelf.toString());
  1949. }
  1950. else {
  1951. oSelf._elContent.style.display = "none";
  1952. oSelf._bContainerOpen = bShow;
  1953. oSelf.containerCollapseEvent.fire(oSelf);
  1954. YAHOO.log("Container collapsed", "info", oSelf.toString());
  1955. }
  1956. };
  1957. // Display container and animate it
  1958. this._toggleContainerHelpers(false); // Bug 1424486: Be early to hide, late to show;
  1959. this._elContent.style.display = "";
  1960. oAnim.onComplete.subscribe(onAnimComplete);
  1961. oAnim.animate();
  1962. }
  1963. // Else don't animate, just show or hide
  1964. else {
  1965. if(bShow) {
  1966. this._elContent.style.display = "";
  1967. this._toggleContainerHelpers(true);
  1968. this._bContainerOpen = bShow;
  1969. this.containerExpandEvent.fire(this);
  1970. YAHOO.log("Container expanded", "info", this.toString());
  1971. }
  1972. else {
  1973. this._toggleContainerHelpers(false);
  1974. this._elContent.style.display = "none";
  1975. this._bContainerOpen = bShow;
  1976. this.containerCollapseEvent.fire(this);
  1977. YAHOO.log("Container collapsed", "info", this.toString());
  1978. }
  1979. }
  1980. };
  1981. /**
  1982. * Toggles the highlight on or off for an item in the container, and also cleans
  1983. * up highlighting of any previous item.
  1984. *
  1985. * @method _toggleHighlight
  1986. * @param elNewListItem {HTMLElement} The &lt;li&gt; element item to receive highlight behavior.
  1987. * @param sType {String} Type "mouseover" will toggle highlight on, and "mouseout" will toggle highlight off.
  1988. * @private
  1989. */
  1990. YAHOO.widget.AutoComplete.prototype._toggleHighlight = function(elNewListItem, sType) {
  1991. if(elNewListItem) {
  1992. var sHighlight = this.highlightClassName;
  1993. if(this._elCurListItem) {
  1994. // Remove highlight from old item
  1995. YAHOO.util.Dom.removeClass(this._elCurListItem, sHighlight);
  1996. this._elCurListItem = null;
  1997. }
  1998. if((sType == "to") && sHighlight) {
  1999. // Apply highlight to new item
  2000. YAHOO.util.Dom.addClass(elNewListItem, sHighlight);
  2001. this._elCurListItem = elNewListItem;
  2002. }
  2003. }
  2004. };
  2005. /**
  2006. * Toggles the pre-highlight on or off for an item in the container.
  2007. *
  2008. * @method _togglePrehighlight
  2009. * @param elNewListItem {HTMLElement} The &lt;li&gt; element item to receive highlight behavior.
  2010. * @param sType {String} Type "mouseover" will toggle highlight on, and "mouseout" will toggle highlight off.
  2011. * @private
  2012. */
  2013. YAHOO.widget.AutoComplete.prototype._togglePrehighlight = function(elNewListItem, sType) {
  2014. if(elNewListItem == this._elCurListItem) {
  2015. return;
  2016. }
  2017. var sPrehighlight = this.prehighlightClassName;
  2018. if((sType == "mouseover") && sPrehighlight) {
  2019. // Apply prehighlight to new item
  2020. YAHOO.util.Dom.addClass(elNewListItem, sPrehighlight);
  2021. }
  2022. else {
  2023. // Remove prehighlight from old item
  2024. YAHOO.util.Dom.removeClass(elNewListItem, sPrehighlight);
  2025. }
  2026. };
  2027. /**
  2028. * Updates the text input box value with selected query result. If a delimiter
  2029. * has been defined, then the value gets appended with the delimiter.
  2030. *
  2031. * @method _updateValue
  2032. * @param elListItem {HTMLElement} The &lt;li&gt; element item with which to update the value.
  2033. * @private
  2034. */
  2035. YAHOO.widget.AutoComplete.prototype._updateValue = function(elListItem) {
  2036. if(!this.suppressInputUpdate) {
  2037. var elTextbox = this._elTextbox;
  2038. var sDelimChar = (this.delimChar) ? (this.delimChar[0] || this.delimChar) : null;
  2039. var sResultMatch = elListItem._sResultMatch;
  2040. // Calculate the new value
  2041. var sNewValue = "";
  2042. if(sDelimChar) {
  2043. // Preserve selections from past queries
  2044. sNewValue = this._sPastSelections;
  2045. // Add new selection plus delimiter
  2046. sNewValue += sResultMatch + sDelimChar;
  2047. if(sDelimChar != " ") {
  2048. sNewValue += " ";
  2049. }
  2050. }
  2051. else {
  2052. sNewValue = sResultMatch;
  2053. }
  2054. // Update input field
  2055. elTextbox.value = sNewValue;
  2056. // Scroll to bottom of textarea if necessary
  2057. if(elTextbox.type == "textarea") {
  2058. elTextbox.scrollTop = elTextbox.scrollHeight;
  2059. }
  2060. // Move cursor to end
  2061. var end = elTextbox.value.length;
  2062. this._selectText(elTextbox,end,end);
  2063. this._elCurListItem = elListItem;
  2064. }
  2065. };
  2066. /**
  2067. * Selects a result item from the container
  2068. *
  2069. * @method _selectItem
  2070. * @param elListItem {HTMLElement} The selected &lt;li&gt; element item.
  2071. * @private
  2072. */
  2073. YAHOO.widget.AutoComplete.prototype._selectItem = function(elListItem) {
  2074. this._bItemSelected = true;
  2075. this._updateValue(elListItem);
  2076. this._sPastSelections = this._elTextbox.value;
  2077. this._clearInterval();
  2078. this.itemSelectEvent.fire(this, elListItem, elListItem._oResultData);
  2079. YAHOO.log("Item selected: " + YAHOO.lang.dump(elListItem._oResultData), "info", this.toString());
  2080. this._toggleContainer(false);
  2081. };
  2082. /**
  2083. * If an item is highlighted in the container, the right arrow key jumps to the
  2084. * end of the textbox and selects the highlighted item, otherwise the container
  2085. * is closed.
  2086. *
  2087. * @method _jumpSelection
  2088. * @private
  2089. */
  2090. YAHOO.widget.AutoComplete.prototype._jumpSelection = function() {
  2091. if(this._elCurListItem) {
  2092. this._selectItem(this._elCurListItem);
  2093. }
  2094. else {
  2095. this._toggleContainer(false);
  2096. }
  2097. };
  2098. /**
  2099. * Triggered by up and down arrow keys, changes the current highlighted
  2100. * &lt;li&gt; element item. Scrolls container if necessary.
  2101. *
  2102. * @method _moveSelection
  2103. * @param nKeyCode {Number} Code of key pressed.
  2104. * @private
  2105. */
  2106. YAHOO.widget.AutoComplete.prototype._moveSelection = function(nKeyCode) {
  2107. if(this._bContainerOpen) {
  2108. // Determine current item's id number
  2109. var elCurListItem = this._elCurListItem,
  2110. nCurItemIndex = -1;
  2111. if(elCurListItem) {
  2112. nCurItemIndex = elCurListItem._nItemIndex;
  2113. }
  2114. var nNewItemIndex = (nKeyCode == 40) ?
  2115. (nCurItemIndex + 1) : (nCurItemIndex - 1);
  2116. // Out of bounds
  2117. if(nNewItemIndex < -2 || nNewItemIndex >= this._nDisplayedItems) {
  2118. return;
  2119. }
  2120. if(elCurListItem) {
  2121. // Unhighlight current item
  2122. this._toggleHighlight(elCurListItem, "from");
  2123. this.itemArrowFromEvent.fire(this, elCurListItem);
  2124. YAHOO.log("Item arrowed from: " + elCurListItem._nItemIndex, "info", this.toString());
  2125. }
  2126. if(nNewItemIndex == -1) {
  2127. // Go back to query (remove type-ahead string)
  2128. if(this.delimChar) {
  2129. this._elTextbox.value = this._sPastSelections + this._sCurQuery;
  2130. }
  2131. else {
  2132. this._elTextbox.value = this._sCurQuery;
  2133. }
  2134. return;
  2135. }
  2136. if(nNewItemIndex == -2) {
  2137. // Close container
  2138. this._toggleContainer(false);
  2139. return;
  2140. }
  2141. var elNewListItem = this._elList.childNodes[nNewItemIndex],
  2142. // Scroll the container if necessary
  2143. elContent = this._elContent,
  2144. sOF = YAHOO.util.Dom.getStyle(elContent,"overflow"),
  2145. sOFY = YAHOO.util.Dom.getStyle(elContent,"overflowY"),
  2146. scrollOn = ((sOF == "auto") || (sOF == "scroll") || (sOFY == "auto") || (sOFY == "scroll"));
  2147. if(scrollOn && (nNewItemIndex > -1) &&
  2148. (nNewItemIndex < this._nDisplayedItems)) {
  2149. // User is keying down
  2150. if(nKeyCode == 40) {
  2151. // Bottom of selected item is below scroll area...
  2152. if((elNewListItem.offsetTop+elNewListItem.offsetHeight) > (elContent.scrollTop + elContent.offsetHeight)) {
  2153. // Set bottom of scroll area to bottom of selected item
  2154. elContent.scrollTop = (elNewListItem.offsetTop+elNewListItem.offsetHeight) - elContent.offsetHeight;
  2155. }
  2156. // Bottom of selected item is above scroll area...
  2157. else if((elNewListItem.offsetTop+elNewListItem.offsetHeight) < elContent.scrollTop) {
  2158. // Set top of selected item to top of scroll area
  2159. elContent.scrollTop = elNewListItem.offsetTop;
  2160. }
  2161. }
  2162. // User is keying up
  2163. else {
  2164. // Top of selected item is above scroll area
  2165. if(elNewListItem.offsetTop < elContent.scrollTop) {
  2166. // Set top of scroll area to top of selected item
  2167. this._elContent.scrollTop = elNewListItem.offsetTop;
  2168. }
  2169. // Top of selected item is below scroll area
  2170. else if(elNewListItem.offsetTop > (elContent.scrollTop + elContent.offsetHeight)) {
  2171. // Set bottom of selected item to bottom of scroll area
  2172. this._elContent.scrollTop = (elNewListItem.offsetTop+elNewListItem.offsetHeight) - elContent.offsetHeight;
  2173. }
  2174. }
  2175. }
  2176. this._toggleHighlight(elNewListItem, "to");
  2177. this.itemArrowToEvent.fire(this, elNewListItem);
  2178. YAHOO.log("Item arrowed to " + elNewListItem._nItemIndex, "info", this.toString());
  2179. if(this.typeAhead) {
  2180. this._updateValue(elNewListItem);
  2181. }
  2182. }
  2183. };
  2184. /////////////////////////////////////////////////////////////////////////////
  2185. //
  2186. // Private event handlers
  2187. //
  2188. /////////////////////////////////////////////////////////////////////////////
  2189. /**
  2190. * Handles container mouseover events.
  2191. *
  2192. * @method _onContainerMouseover
  2193. * @param v {HTMLEvent} The mouseover event.
  2194. * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
  2195. * @private
  2196. */
  2197. YAHOO.widget.AutoComplete.prototype._onContainerMouseover = function(v,oSelf) {
  2198. var elTarget = YAHOO.util.Event.getTarget(v);
  2199. var elTag = elTarget.nodeName.toLowerCase();
  2200. while(elTarget && (elTag != "table")) {
  2201. switch(elTag) {
  2202. case "body":
  2203. return;
  2204. case "li":
  2205. if(oSelf.prehighlightClassName) {
  2206. oSelf._togglePrehighlight(elTarget,"mouseover");
  2207. }
  2208. else {
  2209. oSelf._toggleHighlight(elTarget,"to");
  2210. }
  2211. oSelf.itemMouseOverEvent.fire(oSelf, elTarget);
  2212. YAHOO.log("Item moused over " + elTarget._nItemIndex, "info", oSelf.toString());
  2213. break;
  2214. case "div":
  2215. if(YAHOO.util.Dom.hasClass(elTarget,"yui-ac-container")) {
  2216. oSelf._bOverContainer = true;
  2217. return;
  2218. }
  2219. break;
  2220. default:
  2221. break;
  2222. }
  2223. elTarget = elTarget.parentNode;
  2224. if(elTarget) {
  2225. elTag = elTarget.nodeName.toLowerCase();
  2226. }
  2227. }
  2228. };
  2229. /**
  2230. * Handles container mouseout events.
  2231. *
  2232. * @method _onContainerMouseout
  2233. * @param v {HTMLEvent} The mouseout event.
  2234. * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
  2235. * @private
  2236. */
  2237. YAHOO.widget.AutoComplete.prototype._onContainerMouseout = function(v,oSelf) {
  2238. var elTarget = YAHOO.util.Event.getTarget(v);
  2239. var elTag = elTarget.nodeName.toLowerCase();
  2240. while(elTarget && (elTag != "table")) {
  2241. switch(elTag) {
  2242. case "body":
  2243. return;
  2244. case "li":
  2245. if(oSelf.prehighlightClassName) {
  2246. oSelf._togglePrehighlight(elTarget,"mouseout");
  2247. }
  2248. else {
  2249. oSelf._toggleHighlight(elTarget,"from");
  2250. }
  2251. oSelf.itemMouseOutEvent.fire(oSelf, elTarget);
  2252. YAHOO.log("Item moused out " + elTarget._nItemIndex, "info", oSelf.toString());
  2253. break;
  2254. case "ul":
  2255. oSelf._toggleHighlight(oSelf._elCurListItem,"to");
  2256. break;
  2257. case "div":
  2258. if(YAHOO.util.Dom.hasClass(elTarget,"yui-ac-container")) {
  2259. oSelf._bOverContainer = false;
  2260. return;
  2261. }
  2262. break;
  2263. default:
  2264. break;
  2265. }
  2266. elTarget = elTarget.parentNode;
  2267. if(elTarget) {
  2268. elTag = elTarget.nodeName.toLowerCase();
  2269. }
  2270. }
  2271. };
  2272. /**
  2273. * Handles container click events.
  2274. *
  2275. * @method _onContainerClick
  2276. * @param v {HTMLEvent} The click event.
  2277. * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
  2278. * @private
  2279. */
  2280. YAHOO.widget.AutoComplete.prototype._onContainerClick = function(v,oSelf) {
  2281. var elTarget = YAHOO.util.Event.getTarget(v);
  2282. var elTag = elTarget.nodeName.toLowerCase();
  2283. while(elTarget && (elTag != "table")) {
  2284. switch(elTag) {
  2285. case "body":
  2286. return;
  2287. case "li":
  2288. // In case item has not been moused over
  2289. oSelf._toggleHighlight(elTarget,"to");
  2290. oSelf._selectItem(elTarget);
  2291. return;
  2292. default:
  2293. break;
  2294. }
  2295. elTarget = elTarget.parentNode;
  2296. if(elTarget) {
  2297. elTag = elTarget.nodeName.toLowerCase();
  2298. }
  2299. }
  2300. };
  2301. /**
  2302. * Handles container scroll events.
  2303. *
  2304. * @method _onContainerScroll
  2305. * @param v {HTMLEvent} The scroll event.
  2306. * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
  2307. * @private
  2308. */
  2309. YAHOO.widget.AutoComplete.prototype._onContainerScroll = function(v,oSelf) {
  2310. oSelf._focus();
  2311. };
  2312. /**
  2313. * Handles container resize events.
  2314. *
  2315. * @method _onContainerResize
  2316. * @param v {HTMLEvent} The resize event.
  2317. * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
  2318. * @private
  2319. */
  2320. YAHOO.widget.AutoComplete.prototype._onContainerResize = function(v,oSelf) {
  2321. oSelf._toggleContainerHelpers(oSelf._bContainerOpen);
  2322. };
  2323. /**
  2324. * Handles textbox keydown events of functional keys, mainly for UI behavior.
  2325. *
  2326. * @method _onTextboxKeyDown
  2327. * @param v {HTMLEvent} The keydown event.
  2328. * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
  2329. * @private
  2330. */
  2331. YAHOO.widget.AutoComplete.prototype._onTextboxKeyDown = function(v,oSelf) {
  2332. var nKeyCode = v.keyCode;
  2333. // Clear timeout
  2334. if(oSelf._nTypeAheadDelayID != -1) {
  2335. clearTimeout(oSelf._nTypeAheadDelayID);
  2336. }
  2337. switch (nKeyCode) {
  2338. case 9: // tab
  2339. if(!YAHOO.env.ua.opera && (navigator.userAgent.toLowerCase().indexOf("mac") == -1) || (YAHOO.env.ua.webkit>420)) {
  2340. // select an item or clear out
  2341. if(oSelf._elCurListItem) {
  2342. if(oSelf.delimChar && (oSelf._nKeyCode != nKeyCode)) {
  2343. if(oSelf._bContainerOpen) {
  2344. YAHOO.util.Event.stopEvent(v);
  2345. }
  2346. }
  2347. oSelf._selectItem(oSelf._elCurListItem);
  2348. }
  2349. else {
  2350. oSelf._toggleContainer(false);
  2351. }
  2352. }
  2353. break;
  2354. case 13: // enter
  2355. if(!YAHOO.env.ua.opera && (navigator.userAgent.toLowerCase().indexOf("mac") == -1) || (YAHOO.env.ua.webkit>420)) {
  2356. if(oSelf._elCurListItem) {
  2357. if(oSelf._nKeyCode != nKeyCode) {
  2358. if(oSelf._bContainerOpen) {
  2359. YAHOO.util.Event.stopEvent(v);
  2360. }
  2361. }
  2362. oSelf._selectItem(oSelf._elCurListItem);
  2363. }
  2364. else {
  2365. oSelf._toggleContainer(false);
  2366. }
  2367. }
  2368. break;
  2369. case 27: // esc
  2370. oSelf._toggleContainer(false);
  2371. return;
  2372. case 39: // right
  2373. oSelf._jumpSelection();
  2374. break;
  2375. case 38: // up
  2376. if(oSelf._bContainerOpen) {
  2377. YAHOO.util.Event.stopEvent(v);
  2378. oSelf._moveSelection(nKeyCode);
  2379. }
  2380. break;
  2381. case 40: // down
  2382. if(oSelf._bContainerOpen) {
  2383. YAHOO.util.Event.stopEvent(v);
  2384. oSelf._moveSelection(nKeyCode);
  2385. }
  2386. break;
  2387. default:
  2388. oSelf._bItemSelected = false;
  2389. oSelf._toggleHighlight(oSelf._elCurListItem, "from");
  2390. oSelf.textboxKeyEvent.fire(oSelf, nKeyCode);
  2391. YAHOO.log("Textbox keyed", "info", oSelf.toString());
  2392. break;
  2393. }
  2394. if(nKeyCode === 18){
  2395. oSelf._enableIntervalDetection();
  2396. }
  2397. oSelf._nKeyCode = nKeyCode;
  2398. };
  2399. /**
  2400. * Handles textbox keypress events.
  2401. * @method _onTextboxKeyPress
  2402. * @param v {HTMLEvent} The keypress event.
  2403. * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
  2404. * @private
  2405. */
  2406. YAHOO.widget.AutoComplete.prototype._onTextboxKeyPress = function(v,oSelf) {
  2407. var nKeyCode = v.keyCode;
  2408. // Expose only to non SF3 (bug 1978549) Mac browsers (bug 790337) and Opera browsers (bug 583531),
  2409. // where stopEvent is ineffective on keydown events
  2410. if(YAHOO.env.ua.opera || (navigator.userAgent.toLowerCase().indexOf("mac") != -1) && (YAHOO.env.ua.webkit < 420)) {
  2411. switch (nKeyCode) {
  2412. case 9: // tab
  2413. // select an item or clear out
  2414. if(oSelf._bContainerOpen) {
  2415. if(oSelf.delimChar) {
  2416. YAHOO.util.Event.stopEvent(v);
  2417. }
  2418. if(oSelf._elCurListItem) {
  2419. oSelf._selectItem(oSelf._elCurListItem);
  2420. }
  2421. else {
  2422. oSelf._toggleContainer(false);
  2423. }
  2424. }
  2425. break;
  2426. case 13: // enter
  2427. if(oSelf._bContainerOpen) {
  2428. YAHOO.util.Event.stopEvent(v);
  2429. if(oSelf._elCurListItem) {
  2430. oSelf._selectItem(oSelf._elCurListItem);
  2431. }
  2432. else {
  2433. oSelf._toggleContainer(false);
  2434. }
  2435. }
  2436. break;
  2437. default:
  2438. break;
  2439. }
  2440. }
  2441. //TODO: (?) limit only to non-IE, non-Mac-FF for Korean IME support (bug 811948)
  2442. // Korean IME detected
  2443. else if(nKeyCode == 229) {
  2444. oSelf._enableIntervalDetection();
  2445. }
  2446. };
  2447. /**
  2448. * Handles textbox keyup events to trigger queries.
  2449. *
  2450. * @method _onTextboxKeyUp
  2451. * @param v {HTMLEvent} The keyup event.
  2452. * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
  2453. * @private
  2454. */
  2455. YAHOO.widget.AutoComplete.prototype._onTextboxKeyUp = function(v,oSelf) {
  2456. var sText = this.value; //string in textbox
  2457. // Check to see if any of the public properties have been updated
  2458. oSelf._initProps();
  2459. // Filter out chars that don't trigger queries
  2460. var nKeyCode = v.keyCode;
  2461. if(oSelf._isIgnoreKey(nKeyCode)) {
  2462. return;
  2463. }
  2464. // Clear previous timeout
  2465. /*if(oSelf._nTypeAheadDelayID != -1) {
  2466. clearTimeout(oSelf._nTypeAheadDelayID);
  2467. }*/
  2468. if(oSelf._nDelayID != -1) {
  2469. clearTimeout(oSelf._nDelayID);
  2470. }
  2471. // Set new timeout
  2472. oSelf._nDelayID = setTimeout(function(){
  2473. oSelf._sendQuery(sText);
  2474. },(oSelf.queryDelay * 1000));
  2475. //= nDelayID;
  2476. //else {
  2477. // No delay so send request immediately
  2478. //oSelf._sendQuery(sText);
  2479. //}
  2480. };
  2481. /**
  2482. * Handles text input box receiving focus.
  2483. *
  2484. * @method _onTextboxFocus
  2485. * @param v {HTMLEvent} The focus event.
  2486. * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
  2487. * @private
  2488. */
  2489. YAHOO.widget.AutoComplete.prototype._onTextboxFocus = function (v,oSelf) {
  2490. // Start of a new interaction
  2491. if(!oSelf._bFocused) {
  2492. oSelf._elTextbox.setAttribute("autocomplete","off");
  2493. oSelf._bFocused = true;
  2494. oSelf._sInitInputValue = oSelf._elTextbox.value;
  2495. oSelf.textboxFocusEvent.fire(oSelf);
  2496. YAHOO.log("Textbox focused", "info", oSelf.toString());
  2497. }
  2498. };
  2499. /**
  2500. * Handles text input box losing focus.
  2501. *
  2502. * @method _onTextboxBlur
  2503. * @param v {HTMLEvent} The focus event.
  2504. * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
  2505. * @private
  2506. */
  2507. YAHOO.widget.AutoComplete.prototype._onTextboxBlur = function (v,oSelf) {
  2508. // Is a true blur
  2509. if(!oSelf._bOverContainer || (oSelf._nKeyCode == 9)) {
  2510. // Current query needs to be validated as a selection
  2511. if(!oSelf._bItemSelected) {
  2512. var elMatchListItem = oSelf._textMatchesOption();
  2513. // Container is closed or current query doesn't match any result
  2514. if(!oSelf._bContainerOpen || (oSelf._bContainerOpen && (elMatchListItem === null))) {
  2515. // Force selection is enabled so clear the current query
  2516. if(oSelf.forceSelection) {
  2517. oSelf._clearSelection();
  2518. }
  2519. // Treat current query as a valid selection
  2520. else {
  2521. oSelf.unmatchedItemSelectEvent.fire(oSelf, oSelf._sCurQuery);
  2522. YAHOO.log("Unmatched item selected: " + oSelf._sCurQuery, "info", oSelf.toString());
  2523. }
  2524. }
  2525. // Container is open and current query matches a result
  2526. else {
  2527. // Force a selection when textbox is blurred with a match
  2528. if(oSelf.forceSelection) {
  2529. oSelf._selectItem(elMatchListItem);
  2530. }
  2531. }
  2532. }
  2533. oSelf._clearInterval();
  2534. oSelf._bFocused = false;
  2535. if(oSelf._sInitInputValue !== oSelf._elTextbox.value) {
  2536. oSelf.textboxChangeEvent.fire(oSelf);
  2537. }
  2538. oSelf.textboxBlurEvent.fire(oSelf);
  2539. YAHOO.log("Textbox blurred", "info", oSelf.toString());
  2540. oSelf._toggleContainer(false);
  2541. }
  2542. // Not a true blur if it was a selection via mouse click
  2543. else {
  2544. oSelf._focus();
  2545. }
  2546. };
  2547. /**
  2548. * Handles window unload event.
  2549. *
  2550. * @method _onWindowUnload
  2551. * @param v {HTMLEvent} The unload event.
  2552. * @param oSelf {YAHOO.widget.AutoComplete} The AutoComplete instance.
  2553. * @private
  2554. */
  2555. YAHOO.widget.AutoComplete.prototype._onWindowUnload = function(v,oSelf) {
  2556. if(oSelf && oSelf._elTextbox && oSelf.allowBrowserAutocomplete) {
  2557. oSelf._elTextbox.setAttribute("autocomplete","on");
  2558. }
  2559. };
  2560. /////////////////////////////////////////////////////////////////////////////
  2561. //
  2562. // Deprecated for Backwards Compatibility
  2563. //
  2564. /////////////////////////////////////////////////////////////////////////////
  2565. /**
  2566. * @method doBeforeSendQuery
  2567. * @deprecated Use generateRequest.
  2568. */
  2569. YAHOO.widget.AutoComplete.prototype.doBeforeSendQuery = function(sQuery) {
  2570. return this.generateRequest(sQuery);
  2571. };
  2572. /**
  2573. * @method getListItems
  2574. * @deprecated Use getListEl().childNodes.
  2575. */
  2576. YAHOO.widget.AutoComplete.prototype.getListItems = function() {
  2577. var allListItemEls = [],
  2578. els = this._elList.childNodes;
  2579. for(var i=els.length-1; i>=0; i--) {
  2580. allListItemEls[i] = els[i];
  2581. }
  2582. return allListItemEls;
  2583. };
  2584. /////////////////////////////////////////////////////////////////////////
  2585. //
  2586. // Private static methods
  2587. //
  2588. /////////////////////////////////////////////////////////////////////////
  2589. /**
  2590. * Clones object literal or array of object literals.
  2591. *
  2592. * @method AutoComplete._cloneObject
  2593. * @param o {Object} Object.
  2594. * @private
  2595. * @static
  2596. */
  2597. YAHOO.widget.AutoComplete._cloneObject = function(o) {
  2598. if(!YAHOO.lang.isValue(o)) {
  2599. return o;
  2600. }
  2601. var copy = {};
  2602. if(YAHOO.lang.isFunction(o)) {
  2603. copy = o;
  2604. }
  2605. else if(YAHOO.lang.isArray(o)) {
  2606. var array = [];
  2607. for(var i=0,len=o.length;i<len;i++) {
  2608. array[i] = YAHOO.widget.AutoComplete._cloneObject(o[i]);
  2609. }
  2610. copy = array;
  2611. }
  2612. else if(YAHOO.lang.isObject(o)) {
  2613. for (var x in o){
  2614. if(YAHOO.lang.hasOwnProperty(o, x)) {
  2615. if(YAHOO.lang.isValue(o[x]) && YAHOO.lang.isObject(o[x]) || YAHOO.lang.isArray(o[x])) {
  2616. copy[x] = YAHOO.widget.AutoComplete._cloneObject(o[x]);
  2617. }
  2618. else {
  2619. copy[x] = o[x];
  2620. }
  2621. }
  2622. }
  2623. }
  2624. else {
  2625. copy = o;
  2626. }
  2627. return copy;
  2628. };
  2629. YAHOO.register("autocomplete", YAHOO.widget.AutoComplete, {version: "2.7.0", build: "1799"});