浏览代码

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.
Kevin Brown 10 年之前
父节点
当前提交
4f8fb28d93
共有 1 个文件被更改,包括 47 次插入0 次删除
  1. 47 0
      src/js/select2/compat/matcher.js

+ 47 - 0
src/js/select2/compat/matcher.js

@@ -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;
+  };
+});