2 YUI 3.13.0 (build 508226d)
3 Copyright 2013 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
8 YUI.add('autocomplete-highlighters', function (Y, NAME) {
11 Provides pre-built result highlighters for 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 result.
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 `charMatch()`.
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. Case-
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 `phraseMatch()`.
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 `startsWith()`.
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 portions of results in which words from the query match either
133 whole words or parts of words in the result. Non-word characters like
134 whitespace and certain punctuation are ignored. Case-insensitive.
137 @param {String} query Query to match
138 @param {Array} results Results to highlight
139 @return {Array} Highlighted results
142 subWordMatch: function (query, results, caseSensitive) {
143 // The caseSensitive parameter is only intended for use by
144 // subWordMatchCase(). It's intentionally undocumented.
146 var queryWords = Y.Text.WordBreak.getUniqueWords(query, {
147 ignoreCase: !caseSensitive
150 return YArray.map(results, function (result) {
151 return Highlight.all(result.text, queryWords, {
152 caseSensitive: caseSensitive
158 Case-sensitive version of `subWordMatch()`.
160 @method subWordMatchCase
161 @param {String} query Query to match
162 @param {Array} results Results to highlight
163 @return {Array} Highlighted results
166 subWordMatchCase: function (query, results) {
167 return Highlighters.subWordMatch(query, results, true);
171 Highlights individual words in results that are also in the query. Non-word
172 characters like punctuation are ignored. Case-insensitive.
175 @param {String} query Query to match
176 @param {Array} results Results to highlight
177 @return {Array} Highlighted results
180 wordMatch: function (query, results, caseSensitive) {
181 // The caseSensitive parameter is only intended for use by
182 // wordMatchCase(). It's intentionally undocumented.
184 return YArray.map(results, function (result) {
185 return Highlight.words(result.text, query, {
186 caseSensitive: caseSensitive
192 Case-sensitive version of `wordMatch()`.
194 @method wordMatchCase
195 @param {String} query Query to match
196 @param {Array} results Results to highlight
197 @return {Array} Highlighted results
200 wordMatchCase: function (query, results) {
201 return Highlighters.wordMatch(query, results, true);
206 }, '3.13.0', {"requires": ["array-extras", "highlight-base"]});