chiark / gitweb /
mason/dhandler (.contact): Determine the `count' strings in advance.
[tgal] / mason / dhandler
index 611567a9d9c27f1bcaaf00ae006a18353ae501a9..78f0f951fe2aa02e44d02b7a1eb8ec30a54b731d 100755 (executable)
@@ -85,14 +85,19 @@ Failed to find &lsquo;<% $path |h %>&rsquo;.
 
        my $size = "medthumb";
        my %tn;
-       my (%nd, %nf);
+       my %count;
        for my $f (@$ff)
          { $tn{$f} = TrivGal::Image->new($path . $f->name)->scale($size); }
        for my $d (@$dd) {
          my $p = join_paths $path, $d->name;
          my ($ddd, $fff, $iii);
          ($ddd, $fff, $iii) = listdir join_paths $IMGROOT, $p;
-         $nd{$d} = @$ddd; $nf{$d} = @$fff;
+
+         my $count = "";
+         $count .= scalar(@$ddd) . "/" if @$ddd;
+         $count .= scalar(@$fff) if @$fff;
+         $count{$d} = $count;
+
          DIR: for (;;) {
            if (defined $iii) {
              my $index = join_paths $p, $iii->name;
@@ -125,14 +130,11 @@ Failed to find &lsquo;<% $path |h %>&rsquo;.
 % if (@$dd) {
 <h2>Subfolders</h2>
 <div class="gallery <% $size %>">
-%   for my $d (@$dd) {
-%     my $count = "";
-%     $count .= "$nd{$d}/" if $nd{$d};
-%     $count .= "$nf{$d}" if $nf{$d};
   <& .thumbnail, target => $d->name, comment => $d->comment,
                 tn => $tn{$d}, size => $size,
                 caption =>
-                  $m->interp->apply_escapes($d->name, "h") . " [$count]" &>\
+                  $m->interp->apply_escapes($d->name, "h") .
+                  " [$count{$d}]" &>\
 %   }
 </div>
 % }
@@ -222,16 +224,26 @@ Failed to find &lsquo;<% $path |h %>&rsquo;.
        my $urldir = join_paths $SCRIPTURL, $dir;
        my ($dd, $ff, $ii) = listdir $realdir;
        my @thumbsz = qw{smallthumb medthumb bigthumb};
+       my @imgsz = sort { $SIZE{$a} <=> $SIZE{$b} } keys %SIZE;
+       my ($wd, $ht, $max);
        my %tn;
-       my $vw;
+       my %vw;
 
        my $fi = undef;
        FILE: for (my $i = 0; $i < @$ff; $i++) {
          my $f = $ff->[$i];
          my $img = TrivGal::Image->new(join_paths $dir, $f->name);
          for my $sz (@thumbsz) { $tn{$f->name}{$sz} = $img->scale($sz); }
-         if ($ff->[$i]->name eq "$base$ext")
-           { $fi = $i; $vw = $img->scale("medium"); }
+         if ($ff->[$i]->name eq "$base$ext") {
+           $fi = $i;
+           ($wd, $ht) = ($img->wd, $img->ht);
+           $max = $img->sz;
+           SIZE: for my $sc (@imgsz) {
+             my $sz = $SIZE{$sc};
+             last SIZE if $max < $sz;
+             $vw{$sc} = $img->scale($sc);
+           }
+         }
        }
        defined $fi or die "image not found in its folder?";
        my $this = $ff->[$fi];
@@ -248,12 +260,11 @@ Failed to find &lsquo;<% $path |h %>&rsquo;.
        }
 
        my $links = "";
-       my $pre =
-         urlencode join_paths $SCRIPTURL, $dir;
+       my $pre = urlencode join_paths $SCRIPTURL, $dir;
        for my $rel (qw{up first prev next last}) {
-         exists $link{$rel} and 
-           $links .= sprintf "  <link rel=%s href=\"%s\">\n",
-             $rel, urlencode "$pre/$link{$rel}";
+         $links .= sprintf "  <link rel=%s href=\"%s\">\n", $rel,
+                           urlencode "$pre/$link{$rel}"
+           if exists $link{$rel};
        }
 </%perl>
 %
@@ -271,7 +282,19 @@ Failed to find &lsquo;<% $path |h %>&rsquo;.
   <div class=prev><a class=prev href="<% "$pre/$link{prev}" |hu %>">&lsaquo;</a></div>
 % }
   <a class=view href="<% $url |h %>">
-    <img src="<% $vw |h %>">
+    <picture>
+% my ($hoff, $voff) = (60, 480);
+% SIZE: for (my $i = 0; $i < @imgsz; $i++) {
+%   my $scale = $imgsz[$i];
+%   last SIZE unless exists $vw{$scale};
+%   my $scsz = $SIZE{$scale};
+%   my $f = $scsz/$max;
+%   my ($thiswd, $thisht) = map int, ($f*$wd + $hoff, $f*$ht + $voff);
+      <source srcset="<% $vw{$scale} |h %>"
+             media="(max-width: <% $thiswd %>px) or (max-height: <% $thisht %>px)">
+% }
+      <img src="<% "$IMGURL/$path" |hu %>">
+    </picture>
   </a>
 % if ($link{next}) {
   <div class=next><a class=next href="<% "$pre/$link{next}" |hu %>">&rsaquo;</a></div>