word edit: named form input options
authorMischa POSLAWSKY <perl@shiar.org>
Sat, 30 May 2020 16:59:21 +0000 (18:59 +0200)
committerMischa POSLAWSKY <perl@shiar.org>
Sat, 6 Jun 2020 01:49:20 +0000 (03:49 +0200)
Parameters replaced by prefixed -label and -option;
other attributes are appended literally.

writer.plp

index 15a95159ce7fc9ebc17ee51af9a2a26ab0c9097d..ca27e214615983c44ce04cfacf2d0b5c000643fa 100644 (file)
@@ -208,33 +208,37 @@ my $title = $row->{id} ? "entry <small>#$row->{id}</small>" : 'new entry';
 
 package Shiar_Sheet::FormRow {
        sub input {
-               my ($row, $col, $type, $label) = @_;
+               my ($row, $col, $attr) = @_;
                my $val = $row->{$col} // '';
                $val = '{'.join(',', map {s/,/\\,/gr} @{$val}).'}' if ref $val eq 'ARRAY';
-               if (ref $type eq 'ARRAY') {
+               my $html = '';
+               $html .= qq( $_="$attr->{$_}") for sort grep {!/^-/} keys %{$attr // {}};
+
+               if (my $options = $attr->{-select}) {
                        return (
                                sprintf('<select id="%s" name="%1$s">', $col),
                                (map { sprintf('<option value="%s"%s>%s</option>',
-                                       $_, $val eq $_ && ' selected', $type->[$_]
-                               ) } 0 .. $#{$type}),
+                                       $_, $val eq $_ && ' selected', $options->[$_]
+                               ) } 0 .. $#{$options}),
                                '</select>',
                        );
                }
-               elsif ($type eq 'checkbox') {
+               elsif ($attr->{type} eq 'checkbox') {
+                       $html .= ' checked' if $val;
                        return sprintf(
                                join('',
                                        '<label>',
-                                       '<input id="%1$s" name="%1$s" value="0" type="hidden" />',
-                                       '<input id="%s" name="%1$s" value="1" type="%s"%s>',
+                                       '<input name="%1$s" value="0" type="hidden" />',
+                                       '<input id="%s" name="%1$s" value="1"%s>',
                                        ' %s</label>',
-                               ), $col, $type, !!$val && ' checked', $label
+                               ), $col, $html, $attr->{-label}
                        );
                }
                else {
-                       my $html = '';
-                       $html .= qq( $_="$type->{$_}") for sort keys %{$type // {}};
                        return (
-                               $label ? sprintf('<label for="%s">%s</label>', $col, $label) : (),
+                               (map {
+                                       sprintf('<label for="%s">%s</label>', $col, $_)
+                               } $attr->{-label} // ()),
                                sprintf('<input id="%s" name="%1$s" value="%s"%s />',
                                        $col, PLP::Functions::EscapeHTML($val), $html
                                ),
@@ -269,13 +273,13 @@ for my $colinfo (pairs @wordcols) {
        printf '<li><label for="%s">%s</label><p>', $col, $title;
                printf '<span class=inline>';
        if ($col eq 'prio') {
-               print $row->input($col => \@prioenum);
-               print $row->input(cover => 'checkbox', 'Highlighted');
-               print $row->input(grade => {type => 'number'}, 'Order');
+               print $row->input($col => {-select => \@prioenum});
+               print $row->input(cover => {type => 'checkbox', -label => 'Highlighted'});
+               print $row->input(grade => {type => 'number', -label => 'Order'});
        }
        else {
                print $row->input($col);
-               print $row->input(ref => {}, 'Reference') if $col eq 'cat';
+               print $row->input(ref => {-label => 'Reference'}) if $col eq 'cat';
        }
                print '</span>';
        say '</p></li>';