[tools 3/5] testbot/cgi: Let DisplayProperty() specify how to format the property.

Francois Gouget fgouget at codeweavers.com
Mon Apr 25 13:50:29 CDT 2022


This applies to CollectionBlocks as well as FormPages.

Signed-off-by: Francois Gouget <fgouget at codeweavers.com>
---
 .../lib/ObjectModel/CGI/CollectionBlock.pm    | 24 +++++++++---
 testbot/lib/ObjectModel/CGI/FormPage.pm       | 39 +++++++++++--------
 2 files changed, 42 insertions(+), 21 deletions(-)

diff --git a/testbot/lib/ObjectModel/CGI/CollectionBlock.pm b/testbot/lib/ObjectModel/CGI/CollectionBlock.pm
index 14e3cfe54..b2ebb2eec 100644
--- a/testbot/lib/ObjectModel/CGI/CollectionBlock.pm
+++ b/testbot/lib/ObjectModel/CGI/CollectionBlock.pm
@@ -166,10 +166,15 @@ sub GenerateErrorPopup($)
 
 =item C<DisplayProperty()>
 
-Specifies whether to hide the given property.
+Specifies whether to hide the given property and how to format it.
 
-Any value evaluating to false specifies that the property should be hidden and
-all others that it should be shown.
+Returns a (display, format) array where the format value is optional.
+If the display value is false the property is hidden. Otherwise it should be
+"ro" to get a read-only view.
+
+The format value is passed to GenerateDataView() which can use it to decide
+how to format the property. By default it is simply passed to
+ValueFormatter::GenerateValueHTML().
 
 =back
 =cut
@@ -299,6 +304,14 @@ The object for the current row.
 Furthermore one can get the link to the current object's details page by
 passing the row to GetDetailsLink().
 
+The Col parameter has the following extra field:
+=over 12
+=item Format
+The format to be used to display the property.
+If undefined the default formatting is used.
+
+=back
+
 =back
 =cut
 
@@ -308,7 +321,7 @@ sub GenerateDataView($$$)
 
   my $PropertyName = $Col->{Descriptor}->GetName();
   my $Value = $Row->{Item}->$PropertyName;
-  GenerateValueHTML($self, $Col->{Descriptor}, $Value);
+  GenerateValueHTML($self, $Col->{Descriptor}, $Value, $Col->{Format});
 }
 
 
