|
1 /* |
|
2 YUI 3.10.3 (build 2fb5187) |
|
3 Copyright 2013 Yahoo! Inc. All rights reserved. |
|
4 Licensed under the BSD License. |
|
5 http://yuilibrary.com/license/ |
|
6 */ |
|
7 |
|
8 YUI.add('autocomplete-highlighters', function (Y, NAME) { |
|
9 |
|
10 /** |
|
11 Provides pre-built result highlighters for AutoComplete. |
|
12 |
|
13 @module autocomplete |
|
14 @submodule autocomplete-highlighters |
|
15 @class AutoCompleteHighlighters |
|
16 @static |
|
17 **/ |
|
18 |
|
19 var YArray = Y.Array, |
|
20 Highlight = Y.Highlight, |
|
21 |
|
22 Highlighters = Y.mix(Y.namespace('AutoCompleteHighlighters'), { |
|
23 // -- Public Methods ------------------------------------------------------- |
|
24 |
|
25 /** |
|
26 Highlights any individual query character that occurs anywhere in a result. |
|
27 Case-insensitive. |
|
28 |
|
29 @method charMatch |
|
30 @param {String} query Query to match |
|
31 @param {Array} results Results to highlight |
|
32 @return {Array} Highlighted results |
|
33 @static |
|
34 **/ |
|
35 charMatch: function (query, results, caseSensitive) { |
|
36 // The caseSensitive parameter is only intended for use by |
|
37 // charMatchCase(). It's intentionally undocumented. |
|
38 |
|
39 var queryChars = YArray.unique((caseSensitive ? query : |
|
40 query.toLowerCase()).split('')); |
|
41 |
|
42 return YArray.map(results, function (result) { |
|
43 return Highlight.all(result.text, queryChars, { |
|
44 caseSensitive: caseSensitive |
|
45 }); |
|
46 }); |
|
47 }, |
|
48 |
|
49 /** |
|
50 Case-sensitive version of `charMatch()`. |
|
51 |
|
52 @method charMatchCase |
|
53 @param {String} query Query to match |
|
54 @param {Array} results Results to highlight |
|
55 @return {Array} Highlighted results |
|
56 @static |
|
57 **/ |
|
58 charMatchCase: function (query, results) { |
|
59 return Highlighters.charMatch(query, results, true); |
|
60 }, |
|
61 |
|
62 /** |
|
63 Highlights the complete query as a phrase anywhere within a result. Case- |
|
64 insensitive. |
|
65 |
|
66 @method phraseMatch |
|
67 @param {String} query Query to match |
|
68 @param {Array} results Results to highlight |
|
69 @return {Array} Highlighted results |
|
70 @static |
|
71 **/ |
|
72 phraseMatch: function (query, results, caseSensitive) { |
|
73 // The caseSensitive parameter is only intended for use by |
|
74 // phraseMatchCase(). It's intentionally undocumented. |
|
75 |
|
76 return YArray.map(results, function (result) { |
|
77 return Highlight.all(result.text, [query], { |
|
78 caseSensitive: caseSensitive |
|
79 }); |
|
80 }); |
|
81 }, |
|
82 |
|
83 /** |
|
84 Case-sensitive version of `phraseMatch()`. |
|
85 |
|
86 @method phraseMatchCase |
|
87 @param {String} query Query to match |
|
88 @param {Array} results Results to highlight |
|
89 @return {Array} Highlighted results |
|
90 @static |
|
91 **/ |
|
92 phraseMatchCase: function (query, results) { |
|
93 return Highlighters.phraseMatch(query, results, true); |
|
94 }, |
|
95 |
|
96 /** |
|
97 Highlights the complete query as a phrase at the beginning of a result. |
|
98 Case-insensitive. |
|
99 |
|
100 @method startsWith |
|
101 @param {String} query Query to match |
|
102 @param {Array} results Results to highlight |
|
103 @return {Array} Highlighted results |
|
104 @static |
|
105 **/ |
|
106 startsWith: function (query, results, caseSensitive) { |
|
107 // The caseSensitive parameter is only intended for use by |
|
108 // startsWithCase(). It's intentionally undocumented. |
|
109 |
|
110 return YArray.map(results, function (result) { |
|
111 return Highlight.all(result.text, [query], { |
|
112 caseSensitive: caseSensitive, |
|
113 startsWith : true |
|
114 }); |
|
115 }); |
|
116 }, |
|
117 |
|
118 /** |
|
119 Case-sensitive version of `startsWith()`. |
|
120 |
|
121 @method startsWithCase |
|
122 @param {String} query Query to match |
|
123 @param {Array} results Results to highlight |
|
124 @return {Array} Highlighted results |
|
125 @static |
|
126 **/ |
|
127 startsWithCase: function (query, results) { |
|
128 return Highlighters.startsWith(query, results, true); |
|
129 }, |
|
130 |
|
131 /** |
|
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. |
|
135 |
|
136 @method subWordMatch |
|
137 @param {String} query Query to match |
|
138 @param {Array} results Results to highlight |
|
139 @return {Array} Highlighted results |
|
140 @static |
|
141 **/ |
|
142 subWordMatch: function (query, results, caseSensitive) { |
|
143 // The caseSensitive parameter is only intended for use by |
|
144 // subWordMatchCase(). It's intentionally undocumented. |
|
145 |
|
146 var queryWords = Y.Text.WordBreak.getUniqueWords(query, { |
|
147 ignoreCase: !caseSensitive |
|
148 }); |
|
149 |
|
150 return YArray.map(results, function (result) { |
|
151 return Highlight.all(result.text, queryWords, { |
|
152 caseSensitive: caseSensitive |
|
153 }); |
|
154 }); |
|
155 }, |
|
156 |
|
157 /** |
|
158 Case-sensitive version of `subWordMatch()`. |
|
159 |
|
160 @method subWordMatchCase |
|
161 @param {String} query Query to match |
|
162 @param {Array} results Results to highlight |
|
163 @return {Array} Highlighted results |
|
164 @static |
|
165 **/ |
|
166 subWordMatchCase: function (query, results) { |
|
167 return Highlighters.subWordMatch(query, results, true); |
|
168 }, |
|
169 |
|
170 /** |
|
171 Highlights individual words in results that are also in the query. Non-word |
|
172 characters like punctuation are ignored. Case-insensitive. |
|
173 |
|
174 @method wordMatch |
|
175 @param {String} query Query to match |
|
176 @param {Array} results Results to highlight |
|
177 @return {Array} Highlighted results |
|
178 @static |
|
179 **/ |
|
180 wordMatch: function (query, results, caseSensitive) { |
|
181 // The caseSensitive parameter is only intended for use by |
|
182 // wordMatchCase(). It's intentionally undocumented. |
|
183 |
|
184 return YArray.map(results, function (result) { |
|
185 return Highlight.words(result.text, query, { |
|
186 caseSensitive: caseSensitive |
|
187 }); |
|
188 }); |
|
189 }, |
|
190 |
|
191 /** |
|
192 Case-sensitive version of `wordMatch()`. |
|
193 |
|
194 @method wordMatchCase |
|
195 @param {String} query Query to match |
|
196 @param {Array} results Results to highlight |
|
197 @return {Array} Highlighted results |
|
198 @static |
|
199 **/ |
|
200 wordMatchCase: function (query, results) { |
|
201 return Highlighters.wordMatch(query, results, true); |
|
202 } |
|
203 }); |
|
204 |
|
205 |
|
206 }, '3.10.3', {"requires": ["array-extras", "highlight-base"]}); |