Hi All,
I have made some changes to define three new custom field types. This required no changes to any python code.

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 included patches for release 0.9.3 and for head at revision 2827.
Not much seems to have changed in the files touched so the patches are
the same anyway.

Files changed:

ticket.cs
newticket.cs
trac.js

I hope someone with commit privileges deems these additions worthy and
merges them.  If someone does merge them could they let me know and I'll
update the wiki on the subject.

If anyone finds any bugs with these fields or they would like to request
further additions to the custom fields, let me know.

Regards,
Felix

PS: I added a title attribute to a couple of the input fields to create a tool-tip. Is this compatible with the Trac standards for HTML? If so would it be considered useful to add an option to the config file to allow the administrator to set tool tips on custom fields. Something like...

mycustomfield.tooltip = Please enter your value here. (numbers only)


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

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

_______________________________________________
Trac-dev mailing list
[email protected]
http://lists.edgewall.com/mailman/listinfo/trac-dev

Reply via email to