autocomplete-base.js revision c7898fc68c0944de3b9a9081bfa0a3b66bfc518c
d2a5a45ff58ab15a8ee0339edcd03f0243373d59Dav Glass * Provides automatic input completion or suggestions for text input fields and
d2a5a45ff58ab15a8ee0339edcd03f0243373d59Dav Glass * textareas.
d2a5a45ff58ab15a8ee0339edcd03f0243373d59Dav Glass * @module autocomplete
d2a5a45ff58ab15a8ee0339edcd03f0243373d59Dav Glass * @since 3.3.0
d2a5a45ff58ab15a8ee0339edcd03f0243373d59Dav Glass * <code>Y.Base</code> extension that provides core autocomplete logic (but no
d2a5a45ff58ab15a8ee0339edcd03f0243373d59Dav Glass * UI implementation) for a text input field or textarea. Must be mixed into a
d2a5a45ff58ab15a8ee0339edcd03f0243373d59Dav Glass * <code>Y.Base</code>-derived class to be useful.
d2a5a45ff58ab15a8ee0339edcd03f0243373d59Dav Glass * @module autocomplete
d2a5a45ff58ab15a8ee0339edcd03f0243373d59Dav Glass * @submodule autocomplete-base
162527ab925c04aa8d6bbf78d0484a133a8076f1Dav Glass * Extension that provides core autocomplete logic (but no UI implementation)
d2a5a45ff58ab15a8ee0339edcd03f0243373d59Dav Glass * for a text input field or textarea.
d2a5a45ff58ab15a8ee0339edcd03f0243373d59Dav Glass * The <code>AutoCompleteBase</code> class provides events and attributes that
d2a5a45ff58ab15a8ee0339edcd03f0243373d59Dav Glass * abstract away core autocomplete logic and configuration, but does not provide
d2a5a45ff58ab15a8ee0339edcd03f0243373d59Dav Glass * a widget implementation or suggestion UI. For a prepackaged autocomplete
d2a5a45ff58ab15a8ee0339edcd03f0243373d59Dav Glass * widget, see <code>AutoCompleteList</code>.
d2a5a45ff58ab15a8ee0339edcd03f0243373d59Dav Glass * This extension cannot be instantiated directly, since it doesn't provide an
d2a5a45ff58ab15a8ee0339edcd03f0243373d59Dav Glass * actual implementation. It's intended to be mixed into a
d2a5a45ff58ab15a8ee0339edcd03f0243373d59Dav Glass * <code>Base</code>-based class or widget, as illustrated in the following
d2a5a45ff58ab15a8ee0339edcd03f0243373d59Dav Glass * YUI().use('autocomplete-base', 'base', function (Y) {
d2a5a45ff58ab15a8ee0339edcd03f0243373d59Dav Glass * var MyAutoComplete = Y.Base.create('myAutocomplete', Y.Base, [Y.AutoComplete], {
d2a5a45ff58ab15a8ee0339edcd03f0243373d59Dav Glass * initializer: function () {
d2a5a45ff58ab15a8ee0339edcd03f0243373d59Dav Glass * this.bindInput();
162527ab925c04aa8d6bbf78d0484a133a8076f1Dav Glass * this.syncInput();
162527ab925c04aa8d6bbf78d0484a133a8076f1Dav Glass * },
162527ab925c04aa8d6bbf78d0484a133a8076f1Dav Glass * destructor: function () {
162527ab925c04aa8d6bbf78d0484a133a8076f1Dav Glass * this.unbindInput();
162527ab925c04aa8d6bbf78d0484a133a8076f1Dav Glass * }
162527ab925c04aa8d6bbf78d0484a133a8076f1Dav Glass * });
162527ab925c04aa8d6bbf78d0484a133a8076f1Dav Glass * // ... custom implementation code ...
162527ab925c04aa8d6bbf78d0484a133a8076f1Dav Glass * @class AutoCompleteBase
5432371fbb6d790a76159481f0dd16e806812153Dav Glass * Fires after the contents of the input field have been completely cleared.
216633e2ad28e9568a902f3763c3bef052c5f908Dav Glass * @event clear
5432371fbb6d790a76159481f0dd16e806812153Dav Glass * @param {EventFacade} e Event facade with the following additional
216633e2ad28e9568a902f3763c3bef052c5f908Dav Glass * properties:
5432371fbb6d790a76159481f0dd16e806812153Dav Glass * <dt>prevVal (String)</dt>
5432371fbb6d790a76159481f0dd16e806812153Dav Glass * Value of the input node before it was cleared.
5432371fbb6d790a76159481f0dd16e806812153Dav Glass * @preventable _defClearFn
f7aa62ea2e8cf43fbb9d83db5060db540ff1893fDav Glass * Fires when the contents of the input field have changed and the input
5432371fbb6d790a76159481f0dd16e806812153Dav Glass * value meets the criteria necessary to generate an autocomplete query.
5432371fbb6d790a76159481f0dd16e806812153Dav Glass * @event query
5432371fbb6d790a76159481f0dd16e806812153Dav Glass * @param {EventFacade} e Event facade with the following additional
f7aa62ea2e8cf43fbb9d83db5060db540ff1893fDav Glass * properties:
f7aa62ea2e8cf43fbb9d83db5060db540ff1893fDav Glass * <dt>inputValue (String)</dt>
f7aa62ea2e8cf43fbb9d83db5060db540ff1893fDav Glass * Full contents of the text input field or textarea that generated
f7aa62ea2e8cf43fbb9d83db5060db540ff1893fDav Glass * the query.
f7aa62ea2e8cf43fbb9d83db5060db540ff1893fDav Glass * <dt>query (String)</dt>
f7aa62ea2e8cf43fbb9d83db5060db540ff1893fDav Glass * Autocomplete query. This is the string that will be used to
f7aa62ea2e8cf43fbb9d83db5060db540ff1893fDav Glass * request completion results. It may or may not be the same as
f7aa62ea2e8cf43fbb9d83db5060db540ff1893fDav Glass * <code>inputValue</code>.
d2a5a45ff58ab15a8ee0339edcd03f0243373d59Dav Glass * @preventable _defQueryFn
d2a5a45ff58ab15a8ee0339edcd03f0243373d59Dav Glass * Fires after query results are received from the DataSource. If no
d2a5a45ff58ab15a8ee0339edcd03f0243373d59Dav Glass * DataSource has been set, this event will not fire.
d2a5a45ff58ab15a8ee0339edcd03f0243373d59Dav Glass * @event results
d2a5a45ff58ab15a8ee0339edcd03f0243373d59Dav Glass * @param {EventFacade} e Event facade with the following additional
162527ab925c04aa8d6bbf78d0484a133a8076f1Dav Glass * properties:
d2a5a45ff58ab15a8ee0339edcd03f0243373d59Dav Glass * <dt>data (Array|Object)</dt>
d2a5a45ff58ab15a8ee0339edcd03f0243373d59Dav Glass * Raw, unfiltered result data (if available).
d2a5a45ff58ab15a8ee0339edcd03f0243373d59Dav Glass * <dt>query (String)</dt>
d2a5a45ff58ab15a8ee0339edcd03f0243373d59Dav Glass * Query that generated these results.
d2a5a45ff58ab15a8ee0339edcd03f0243373d59Dav Glass * <dt>results (Array)</dt>
d2a5a45ff58ab15a8ee0339edcd03f0243373d59Dav Glass * Array of filtered, formatted, and highlighted results. Each item in
d2a5a45ff58ab15a8ee0339edcd03f0243373d59Dav Glass * the array is an object with the following properties:
d2a5a45ff58ab15a8ee0339edcd03f0243373d59Dav Glass * <dt>display (Node|HTMLElement|String)</dt>
d2a5a45ff58ab15a8ee0339edcd03f0243373d59Dav Glass * Formatted result HTML suitable for display to the user.
d2a5a45ff58ab15a8ee0339edcd03f0243373d59Dav Glass * <dt>raw (mixed)</dt>
d2a5a45ff58ab15a8ee0339edcd03f0243373d59Dav Glass * Raw, unformatted result in whatever form it was provided by the
d2a5a45ff58ab15a8ee0339edcd03f0243373d59Dav Glass * DataSource.
d2a5a45ff58ab15a8ee0339edcd03f0243373d59Dav Glass * <dt>text (String)</dt>
d2a5a45ff58ab15a8ee0339edcd03f0243373d59Dav Glass * Plain text version of the result, suitable for being inserted
d2a5a45ff58ab15a8ee0339edcd03f0243373d59Dav Glass * into the value of a text input field or textarea when the result
d2a5a45ff58ab15a8ee0339edcd03f0243373d59Dav Glass * is selected by a user.
d2a5a45ff58ab15a8ee0339edcd03f0243373d59Dav Glass * @preventable _defResultsFn
d0bccce76452becc96b65acaaa684aa6fabaf386Dav Glass * Fires after the input node's value changes, and before the
d2a5a45ff58ab15a8ee0339edcd03f0243373d59Dav Glass * <code>query</code> event.
d2a5a45ff58ab15a8ee0339edcd03f0243373d59Dav Glass * @event valueChange
d2a5a45ff58ab15a8ee0339edcd03f0243373d59Dav Glass * @param {EventFacade} e Event facade with the following additional
d2a5a45ff58ab15a8ee0339edcd03f0243373d59Dav Glass * properties:
d2a5a45ff58ab15a8ee0339edcd03f0243373d59Dav Glass * <dt>newVal (String)</dt>
d2a5a45ff58ab15a8ee0339edcd03f0243373d59Dav Glass * Value of the input node after the change.
d2a5a45ff58ab15a8ee0339edcd03f0243373d59Dav Glass * <dt>prevVal (String)</dt>
d2a5a45ff58ab15a8ee0339edcd03f0243373d59Dav Glass * Value of the input node prior to the change.
preventable: false,
queueable: true
value: false,
dataSource: {
inputNode: {
query: {
readOnly: true,
value: null
queryDelay: {
return template;
return function (query) {
return template.
value: []
return locator;
return function (result) {
results: {
readOnly: true,
value: []
bindInput: function () {
if (!inputNode) {
this.unbindInput();
this._inputEvents = [
syncInput: function () {
unbindInput: function () {
var facade = {
results: []
results = [],
len,
if (unfiltered) {
if (locator) {
if (locator) {
raw = [];
if (formatter) {
return value;
_onResponse: function (e) {
_onValueChange: function (e) {
var delay,
fire,
that;
that = this;
fire = function () {
if (delay) {
fire();
query : null
_defClearFn: function () {
_defQueryFn: function (e) {
callback: {
_defResultsFn: function (e) {