2 Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.com/yui/license.html
8 YUI.add('autocomplete-highlighters', function(Y) {
11 * Provides pre-built result highlighters for AutoComplete.
13 * @module autocomplete
14 * @submodule autocomplete-highlighters
15 * @class AutoCompleteHighlighters
20 Highlight = Y.Highlight,
22 Highlighters = Y.mix(Y.namespace('AutoCompleteHighlighters'), {
23 // -- Public Methods -------------------------------------------------------
26 * Highlights any individual query character that occurs anywhere in a
27 * result. Case-insensitive.
30 * @param {String} query Query to match
31 * @param {Array} results Results to highlight
32 * @return {Array} Highlighted results
35 charMatch: function (query, results, caseSensitive) {
36 // The caseSensitive parameter is only intended for use by
37 // charMatchCase(). It's intentionally undocumented.
39 var queryChars = YArray.unique((caseSensitive ? query :
40 query.toLowerCase()).split(''));
42 return YArray.map(results, function (result) {
43 return Highlight.all(result.text, queryChars, {
44 caseSensitive: caseSensitive
50 * Case-sensitive version of <code>charMatch()</code>.
52 * @method charMatchCase
53 * @param {String} query Query to match
54 * @param {Array} results Results to highlight
55 * @return {Array} Highlighted results
58 charMatchCase: function (query, results) {
59 return Highlighters.charMatch(query, results, true);
63 * Highlights the complete query as a phrase anywhere within a result.
67 * @param {String} query Query to match
68 * @param {Array} results Results to highlight
69 * @return {Array} Highlighted results
72 phraseMatch: function (query, results, caseSensitive) {
73 // The caseSensitive parameter is only intended for use by
74 // phraseMatchCase(). It's intentionally undocumented.
76 return YArray.map(results, function (result) {
77 return Highlight.all(result.text, [query], {
78 caseSensitive: caseSensitive
84 * Case-sensitive version of <code>phraseMatch()</code>.
86 * @method phraseMatchCase
87 * @param {String} query Query to match
88 * @param {Array} results Results to highlight
89 * @return {Array} Highlighted results
92 phraseMatchCase: function (query, results) {
93 return Highlighters.phraseMatch(query, results, true);
97 * Highlights the complete query as a phrase at the beginning of a result.
101 * @param {String} query Query to match
102 * @param {Array} results Results to highlight
103 * @return {Array} Highlighted results
106 startsWith: function (query, results, caseSensitive) {
107 // The caseSensitive parameter is only intended for use by
108 // startsWithCase(). It's intentionally undocumented.
110 return YArray.map(results, function (result) {
111 return Highlight.all(result.text, [query], {
112 caseSensitive: caseSensitive,
119 * Case-sensitive version of <code>startsWith()</code>.
121 * @method startsWithCase
122 * @param {String} query Query to match
123 * @param {Array} results Results to highlight
124 * @return {Array} Highlighted results
127 startsWithCase: function (query, results) {
128 return Highlighters.startsWith(query, results, true);
132 * Highlights individual words in results that are also in the query.
133 * Non-word characters like punctuation are ignored. Case-insensitive.
136 * @param {String} query Query to match
137 * @param {Array} results Results to filter
138 * @return {Array} Filtered results
141 wordMatch: function (query, results, caseSensitive) {
142 // The caseSensitive parameter is only intended for use by
143 // wordMatchCase(). It's intentionally undocumented.
145 return YArray.map(results, function (result) {
146 return Highlight.words(result.text, query, {
147 caseSensitive: caseSensitive
153 * Case-sensitive version of <code>wordMatch()</code>.
155 * @method wordMatchCase
156 * @param {String} query Query to match
157 * @param {Array} results Results to filter
158 * @return {Array} Filtered results
161 wordMatchCase: function (query, results) {
162 return Highlighters.wordMatch(query, results, true);
167 }, '3.3.0' ,{requires:['array-extras', 'highlight-base']});