1
0
mirror of synced 2025-02-09 16:49:24 +03:00

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.
This commit is contained in:
Kevin Brown 2014-10-16 18:28:45 -04:00
parent 6d5b0a6c59
commit 4f8fb28d93

47
src/js/select2/compat/matcher.js vendored Normal file
View File

@ -0,0 +1,47 @@
define([
], function () {
function OldMatcher (decorated, $element, options) {
decorated.call(this, $element, options);
this.matcher = options.get('matcher');
}
OldMatcher.prototype.matches = function (decorated, params, data) {
// If there is no custom matcher, call the original matcher function
if (this.matcher == null) {
return decorated.call(params, data);
}
var match = $.extend(true, {}, data);
if (data.children) {
for (var c = data.children.length - 1; c >= 0; c--) {
var child = data.children[c];
// Check if the child object matches
// The old matcher returned a boolean true or false
var doesMatch = this.matcher(params.term, child.text);
// If the child didn't match, pop it off
if (!doesMatch) {
match.children.splice(c, 1);
}
}
if (match.children.length > 0) {
return match;
}
}
if ($.trim(params.term) === '') {
return match;
}
if (this.matcher(params.term, data.text)) {
return match;
}
return null;
};
});