Emm wrote:
> Felix,
> 
> I would be very interested in this - my users can't enter a date
> properly in a "text" area. 
> 
> Thanks for posting it here.
> 
> 
This is excerpted from an email to the lists 25 January 2006 with the 
subject "Patch for New Custom Fields"

As I understand it, this patch was not accepted for Trac because;

1. It will only work with Java Script enabled browsers
2. It is bad architecturally because the client is checking the data 
validity but it is not enforced in the server.  (ie someone could 
circumvent the restrictions.)

However, in a controlled environment it works very well and we use it 
every day here.  It should be considered an interim solution until Trac 
provides something similar in a better way.

Emmanuel, this should provide a starting point for you to add a date 
field.  You probably need to read up a bit on javascript, and 
clearsilver templates (if you haven't already) to understand the patch.

Description of new types:

integer
Allows only integer values such as "12" and "-98329847" to be entered
into the custom field.

cumulative_integer
Like integer but only allows the user to enter a value that is added to
the current value. Useful for accumulating work done on a ticket or
dollars spent.

set_once_integer
Like integer but only allows the user to set a value when the ticket is
created.  From then on the value is fixed.

Syntax of ini file:

<name> = <type>

Example:

[ticket-custom]
mycustominteger = integer
mycustominteger_cumulative = cumulative_integer
mycustominteger_setonce = set_once_integer

I've attached a patch for release 0.9.3.

Files changed:

ticket.cs
newticket.cs
trac.js



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Trac 
Users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/trac-users
-~----------~----~----~----~------~----~------~--~---
Index: htdocs/js/trac.js
===================================================================
--- htdocs/js/trac.js   (revision 2827)
+++ htdocs/js/trac.js   (working copy)
@@ -118,3 +118,78 @@
     addLinks(container.getElementsByTagName('h' + lvl));
   }
 }
+
+
+//Functions for the processing of special custom fields
+
+//Add the contents of newfield to oldfield
+function accumulateInt(oldfield,newfield)
+{
+  if (checkInt(newfield.value))
+  {
+    if (checkInt(oldfield.defaultValue))
+    {
+      oldfield.value = parseInt(oldfield.defaultValue, 10) + 
parseInt(newfield.value, 10); 
+    }
+    else
+    {
+      oldfield.value = newfield.value;
+    }
+    newfield.value = "0";
+  }
+  else
+  {
+    newfield.value = newfield.defaultValue;
+    oldfield.value = oldfield.defaultValue;
+  }
+}
+
+//Check whether the string can be parsed into an integer
+function checkInt(text)
+{
+  var result = true;
+  if (isBlank(text))
+  {
+    result = false;
+  }
+  for(var i=0;i<text.length;i++)
+  {
+    if(!isDigit(text.charAt(i)))
+    {
+      result = false;
+    }
+  }
+
+  return result;
+}
+
+function isDigit(num)
+{
+  if (num.length>1)
+  {
+    return false;
+  }
+  var string="-1234567890";
+  if (string.indexOf(num)!=-1)
+  {
+    return true;
+  }
+  return false;
+}
+
+function isBlank(val)
+{
+  if(val==null)
+  {
+    return true;
+  }
+  for(var i=0;i<val.length;i++) 
+  {
+    if ((val.charAt(i)!=' 
')&&(val.charAt(i)!="\t")&&(val.charAt(i)!="\n")&&(val.charAt(i)!="\r"))
+    {
+      return false;
+    }
+  }
+return true;
+}
+
Index: templates/newticket.cs
===================================================================
--- templates/newticket.cs      (revision 2827)
+++ templates/newticket.cs      (working copy)
@@ -61,6 +61,22 @@
       if:field.type == 'text' ?><input type="text" id="<?cs
         var:name(field) ?>" name="<?cs
         var:name(field) ?>" value="<?cs var:newticket[name(field)] ?>" /><?cs
+      elif:field.type == 'integer' ?><input type="text" id="<?cs
+        var:name(field) ?>" name="<?cs
+        var:name(field) ?>" value="<?cs var:newticket[name(field)] ?>"
+        onchange="javascript:if (!checkInt(this.value)){this.value = 
this.defaultValue;};"/><?cs
+      elif:field.type == 'set_once_integer' ?><input type="text" id="<?cs
+        var:name(field) ?>" name="<?cs
+        var:name(field) ?>" value="<?cs var:newticket[name(field)] ?>"
+        title="This field will not be editable once the ticket is created."
+        onchange="javascript:if (!checkInt(this.value)){this.value = 
this.defaultValue;};"/><?cs
+      elif:field.type == 'cumulative_integer' ?><input type="text" readonly 
id="<?cs
+        var:name(field) ?>" name="<?cs
+        var:name(field) ?>" value="<?cs var:ticket[name(field)] ?>"/>
+        <input type="text" id="<?cs var:name(field) ?>_e" name="<?cs
+        var:name(field) ?>_e" value="0"
+        title="Enter value to add to total."
+        onchange="javascript:accumulateInt(document.getElementById('<?cs 
var:name(field) ?>'),this);"/><?cs
       elif:field.type == 'select' ?><select id="<?cs
         var:name(field) ?>" name="<?cs var:name(field) ?>"><?cs
         if:field.optional ?><option></option><?cs /if ?><?cs
Index: templates/ticket.cs
===================================================================
--- templates/ticket.cs (revision 2827)
+++ templates/ticket.cs (working copy)
@@ -190,6 +190,21 @@
       if:field.type == 'text' ?><input type="text" id="<?cs
         var:name(field) ?>" name="<?cs
         var:name(field) ?>" value="<?cs var:ticket[name(field)] ?>" /><?cs
+     elif:field.type == 'integer' ?><input type="text" id="<?cs
+        var:name(field) ?>" name="<?cs
+        var:name(field) ?>" value="<?cs var:ticket[name(field)] ?>"
+        onchange="javascript:if (!checkInt(this.value)){this.value = 
this.defaultValue;};"/><?cs
+     elif:field.type == 'set_once_integer' ?><input type="text" disabled 
id="<?cs
+        var:name(field) ?>" name="<?cs
+        var:name(field) ?>" value="<?cs var:ticket[name(field)] ?>"/><?cs
+      elif:field.type == 'cumulative_integer' ?><input type="text" readonly 
id="<?cs
+        var:name(field) ?>" name="<?cs
+        var:name(field) ?>" value="<?cs var:ticket[name(field)] ?>"/>
+        <input type="text" id="<?cs var:name(field) ?>_e" name="<?cs
+        var:name(field) ?>_e" value="0"
+        title="Enter value to add to total."
+        onchange="javascript:accumulateInt(document.getElementById('<?cs
+        var:name(field) ?>'),this);"/><?cs
       elif:field.type == 'select' ?><select id="<?cs
         var:name(field) ?>" name="<?cs
         var:name(field) ?>"><?cs

Reply via email to