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 2020/01/28 08:22:11 UTC

[myfaces-tobago] branch TOBAGO-1999_Select2 updated: Tobago-1999: suggest

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


The following commit(s) were added to refs/heads/TOBAGO-1999_Select2 by this push:
     new 3290d08  Tobago-1999: suggest
3290d08 is described below

commit 3290d084b23a91efa04f0aafd81cc3c7689fa7a1
Author: Volker Weber <v....@inexso.de>
AuthorDate: Wed Jan 22 10:55:26 2020 +0100

    Tobago-1999: suggest
---
 .../content/25-select/00-select2/select2.xhtml     |   3 +
 .../standard/standard/script/tobago-select2.js     | 121 ++++++++++++++++++---
 2 files changed, 108 insertions(+), 16 deletions(-)

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 8ba3d59..d5febd3 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
@@ -203,10 +203,13 @@
         <tc:label value="Suggest countries" for="many_7"/>
         <tc:selectManyBox id="many_7"
                           placeholder="Select countries"
+                          allowCustom="true"
+                          tokenSeparators=","
                           value="#{select2Controller.many7Countries}">
 
           <tc:selectItems value="#{select2Controller.many7CountryItems}"/>
           <tc:suggest suggestMethod="#{countries.prefixed}" minimumCharacters="2" />
+          <tc:dataAttribute name="tobago-select2-extend" value='{"resultsAdapter": "suppressMessages"}'/>
 
 
         </tc:selectManyBox>
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 63bb0cb..86b7c95 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
@@ -25,10 +25,34 @@ Tobago.Select2 = {
         .each( function () {
           var element = jQuery(this);
 
-          var select2Options = jQuery.extend({}, element.data("tobago-select2"), Tobago.Select2.getExtensions(element));
+          var select2Options = jQuery.extend({}, element.data("tobago-select2"));
+
+          var suggestId = element.data("tobago-suggest-id");
+          if (typeof suggestId === "string") {
+            var suggest = jQuery(Tobago.Utils.escapeClientId(suggestId));
+            if (suggest.length) {
+              select2Options.ajax = {
+                suggestId: suggestId,
+                url: "http://localhost/just/a/dummy/url",
+                transport: Tobago.Select2.transport
+              };
+              var delay = suggest.data("tobago-suggest-delay");
+              if (delay) {
+                select2Options.ajax.delay = delay;
+              }
+            } else {
+              console.error("Suggest2 ajax problem: could not find element with id " + suggestId);
+              suggestId = undefined;
+            }
+          }
 
           if (element.hasClass("tobago-selectManyBox")) {
             select2Options.containerCss = {height: element.data("tobago-style").height};
+            if (suggestId) {
+              select2Options.dropdownCssClass = undefined; // it makes no sense to hide the ajax response
+              select2Options.createTag = Tobago.Select2.createResponseTag;
+              select2Options.templateResult = Tobago.Select2.suggestTemplateResult;
+            }
           }
           console.info("Select2.init" + element.attr("id") + " with data: " // @DEV_ONLY
               + JSON.stringify(select2Options));                            // @DEV_ONLY
@@ -41,20 +65,6 @@ Tobago.Select2 = {
             select2Options.tokenizer = eval(select2Options.tokenizer)
           }
 
-          var suggestId = element.data("tobago-suggest-id");
-          if (typeof suggestId === "string") {
-            select2Options.ajax = {
-              suggestId: suggestId,
-              url: "http://localhost/just/a/dummy/url",
-              transport: Tobago.Select2.transport
-            };
-            var suggest = jQuery(Tobago.Utils.escapeClientId(suggestId));
-            var delay = suggest.data("tobago-suggest-delay");
-            if (delay) {
-              select2Options.ajax.delay = delay;
-            }
-          }
-
           var commands = element.data("tobago-commands");
 
           if (commands) {
@@ -85,12 +95,35 @@ Tobago.Select2 = {
             }
 
           }
+          select2Options = jQuery.extend(select2Options, Tobago.Select2.getExtensions(element));
           console.info("Select2 select2Options " + element.attr("id") + " with data: " // @DEV_ONLY
               + JSON.stringify(select2Options));                                       // @DEV_ONLY
           element.select2(select2Options);
         });
   },
 
+  createResponseTag: function (params) {
+
+    var term = jQuery.trim(params.term);
+
+    if (term === '') {
+      return null;
+    }
+
+    return {
+      id: term,
+      text: term,
+      hide: "query" === params._type
+    }
+  },
+
+  suggestTemplateResult: function (result) {
+    if (result.loading || result.hide) {
+      return null;
+    }
+    return result.text;
+  },
+
   transport: function (params, success, failure) {
 
     if (params.data && params.data.q !== undefined) {
@@ -106,6 +139,12 @@ Tobago.Select2 = {
       afterDoUpdateSuccess: function (requestOptions) {
         var data = jQuery(Tobago.Utils.escapeClientId(requestOptions.suggestId)).data("tobago-suggest-response-data");
         console.debug("SELECT2 tobago SUCCESS"); // @DEV_ONLY
+        var currentValues = jQuery(Tobago.Utils.escapeClientId(params.suggestId))
+            .next().children('.tobago-selectManyBox').val();
+        if (currentValues !== undefined) {
+          // remove already selected items in select many box
+          data.results = Tobago.Select2.removeSelected(data.results, currentValues);
+        }
         success(data);
       },
       afterDoUpdateError: function () {
@@ -122,13 +161,43 @@ Tobago.Select2 = {
     }
   },
 
+  removeSelected: function (results, currentValue) {
+    if (currentValue.length === 0) {
+      return results;
+    }
+    var newData = [];
+    for (var i = 0; i < results.length; i++) {
+      var item = results[i];
+      if (!currentValue.includes(item.id)) {
+        newData.push(item);
+      }
+    }
+    return newData;
+  },
+
   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]];
+          var extensionName = extend[extName];
+          var value = Tobago.Select2.registry[extensionName];
+          if (value) {
+            extend[extName] = value;
+          } else {
+            try {
+              value = jQuery(extensionName);
+            } catch (e) {}
+            if (!(value && value.length) && extend[extName].charAt(0) !== '#') {
+              try {
+                value = jQuery(Tobago.Utils.escapeClientId(extensionName));
+              } catch (e) {}
+            }
+            if (value && value.length) {
+              extend[extName] = value;
+            }
+          }
         }
       }
     }
@@ -189,5 +258,25 @@ Tobago.Select2.register('caseSensitiveMatcher', function (params, data) {
   return null;
 });
 
+Tobago.Select2.register('suppressMessages', (function() {
+
+  var suppressMessages = (function () {
+
+    function SuppressMessage (decorated, $element, options, dataAdapter) {
+      decorated.call(this, $element, options, dataAdapter);
+    }
+
+    SuppressMessage.prototype.displayMessage = function(decorated, params) {
+    };
+
+    return SuppressMessage;
+  })();
+
+  var Utils = jQuery.fn.select2.amd.require('select2/utils');
+  var resultAdapter = jQuery.fn.select2.amd.require('select2/results');
+  return Utils.Decorate(resultAdapter, suppressMessages);
+
+})());
+
 Tobago.registerListener(Tobago.Select2.init, Tobago.Phase.DOCUMENT_READY);
 Tobago.registerListener(Tobago.Select2.init, Tobago.Phase.AFTER_UPDATE);