word/edit: generic column attribute to determine image paths
authorMischa POSLAWSKY <perl@shiar.org>
Thu, 22 Jul 2021 15:52:52 +0000 (17:52 +0200)
committerMischa POSLAWSKY <perl@shiar.org>
Wed, 25 Aug 2021 04:53:32 +0000 (06:53 +0200)
Replaces overly specific imagepath() method.

writer.plp

index 6177fcd2d8cf1be026112f7d1a1a4e80ba83ebcc..275819b7b015b99e90b3b65dbd4e1b9180367ec7 100644 (file)
@@ -98,8 +98,12 @@ my %wordcol = (
        form    => {-label => 'Title'},
        alt     => {-label => 'Synonyms', -multiple => 1},
        wptitle => {-label => 'Wikipedia'},
-       source  => {-label => 'Image'},
-       thumb   => {-label => 'Convert options', -multiple => 1},
+       source  => {-label => 'Image', -src => sub {
+               return "data/word/org/$_[0]->{id}.jpg";
+       }},
+       thumb   => {-label => 'Convert options', -multiple => 1, -src => sub {
+               return "data/word/en/$_[0]->{id}.jpg";
+       }},
        story   => {-label => 'Story', type => 'textarea'},
 );
 
@@ -109,7 +113,7 @@ if (my $search = $fields{q}) {
        say '<h1>Search</h1><ul>';
        printf("<li><small>%s</small> %s %s</li>\n",
                $_->{id}, showlink($_->{form}, "/writer/$_->{id}"),
-               sprintf('<img src="/%s" style="height:3ex; width:auto" />', Shiar_Sheet::FormRow::imagepath($_ => 'thumb')) x defined $_->{thumb}
+               sprintf('<img src="/%s" style="height:3ex; width:auto" />', $wordcol{thumb}->{-src}->($_)) x defined $_->{thumb}
        ) for $results->hashes;
        say "</ul>\n";
        exit;
@@ -183,7 +187,7 @@ elsif (defined $post{form}) {{
                return 1;
        } or Alert('Error creating translation entries', $@);
 
-       my $imgpath = Shiar_Sheet::FormRow::imagepath($row, 'source');
+       my $imgpath = $wordcol{source}->{-src}->($row);
        my $reimage = eval {
                ($row->{source} // '') ne ($replace->{source} // '') or return;
                # copy changed remote url to local file
@@ -202,8 +206,8 @@ elsif (defined $post{form}) {{
        $reimage ||= $row->{cover} ~~ $replace->{cover};  # resize
        $reimage++ if $fields{rethumb};  # force refresh
 
-       my $thumbpath = Shiar_Sheet::FormRow::imagepath($row => 'thumb');
        if ($reimage) {
+               my $thumbpath = $wordcol{thumb}->{-src}->($row);
                if (-e $imgpath) {
                        my $xyres = $row->{cover} ? '600x400' : '300x200';
                        my @cmds = @{ $row->{thumb} // [] };
@@ -309,20 +313,9 @@ package Shiar_Sheet::FormRow {
                                        $col, $multiple ? '' : EscapeHTML($val), $html
                                ),
                                $multiple ? '</span>' : (),
-                               (map {
-                                       sprintf '<img id="%spreview" src="/%s" alt="%s"%s />',
-                                               $col, $_, $row->{form}, $col eq 'source' ? ' hidden' : '';
-                               } grep { -e } $row->imagepath($col)),
                        );
                }
        }
-
-       sub imagepath {
-               my ($row, $col) = @_;
-               return "data/word/org/$row->{id}.jpg"   if $col eq 'source';
-               return "data/word/en/$row->{id}.jpg"  if $col eq 'thumb';
-               return;
-       }
 }
 bless $row, 'Shiar_Sheet::FormRow';
 :>
@@ -341,6 +334,11 @@ for my $col (@wordcols) {
        printf '<li><label for="%s">%s</label><p>', $col, $title;
                printf '<span class=inline>';
                print $row->input($col => $attr);
+               if (my $imgsrc = $attr->{-src}) {
+                       printf('<img id="%spreview" src="/%s" alt="%s"%s />',
+                               $col, $_, $row->{form}, $col eq 'source' && ' hidden'
+                       ) for grep { -e } $imgsrc->($row);
+               }
                print $row->input($_ => delete $wordcol{$_}) for @span;
                print '</span>';
        say '</p></li>';