]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/src_files/include/javascript/yui3/build/autocomplete/autocomplete-highlighters.js
Release 6.5.0
[Github/sugarcrm.git] / jssource / src_files / include / javascript / yui3 / build / autocomplete / autocomplete-highlighters.js
1 /*
2 Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.com/yui/license.html
5 version: 3.3.0
6 build: 3167
7 */
8 YUI.add('autocomplete-highlighters', function(Y) {
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
27      * result. 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 <code>charMatch()</code>.
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.
64      * Case-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 <code>phraseMatch()</code>.
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 <code>startsWith()</code>.
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 individual words in results that are also in the query.
133      * Non-word characters like punctuation are ignored. Case-insensitive.
134      *
135      * @method wordMatch
136      * @param {String} query Query to match
137      * @param {Array} results Results to filter
138      * @return {Array} Filtered results
139      * @static
140      */
141     wordMatch: function (query, results, caseSensitive) {
142         // The caseSensitive parameter is only intended for use by
143         // wordMatchCase(). It's intentionally undocumented.
144
145         return YArray.map(results, function (result) {
146             return Highlight.words(result.text, query, {
147                 caseSensitive: caseSensitive
148             });
149         });
150     },
151
152     /**
153      * Case-sensitive version of <code>wordMatch()</code>.
154      *
155      * @method wordMatchCase
156      * @param {String} query Query to match
157      * @param {Array} results Results to filter
158      * @return {Array} Filtered results
159      * @static
160      */
161     wordMatchCase: function (query, results) {
162         return Highlighters.wordMatch(query, results, true);
163     }
164 });
165
166
167 }, '3.3.0' ,{requires:['array-extras', 'highlight-base']});