@@ -419,10 +432,11 @@ sub GenerateList($)
   my (@Cols, $HasDT);
   foreach my $PropertyDescriptor (@$PropertyDescriptors)
   {
-    my $Display = $self->DisplayProperty($PropertyDescriptor);
+    my ($Display, $Format) = $self->DisplayProperty($PropertyDescriptor);
     next if (!$Display);
     push @Cols, {Descriptor => $PropertyDescriptor,
                  Display => $Display,
+                 Format => $Format,
                  Index => $ColIndex++};
     $HasDT ||= ($PropertyDescriptor->GetClass() eq "Basic" and
                 $PropertyDescriptor->GetType() eq "DT");
diff --git a/testbot/lib/ObjectModel/CGI/FormPage.pm b/testbot/lib/ObjectModel/CGI/FormPage.pm
index 7fd2f561e..e286880fd 100644
--- a/testbot/lib/ObjectModel/CGI/FormPage.pm
+++ b/testbot/lib/ObjectModel/CGI/FormPage.pm
@@ -156,16 +156,18 @@ sub GetPropertyDescriptorByName($$)
 
 =item C<DisplayProperty()>
 
-Specifies whether to hide, display or allow editing the given property.
+Specifies whether to hide, display or allow editing the given property, and
+optionally how to format it.
 
-Return values:
+Returns a (display, format) array where the display value is one of the
+following:
 =over
 =item ""
 Any value evaluating to false specifies that the property should be hidden.
 
 =item "ro"
 Specifies that the property is read-only, i.e. GenerateField() should display
-it instead of generating an edit field.
+it using the default format instead of generating an edit field.
 
 =item "rw"
 Specifies that GenerateField() should allow editing the property.
@@ -174,6 +176,11 @@ HTTP parameter name.
 
 =back
 
+The optional format value is passed to GenerateValueInput() and
+GenerateValueView() which can use it to decide how to present the value.
+In the default implementation the format is only used for read-only properties
+and is passed to ValueFormatter::GenerateValueHTML().
+
 =back
 =cut
 
@@ -278,11 +285,11 @@ Generates an HTML snippet representing the value in a user-readable form.
 =back
 =cut
 
-sub GenerateValueView($$$)
+sub GenerateValueView($$$$)
 {
-  my ($self, $PropertyDescriptor, $Value) = @_;
+  my ($self, $PropertyDescriptor, $Value, $Format) = @_;
 
-  GenerateValueHTML($self, $PropertyDescriptor, $Value);
+  GenerateValueHTML($self, $PropertyDescriptor, $Value, $Format);
 }
 
 sub GetInputType($$)
@@ -329,9 +336,9 @@ Generates an HTML snippet allowing the specified value to be edited.
 =back
 =cut
 
-sub GenerateValueInput($$$)
+sub GenerateValueInput($$$$)
 {
-  my ($self, $PropertyDescriptor, $Value) = @_;
+  my ($self, $PropertyDescriptor, $Value, $_Format) = @_;
 
   my $InputType = $self->GetInputType($PropertyDescriptor);
   if ($InputType eq "checkbox")
@@ -419,9 +426,9 @@ sub GenerateFormStart($)
        "' method='$self->{Method}' enctype='multipart/form-data'>\n";
 }
 
-sub GenerateField($$$)
+sub GenerateField($$$$)
 {
-  my ($self, $PropertyDescriptor, $Display) = @_;
+  my ($self, $PropertyDescriptor, $Display, $Format) = @_;
 
   print "<div class='ItemProperty'><label>",
         $self->escapeHTML($self->GetDisplayName($PropertyDescriptor)),
@@ -431,12 +438,12 @@ sub GenerateField($$$)
   if ($Display eq "rw")
   {
     print "<div class='ItemValue'>";
-    $self->GenerateValueInput($PropertyDescriptor, $Value);
+    $self->GenerateValueInput($PropertyDescriptor, $Value, $Format);
     print "</div>";
   }
   else
   {
-    $self->GenerateValueView($PropertyDescriptor, $Value);
+    $self->GenerateValueView($PropertyDescriptor, $Value, $Format);
   }
 
   print "</div>\n";
@@ -448,8 +455,8 @@ sub GenerateFields($)
 
   foreach my $PropertyDescriptor (@{$self->GetPropertyDescriptors()})
   {
-    my $Display = $self->DisplayProperty($PropertyDescriptor);
-    $self->GenerateField($PropertyDescriptor, $Display) if ($Display);
+    my ($Display, $Format) = $self->DisplayProperty($PropertyDescriptor);
+    $self->GenerateField($PropertyDescriptor, $Display, $Format) if ($Display);
   }
 }
 
@@ -498,7 +505,7 @@ sub GenerateBody($)
     if (defined $self->{ErrField})
     {
       my $PropertyDescriptor = $self->GetPropertyDescriptorByName($self->{ErrField});
-      if ($PropertyDescriptor and !$self->DisplayProperty($PropertyDescriptor))
+      if ($PropertyDescriptor and !($self->DisplayProperty($PropertyDescriptor))[0])
       {
         $self->{ErrMessage} = "Internal error?\n$self->{ErrMessage}";
       }
@@ -599,7 +606,7 @@ sub Save($)
   {
     if ($PropertyDescriptor->GetClass() eq "Basic" &&
         $PropertyDescriptor->GetType() eq "B" &&
-        $self->DisplayProperty($PropertyDescriptor) eq "rw" &&
+        ($self->DisplayProperty($PropertyDescriptor))[0] eq "rw" &&
         ! defined($self->GetParam($PropertyDescriptor->GetName())))
     {
       if (! $self->SaveProperty($PropertyDescriptor, ""))
-- 
2.30.2




More information about the wine-devel mailing list