You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by bo...@apache.org on 2009/02/15 23:16:31 UTC

svn commit: r744771 - /myfaces/tobago/trunk/theme/standard/src/main/resources/org/apache/myfaces/tobago/renderkit/html/standard/standard/script/tobago.js

Author: bommel
Date: Sun Feb 15 22:16:31 2009
New Revision: 744771

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

Maybe the keypress event is a better choice.
Can't really test and debug the paste event.

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

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=744771&r1=744770&r2=744771&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 Sun Feb 15 22:16:31 2009
@@ -1754,8 +1754,9 @@
 };
 
 Tobago.In.prototype.setup = function() {
+  var ctrl;
   if (this.required) {
-    var ctrl = Tobago.element(this.id);
+    ctrl = Tobago.element(this.id);
     if (ctrl.value && ctrl.value.length > 0) {
       Tobago.removeCssClass(this.id, this.cssPrefix + "-required" );
     }
@@ -1763,17 +1764,35 @@
     Tobago.addBindEventListener(ctrl, "blur", this, "leaveRequired");
   }
   if (this.maxLength && this.maxLength > 0) {
-    var ctrl = Tobago.element(this.id);
+    ctrl = Tobago.element(this.id);
     Tobago.addBindEventListener(ctrl, "change", this, "checkMaxLength");
-    Tobago.addBindEventListener(ctrl, "keyup", this, "checkMaxLength");
+    Tobago.addBindEventListener(ctrl, "keypress", this, "checkMaxLength");
+    if (Tobago.getBrowser().type == "msie") {
+      Tobago.addBindEventListener(ctrl, "paste", this, "checkMaxLengthOnPaste");
+    }
+  }
+};
+
+Tobago.In.prototype.checkMaxLengthOnPaste = function(event) {
+  if (!event) {
+    event = window.event;
   }
+  var input = Tobago.element(event);
+  var pasteText = window.clipboardData.getData("Text");
+  var range = document.selection.createRange();
+  if (input.value.length - range.text.length + pasteText.length > this.maxLength) {
+    pasteText = pasteText.substring(0, maxLength - input.value.length + range.text.length);
+    range.text = pasteText;
+    return false;
+  }
+  return true;
 };
 
 Tobago.In.prototype.checkMaxLength = function(event) {
   if (!event) {
     event = window.event;
   }
-  var ctrl = Tobago.element(this.id);
+  var ctrl = Tobago.element(event);
   var elementLength = ctrl.value.length;
   if (elementLength > this.maxLength) {
     // Input is longer than max, truncate and return false.