word/edit: move FormRow class into separate file
[sheet.git] / writer.plp
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>