autocomplete-list-debug.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  1. /*
  2. YUI 3.4.1 (build 4118)
  3. Copyright 2011 Yahoo! Inc. All rights reserved.
  4. Licensed under the BSD License.
  5. http://yuilibrary.com/license/
  6. */
  7. YUI.add('autocomplete-list', function(Y) {
  8. /**
  9. * Traditional autocomplete dropdown list widget, just like Mom used to make.
  10. *
  11. * @submodule autocomplete-list
  12. */
  13. /**
  14. * Traditional autocomplete dropdown list widget, just like Mom used to make.
  15. *
  16. * @class AutoCompleteList
  17. * @extends Widget
  18. * @uses AutoCompleteBase
  19. * @uses WidgetPosition
  20. * @uses WidgetPositionAlign
  21. * @constructor
  22. * @param {Object} config Configuration object.
  23. */
  24. var Lang = Y.Lang,
  25. Node = Y.Node,
  26. YArray = Y.Array,
  27. // Whether or not we need an iframe shim.
  28. useShim = Y.UA.ie && Y.UA.ie < 7,
  29. // keyCode constants.
  30. KEY_TAB = 9,
  31. // String shorthand.
  32. _CLASS_ITEM = '_CLASS_ITEM',
  33. _CLASS_ITEM_ACTIVE = '_CLASS_ITEM_ACTIVE',
  34. _CLASS_ITEM_HOVER = '_CLASS_ITEM_HOVER',
  35. _SELECTOR_ITEM = '_SELECTOR_ITEM',
  36. ACTIVE_ITEM = 'activeItem',
  37. ALWAYS_SHOW_LIST = 'alwaysShowList',
  38. CIRCULAR = 'circular',
  39. HOVERED_ITEM = 'hoveredItem',
  40. ID = 'id',
  41. ITEM = 'item',
  42. LIST = 'list',
  43. RESULT = 'result',
  44. RESULTS = 'results',
  45. VISIBLE = 'visible',
  46. WIDTH = 'width',
  47. // Event names.
  48. EVT_SELECT = 'select',
  49. List = Y.Base.create('autocompleteList', Y.Widget, [
  50. Y.AutoCompleteBase,
  51. Y.WidgetPosition,
  52. Y.WidgetPositionAlign
  53. ], {
  54. // -- Prototype Properties -------------------------------------------------
  55. ARIA_TEMPLATE: '<div/>',
  56. ITEM_TEMPLATE: '<li/>',
  57. LIST_TEMPLATE: '<ul/>',
  58. // -- Lifecycle Prototype Methods ------------------------------------------
  59. initializer: function () {
  60. var inputNode = this.get('inputNode');
  61. if (!inputNode) {
  62. Y.error('No inputNode specified.');
  63. return;
  64. }
  65. this._inputNode = inputNode;
  66. this._listEvents = [];
  67. // This ensures that the list is rendered inside the same parent as the
  68. // input node by default, which is necessary for proper ARIA support.
  69. this.DEF_PARENT_NODE = inputNode.get('parentNode');
  70. // Cache commonly used classnames and selectors for performance.
  71. this[_CLASS_ITEM] = this.getClassName(ITEM);
  72. this[_CLASS_ITEM_ACTIVE] = this.getClassName(ITEM, 'active');
  73. this[_CLASS_ITEM_HOVER] = this.getClassName(ITEM, 'hover');
  74. this[_SELECTOR_ITEM] = '.' + this[_CLASS_ITEM];
  75. /**
  76. * Fires when an autocomplete suggestion is selected from the list,
  77. * typically via a keyboard action or mouse click.
  78. *
  79. * @event select
  80. * @param {EventFacade} e Event facade with the following additional
  81. * properties:
  82. *
  83. * <dl>
  84. * <dt>itemNode (Node)</dt>
  85. * <dd>
  86. * List item node that was selected.
  87. * </dd>
  88. *
  89. * <dt>result (Object)</dt>
  90. * <dd>
  91. * AutoComplete result object.
  92. * </dd>
  93. * </dl>
  94. *
  95. * @preventable _defSelectFn
  96. */
  97. this.publish(EVT_SELECT, {
  98. defaultFn: this._defSelectFn
  99. });
  100. },
  101. destructor: function () {
  102. while (this._listEvents.length) {
  103. this._listEvents.pop().detach();
  104. }
  105. if (this._ariaNode) {
  106. this._ariaNode.remove().destroy(true);
  107. }
  108. },
  109. bindUI: function () {
  110. this._bindInput();
  111. this._bindList();
  112. },
  113. renderUI: function () {
  114. var ariaNode = this._createAriaNode(),
  115. boundingBox = this.get('boundingBox'),
  116. contentBox = this.get('contentBox'),
  117. inputNode = this._inputNode,
  118. listNode = this._createListNode(),
  119. parentNode = inputNode.get('parentNode');
  120. inputNode.addClass(this.getClassName('input')).setAttrs({
  121. 'aria-autocomplete': LIST,
  122. 'aria-expanded' : false,
  123. 'aria-owns' : listNode.get('id')
  124. });
  125. // ARIA node must be outside the widget or announcements won't be made
  126. // when the widget is hidden.
  127. parentNode.append(ariaNode);
  128. // Add an iframe shim for IE6.
  129. if (useShim) {
  130. boundingBox.plug(Y.Plugin.Shim);
  131. }
  132. // Force position: absolute on the boundingBox. This works around a
  133. // potential CSS loading race condition in Gecko that can cause the
  134. // boundingBox to become relatively positioned, which is all kinds of
  135. // no good.
  136. boundingBox.setStyle('position', 'absolute');
  137. this._ariaNode = ariaNode;
  138. this._boundingBox = boundingBox;
  139. this._contentBox = contentBox;
  140. this._listNode = listNode;
  141. this._parentNode = parentNode;
  142. },
  143. syncUI: function () {
  144. // No need to call _syncPosition() here; the other _sync methods will
  145. // call it when necessary.
  146. this._syncResults();
  147. this._syncVisibility();
  148. },
  149. // -- Public Prototype Methods ---------------------------------------------
  150. /**
  151. * Hides the list, unless the <code>alwaysShowList</code> attribute is
  152. * <code>true</code>.
  153. *
  154. * @method hide
  155. * @see show
  156. * @chainable
  157. */
  158. hide: function () {
  159. return this.get(ALWAYS_SHOW_LIST) ? this : this.set(VISIBLE, false);
  160. },
  161. /**
  162. * Selects the specified <i>itemNode</i>, or the current
  163. * <code>activeItem</code> if <i>itemNode</i> is not specified.
  164. *
  165. * @method selectItem
  166. * @param {Node} itemNode (optional) Item node to select.
  167. * @param {EventFacade} originEvent (optional) Event that triggered the
  168. * selection, if any.
  169. * @chainable
  170. */
  171. selectItem: function (itemNode, originEvent) {
  172. if (itemNode) {
  173. if (!itemNode.hasClass(this[_CLASS_ITEM])) {
  174. return this;
  175. }
  176. } else {
  177. itemNode = this.get(ACTIVE_ITEM);
  178. if (!itemNode) {
  179. return this;
  180. }
  181. }
  182. this.fire(EVT_SELECT, {
  183. itemNode : itemNode,
  184. originEvent: originEvent || null,
  185. result : itemNode.getData(RESULT)
  186. });
  187. return this;
  188. },
  189. // -- Protected Prototype Methods ------------------------------------------
  190. /**
  191. * Activates the next item after the currently active item. If there is no
  192. * next item and the <code>circular</code> attribute is <code>true</code>,
  193. * focus will wrap back to the input node.
  194. *
  195. * @method _activateNextItem
  196. * @chainable
  197. * @protected
  198. */
  199. _activateNextItem: function () {
  200. var item = this.get(ACTIVE_ITEM),
  201. nextItem;
  202. if (item) {
  203. nextItem = item.next(this[_SELECTOR_ITEM]) ||
  204. (this.get(CIRCULAR) ? null : item);
  205. } else {
  206. nextItem = this._getFirstItemNode();
  207. }
  208. this.set(ACTIVE_ITEM, nextItem);
  209. return this;
  210. },
  211. /**
  212. * Activates the item previous to the currently active item. If there is no
  213. * previous item and the <code>circular</code> attribute is
  214. * <code>true</code>, focus will wrap back to the input node.
  215. *
  216. * @method _activatePrevItem
  217. * @chainable
  218. * @protected
  219. */
  220. _activatePrevItem: function () {
  221. var item = this.get(ACTIVE_ITEM),
  222. prevItem = item ? item.previous(this[_SELECTOR_ITEM]) :
  223. this.get(CIRCULAR) && this._getLastItemNode();
  224. this.set(ACTIVE_ITEM, prevItem || null);
  225. return this;
  226. },
  227. /**
  228. * Appends the specified result <i>items</i> to the list inside a new item
  229. * node.
  230. *
  231. * @method _add
  232. * @param {Array|Node|HTMLElement|String} items Result item or array of
  233. * result items.
  234. * @return {NodeList} Added nodes.
  235. * @protected
  236. */
  237. _add: function (items) {
  238. var itemNodes = [];
  239. YArray.each(Lang.isArray(items) ? items : [items], function (item) {
  240. itemNodes.push(this._createItemNode(item).setData(RESULT, item));
  241. }, this);
  242. itemNodes = Y.all(itemNodes);
  243. this._listNode.append(itemNodes.toFrag());
  244. return itemNodes;
  245. },
  246. /**
  247. * Updates the ARIA live region with the specified message.
  248. *
  249. * @method _ariaSay
  250. * @param {String} stringId String id (from the <code>strings</code>
  251. * attribute) of the message to speak.
  252. * @param {Object} subs (optional) Substitutions for placeholders in the
  253. * string.
  254. * @protected
  255. */
  256. _ariaSay: function (stringId, subs) {
  257. var message = this.get('strings.' + stringId);
  258. this._ariaNode.setContent(subs ? Lang.sub(message, subs) : message);
  259. },
  260. /**
  261. * Binds <code>inputNode</code> events and behavior.
  262. *
  263. * @method _bindInput
  264. * @protected
  265. */
  266. _bindInput: function () {
  267. var inputNode = this._inputNode,
  268. alignNode, alignWidth, tokenInput;
  269. // Null align means we can auto-align. Set align to false to prevent
  270. // auto-alignment, or a valid alignment config to customize the
  271. // alignment.
  272. if (this.get('align') === null) {
  273. // If this is a tokenInput, align with its bounding box.
  274. // Otherwise, align with the inputNode. Bit of a cheat.
  275. tokenInput = this.get('tokenInput');
  276. alignNode = (tokenInput && tokenInput.get('boundingBox')) || inputNode;
  277. this.set('align', {
  278. node : alignNode,
  279. points: ['tl', 'bl']
  280. });
  281. // If no width config is set, attempt to set the list's width to the
  282. // width of the alignment node. If the alignment node's width is
  283. // falsy, do nothing.
  284. if (!this.get(WIDTH) && (alignWidth = alignNode.get('offsetWidth'))) {
  285. this.set(WIDTH, alignWidth);
  286. }
  287. }
  288. // Attach inputNode events.
  289. this._listEvents.concat([
  290. inputNode.after('blur', this._afterListInputBlur, this),
  291. inputNode.after('focus', this._afterListInputFocus, this)
  292. ]);
  293. },
  294. /**
  295. * Binds list events.
  296. *
  297. * @method _bindList
  298. * @protected
  299. */
  300. _bindList: function () {
  301. this._listEvents.concat([
  302. Y.on('windowresize', this._syncPosition, this),
  303. this.after({
  304. blur : this._afterListBlur,
  305. focus : this._afterListFocus,
  306. mouseover: this._afterMouseOver,
  307. mouseout : this._afterMouseOut,
  308. activeItemChange : this._afterActiveItemChange,
  309. alwaysShowListChange: this._afterAlwaysShowListChange,
  310. hoveredItemChange : this._afterHoveredItemChange,
  311. resultsChange : this._afterResultsChange,
  312. visibleChange : this._afterVisibleChange
  313. }),
  314. this._listNode.delegate('click', this._onItemClick,
  315. this[_SELECTOR_ITEM], this)
  316. ]);
  317. },
  318. /**
  319. * Clears the contents of the tray.
  320. *
  321. * @method _clear
  322. * @protected
  323. */
  324. _clear: function () {
  325. this.set(ACTIVE_ITEM, null);
  326. this._set(HOVERED_ITEM, null);
  327. this._listNode.get('children').remove(true);
  328. },
  329. /**
  330. * Creates and returns an ARIA live region node.
  331. *
  332. * @method _createAriaNode
  333. * @return {Node} ARIA node.
  334. * @protected
  335. */
  336. _createAriaNode: function () {
  337. var ariaNode = Node.create(this.ARIA_TEMPLATE);
  338. return ariaNode.addClass(this.getClassName('aria')).setAttrs({
  339. 'aria-live': 'polite',
  340. role : 'status'
  341. });
  342. },
  343. /**
  344. * Creates and returns an item node with the specified <i>content</i>.
  345. *
  346. * @method _createItemNode
  347. * @param {Object} result Result object.
  348. * @return {Node} Item node.
  349. * @protected
  350. */
  351. _createItemNode: function (result) {
  352. var itemNode = Node.create(this.ITEM_TEMPLATE);
  353. return itemNode.addClass(this[_CLASS_ITEM]).setAttrs({
  354. id : Y.stamp(itemNode),
  355. role: 'option'
  356. }).setAttribute('data-text', result.text).append(result.display);
  357. },
  358. /**
  359. * Creates and returns a list node. If the `listNode` attribute is already
  360. * set to an existing node, that node will be used.
  361. *
  362. * @method _createListNode
  363. * @return {Node} List node.
  364. * @protected
  365. */
  366. _createListNode: function () {
  367. var listNode = this.get('listNode') || Node.create(this.LIST_TEMPLATE);
  368. listNode.addClass(this.getClassName(LIST)).setAttrs({
  369. id : Y.stamp(listNode),
  370. role: 'listbox'
  371. });
  372. this._set('listNode', listNode);
  373. this.get('contentBox').append(listNode);
  374. return listNode;
  375. },
  376. /**
  377. * Gets the first item node in the list, or <code>null</code> if the list is
  378. * empty.
  379. *
  380. * @method _getFirstItemNode
  381. * @return {Node|null}
  382. * @protected
  383. */
  384. _getFirstItemNode: function () {
  385. return this._listNode.one(this[_SELECTOR_ITEM]);
  386. },
  387. /**
  388. * Gets the last item node in the list, or <code>null</code> if the list is
  389. * empty.
  390. *
  391. * @method _getLastItemNode
  392. * @return {Node|null}
  393. * @protected
  394. */
  395. _getLastItemNode: function () {
  396. return this._listNode.one(this[_SELECTOR_ITEM] + ':last-child');
  397. },
  398. /**
  399. * Synchronizes the result list's position and alignment.
  400. *
  401. * @method _syncPosition
  402. * @protected
  403. */
  404. _syncPosition: function () {
  405. // Force WidgetPositionAlign to refresh its alignment.
  406. this._syncUIPosAlign();
  407. // Resize the IE6 iframe shim to match the list's dimensions.
  408. this._syncShim();
  409. },
  410. /**
  411. * Synchronizes the results displayed in the list with those in the
  412. * <i>results</i> argument, or with the <code>results</code> attribute if an
  413. * argument is not provided.
  414. *
  415. * @method _syncResults
  416. * @param {Array} results (optional) Results.
  417. * @protected
  418. */
  419. _syncResults: function (results) {
  420. if (!results) {
  421. results = this.get(RESULTS);
  422. }
  423. this._clear();
  424. if (results.length) {
  425. this._add(results);
  426. this._ariaSay('items_available');
  427. }
  428. this._syncPosition();
  429. if (this.get('activateFirstItem') && !this.get(ACTIVE_ITEM)) {
  430. this.set(ACTIVE_ITEM, this._getFirstItemNode());
  431. }
  432. },
  433. /**
  434. * Synchronizes the size of the iframe shim used for IE6 and lower. In other
  435. * browsers, this method is a noop.
  436. *
  437. * @method _syncShim
  438. * @protected
  439. */
  440. _syncShim: useShim ? function () {
  441. this._boundingBox.shim.sync();
  442. } : function () {},
  443. /**
  444. * Synchronizes the visibility of the tray with the <i>visible</i> argument,
  445. * or with the <code>visible</code> attribute if an argument is not
  446. * provided.
  447. *
  448. * @method _syncVisibility
  449. * @param {Boolean} visible (optional) Visibility.
  450. * @protected
  451. */
  452. _syncVisibility: function (visible) {
  453. if (this.get(ALWAYS_SHOW_LIST)) {
  454. visible = true;
  455. this.set(VISIBLE, visible);
  456. }
  457. if (typeof visible === 'undefined') {
  458. visible = this.get(VISIBLE);
  459. }
  460. this._inputNode.set('aria-expanded', visible);
  461. this._boundingBox.set('aria-hidden', !visible);
  462. if (visible) {
  463. this._syncPosition();
  464. } else {
  465. this.set(ACTIVE_ITEM, null);
  466. this._set(HOVERED_ITEM, null);
  467. // Force a reflow to work around a glitch in IE6 and 7 where some of
  468. // the contents of the list will sometimes remain visible after the
  469. // container is hidden.
  470. this._boundingBox.get('offsetWidth');
  471. }
  472. },
  473. // -- Protected Event Handlers ---------------------------------------------
  474. /**
  475. * Handles <code>activeItemChange</code> events.
  476. *
  477. * @method _afterActiveItemChange
  478. * @param {EventTarget} e
  479. * @protected
  480. */
  481. _afterActiveItemChange: function (e) {
  482. var inputNode = this._inputNode,
  483. newVal = e.newVal,
  484. prevVal = e.prevVal,
  485. node;
  486. // The previous item may have disappeared by the time this handler runs,
  487. // so we need to be careful.
  488. if (prevVal && prevVal._node) {
  489. prevVal.removeClass(this[_CLASS_ITEM_ACTIVE]);
  490. }
  491. if (newVal) {
  492. newVal.addClass(this[_CLASS_ITEM_ACTIVE]);
  493. inputNode.set('aria-activedescendant', newVal.get(ID));
  494. } else {
  495. inputNode.removeAttribute('aria-activedescendant');
  496. }
  497. if (this.get('scrollIntoView')) {
  498. node = newVal || inputNode;
  499. if (!node.inRegion(Y.DOM.viewportRegion(), true)
  500. || !node.inRegion(this._contentBox, true)) {
  501. node.scrollIntoView();
  502. }
  503. }
  504. },
  505. /**
  506. * Handles <code>alwaysShowListChange</code> events.
  507. *
  508. * @method _afterAlwaysShowListChange
  509. * @param {EventTarget} e
  510. * @protected
  511. */
  512. _afterAlwaysShowListChange: function (e) {
  513. this.set(VISIBLE, e.newVal || this.get(RESULTS).length > 0);
  514. },
  515. /**
  516. * Handles <code>hoveredItemChange</code> events.
  517. *
  518. * @method _afterHoveredItemChange
  519. * @param {EventTarget} e
  520. * @protected
  521. */
  522. _afterHoveredItemChange: function (e) {
  523. var newVal = e.newVal,
  524. prevVal = e.prevVal;
  525. if (prevVal) {
  526. prevVal.removeClass(this[_CLASS_ITEM_HOVER]);
  527. }
  528. if (newVal) {
  529. newVal.addClass(this[_CLASS_ITEM_HOVER]);
  530. }
  531. },
  532. /**
  533. * Handles list blur events.
  534. *
  535. * @method _afterListBlur
  536. * @protected
  537. */
  538. _afterListBlur: function () {
  539. this._listFocused = false;
  540. // Hide the list unless focus switched to the input node.
  541. if (!this._listInputFocused) {
  542. this.hide();
  543. }
  544. },
  545. /**
  546. * Handles list focus events.
  547. *
  548. * @method _afterListFocus
  549. * @protected
  550. */
  551. _afterListFocus: function () {
  552. this._listFocused = true;
  553. },
  554. /**
  555. * Handles `inputNode` blur events.
  556. *
  557. * @method _afterListInputBlur
  558. * @protected
  559. */
  560. _afterListInputBlur: function () {
  561. this._listInputFocused = false;
  562. // Hide the list on inputNode blur events, unless the mouse is currently
  563. // over the list (which indicates that the user is probably interacting
  564. // with it). The _lastInputKey property comes from the
  565. // autocomplete-list-keys module.
  566. if ((!this._mouseOverList && !this._listFocused)
  567. || this._lastInputKey === KEY_TAB) {
  568. this.hide();
  569. }
  570. },
  571. /**
  572. * Handles `inputNode` focus events.
  573. *
  574. * @method _afterListInputFocus
  575. * @protected
  576. */
  577. _afterListInputFocus: function () {
  578. this._listInputFocused = true;
  579. },
  580. /**
  581. * Handles <code>mouseover</code> events.
  582. *
  583. * @method _afterMouseOver
  584. * @param {EventTarget} e
  585. * @protected
  586. */
  587. _afterMouseOver: function (e) {
  588. var itemNode = e.domEvent.target.ancestor(this[_SELECTOR_ITEM], true);
  589. this._mouseOverList = true;
  590. if (itemNode) {
  591. this._set(HOVERED_ITEM, itemNode);
  592. }
  593. },
  594. /**
  595. * Handles <code>mouseout</code> events.
  596. *
  597. * @method _afterMouseOut
  598. * @param {EventTarget} e
  599. * @protected
  600. */
  601. _afterMouseOut: function () {
  602. this._mouseOverList = false;
  603. this._set(HOVERED_ITEM, null);
  604. // This takes care of the edge case where the user right-clicks on a
  605. // list item, then clicks elsewhere in the document.
  606. if (!this._listFocused && !this._listInputFocused) {
  607. this.hide();
  608. }
  609. },
  610. /**
  611. * Handles <code>resultsChange</code> events.
  612. *
  613. * @method _afterResultsChange
  614. * @param {EventFacade} e
  615. * @protected
  616. */
  617. _afterResultsChange: function (e) {
  618. this._syncResults(e.newVal);
  619. if (!this.get(ALWAYS_SHOW_LIST)) {
  620. this.set(VISIBLE, !!e.newVal.length);
  621. }
  622. },
  623. /**
  624. * Handles <code>visibleChange</code> events.
  625. *
  626. * @method _afterVisibleChange
  627. * @param {EventFacade} e
  628. * @protected
  629. */
  630. _afterVisibleChange: function (e) {
  631. this._syncVisibility(!!e.newVal);
  632. },
  633. /**
  634. * Delegated event handler for item <code>click</code> events.
  635. *
  636. * @method _onItemClick
  637. * @param {EventTarget} e
  638. * @protected
  639. */
  640. _onItemClick: function (e) {
  641. var itemNode = e.currentTarget;
  642. this.set(ACTIVE_ITEM, itemNode);
  643. this.selectItem(itemNode, e);
  644. },
  645. // -- Protected Default Event Handlers -------------------------------------
  646. /**
  647. * Default <code>select</code> event handler.
  648. *
  649. * @method _defSelectFn
  650. * @param {EventTarget} e
  651. * @protected
  652. */
  653. _defSelectFn: function (e) {
  654. var text = e.result.text;
  655. // TODO: support typeahead completion, etc.
  656. this._inputNode.focus();
  657. this._updateValue(text);
  658. this._ariaSay('item_selected', {item: text});
  659. this.hide();
  660. }
  661. }, {
  662. ATTRS: {
  663. /**
  664. * If <code>true</code>, the first item in the list will be activated by
  665. * default when the list is initially displayed and when results change.
  666. *
  667. * @attribute activateFirstItem
  668. * @type Boolean
  669. * @default false
  670. */
  671. activateFirstItem: {
  672. value: false
  673. },
  674. /**
  675. * Item that's currently active, if any. When the user presses enter,
  676. * this is the item that will be selected.
  677. *
  678. * @attribute activeItem
  679. * @type Node
  680. */
  681. activeItem: {
  682. setter: Y.one,
  683. value: null
  684. },
  685. /**
  686. * If <code>true</code>, the list will remain visible even when there
  687. * are no results to display.
  688. *
  689. * @attribute alwaysShowList
  690. * @type Boolean
  691. * @default false
  692. */
  693. alwaysShowList: {
  694. value: false
  695. },
  696. /**
  697. * If <code>true</code>, keyboard navigation will wrap around to the
  698. * opposite end of the list when navigating past the first or last item.
  699. *
  700. * @attribute circular
  701. * @type Boolean
  702. * @default true
  703. */
  704. circular: {
  705. value: true
  706. },
  707. /**
  708. * Item currently being hovered over by the mouse, if any.
  709. *
  710. * @attribute hoveredItem
  711. * @type Node|null
  712. * @readOnly
  713. */
  714. hoveredItem: {
  715. readOnly: true,
  716. value: null
  717. },
  718. /**
  719. * Node that will contain result items.
  720. *
  721. * @attribute listNode
  722. * @type Node|null
  723. * @initOnly
  724. */
  725. listNode: {
  726. writeOnce: 'initOnly',
  727. value: null
  728. },
  729. /**
  730. * If <code>true</code>, the viewport will be scrolled to ensure that
  731. * the active list item is visible when necessary.
  732. *
  733. * @attribute scrollIntoView
  734. * @type Boolean
  735. * @default false
  736. */
  737. scrollIntoView: {
  738. value: false
  739. },
  740. /**
  741. * Translatable strings used by the AutoCompleteList widget.
  742. *
  743. * @attribute strings
  744. * @type Object
  745. */
  746. strings: {
  747. valueFn: function () {
  748. return Y.Intl.get('autocomplete-list');
  749. }
  750. },
  751. /**
  752. * If <code>true</code>, pressing the tab key while the list is visible
  753. * will select the active item, if any.
  754. *
  755. * @attribute tabSelect
  756. * @type Boolean
  757. * @default true
  758. */
  759. tabSelect: {
  760. value: true
  761. },
  762. // The "visible" attribute is documented in Widget.
  763. visible: {
  764. value: false
  765. }
  766. },
  767. CSS_PREFIX: Y.ClassNameManager.getClassName('aclist')
  768. });
  769. Y.AutoCompleteList = List;
  770. /**
  771. * Alias for <a href="AutoCompleteList.html"><code>AutoCompleteList</code></a>.
  772. * See that class for API docs.
  773. *
  774. * @class AutoComplete
  775. */
  776. Y.AutoComplete = List;
  777. }, '3.4.1' ,{lang:['en'], after:['autocomplete-sources'], requires:['autocomplete-base', 'event-resize', 'node-screen', 'selector-css3', 'shim-plugin', 'widget', 'widget-position', 'widget-position-align'], skinnable:true});