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

svn commit: r1708762 - in /myfaces/tobago/branches/tobago-3.0.x: tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/overview/ tobago-example/tobago-example-demo/src/main/webapp/content/01-basic/ tobago-theme/tobago-...

Author: lofwyr
Date: Thu Oct 15 09:59:26 2015
New Revision: 1708762

URL: http://svn.apache.org/viewvc?rev=1708762&view=rev
Log:
TOBAGO-1475: Implement tc:suggest for Bootstrap
- implement the AJAX part

Modified:
    myfaces/tobago/branches/tobago-3.0.x/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/overview/OverviewController.java
    myfaces/tobago/branches/tobago-3.0.x/tobago-example/tobago-example-demo/src/main/webapp/content/01-basic/basic.xhtml
    myfaces/tobago/branches/tobago-3.0.x/tobago-theme/tobago-theme-standard/src/main/java/org/apache/myfaces/tobago/renderkit/html/standard/standard/tag/SuggestRenderer.java
    myfaces/tobago/branches/tobago-3.0.x/tobago-theme/tobago-theme-standard/src/main/resources/org/apache/myfaces/tobago/renderkit/html/standard/standard/script/tobago-suggest.js

Modified: myfaces/tobago/branches/tobago-3.0.x/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/overview/OverviewController.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/branches/tobago-3.0.x/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/overview/OverviewController.java?rev=1708762&r1=1708761&r2=1708762&view=diff
==============================================================================
--- myfaces/tobago/branches/tobago-3.0.x/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/overview/OverviewController.java (original)
+++ myfaces/tobago/branches/tobago-3.0.x/tobago-example/tobago-example-demo/src/main/java/org/apache/myfaces/tobago/example/demo/overview/OverviewController.java Thu Oct 15 09:59:26 2015
@@ -369,17 +369,17 @@ public class OverviewController implemen
     }
 
   public List<String> getInputSuggestItems(final UIInput component) {
-    String prefix = (String) component.getSubmittedValue();
-    if (prefix == null) {
-      prefix = "";
+    String substring = (String) component.getSubmittedValue();
+    if (substring == null) {
+      substring = "";
     }
-    LOG.info("Creating items for prefix: '" + prefix + "'");
+    LOG.info("Creating items for substring: '" + substring + "'");
     final List<String> result = new ArrayList<String>();
     for (final String name : LocaleList.COUNTRY_LANGUAGE) {
-      if (StringUtils.startsWithIgnoreCase(name, prefix)) {
+      if (StringUtils.containsIgnoreCase(name, substring)) {
         result.add(name);
       }
-      if (result.size() > 100) { // this value should be greater than the value of the input control
+      if (result.size() > 100) { // this value should not be smaller than the value of the suggest control
         break;
       }
     }

Modified: myfaces/tobago/branches/tobago-3.0.x/tobago-example/tobago-example-demo/src/main/webapp/content/01-basic/basic.xhtml
URL: http://svn.apache.org/viewvc/myfaces/tobago/branches/tobago-3.0.x/tobago-example/tobago-example-demo/src/main/webapp/content/01-basic/basic.xhtml?rev=1708762&r1=1708761&r2=1708762&view=diff
==============================================================================
--- myfaces/tobago/branches/tobago-3.0.x/tobago-example/tobago-example-demo/src/main/webapp/content/01-basic/basic.xhtml (original)
+++ myfaces/tobago/branches/tobago-3.0.x/tobago-example/tobago-example-demo/src/main/webapp/content/01-basic/basic.xhtml Thu Oct 15 09:59:26 2015
@@ -47,10 +47,11 @@
         </f:facet>
         <tc:in value="#{overviewController.basicInput}" required="true" tabIndex="1" id="basic_input"
                label="#{overviewBundle.basic_textboxLabel}" tip="#{overviewBundle.basic_textboxTip}"/>
-        <tc:in value="#{overviewController.suggestInput}" tabIndex="3" id="suggest"
-               label="#{overviewBundle.basic_suggestLabel}" tip="#{overviewBundle.basic_suggestTip}">
+        <tc:in value="#{overviewController.suggestInput}" tabIndex="3" id="suggest_input"
+               label="#{overviewBundle.basic_suggestLabel}" tip="#{overviewBundle.basic_suggestTip}"
+               placeholder="Country (Language)">
           <tc:suggest suggestMethod="#{overviewController.getInputSuggestItems}"
-                      delay="300" minimumCharacters="1" maximumItems="15"/>
+                      id="suggest" delay="300" minimumCharacters="1" maximumItems="10"/>
         </tc:in>
         <tc:date value="#{overviewController.basicDate}" tabIndex="4" id="basic_date"
                  label="#{overviewBundle.basic_dateLabel}" required="true">

Modified: myfaces/tobago/branches/tobago-3.0.x/tobago-theme/tobago-theme-standard/src/main/java/org/apache/myfaces/tobago/renderkit/html/standard/standard/tag/SuggestRenderer.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/branches/tobago-3.0.x/tobago-theme/tobago-theme-standard/src/main/java/org/apache/myfaces/tobago/renderkit/html/standard/standard/tag/SuggestRenderer.java?rev=1708762&r1=1708761&r2=1708762&view=diff
==============================================================================
--- myfaces/tobago/branches/tobago-3.0.x/tobago-theme/tobago-theme-standard/src/main/java/org/apache/myfaces/tobago/renderkit/html/standard/standard/tag/SuggestRenderer.java (original)
+++ myfaces/tobago/branches/tobago-3.0.x/tobago-theme/tobago-theme-standard/src/main/java/org/apache/myfaces/tobago/renderkit/html/standard/standard/tag/SuggestRenderer.java Thu Oct 15 09:59:26 2015
@@ -19,6 +19,7 @@
 
 package org.apache.myfaces.tobago.renderkit.html.standard.standard.tag;
 
+import org.apache.myfaces.tobago.component.Attributes;
 import org.apache.myfaces.tobago.component.UIIn;
 import org.apache.myfaces.tobago.component.UISuggest;
 import org.apache.myfaces.tobago.model.AutoSuggestItem;
@@ -26,10 +27,15 @@ import org.apache.myfaces.tobago.model.A
 import org.apache.myfaces.tobago.renderkit.RendererBase;
 import org.apache.myfaces.tobago.renderkit.css.TobagoClass;
 import org.apache.myfaces.tobago.renderkit.html.DataAttributes;
+import org.apache.myfaces.tobago.renderkit.html.HtmlAttributes;
 import org.apache.myfaces.tobago.renderkit.html.HtmlElements;
+import org.apache.myfaces.tobago.renderkit.html.HtmlInputTypes;
 import org.apache.myfaces.tobago.renderkit.html.JsonUtils;
 import org.apache.myfaces.tobago.renderkit.html.util.HtmlRendererUtils;
+import org.apache.myfaces.tobago.util.ComponentUtils;
 import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import javax.el.MethodExpression;
 import javax.faces.component.UIComponent;
@@ -38,14 +44,33 @@ import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
+import java.util.Map;
 
 public class SuggestRenderer extends RendererBase {
 
+  private static final Logger LOG = LoggerFactory.getLogger(SuggestRenderer.class);
+
+  public void decode(final FacesContext facesContext, final UIComponent component) {
+
+    final UISuggest suggest = (UISuggest) component;
+    final String clientId = suggest.getClientId(facesContext);
+    final Map<String, String> requestParameterMap = facesContext.getExternalContext().getRequestParameterMap();
+    if (requestParameterMap.containsKey(clientId)) {
+      String query = requestParameterMap.get(clientId);
+      if (LOG.isDebugEnabled()) {
+        LOG.debug("suggest query='{}'", query);
+      }
+      final UIIn in = ComponentUtils.findAncestor(suggest, UIIn.class);
+      in.setSubmittedValue(query);
+    }
+  }
+
   @Override
   public void encodeBegin(FacesContext facesContext, UIComponent component) throws IOException {
     final UISuggest suggest = (UISuggest) component;
-    final UIIn in = (UIIn) suggest.getParent();
+    final UIIn in = ComponentUtils.findAncestor(suggest, UIIn.class);
     final MethodExpression suggestMethodExpression = suggest.getSuggestMethodExpression();
+    // tbd: may use "suggest" instead of "in" for the method call?
     final AutoSuggestItems items
         = createAutoSuggestItems(suggestMethodExpression.invoke(facesContext.getELContext(), new Object[]{in}));
     int totalCount = suggest.getTotalCount();
@@ -64,22 +89,27 @@ public class SuggestRenderer extends Ren
 
     final TobagoResponseWriter writer = HtmlRendererUtils.getTobagoResponseWriter(facesContext);
 
-    writer.startElement(HtmlElements.SPAN);
+    writer.startElement(HtmlElements.INPUT);
+    writer.writeAttribute(HtmlAttributes.TYPE, HtmlInputTypes.HIDDEN);
     writer.writeClassAttribute(TobagoClass.SUGGEST);
+    final String clientId = suggest.getClientId(facesContext);
+    writer.writeIdAttribute(clientId);
+    writer.writeNameAttribute(clientId);
+    writer.writeAttribute(HtmlAttributes.VALUE, (String) suggest.getAttributes().get(Attributes.VALUE), true);
 
-    writer.writeAttribute(DataAttributes.SUGGEST_FOR, in.getClientId(facesContext), true);
+    writer.writeAttribute(DataAttributes.SUGGEST_FOR, in.getClientId(facesContext), false);
     writer.writeAttribute(DataAttributes.SUGGEST_MIN_CHARS, suggest.getMinimumCharacters());
     writer.writeAttribute(DataAttributes.SUGGEST_DELAY, suggest.getDelay());
     writer.writeAttribute(DataAttributes.SUGGEST_MAX_ITEMS, suggest.getMaximumItems());
     writer.writeAttribute(DataAttributes.SUGGEST_UPDATE, suggest.isUpdate());
     writer.writeAttribute(DataAttributes.SUGGEST_TOTAL_COUNT, totalCount);
     writer.writeAttribute(DataAttributes.SUGGEST_DATA, JsonUtils.encode(array), true);
-  }
 
-  @Override
-  public void encodeEnd(FacesContext facesContext, UIComponent component) throws IOException {
-    final TobagoResponseWriter writer = HtmlRendererUtils.getTobagoResponseWriter(facesContext);
-    writer.endElement(HtmlElements.SPAN);
+    if (LOG.isDebugEnabled()) {
+      LOG.debug("suggest list: " + JsonUtils.encode(array));
+    }
+
+    writer.endElement(HtmlElements.INPUT);
   }
 
   private AutoSuggestItems createAutoSuggestItems(final Object object) {

Modified: myfaces/tobago/branches/tobago-3.0.x/tobago-theme/tobago-theme-standard/src/main/resources/org/apache/myfaces/tobago/renderkit/html/standard/standard/script/tobago-suggest.js
URL: http://svn.apache.org/viewvc/myfaces/tobago/branches/tobago-3.0.x/tobago-theme/tobago-theme-standard/src/main/resources/org/apache/myfaces/tobago/renderkit/html/standard/standard/script/tobago-suggest.js?rev=1708762&r1=1708761&r2=1708762&view=diff
==============================================================================
--- myfaces/tobago/branches/tobago-3.0.x/tobago-theme/tobago-theme-standard/src/main/resources/org/apache/myfaces/tobago/renderkit/html/standard/standard/script/tobago-suggest.js (original)
+++ myfaces/tobago/branches/tobago-3.0.x/tobago-theme/tobago-theme-standard/src/main/resources/org/apache/myfaces/tobago/renderkit/html/standard/standard/script/tobago-suggest.js Thu Oct 15 09:59:26 2015
@@ -17,23 +17,18 @@
 
 Tobago.Suggest = {};
 
-Tobago.Suggest.substring = function(strings) {
-  return function findMatches(query, callback) {
-    // an array that will be populated with substring matches
-    var matches = [];
-
-    // regex used to determine if a string contains the substring `query`
-    var substringRegex = new RegExp(query, 'i');
-
-    // iterate through the pool of strings and for any string that
-    // contains the substring `query`, add it to the `matches` array
-    $.each(strings, function(i, string) {
-      if (substringRegex.test(string)) {
-        matches.push(string);
-      }
-    });
+Tobago.Suggest.loadFromServer = function (input) {
 
-    callback(matches);
+  return function findMatches(query, syncResults, asyncResults) {
+
+    var suggest = jQuery(Tobago.Utils.escapeClientId(input.data("tobago-suggest-for")));
+    if (suggest.val() != query) {
+      suggest.val(query);
+      input.data("tobago-suggest-callback", asyncResults);
+      var id = suggest.attr("id");
+      console.info("query: '" + query + "'");
+      Tobago.Updater.update(suggest, id, id, {});
+    }
   };
 };
 
@@ -44,6 +39,7 @@ Tobago.Suggest.init = function (elements
   suggests.each(function () {
     var suggest = jQuery(this);
     var input = jQuery(Tobago.Utils.escapeClientId(suggest.data("tobago-suggest-for")));
+    input.data("tobago-suggest-for", suggest.attr("id"));
 
     var minChars = suggest.data("tobago-suggest-min-chars");
     var maxItems = suggest.data("tobago-suggest-max-items");
@@ -56,15 +52,21 @@ Tobago.Suggest.init = function (elements
 
     input.attr("autocomplete", "off");
 
-    input.typeahead({
-      minLength: minChars,
-      hint: true,// todo
-      highlight: true // todo
-    }, {
-      //name: 'test',// todo
-      limit: maxItems,
-      source: Tobago.Suggest.substring(list)
-    });
+    var asyncResults = input.data("tobago-suggest-callback");
+    if (asyncResults) {
+      console.info("data from server: '" + list + "'");
+      asyncResults(list);
+    } else {
+      input.typeahead({
+        minLength: minChars,
+        hint: true,// todo
+        highlight: true // todo
+      }, {
+        //name: 'test',// todo
+        limit: maxItems,
+        source: Tobago.Suggest.loadFromServer(input)
+      });
+    }
   });
 };