An all-new AutoComplete widget is landing in YUI 3.3.0. In this talk from YUIConf 2010, AutoComplete author Ryan Grove will take you on a whirlwind tour of some of the many autocomplete patterns it makes possible, as well as a deep dive into its powerful new YQL integration, filtering, and highlighting capabilities.
12. New modular API provides more
flexibility and easier extension to
allow for countless autocomplete
patterns.
Photo: http://www.flickr.com/photos/grdloizaga/817443503/
14. Synthetic event; fires when an
input field’s value changes.
AutoComplete modules
autocomplete-base
event-valuechange
autocomplete-
filters
autocomplete-
highlighters
autocomplete-list
Core logic and API. No UI,
minimal dependencies.
Optional prepackaged result
filters and highlighters.
Traditional list widget, just like
Mom used to make.
29. // Plugin-style.
YUI().use('autocomplete', function (Y) {
Y.one('#my-input').plug(Y.Plugin.AutoComplete, {
source: 'select * from search.suggest where query="{query}"'
});
});
Basic usage (YQL result source)
30. // Plugin-style.
YUI().use('autocomplete', function (Y) {
Y.one('#my-input').plug(Y.Plugin.AutoComplete, {
source: 'select * from search.suggest where query="{query}"'
});
});
Basic usage (YQL result source)
31. // Plugin-style.
YUI().use('autocomplete', function (Y) {
Y.one('#my-input').plug(Y.Plugin.AutoComplete, {
source: 'select * from search.suggest where query="{query}"'
});
});
Basic usage (YQL result source)
32. // Plugin-style.
YUI().use('autocomplete', function (Y) {
Y.one('#my-input').plug(Y.Plugin.AutoComplete, {
source: 'select * from search.suggest where query="{query}"'
});
});
Basic usage (YQL result source)
33. // Plugin-style.
YUI().use('autocomplete', function (Y) {
Y.one('#my-input').plug(Y.Plugin.AutoComplete, {
source: 'select * from search.suggest where query="{query}"'
});
});
Basic usage (YQL result source)
34. // Plugin-style.
YUI().use('autocomplete', function (Y) {
Y.one('#my-input').plug(Y.Plugin.AutoComplete, {
source: 'select * from search.suggest where query="{query}"'
});
});
Basic usage (YQL result source)
35. // Plugin-style.
YUI().use('autocomplete', function (Y) {
Y.one('#my-input').plug(Y.Plugin.AutoComplete, {
source: 'select * from search.suggest where query="{query}"'
});
});
Basic usage (YQL result source)
// Widget-style.
YUI().use('autocomplete', function (Y) {
var ac = new Y.AutoComplete({
inputNode: '#my-input',
render : true,
source : 'select * from search.suggest where query="{query}"'
});
});
36. // Plugin-style.
YUI().use('autocomplete', function (Y) {
Y.one('#my-input').plug(Y.Plugin.AutoComplete, {
source: 'select * from search.suggest where query="{query}"'
});
});
Basic usage (YQL result source)
// Widget-style.
YUI().use('autocomplete', function (Y) {
var ac = new Y.AutoComplete({
inputNode: '#my-input',
render : true,
source : 'select * from search.suggest where query="{query}"'
});
});
37. // Plugin-style.
YUI().use('autocomplete', function (Y) {
Y.one('#my-input').plug(Y.Plugin.AutoComplete, {
source: 'select * from search.suggest where query="{query}"'
});
});
Basic usage (YQL result source)
// Widget-style.
YUI().use('autocomplete', function (Y) {
var ac = new Y.AutoComplete({
inputNode: '#my-input',
render : true,
source : 'select * from search.suggest where query="{query}"'
});
});
38. // Plugin-style.
YUI().use('autocomplete', function (Y) {
Y.one('#my-input').plug(Y.Plugin.AutoComplete, {
source: 'select * from search.suggest where query="{query}"'
});
});
Basic usage (YQL result source)
// Widget-style.
YUI().use('autocomplete', function (Y) {
var ac = new Y.AutoComplete({
inputNode: '#my-input',
render : true,
source : 'select * from search.suggest where query="{query}"'
});
});
61. Prepackaged Filters &
Highlighters
• All are case-insensitive by default, but case-
sensitive versions are available (charMatchCase,
phraseMatchCase, etc.).
• Accent-folding versions are available as well
(charMatchFold, phraseMatchFold, etc.).
charMatch
phraseMatch
startsWith
wordMatch
66. function customFilter(query, results) {
return Y.Array.filter(results, function (result) {
result = result.toLowerCase();
// Only include results that start with an "a" and contain
// the query string.
return result.charAt(0) === 'a' &&
result.indexOf(query) !== -1;
});
}
Custom Filter
67. function customFilter(query, results) {
return Y.Array.filter(results, function (result) {
result = result.toLowerCase();
// Only include results that start with an "a" and contain
// the query string.
return result.charAt(0) === 'a' &&
result.indexOf(query) !== -1;
});
}
Custom Filter
68. function customFilter(query, results) {
return Y.Array.filter(results, function (result) {
result = result.toLowerCase();
// Only include results that start with an "a" and contain
// the query string.
return result.charAt(0) === 'a' &&
result.indexOf(query) !== -1;
});
}
Custom Filter
69. function customFilter(query, results) {
return Y.Array.filter(results, function (result) {
result = result.toLowerCase();
// Only include results that start with an "a" and contain
// the query string.
return result.charAt(0) === 'a' &&
result.indexOf(query) !== -1;
});
}
Custom Filter
70. function customFilter(query, results) {
return Y.Array.filter(results, function (result) {
result = result.toLowerCase();
// Only include results that start with an "a" and contain
// the query string.
return result.charAt(0) === 'a' &&
result.indexOf(query) !== -1;
});
}
Custom Filter
99. • Synthetic event. Fires when an input node’s value
changes due to user input.
• Handles keystrokes, pastes, and IME input.
• Creates a level playing field across browsers,
particularly with languages that have multi-stroke
characters.
event-valuechange
103. escape
• Static utility methods for escaping strings.
• Currently has methods for escaping HTML and
regex strings. More to come in future releases.
• Feel free to add your own escaping methods to
the Y.Escape namespace (and share on the
Gallery).
111. And now, a brief interlude wherein I
whine about having discovered, yesterday
afternoon, that “Unicode” is a
registered trademark of the
Unicode® Consortium.
116. ☃-accentfold
• Helpers for creating and working with accent-
folded strings.
• Accent folding converts “résumé” to
“resume” (etc.) for easier comparison.
• Should be a last resort. Only use it when better
tools aren’t available on the server. Don’t fold
strings you will display to the user.
• Will have an actual name before 3.3.0 final.
121. ☃-wordbreak
• Implements ☃ text segmentation guidelines for
word breaking.
• Handles edge cases like contractions, decimals,
thousands separators, Katakana, non-Latin
punctuation, etc.
• Not perfect, but much smarter than /b/.
• Will have an actual name before 3.3.0 final.
125. highlight
• Static utility methods for highlighting strings using
HTML.
• Supports phrase highlighting, start-of-string
highlighting, and word highlighting.
• Can use accent folded strings for comparisons
while highlighting the original, non-folded string.
• Smart enough not to highlight inside HTML
entities like ".
• Doesn’t infringe any trademarks.
137. Why <b>?
• HTML5 defines a new <mark> element for
denoting the relevance (as opposed to the
importance) of a span of text.
• It recommends using <mark> “to highlight a part
of quoted text that was not originally emphasized”.
• But, since <mark> isn’t yet supported in all
browsers, we use <b>, which HTML5 defines as a
last resort for highlighting text without implying
importance.
• You can customize the highlighting markup by
changing Y.Highlight._TEMPLATE.
More details: http://www.whatwg.org/specs/web-apps/current-work/multipage/text-
level-semantics.html#the-mark-element
139. What I just said
but with bullets
• Modular architecture makes it easy to
implement a range of autocomplete patterns.
• Dead simple JSONP and YQL support.
• Use prepackaged filters and highlighters with
word breaking and accent folding, or roll
your own.
• Create custom result formatters with just a
few lines of code.
• AutoComplete and friends are available now in
YUI 3.3.0pr1.
Photo: http://www.flickr.com/photos/tim_norris/2789759648/
148. Ryan Grove,YUI Team
@yaypie on Twitter
https://github.com/rgrove/
Slides: http://lanyrd.com/smym
Photo: http://www.flickr.com/photos/damaradeaella/2822846819/