myfaces-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bom...@apache.org
Subject svn commit: r743495 - in /myfaces/tobago/trunk/theme: scarborough/src/main/java/org/apache/myfaces/tobago/renderkit/html/scarborough/standard/tag/ standard/src/main/resources/org/apache/myfaces/tobago/renderkit/html/standard/standard/script/
Date Wed, 11 Feb 2009 20:38:52 GMT
Author: bommel
Date: Wed Feb 11 20:38:52 2009
New Revision: 743495

URL: http://svn.apache.org/viewvc?rev=743495&view=rev
Log:
(TOBAGO-626) Input length restriction for tc:textarea

Start with a implementation based on the trinidad javascript

Modified:
    myfaces/tobago/trunk/theme/scarborough/src/main/java/org/apache/myfaces/tobago/renderkit/html/scarborough/standard/tag/TextAreaRenderer.java
    myfaces/tobago/trunk/theme/standard/src/main/resources/org/apache/myfaces/tobago/renderkit/html/standard/standard/script/tobago.js

Modified: myfaces/tobago/trunk/theme/scarborough/src/main/java/org/apache/myfaces/tobago/renderkit/html/scarborough/standard/tag/TextAreaRenderer.java
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/theme/scarborough/src/main/java/org/apache/myfaces/tobago/renderkit/html/scarborough/standard/tag/TextAreaRenderer.java?rev=743495&r1=743494&r2=743495&view=diff
==============================================================================
--- myfaces/tobago/trunk/theme/scarborough/src/main/java/org/apache/myfaces/tobago/renderkit/html/scarborough/standard/tag/TextAreaRenderer.java
(original)
+++ myfaces/tobago/trunk/theme/scarborough/src/main/java/org/apache/myfaces/tobago/renderkit/html/scarborough/standard/tag/TextAreaRenderer.java
Wed Feb 11 20:38:52 2009
@@ -33,6 +33,7 @@
 import org.apache.myfaces.tobago.renderkit.html.HtmlAttributes;
 import org.apache.myfaces.tobago.renderkit.html.HtmlConstants;
 import org.apache.myfaces.tobago.renderkit.html.HtmlStyleMap;
+import org.apache.myfaces.tobago.renderkit.html.StyleClasses;
 import org.apache.myfaces.tobago.renderkit.html.util.HtmlRendererUtil;
 import org.apache.myfaces.tobago.renderkit.util.RenderUtil;
 import org.apache.myfaces.tobago.util.ComponentUtil;
@@ -40,6 +41,8 @@
 
 import javax.faces.component.UIComponent;
 import javax.faces.context.FacesContext;
+import javax.faces.validator.Validator;
+import javax.faces.validator.LengthValidator;
 import java.io.IOException;
 
 public class TextAreaRenderer extends InputRendererBase {
@@ -109,6 +112,22 @@
     writer.endElement(HtmlConstants.TEXTAREA);
 
     HtmlRendererUtil.checkForCommandFacet(input, facesContext, writer);
+    int maxLength = -1;
+    for (Validator validator : input.getValidators()) {
+      if (validator instanceof LengthValidator) {
+        LengthValidator lengthValidator = (LengthValidator) validator;
+        maxLength = lengthValidator.getMaximum();
+      }
+    }
+    boolean required = ComponentUtil.getBooleanAttribute(input, Attributes.REQUIRED);
+    if (required || maxLength > -1) {
+      String rendererName = HtmlRendererUtil.getRendererName(facesContext, input);
+      final String[] cmds = {
+          "new Tobago.In(\"" + input.getClientId(facesContext) + "\", true ,\""
+                  + StyleClasses.PREFIX + rendererName + "\" " + (maxLength > -1? ","
+ maxLength: "")  + "  );"
+      };
+      HtmlRendererUtil.writeScriptLoader(facesContext, null, cmds);
+    }
 
     // focus
     HtmlRendererUtil.renderFocusId(facesContext, component);

Modified: myfaces/tobago/trunk/theme/standard/src/main/resources/org/apache/myfaces/tobago/renderkit/html/standard/standard/script/tobago.js
URL: http://svn.apache.org/viewvc/myfaces/tobago/trunk/theme/standard/src/main/resources/org/apache/myfaces/tobago/renderkit/html/standard/standard/script/tobago.js?rev=743495&r1=743494&r2=743495&view=diff
==============================================================================
--- myfaces/tobago/trunk/theme/standard/src/main/resources/org/apache/myfaces/tobago/renderkit/html/standard/standard/script/tobago.js
(original)
+++ myfaces/tobago/trunk/theme/standard/src/main/resources/org/apache/myfaces/tobago/renderkit/html/standard/standard/script/tobago.js
Wed Feb 11 20:38:52 2009
@@ -1745,10 +1745,11 @@
   Tobago.images[id] = this;
 };
 
-Tobago.In = function(inId, required, cssPrefix) {
+Tobago.In = function(inId, required, cssPrefix, maxLength) {
   this.id = inId;
   this.required = required;
   this.cssPrefix = cssPrefix;
+  this.maxLength = maxLength;
   this.setup();
 };
 
@@ -1761,6 +1762,70 @@
     Tobago.addBindEventListener(ctrl, "focus", this, "enterRequired");
     Tobago.addBindEventListener(ctrl, "blur", this, "leaveRequired");
   }
+  if (this.maxLength && this.maxLength > 0) {
+    LOG.error("register " + this.maxLength);
+    var ctrl = Tobago.element(this.id);
+    Tobago.addBindEventListener(ctrl, "change", this, "checkMaxLength");
+    Tobago.addBindEventListener(ctrl, "keyup", this, "checkMaxLength");
+  }
+};
+
+Tobago.In.prototype.checkMaxLength = function(event) {
+  if (!event) {
+    event = window.event;
+  }
+  var ctrl = Tobago.element(this.id);
+  var elementLength = ctrl.value.length;
+  if (elementLength > this.maxLength) {
+    // Input is longer than max, truncate and return false.
+    // This takes care of the case where the user has pasted in text
+    // that's too long. Return true here because the onChange event can
+    // continue (now that we've truncated the value). This allows chained
+    // handlers to work.
+    ctrl.value = ctrl.value.substr(0, this.maxLength);
+    return true;
+  }
+
+  // If less than max length (i.e. within acceptable range), return true
+  if (elementLength < this.maxLength) {
+    return true;
+  }
+
+  // If we've made it to here, we know that elementLength == length
+
+  if (Tobago.getBrowser().type == "msie") {
+    // in many forms there is a hidden field named "event"
+    // Sometimes IE gets confused and sends us that instead of
+    // the true event, so...
+    if (event["type"] == "hidden") {
+      event = window.event;
+    }
+  }
+
+  // If this is a change event, the field has already been updated to a string
+  // of the maximum allowable length. This is fine. Continue processing.
+  if (event.type == 'change') {
+    return true;
+  }
+
+  // If we've made it to here, we know that this is a keyPress event
+
+  // If the input is something less than a space (e.g. tab, CR, etc.)
+  // return true.
+  // If key was CTRL-v, which will be used to paste some new text,
+  // pass it along.
+  if (event) {
+    if ((event.which < 32)
+        || ((event.which == 118) && (event["ctrlKey"]))) {
+      return true;
+    }
+  }
+
+  // Default return FALSE. If we're here, this is an onKeyPress event, it's a
+  // printable character, and elementLength already equals the maximum allowed.
+  // We need to return false here to cancel the event otherwise this last
+  // character will end up in the input field in position MAX+1.
+  return false;
 };
 
 Tobago.In.prototype.enterRequired = function(e) {



Mime
View raw message