You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by we...@apache.org on 2019/11/29 15:46:10 UTC

[myfaces-tobago] 19/19: Tobago-1999: caseSensitiveMatcher

This is an automated email from the ASF dual-hosted git repository.

weber pushed a commit to branch TOBAGO-1999_Select2
in repository https://gitbox.apache.org/repos/asf/myfaces-tobago.git

commit d4b26cd31306bd7f34c537a269179af44038f7e8
Author: Volker Weber <v....@inexso.de>
AuthorDate: Fri Nov 29 16:03:24 2019 +0100

    Tobago-1999: caseSensitiveMatcher
---
 .../tobago/example/demo/Select2Controller.java     |  7 ++
 .../content/25-select/00-select2/select2.xhtml     |  1 +
 .../standard/standard/script/tobago-select2.js     | 79 ++++++++++++++++++++--
 3 files changed, 81 insertions(+), 6 deletions(-)

diff --git a/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/Select2Controller.java b/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/Select2Controller.java
index db6e987..1e81080 100644
--- a/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/Select2Controller.java
+++ b/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/Select2Controller.java
@@ -1,5 +1,7 @@
 package org.apache.myfaces.tobago.example.demo;
 
+import org.apache.myfaces.tobago.renderkit.html.JsonUtils;
+
 import javax.enterprise.context.SessionScoped;
 import javax.inject.Named;
 import java.io.Serializable;
@@ -17,4 +19,9 @@ public class Select2Controller implements Serializable {
   public void setOne2(String one2) {
     this.one2 = one2;
   }
+
+  public String getCaseSensitiveMatcher() {
+    return "{\"matcher\": \"Tobago.Select2.caseSensitiveMatcher\"}";
+//    JsonUtils.encode()
+  }
 }
diff --git a/tobago-example/tobago-example-demo/src/main/webapp/content/25-select/00-select2/select2.xhtml b/tobago-example/tobago-example-demo/src/main/webapp/content/25-select/00-select2/select2.xhtml
index d5c36cd..b040dd8 100644
--- a/tobago-example/tobago-example-demo/src/main/webapp/content/25-select/00-select2/select2.xhtml
+++ b/tobago-example/tobago-example-demo/src/main/webapp/content/25-select/00-select2/select2.xhtml
@@ -153,6 +153,7 @@
                           tokenSeparators=", -"
                           allowCustom="true"
                           hideDropdown="true">
+          <tc:dataAttribute name="tobago-select2-extend" value='{"matcher" : "caseSensitiveMatcher"}'/>
         </tc:selectManyBox>
 
       </tc:panel>
diff --git a/tobago-theme/tobago-theme-standard/src/main/resources/org/apache/myfaces/tobago/renderkit/html/standard/standard/script/tobago-select2.js b/tobago-theme/tobago-theme-standard/src/main/resources/org/apache/myfaces/tobago/renderkit/html/standard/standard/script/tobago-select2.js
index f4222af..f9f4883 100644
--- a/tobago-theme/tobago-theme-standard/src/main/resources/org/apache/myfaces/tobago/renderkit/html/standard/standard/script/tobago-select2.js
+++ b/tobago-theme/tobago-theme-standard/src/main/resources/org/apache/myfaces/tobago/renderkit/html/standard/standard/script/tobago-select2.js
@@ -17,14 +17,15 @@
 
 Tobago.Select2 = {
 
+  registry: {},
+
   init: function (elements) {
     console.info("Tobago.Select2.init");                                    // @DEV_ONLY
     Tobago.Utils.selectWithJQuery(elements, "[data-tobago-select2]")
         .each( function () {
           var element = jQuery(this);
-          var options = element.data("tobago-select2");
-          var extend = element.data("tobago-select2-extend");
-          var select2Options = jQuery.extend({}, options, extend);
+
+          var select2Options = jQuery.extend({}, element.data("tobago-select2"), Tobago.Select2.getExtensions(element));
 
           if (element.hasClass("tobago-selectManyBox")) {
             select2Options.containerCss = {height: element.data("tobago-style").height};
@@ -43,7 +44,7 @@ Tobago.Select2 = {
 
           if (commands) {
             for (var name in commands) {
-              if (name.indexOf("select2:") === 0) {
+              if (commands.hasOwnProperty(name) && name.indexOf("select2:") === 0) {
                 var command = commands[name];
                 var actionId = command.action;
                 if (command.script) {
@@ -59,7 +60,7 @@ Tobago.Select2 = {
                     }
                   });
                 } else {
-                  var actionId = command.action;
+                  actionId = command.action;
                   element.on("select2:select", function () {
                     console.info("select2:select submitAction(" + actionId + ")");
                     Tobago.submitAction(this, actionId);
@@ -71,8 +72,74 @@ Tobago.Select2 = {
           }
           element.select2(select2Options);
         });
-  }
+  },
 
+  getExtensions: function (element) {
+    var extend = element.data("tobago-select2-extend");
+    if (extend !== undefined) {
+      for (var extName in extend) {
+        if (extend.hasOwnProperty(extName)
+            && typeof extend[extName] === "string") {
+          extend[extName] = Tobago.Select2.registry[extend[extName]];
+        }
+      }
+    }
+    return extend;
+  },
+
+  register: function (name, object) {
+    Tobago.Select2.registry[name] = object;
+  }
 };
+
+Tobago.Select2.register('caseSensitiveMatcher', function (params, data) {
+  // modified copy of original select2 matcher
+
+  // Always return the object if there is nothing to compare
+  if ($.trim(params.term) === '') {
+    return data;
+  }
+
+  // Do a recursive check for options with children
+  if (data.children && data.children.length > 0) {
+    // Clone the data object if there are children
+    // This is required as we modify the object to remove any non-matches
+    var match = $.extend(true, {}, data);
+
+    // Check each child of the option
+    for (var c = data.children.length - 1; c >= 0; c--) {
+      var child = data.children[c];
+
+      var matches = matcher(params, child);
+
+      // If there wasn't a match, remove the object in the array
+      if (matches == null) {
+        match.children.splice(c, 1);
+      }
+    }
+
+    // If any children matched, return the new object
+    if (match.children.length > 0) {
+      return match;
+    }
+
+    // If there were no matching children, check just the plain object
+    return matcher(params, match);
+  }
+
+  // var original = stripDiacritics(data.text).toUpperCase();
+  // var term = stripDiacritics(params.term).toUpperCase();
+  var original = data.text;
+  var term = params.term;
+
+  // Check if the text contains the term
+  if (original.indexOf(term) > -1) {
+    return data;
+  }
+
+  // If it doesn't contain the term, don't return anything
+  return null;
+});
+
 Tobago.registerListener(Tobago.Select2.init, Tobago.Phase.DOCUMENT_READY);
 Tobago.registerListener(Tobago.Select2.init, Tobago.Phase.AFTER_UPDATE);