~andreserl/maas/packaging_precise_rebase

« back to all changes in this revision

Viewing changes to debian/extras/jslibs/yui/autocomplete-filters/autocomplete-filters.js

  • Committer: Andres Rodriguez
  • Date: 2013-03-20 18:12:30 UTC
  • mfrom: (145.2.22 precise.sru)
  • Revision ID: andreserl@ubuntu.com-20130320181230-6l5guc0nhlv2z4p7
Re-base againts latest quantal released branch towards SRU

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
YUI 3.5.1 (build 22)
 
3
Copyright 2012 Yahoo! Inc. All rights reserved.
 
4
Licensed under the BSD License.
 
5
http://yuilibrary.com/license/
 
6
*/
 
7
YUI.add('autocomplete-filters', function(Y) {
 
8
 
 
9
/**
 
10
Provides pre-built result matching filters for AutoComplete.
 
11
 
 
12
@module autocomplete
 
13
@submodule autocomplete-filters
 
14
@class AutoCompleteFilters
 
15
@static
 
16
**/
 
17
 
 
18
var YArray     = Y.Array,
 
19
    YObject    = Y.Object,
 
20
    WordBreak  = Y.Text.WordBreak,
 
21
 
 
22
Filters = Y.mix(Y.namespace('AutoCompleteFilters'), {
 
23
    // -- Public Methods -------------------------------------------------------
 
24
 
 
25
    /**
 
26
    Returns an array of results that contain all of the characters in the query,
 
27
    in any order (not necessarily consecutive). Case-insensitive.
 
28
 
 
29
    @method charMatch
 
30
    @param {String} query Query to match
 
31
    @param {Array} results Results to filter
 
32
    @return {Array} Filtered 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
        if (!query) { return results; }
 
40
 
 
41
        var queryChars = YArray.unique((caseSensitive ? query :
 
42
                query.toLowerCase()).split(''));
 
43
 
 
44
        return YArray.filter(results, function (result) {
 
45
            result = result.text;
 
46
 
 
47
            if (!caseSensitive) {
 
48
                result = result.toLowerCase();
 
49
            }
 
50
 
 
51
            return YArray.every(queryChars, function (chr) {
 
52
                return result.indexOf(chr) !== -1;
 
53
            });
 
54
        });
 
55
    },
 
56
 
 
57
    /**
 
58
    Case-sensitive version of `charMatch()`.
 
59
 
 
60
    @method charMatchCase
 
61
    @param {String} query Query to match
 
62
    @param {Array} results Results to filter
 
63
    @return {Array} Filtered results
 
64
    @static
 
65
    **/
 
66
    charMatchCase: function (query, results) {
 
67
        return Filters.charMatch(query, results, true);
 
68
    },
 
69
 
 
70
    /**
 
71
    Returns an array of results that contain the complete query as a phrase.
 
72
    Case-insensitive.
 
73
 
 
74
    @method phraseMatch
 
75
    @param {String} query Query to match
 
76
    @param {Array} results Results to filter
 
77
    @return {Array} Filtered results
 
78
    @static
 
79
    **/
 
80
    phraseMatch: function (query, results, caseSensitive) {
 
81
        // The caseSensitive parameter is only intended for use by
 
82
        // phraseMatchCase(). It's intentionally undocumented.
 
83
 
 
84
        if (!query) { return results; }
 
85
 
 
86
        if (!caseSensitive) {
 
87
            query = query.toLowerCase();
 
88
        }
 
89
 
 
90
        return YArray.filter(results, function (result) {
 
91
            return (caseSensitive ? result.text : result.text.toLowerCase()).indexOf(query) !== -1;
 
92
        });
 
93
    },
 
94
 
 
95
    /**
 
96
    Case-sensitive version of `phraseMatch()`.
 
97
 
 
98
    @method phraseMatchCase
 
99
    @param {String} query Query to match
 
100
    @param {Array} results Results to filter
 
101
    @return {Array} Filtered results
 
102
    @static
 
103
    **/
 
104
    phraseMatchCase: function (query, results) {
 
105
        return Filters.phraseMatch(query, results, true);
 
106
    },
 
107
 
 
108
    /**
 
109
    Returns an array of results that start with the complete query as a phrase.
 
110
    Case-insensitive.
 
111
 
 
112
    @method startsWith
 
113
    @param {String} query Query to match
 
114
    @param {Array} results Results to filter
 
115
    @return {Array} Filtered results
 
116
    @static
 
117
    **/
 
118
    startsWith: function (query, results, caseSensitive) {
 
119
        // The caseSensitive parameter is only intended for use by
 
120
        // startsWithCase(). It's intentionally undocumented.
 
121
 
 
122
        if (!query) { return results; }
 
123
 
 
124
        if (!caseSensitive) {
 
125
            query = query.toLowerCase();
 
126
        }
 
127
 
 
128
        return YArray.filter(results, function (result) {
 
129
            return (caseSensitive ? result.text : result.text.toLowerCase()).indexOf(query) === 0;
 
130
        });
 
131
    },
 
132
 
 
133
    /**
 
134
    Case-sensitive version of `startsWith()`.
 
135
 
 
136
    @method startsWithCase
 
137
    @param {String} query Query to match
 
138
    @param {Array} results Results to filter
 
139
    @return {Array} Filtered results
 
140
    @static
 
141
    **/
 
142
    startsWithCase: function (query, results) {
 
143
        return Filters.startsWith(query, results, true);
 
144
    },
 
145
 
 
146
    /**
 
147
    Returns an array of results in which all the words of the query match either
 
148
    whole words or parts of words in the result. Non-word characters like
 
149
    whitespace and certain punctuation are ignored. Case-insensitive.
 
150
 
 
151
    This is basically a combination of `wordMatch()` (by ignoring whitespace and
 
152
    word order) and `phraseMatch()` (by allowing partial matching instead of
 
153
    requiring the entire word to match).
 
154
 
 
155
    Example use case: Trying to find personal names independently of name order
 
156
    (Western or Eastern order) and supporting immediate feedback by allowing
 
157
    partial occurences. So queries like "J. Doe", "Doe, John", and "J. D." would
 
158
    all match "John Doe".
 
159
 
 
160
    @method subWordMatch
 
161
    @param {String} query Query to match
 
162
    @param {Array} results Results to filter
 
163
    @return {Array} Filtered results
 
164
    @static
 
165
    **/
 
166
    subWordMatch: function (query, results, caseSensitive) {
 
167
        // The caseSensitive parameter is only intended for use by
 
168
        // subWordMatchCase(). It's intentionally undocumented.
 
169
 
 
170
        if (!query) { return results; }
 
171
 
 
172
        var queryWords = WordBreak.getUniqueWords(query, {
 
173
            ignoreCase: !caseSensitive
 
174
        });
 
175
 
 
176
        return YArray.filter(results, function (result) {
 
177
            var resultText = caseSensitive ? result.text :
 
178
                    result.text.toLowerCase();
 
179
 
 
180
            return YArray.every(queryWords, function (queryWord) {
 
181
                return resultText.indexOf(queryWord) !== -1;
 
182
            });
 
183
        });
 
184
    },
 
185
 
 
186
    /**
 
187
    Case-sensitive version of `subWordMatch()`.
 
188
 
 
189
    @method subWordMatchCase
 
190
    @param {String} query Query to match
 
191
    @param {Array} results Results to filter
 
192
    @return {Array} Filtered results
 
193
    @static
 
194
    **/
 
195
    subWordMatchCase: function (query, results) {
 
196
        return Filters.subWordMatch(query, results, true);
 
197
    },
 
198
 
 
199
    /**
 
200
    Returns an array of results that contain all of the words in the query, in
 
201
    any order. Non-word characters like whitespace and certain punctuation are
 
202
    ignored. Case-insensitive.
 
203
 
 
204
    @method wordMatch
 
205
    @param {String} query Query to match
 
206
    @param {Array} results Results to filter
 
207
    @return {Array} Filtered results
 
208
    @static
 
209
    **/
 
210
    wordMatch: function (query, results, caseSensitive) {
 
211
        // The caseSensitive parameter is only intended for use by
 
212
        // wordMatchCase(). It's intentionally undocumented.
 
213
 
 
214
        if (!query) { return results; }
 
215
 
 
216
        var options    = {ignoreCase: !caseSensitive},
 
217
            queryWords = WordBreak.getUniqueWords(query, options);
 
218
 
 
219
        return YArray.filter(results, function (result) {
 
220
            // Convert resultWords array to a hash for fast lookup.
 
221
            var resultWords = YArray.hash(WordBreak.getUniqueWords(result.text,
 
222
                                options));
 
223
 
 
224
            return YArray.every(queryWords, function (word) {
 
225
                return YObject.owns(resultWords, word);
 
226
            });
 
227
        });
 
228
    },
 
229
 
 
230
    /**
 
231
    Case-sensitive version of `wordMatch()`.
 
232
 
 
233
    @method wordMatchCase
 
234
    @param {String} query Query to match
 
235
    @param {Array} results Results to filter
 
236
    @return {Array} Filtered results
 
237
    @static
 
238
    **/
 
239
    wordMatchCase: function (query, results) {
 
240
        return Filters.wordMatch(query, results, true);
 
241
    }
 
242
});
 
243
 
 
244
 
 
245
}, '3.5.1' ,{requires:['array-extras', 'text-wordbreak']});