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