word edit: common method to determine image paths
authorMischa POSLAWSKY <perl@shiar.org>
Sat, 30 May 2020 16:45:52 +0000 (18:45 +0200)
committerMischa POSLAWSKY <perl@shiar.org>
Sat, 6 Jun 2020 01:49:20 +0000 (03:49 +0200)
writer.js
writer.plp

index e21b6849e2a59db9ef7b6d641bfff6a2a38077d5..e00f99e2b888cc9d0fad050d4150f7c25551997b 100644 (file)
--- a/writer.js
+++ b/writer.js
@@ -35,6 +35,7 @@ document.addEventListener('DOMContentLoaded', () => {
        let imgpreview = document.getElementById('sourcepreview');
        if (imgpreview) {
                let imginput = document.getElementById('source');
+               imginput.parentNode.parentNode.append(imgpreview); // separate row
                let previewbutton = imginput.parentNode.appendChild(document.createElement('button'));
                previewbutton.type = 'button';
                previewbutton.append('View');
index 99a9736aa782bdd0ff393ffbd763b6558523c5e2..15a95159ce7fc9ebc17ee51af9a2a26ab0c9097d 100644 (file)
@@ -150,7 +150,7 @@ elsif ($ENV{REQUEST_METHOD} eq 'POST') {{
                next;
        };
 
-       my $imgpath = "data/word/org/$row->{id}.jpg";
+       my $imgpath = Shiar_Sheet::FormRow::imagepath($row, 'source');
        my $reimage = eval {
                ($row->{source} // '') ne ($replace->{source} // '') or return;
                # copy changed remote url to local file
@@ -169,7 +169,7 @@ elsif ($ENV{REQUEST_METHOD} eq 'POST') {{
        $reimage ||= $row->{cover} ~~ $replace->{cover};  # resize
        $reimage++ if $fields{rethumb};  # force refresh
 
-       my $thumbpath = "data/word/eng/$row->{form}.jpg";
+       my $thumbpath = Shiar_Sheet::FormRow::imagepath($row => 'thumb');
        if ($reimage) {
                if (-e $imgpath) {
                        my $xyres = $row->{cover} ? '600x400' : '300x200';
@@ -238,9 +238,20 @@ package Shiar_Sheet::FormRow {
                                sprintf('<input id="%s" name="%1$s" value="%s"%s />',
                                        $col, PLP::Functions::EscapeHTML($val), $html
                                ),
+                               (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/eng/$row->{form}.jpg" if $col eq 'thumb';
+               return;
+       }
 }
 bless $row, 'Shiar_Sheet::FormRow';
 :>
@@ -264,14 +275,9 @@ for my $colinfo (pairs @wordcols) {
        }
        else {
                print $row->input($col);
-               -e and printf '<img id="%spreview" src="/%s" alt="%s" />', $col, $_, $row->{form}
-                       for $col eq 'thumb'  ? "data/word/eng/$row->{form}.jpg" : ();
                print $row->input(ref => {}, 'Reference') if $col eq 'cat';
        }
                print '</span>';
-       -e and printf('<img id="%spreview" src="/%s" alt="%s" hidden />',
-               $col, $_, $row->{form}
-       ) for $col eq 'source' ? "data/word/org/$row->{id}.jpg" : ();
        say '</p></li>';
 }