autocomplete-list-debug.js revision 221103f45ef4ea2c4caacb9f367f88cac34b8299
71bbbe94567d63ab107a7eab263595bcc6e47833Allen Rabinovich * Traditional autocomplete dropdown list widget, just like Mom used to make.
71bbbe94567d63ab107a7eab263595bcc6e47833Allen Rabinovich * @submodule autocomplete-list
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * Traditional autocomplete dropdown list widget, just like Mom used to make.
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * @class AutoCompleteList
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * @extends Widget
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * @uses AutoCompleteBase
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * @uses WidgetPosition
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * @uses WidgetPositionAlign
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * @constructor
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * @param {Object} config Configuration object.
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich // Whether or not we need an iframe shim.
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich // keyCode constants.
71bbbe94567d63ab107a7eab263595bcc6e47833Allen Rabinovich // String shorthand.
71bbbe94567d63ab107a7eab263595bcc6e47833Allen Rabinovich // Event names.
f7fe7479a730247687add3053f0760430498bc71Allen RabinovichList = Y.Base.create('autocompleteList', Y.Widget, [
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich // -- Prototype Properties -------------------------------------------------
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich // -- Lifecycle Prototype Methods ------------------------------------------
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich // This ensures that the list is rendered inside the same parent as the
71bbbe94567d63ab107a7eab263595bcc6e47833Allen Rabinovich // input node by default, which is necessary for proper ARIA support.
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich this.DEF_PARENT_NODE = inputNode.get('parentNode');
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich // Cache commonly used classnames and selectors for performance.
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich this[_CLASS_ITEM] = this.getClassName(ITEM);
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich this[_CLASS_ITEM_ACTIVE] = this.getClassName(ITEM, 'active');
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich this[_CLASS_ITEM_HOVER] = this.getClassName(ITEM, 'hover');
71bbbe94567d63ab107a7eab263595bcc6e47833Allen Rabinovich this[_SELECTOR_ITEM] = '.' + this[_CLASS_ITEM];
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * Fires when an autocomplete suggestion is selected from the list,
71bbbe94567d63ab107a7eab263595bcc6e47833Allen Rabinovich * typically via a keyboard action or mouse click.
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * @event select
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * @param {EventFacade} e Event facade with the following additional
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * properties:
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * <dt>itemNode (Node)</dt>
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * List item node that was selected.
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * <dt>result (Object)</dt>
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * AutoComplete result object.
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * @preventable _defSelectFn
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich bindUI: function () {
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich renderUI: function () {
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich inputNode.addClass(this.getClassName('input')).setAttrs({
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich 'aria-expanded' : false,
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich // Add an iframe shim for IE6.
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich // Force position: absolute on the boundingBox. This works around a
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich // potential CSS loading race condition in Gecko that can cause the
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich // boundingBox to become relatively positioned, which is all kinds of
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich boundingBox.setStyle('position', 'absolute');
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich syncUI: function () {
71bbbe94567d63ab107a7eab263595bcc6e47833Allen Rabinovich // No need to call _syncPosition() here; the other _sync methods will
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich // call it when necessary.
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich // -- Public Prototype Methods ---------------------------------------------
71bbbe94567d63ab107a7eab263595bcc6e47833Allen Rabinovich * Hides the list, unless the <code>alwaysShowList</code> attribute is
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * <code>true</code>.
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * @method hide
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich hide: function () {
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich return this.get(ALWAYS_SHOW_LIST) ? this : this.set(VISIBLE, false);
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * Selects the specified <i>itemNode</i>, or the current
71bbbe94567d63ab107a7eab263595bcc6e47833Allen Rabinovich * <code>activeItem</code> if <i>itemNode</i> is not specified.
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * @method selectItem
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * @param {Node} itemNode (optional) Item node to select.
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * @param {EventFacade} originEvent (optional) Event that triggered the
71bbbe94567d63ab107a7eab263595bcc6e47833Allen Rabinovich * selection, if any.
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich selectItem: function (itemNode, originEvent) {
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich if (!itemNode.hasClass(this[_CLASS_ITEM])) {
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich // -- Protected Prototype Methods ------------------------------------------
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * Activates the next item after the currently active item. If there is no
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * next item and the <code>circular</code> attribute is <code>true</code>,
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * focus will wrap back to the input node.
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * @method _activateNextItem
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich nextItem = item.next(this[_SELECTOR_ITEM]) ||
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * Activates the item previous to the currently active item. If there is no
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * previous item and the <code>circular</code> attribute is
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * <code>true</code>, focus will wrap back to the input node.
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * @method _activatePrevItem
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich prevItem = item ? item.previous(this[_SELECTOR_ITEM]) :
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich this.get(CIRCULAR) && this._getLastItemNode();
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * Appends the specified result <i>items</i> to the list inside a new item
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * @method _add
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * @param {Array|Node|HTMLElement|String} items Result item or array of
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * result items.
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * @return {NodeList} Added nodes.
71bbbe94567d63ab107a7eab263595bcc6e47833Allen Rabinovich YArray.each(Lang.isArray(items) ? items : [items], function (item) {
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich itemNodes.push(this._createItemNode(item).setData(RESULT, item));
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * Binds <code>inputNode</code> events and behavior.
71bbbe94567d63ab107a7eab263595bcc6e47833Allen Rabinovich * @method _bindInput
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich // Null align means we can auto-align. Set align to false to prevent
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich // auto-alignment, or a valid alignment config to customize the
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich // alignment.
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich // If this is a tokenInput, align with its bounding box.
71bbbe94567d63ab107a7eab263595bcc6e47833Allen Rabinovich // Otherwise, align with the inputNode. Bit of a cheat.
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich alignNode = (tokenInput && tokenInput.get('boundingBox')) || inputNode;
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich // If no width config is set, attempt to set the list's width to the
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich // width of the alignment node. If the alignment node's width is
71bbbe94567d63ab107a7eab263595bcc6e47833Allen Rabinovich // falsy, do nothing.
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich if (!this.get(WIDTH) && (alignWidth = alignNode.get('offsetWidth'))) {
71bbbe94567d63ab107a7eab263595bcc6e47833Allen Rabinovich // Attach inputNode events.
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich this._listEvents.push(inputNode.on('blur', this._onListInputBlur, this));
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * Binds list events.
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * @method _bindList
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich Y.on('windowresize', this._syncPosition, this),
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich activeItemChange : this._afterActiveItemChange,
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich alwaysShowListChange: this._afterAlwaysShowListChange,
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich hoveredItemChange : this._afterHoveredItemChange,
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich this._listNode.delegate('click', this._onItemClick,
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * Clears the contents of the tray.
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * @method _clear
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich _clear: function () {
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich this._listNode.get('children').remove(true);
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * Creates and returns an item node with the specified <i>content</i>.
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * @method _createItemNode
71bbbe94567d63ab107a7eab263595bcc6e47833Allen Rabinovich * @param {Object} result Result object.
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * @return {Node} Item node.
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich var itemNode = Node.create(this.ITEM_TEMPLATE);
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich return itemNode.addClass(this[_CLASS_ITEM]).setAttrs({
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich }).setAttribute('data-text', result.text).append(result.display);
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * Creates and returns a list node. If the `listNode` attribute is already
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * set to an existing node, that node will be used.
71bbbe94567d63ab107a7eab263595bcc6e47833Allen Rabinovich * @method _createListNode
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * @return {Node} List node.
71bbbe94567d63ab107a7eab263595bcc6e47833Allen Rabinovich var listNode = this.get('listNode') || Node.create(this.LIST_TEMPLATE);
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich listNode.addClass(this.getClassName(LIST)).setAttrs({
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * Gets the first item node in the list, or <code>null</code> if the list is
71bbbe94567d63ab107a7eab263595bcc6e47833Allen Rabinovich * @method _getFirstItemNode
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * @return {Node|null}
71bbbe94567d63ab107a7eab263595bcc6e47833Allen Rabinovich return this._listNode.one(this[_SELECTOR_ITEM]);
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * Gets the last item node in the list, or <code>null</code> if the list is
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * @method _getLastItemNode
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * @return {Node|null}
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich return this._listNode.one(this[_SELECTOR_ITEM] + ':last-child');
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * Synchronizes the result list's position and alignment.
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * @method _syncPosition
71bbbe94567d63ab107a7eab263595bcc6e47833Allen Rabinovich // Force WidgetPositionAlign to refresh its alignment.
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich // Resize the IE6 iframe shim to match the list's dimensions.
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * Synchronizes the results displayed in the list with those in the
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * <i>results</i> argument, or with the <code>results</code> attribute if an
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * argument is not provided.
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * @method _syncResults
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * @param {Array} results (optional) Results.
71bbbe94567d63ab107a7eab263595bcc6e47833Allen Rabinovich if (this.get('activateFirstItem') && !this.get(ACTIVE_ITEM)) {
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich this.set(ACTIVE_ITEM, this._getFirstItemNode());
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * Synchronizes the size of the iframe shim used for IE6 and lower. In other
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * browsers, this method is a noop.
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * @method _syncShim
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich } : function () {},
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * Synchronizes the visibility of the tray with the <i>visible</i> argument,
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * or with the <code>visible</code> attribute if an argument is not
71bbbe94567d63ab107a7eab263595bcc6e47833Allen Rabinovich * @method _syncVisibility
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * @param {Boolean} visible (optional) Visibility.
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich this._inputNode.set('aria-expanded', visible);
71bbbe94567d63ab107a7eab263595bcc6e47833Allen Rabinovich this._boundingBox.set('aria-hidden', !visible);
71bbbe94567d63ab107a7eab263595bcc6e47833Allen Rabinovich // Force a reflow to work around a glitch in IE6 and 7 where some of
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich // the contents of the list will sometimes remain visible after the
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich // container is hidden.
71bbbe94567d63ab107a7eab263595bcc6e47833Allen Rabinovich // -- Protected Event Handlers ---------------------------------------------
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * Handles <code>activeItemChange</code> events.
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * @method _afterActiveItemChange
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * @param {EventTarget} e
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich // The previous item may have disappeared by the time this handler runs,
71bbbe94567d63ab107a7eab263595bcc6e47833Allen Rabinovich // so we need to be careful.
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich prevVal.removeClass(this[_CLASS_ITEM_ACTIVE]);
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich inputNode.set('aria-activedescendant', newVal.get(ID));
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich inputNode.removeAttribute('aria-activedescendant');
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich if (!node.inRegion(Y.DOM.viewportRegion(), true)
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich || !node.inRegion(this._contentBox, true)) {
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * Handles <code>alwaysShowListChange</code> events.
71bbbe94567d63ab107a7eab263595bcc6e47833Allen Rabinovich * @method _afterAlwaysShowListChange
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * @param {EventTarget} e
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich this.set(VISIBLE, e.newVal || this.get(RESULTS).length > 0);
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * Handles <code>hoveredItemChange</code> events.
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * @method _afterHoveredItemChange
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * @param {EventTarget} e
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich prevVal.removeClass(this[_CLASS_ITEM_HOVER]);
71bbbe94567d63ab107a7eab263595bcc6e47833Allen Rabinovich * Handles <code>mouseover</code> events.
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * @method _afterMouseOver
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * @param {EventTarget} e
71bbbe94567d63ab107a7eab263595bcc6e47833Allen Rabinovich var itemNode = e.domEvent.target.ancestor(this[_SELECTOR_ITEM], true);
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * Handles <code>mouseout</code> events.
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * @method _afterMouseOut
f7fe7479a730247687add3053f0760430498bc71Allen Rabinovich * @param {EventTarget} e
_afterResultsChange: function (e) {
_afterVisibleChange: function (e) {
_onListInputBlur: function (e) {
this.hide();
_onItemClick: function (e) {
_defSelectFn: function (e) {
this.hide();
ATTRS: {
value: false
activeItem: {
value: null
value: false
circular: {
value: true
hoveredItem: {
readOnly: true,
value: null
listNode: {
value: null
value: false
tabSelect: {
value: true
visible: {
value: false
* Alias for <a href="AutoCompleteList.html"><code>AutoCompleteList</code></a>.