Skip to content

Commit 4f8fb28

Browse files
committed
Add backwards compatibility with the old matcher
This adds a module for the old matcher, so users can decorate a data adapter with the module to get the old matcher functionality. The third parameter to the old matcher, the full element, will always be the full object now. This does not match the old functionality, where the third parameter for a `<select>` element would be the `<option>` element.
1 parent 6d5b0a6 commit 4f8fb28

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

src/js/select2/compat/matcher.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
define([
2+
3+
], function () {
4+
function OldMatcher (decorated, $element, options) {
5+
decorated.call(this, $element, options);
6+
7+
this.matcher = options.get('matcher');
8+
}
9+
10+
OldMatcher.prototype.matches = function (decorated, params, data) {
11+
// If there is no custom matcher, call the original matcher function
12+
if (this.matcher == null) {
13+
return decorated.call(params, data);
14+
}
15+
16+
var match = $.extend(true, {}, data);
17+
18+
if (data.children) {
19+
for (var c = data.children.length - 1; c >= 0; c--) {
20+
var child = data.children[c];
21+
22+
// Check if the child object matches
23+
// The old matcher returned a boolean true or false
24+
var doesMatch = this.matcher(params.term, child.text);
25+
26+
// If the child didn't match, pop it off
27+
if (!doesMatch) {
28+
match.children.splice(c, 1);
29+
}
30+
}
31+
32+
if (match.children.length > 0) {
33+
return match;
34+
}
35+
}
36+
37+
if ($.trim(params.term) === '') {
38+
return match;
39+
}
40+
41+
if (this.matcher(params.term, data.text)) {
42+
return match;
43+
}
44+
45+
return null;
46+
};
47+
});

0 commit comments

Comments
 (0)