Extension that provides core autocomplete logic (but no UI implementation) for a text input field or textarea.
The AutoCompleteBase
class provides events and attributes that
abstract away core autocomplete logic and configuration, but does not provide
a widget implementation or suggestion UI. For a prepackaged autocomplete
widget, see AutoCompleteList
.
This extension cannot be instantiated directly, since it doesn't provide an
actual implementation. It's intended to be mixed into a
Y.Base
-based class or widget.
Y.Widget
-based example:
YUI().use('autocomplete-base', 'widget', function (Y) { var MyAC = Y.Base.create('myAC', Y.Widget, [Y.AutoCompleteBase], { // Custom prototype methods and properties. }, { // Custom static methods and properties. }); // Custom implementation code. });
Y.Base
-based example:
YUI().use('autocomplete-base', function (Y) { var MyAC = Y.Base.create('myAC', Y.Base, [Y.AutoCompleteBase], { initializer: function () { this._bindUIACBase(); this._syncUIACBase(); }, // Custom prototype methods and properties. }, { // Custom static methods and properties. }); // Custom implementation code. });
protected RegExp
protected
void
_afterValueChange
(
e
)
value
attribute.
e
<EventFacade>
protected
Object
_createArraySource
(
source
)
source
attribute for more details.
source
<Array>
Object
protected
Object
_createFunctionSource
(
source
)
source
attribute for more details.
source
<Function>
Function that accepts a query parameter and
returns an array of results.
Object
protected
Object
_createIOSource
(
source
)
Y.io
as a source.
See the source
attribute for more details.
source
<String>
URL.
Object
protected
Object
_createJSONPSource
(
source
)
source
attribute for more
details.
source
<JSONPRequest|String>
URL string or JSONPRequest instance.
Object
protected
Object
_createObjectSource
(
source
)
source
attribute for more details.
source
<Object>
Object
protected
Object
_createStringSource
(
source
)
source
attribute for more details.
source
<String>
URL or YQL query.
Object
protected
Object
_createYQLSource
(
source
)
source
attribute for
details. If no resultListLocator
is defined, this method
will set a best-guess locator that might work for many typical YQL
queries.
source
<String>
YQL query.
Object
protected
Array
_defaultYQLLocator
(
response
)
response
<Object>
YQL response object.
protected
void
_defClearFn
(
)
clear
event handler. Sets the results
property to an empty array and query
to null.
protected
void
_defQueryFn
(
e
)
query
event handler. Sets the query
property and sends a request to the source if one is configured.
e
<EventFacade>
protected
void
_defResultsFn
(
e
)
results
event handler. Sets the results
property to the latest results.
e
<EventFacade>
protected
void
_functionValidator
(
value
)
true
if value is either a function or
null
.
value
<Function|null>
Value to validate.
protected
mixed
_getObjectValue
(
obj
,
path
)
undefined
nor an object.
obj
<Object>
path
<Array>
mixed
undefined
if the value was
not found at the specified path.
protected
String
_jsonpFormatter
(
url
,
proxy
,
query
)
JSONPRequest
instances.
url
<String>
proxy
<String>
query
<String>
String
protected
void
_onInputBlur
(
e
)
blur
events on the input node.
e
<EventFacade>
protected
void
_onInputValueChange
(
e
)
valueChange
events on the input node and fires a
query
event when the input value meets the configured
criteria.
e
<EventFacade>
protected
void
_onResponse
(
e
)
results
event.
e
<EventFacade>
protected
void
_parseResponse
(
query
,
response
,
data
)
results
event.
query
<String>
Query that generated these results.
response
<Object>
Response containing results.
data
<Object>
Raw response data.
protected
String|null
_parseValue
(
value
)
Returns the query portion of the specified input value, or
null
if there is no suitable query within the input value.
If a query delimiter is defined, the query will be the last delimited part of of the string.
value
<String>
Input value from which to extract the query.
String|null
protected
Function|null
_setLocator
(
locator
)
locator
<Function|String|null>
protected
Function|null
_setRequestTemplate
(
template
)
requestTemplate
attribute.
template
<Function|String|null>
protected
Array
_setResultFilters
(
filters
)
resultFilters
attribute.
filters
<Array|Function|String|null>
null
, a filter
function, an array of filter functions, or a string or array of strings
representing the names of methods on
Y.AutoCompleteFilters
.
Array
null
).
protected
Function|null
_setResultHighlighter
(
highlighter
)
resultHighlighter
attribute.
highlighter
<Function|String|null>
null
, a
highlighter function, or a string representing the name of a method on
Y.AutoCompleteHighlighters
.
protected
DataSource|Object
_setSource
(
source
)
source
attribute. Returns a DataSource or
a DataSource-like object depending on the type of source.
source
<Array|DataSource|Object|String>
AutoComplete source. See
the source
attribute for details.
protected
void
_sourceSuccess
(
data
,
request
)
data
<mixed>
Response data.
request
<Object>
Request object.
protected
void
_syncBrowserAutocomplete
(
)
allowBrowserAutocomplete
attribute.
protected
void
_updateValue
(
newVal
)
Updates the query portion of the value
attribute.
If a query delimiter is defined, the last delimited portion of the input value will be replaced with the specified value.
newVal
<String>
New value.
void
sendRequest
(
query
,
requestTemplate
)
Sends a request to the configured source. If no source is configured, this method won't do anything.
Usually there's no reason to call this method manually; it will be
called automatically when user input causes a query
event to
be fired. The only time you'll need to call this method manually is if
you want to force a request to be sent when no user input has occurred.
query
<String>
(optional) Query to send. If specified, the
query
attribute will be set to this query. If not
specified, the current value of the query
attribute will
be used.
requestTemplate
<Function>
(optional) Request template function.
If not specified, the current value of the requestTemplate
attribute will be used.
allowBrowserAutocompleteChange
(
event
)
event
<Event.Facade>
An Event Facade object with the following attribute specific properties added:
allowTrailingDelimiterChange
(
event
)
event
<Event.Facade>
An Event Facade object with the following attribute specific properties added:
clear
(
e
)
e
<EventFacade>
Event facade with the following additional
properties:
inputNodeChange
(
event
)
event
<Event.Facade>
An Event Facade object with the following attribute specific properties added:
maxResultsChange
(
event
)
event
<Event.Facade>
An Event Facade object with the following attribute specific properties added:
minQueryLengthChange
(
event
)
event
<Event.Facade>
An Event Facade object with the following attribute specific properties added:
query
(
e
)
e
<EventFacade>
Event facade with the following additional
properties:
inputValue
.
queryChange
(
event
)
event
<Event.Facade>
An Event Facade object with the following attribute specific properties added:
queryDelayChange
(
event
)
event
<Event.Facade>
An Event Facade object with the following attribute specific properties added:
queryDelimiterChange
(
event
)
event
<Event.Facade>
An Event Facade object with the following attribute specific properties added:
requestTemplateChange
(
event
)
event
<Event.Facade>
An Event Facade object with the following attribute specific properties added:
resultFiltersChange
(
event
)
event
<Event.Facade>
An Event Facade object with the following attribute specific properties added:
resultFormatterChange
(
event
)
event
<Event.Facade>
An Event Facade object with the following attribute specific properties added:
resultHighlighterChange
(
event
)
event
<Event.Facade>
An Event Facade object with the following attribute specific properties added:
resultListLocatorChange
(
event
)
event
<Event.Facade>
An Event Facade object with the following attribute specific properties added:
results
(
e
)
source
. If
no source has been set, this event will not fire.
e
<EventFacade>
Event facade with the following additional
properties:
text
property.
source
.
resultsChange
(
event
)
event
<Event.Facade>
An Event Facade object with the following attribute specific properties added:
resultTextLocatorChange
(
event
)
event
<Event.Facade>
An Event Facade object with the following attribute specific properties added:
sourceChange
(
event
)
event
<Event.Facade>
An Event Facade object with the following attribute specific properties added:
tokenInputChange
(
event
)
event
<Event.Facade>
An Event Facade object with the following attribute specific properties added:
valueChange
(
event
)
event
<Event.Facade>
An Event Facade object with the following attribute specific properties added:
yqlEnvChange
(
event
)
event
<Event.Facade>
An Event Facade object with the following attribute specific properties added:
yqlProtocolChange
(
event
)
event
<Event.Facade>
An Event Facade object with the following attribute specific properties added: - Boolean
- Boolean
queryDelimiter
is set, trailing delimiters will
automatically be stripped from the input value by default when the
input node loses focus. Set this to true
to allow trailing
delimiters.
- writeonce Node|HTMLElement|String
query
events when appropriate. May be either an input field or a textarea.
- Number
0
or less
will allow an unlimited number of results.
- Number
query
event will be fired. A value of 0
allows empty queries; a negative value will effectively disable all
query
events.
- String|null
Current query, or null
if there is no current query.
The query might not be the same as the current value of the input
node, both for timing reasons (due to queryDelay
) and
because when one or more queryDelimiter
separators are
in use, only the last portion of the delimited input string will be
used as the query value.
- Number
Number of milliseconds to delay after input before triggering a
query
event. If new input occurs before this delay is
over, the previous input event will be ignored and a new delay will
begin.
This can be useful both to throttle queries to a remote data source and to avoid distracting the user by showing them less relevant results before they've paused their typing.
- String|null
query
attribute is
modified.
- Function|String|null
Source request template. This can be a function that accepts a query as a
parameter and returns a request string, or it can be a string containing
the placeholder "{query}", which will be replaced with the actual
URI-encoded query. In either case, the resulting string will be appended
to the request URL when the source
attribute is set to a
remote DataSource, JSONP URL, or XHR URL (it will not be appended to YQL
URLs).
While requestTemplate
may be set to either a function or
a string, it will always be returned as a function that accepts a
query argument and returns a string.
- Array
Array of local result filter functions. If provided, each filter
will be called with two arguments when results are received: the query
and an array of result objects. See the documentation for the
results
event for a list of the properties available on each
result object.
Each filter is expected to return a filtered or modified version of the
results array, which will then be passed on to subsequent filters, then
the resultHighlighter
function (if set), then the
resultFormatter
function (if set), and finally to
subscribers to the results
event.
If no source
is set, result filters will not be called.
Prepackaged result filters provided by the autocomplete-filters and
autocomplete-filters-accentfold modules can be used by specifying the
filter name as a string, such as 'phraseMatch'
(assuming
the necessary filters module is loaded).
- Function|null
Function which will be used to format results. If provided, this function will be called with two arguments after results have been received and filtered: the query and an array of result objects. The formatter is expected to return an array of HTML strings or Node instances containing the desired HTML for each result.
See the documentation for the results
event for a list of
the properties available on each result object.
If no source
is set, the formatter will not be called.
- Function|null
Function which will be used to highlight results. If provided, this function will be called with two arguments after results have been received and filtered: the query and an array of filtered result objects. The highlighter is expected to return an array of highlighted result text in the form of HTML strings.
See the documentation for the results
event for a list of
the properties available on each result object.
If no source
is set, the highlighter will not be called.
- Function|String|null
Locator that should be used to extract an array of results from a non-array response.
By default, no locator is applied, and all responses are assumed to be arrays by default. If all responses are already arrays, you don't need to define a locator.
The locator may be either a function (which will receive the raw response
as an argument and must return an array) or a string representing an
object path, such as "foo.bar.baz" (which would return the value of
result.foo.bar.baz
if the response is an object).
While resultListLocator
may be set to either a function or a
string, it will always be returned as a function that accepts a response
argument and returns an array.
- Function|String|null
Locator that should be used to extract a plain text string from a non-string result item. The resulting text value will typically be the value that ends up being inserted into an input field or textarea when the user of an autocomplete implementation selects a result.
By default, no locator is applied, and all results are assumed to be plain text strings. If all results are already plain text strings, you don't need to define a locator.
The locator may be either a function (which will receive the raw result
as an argument and must return a string) or a string representing an
object path, such as "foo.bar.baz" (which would return the value of
result.foo.bar.baz
if the result is an object).
While resultTextLocator
may be set to either a function or a
string, it will always be returned as a function that accepts a result
argument and returns a string.
- Array|DataSource|Function|Object|String|null
Source for autocomplete results. The following source types are supported:
Example: ['first result', 'second result', 'etc']
The full array will be provided to any configured filters for each query. This is an easy way to create a fully client-side autocomplete implementation.
A DataSource
instance or other object that provides a
DataSource-like sendRequest
method. See the
DataSource
documentation for details.
Example: function (query) { return ['foo', 'bar']; }
A function source will be called with the current query as a parameter, and should return an array of results.
Example: {foo: ['foo result 1', 'foo result 2'], bar: ['bar result']}
An object will be treated as a query hashmap. If a property on the object matches the current query, the value of that property will be used as the response.
The response is assumed to be an array of results by default. If the
response is not an array, provide a resultListLocator
to
process the response and return an array.
If the optional autocomplete-sources
module is loaded, then
the following additional source types will be supported as well:
Example: 'http://example.com/search?q={query}&callback={callback}'
If a URL with a {callback}
placeholder is provided, it
will be used to make a JSONP request. The {query}
placeholder will be replaced with the current query, and the
{callback}
placeholder will be replaced with an
internally-generated JSONP callback name. Both placeholders must
appear in the URL, or the request will fail. An optional
{maxResults}
placeholder may also be provided, and will
be replaced with the value of the maxResults attribute (or 1000 if
the maxResults attribute is 0 or less).
The response is assumed to be an array of results by default. If the
response is not an array, provide a resultListLocator
to
process the response and return an array.
The jsonp
module must be loaded in order for
JSONP URL sources to work. If the jsonp
module
is not already loaded, it will be loaded on demand if possible.
Example: 'http://example.com/search?q={query}'
If a URL without a {callback}
placeholder is provided,
it will be used to make a same-origin XHR request. The
{query}
placeholder will be replaced with the current
query. An optional {maxResults}
placeholder may also be
provided, and will be replaced with the value of the maxResults
attribute (or 1000 if the maxResults attribute is 0 or less).
The response is assumed to be a JSON array of results by default. If
the response is a JSON object and not an array, provide a
resultListLocator
to process the response and return an
array. If the response is in some form other than JSON, you will
need to use a custom DataSource instance as the source.
The io-base
and json-parse
modules
must be loaded in order for XHR URL sources to work. If
these modules are not already loaded, they will be loaded on demand
if possible.
Example: 'select * from search.suggest where query="{query}"'
If a YQL query is provided, it will be used to make a YQL request.
The {query}
placeholder will be replaced with the
current autocomplete query. This placeholder must appear in the YQL
query, or the request will fail. An optional
{maxResults}
placeholder may also be provided, and will
be replaced with the value of the maxResults attribute (or 1000 if
the maxResults attribute is 0 or less).
The yql
module must be loaded in order for YQL
sources to work. If the yql
module is not
already loaded, it will be loaded on demand if possible.
As an alternative to providing a source, you could simply listen for
query
events and handle them any way you see fit. Providing
a source is optional, but will usually be simpler.
- Plugin.TokenInput
inputNode
specified at instantiation time has a
node-tokeninput
plugin attached to it, this attribute will
be a reference to the Y.Plugin.TokenInput
instance.
- String
source
is set to
a YQL query. Set this to null
to use the default Open Data
Tables environment file (http://datatables.org/alltables.env).
- String
source
is set to a YQL query.