word/edit: move FormRow class into separate file
authorMischa POSLAWSKY <perl@shiar.org>
Thu, 22 Jul 2021 15:56:49 +0000 (17:56 +0200)
committerMischa POSLAWSKY <perl@shiar.org>
Wed, 25 Aug 2021 04:53:32 +0000 (06:53 +0200)
Shiar_Sheet/FormRow.pm [new file with mode: 0644]
writer.plp

diff --git a/Shiar_Sheet/FormRow.pm b/Shiar_Sheet/FormRow.pm
new file mode 100644 (file)
index 0000000..03f1c22
--- /dev/null
@@ -0,0 +1,65 @@
+package Shiar_Sheet::FormRow;
+
+use 5.014;
+use warnings;
+use PLP::Functions 'EscapeHTML';
+
+our $VERSION = '1.00';
+
+sub input {
+       my ($row, $col, $attr) = @_;
+       my $val = $row->{$col} // '';
+       my $html = '';
+       $html .= qq( $_="$attr->{$_}") for sort grep {!/^-/} keys %{$attr // {}};
+
+       if (my $options = $attr->{-select}) {
+               $options = $options->(@_) if ref $options eq 'CODE';
+               $options->{$val} //= "unknown ($val)";  # preserve current
+               return (
+                       sprintf('<select id="%s" name="%1$s">', $col),
+                       (map { sprintf('<option value="%s"%s>%s</option>',
+                               $_, $val eq $_ && ' selected', $options->{$_}
+                       ) } sort keys %{$options}),
+                       '</select>',
+               );
+       }
+       elsif ($attr->{type} eq 'textarea') {
+               return (
+                       (map {
+                               sprintf('<label for="%s">%s</label>', $col, $_)
+                       } $attr->{-label} // ()),
+                       sprintf('<textarea id="%s" name="%1$s"%s>%s</textarea>',
+                               $col, $html, EscapeHTML($val)
+                       ),
+               );
+       }
+       elsif ($attr->{type} eq 'checkbox') {
+               $html .= ' checked' if $val;
+               return sprintf(
+                       join('',
+                               '<label>',
+                               '<input name="%1$s" value="0" type="hidden" />',
+                               '<input id="%s" name="%1$s" value="1"%s>',
+                               ' %s</label>',
+                       ), $col, $html, $attr->{-label}
+               );
+       }
+       else {
+               my $multiple = ref $val eq 'ARRAY' || $attr->{-multiple};
+               return (
+                       (map {
+                               sprintf('<label for="%s">%s</label>', $col, $_)
+                       } $attr->{-label} // ()),
+                       $multiple ? '<span class="inline multiinput">' : (),
+                       (map {
+                               sprintf('<input name="%s" value="%s" />', $col, EscapeHTML($_))
+                       } ref $val eq 'ARRAY' ? @{$val} : ()),
+                       sprintf('<input id="%s" name="%1$s" value="%s"%s />',
+                               $col, $multiple ? '' : EscapeHTML($val), $html
+                       ),
+                       $multiple ? '</span>' : (),
+               );
+       }
+}
+
+1;
index 275819b7b015b99e90b3b65dbd4e1b9180367ec7..b5f503253887e3de1e744c31ece007506f420b39 100644 (file)
@@ -11,6 +11,7 @@ EOT
 });
 
 use List::Util qw( pairs pairkeys );
+use Shiar_Sheet::FormRow;
 
 my $db = eval {
        require Shiar_Sheet::DB;
@@ -257,66 +258,6 @@ else {
 }
 
 my $title = $row->{id} ? "entry <small>#$row->{id}</small>" : 'new entry';
-
-package Shiar_Sheet::FormRow {
-       use PLP::Functions 'EscapeHTML';
-
-       sub input {
-               my ($row, $col, $attr) = @_;
-               my $val = $row->{$col} // '';
-               my $html = '';
-               $html .= qq( $_="$attr->{$_}") for sort grep {!/^-/} keys %{$attr // {}};
-
-               if (my $options = $attr->{-select}) {
-                       $options = $options->(@_) if ref $options eq 'CODE';
-                       $options->{$val} //= "unknown ($val)";  # preserve current
-                       return (
-                               sprintf('<select id="%s" name="%1$s">', $col),
-                               (map { sprintf('<option value="%s"%s>%s</option>',
-                                       $_, $val eq $_ && ' selected', $options->{$_}
-                               ) } sort keys %{$options}),
-                               '</select>',
-                       );
-               }
-               elsif ($attr->{type} eq 'textarea') {
-                       return (
-                               (map {
-                                       sprintf('<label for="%s">%s</label>', $col, $_)
-                               } $attr->{-label} // ()),
-                               sprintf('<textarea id="%s" name="%1$s"%s>%s</textarea>',
-                                       $col, $html, EscapeHTML($val)
-                               ),
-                       );
-               }
-               elsif ($attr->{type} eq 'checkbox') {
-                       $html .= ' checked' if $val;
-                       return sprintf(
-                               join('',
-                                       '<label>',
-                                       '<input name="%1$s" value="0" type="hidden" />',
-                                       '<input id="%s" name="%1$s" value="1"%s>',
-                                       ' %s</label>',
-                               ), $col, $html, $attr->{-label}
-                       );
-               }
-               else {
-                       my $multiple = ref $val eq 'ARRAY' || $attr->{-multiple};
-                       return (
-                               (map {
-                                       sprintf('<label for="%s">%s</label>', $col, $_)
-                               } $attr->{-label} // ()),
-                               $multiple ? '<span class="inline multiinput">' : (),
-                               (map {
-                                       sprintf('<input name="%s" value="%s" />', $col, EscapeHTML($_))
-                               } ref $val eq 'ARRAY' ? @{$val} : ()),
-                               sprintf('<input id="%s" name="%1$s" value="%s"%s />',
-                                       $col, $multiple ? '' : EscapeHTML($val), $html
-                               ),
-                               $multiple ? '</span>' : (),
-                       );
-               }
-       }
-}
 bless $row, 'Shiar_Sheet::FormRow';
 :>
 <h1>Words <:= $title :></h1>