PageRenderTime 60ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

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

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