Changeset 14904 in project


Ignore:
Timestamp:
06/06/09 14:01:40 (10 years ago)
Author:
azul
Message:
  1. Link images to themselves. Not very useful as users can just left click on

them. Instead should probably provide more information or something. However,
this is probably better than no link.

  1. Initial code for setting the desired image sizes through a 'size' parameter

in the query string.

  1. Fix URL to the src of the image, to compute it based in path-out-real.

Before, it could get improperly set for things like archives and whatnot.

  1. Memoize svnwiki-image-get-size.
  1. Provide a gallery on every tag that has images.
Location:
release/3/svnwiki-image/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • release/3/svnwiki-image/trunk/svnwiki-image.meta

    r14099 r14904  
    66 (synopsis "Handles images in Svnwiki repositories, providing a convenient way to view them.")
    77 (category web)
    8  (needs svnwiki-extensions srfi-40 stream-ext srfi-1 svn-post-commit-hooks orders format-modular html-stream)
     8 (needs svnwiki-extensions srfi-40 stream-ext srfi-1 svn-post-commit-hooks orders format-modular html-stream memoize)
    99 (license "GPL-3"))
  • release/3/svnwiki-image/trunk/svnwiki-image.scm

    r14099 r14904  
    44
    55(declare (export))
    6 (use svnwiki-extensions-support srfi-40 stream-ext srfi-1 svn-post-commit-hooks orders format-modular html-stream content-type)
     6(use svnwiki-extensions-support srfi-40 stream-ext srfi-1 svn-post-commit-hooks orders format-modular html-stream content-type memoize)
    77
    88(define (image-javascript env)
     
    4444  img.src = prefix + '-' + Math.max(width, height);
    4545  img.onload = function () { svnwikiImage.cleanContent(sizesDiv); };
     46
     47  var imgLink = document.getElementById('svnwikiImageLink' + imgName);
     48  if (imgLink) {
     49    imgLink.setAttribute('href', img.src);
     50  }
     51}
     52
     53svnwikiImage.setInitialImageSize = function (imgName, sizes, prefix) {
     54  svnwikiJavascript.queryStringInit();
     55  var size = parseInt(svnwikiJavascript.queryString['size']);
     56  if (isNaN(size))
     57    return;
     58  var choice = null;
     59  for (var j = 0; j < sizes.length; j ++) {
     60    var m = Math.max(sizes[j][0], sizes[j][1]);
     61    if (m > size)
     62      continue;
     63    if (choice && choice > m)
     64      continue;
     65    choice = m;
     66  }
     67  if (!choice)
     68    return;
     69  svnwikiImage.pickSize(imgName, prefix, sizes[choice][0], sizes[choice][1]);
    4670}
    4771
     
    5781
    5882  var ul = document.createElement('ul');
     83  ul.setAttribute('class', 'svnwiki-image-sizes');
    5984  for (var j = 0; j < sizes.length; j ++) {
    6085    var target = "javascript:svnwikiImage.pickSize('" + imgName + "','" + prefix + "'," + sizes[j][0] + "," + sizes[j][1] + ");";
     
    6590}
    6691
     92svnwikiImage.imageSizes = [ 100, 240, 500 ];
     93svnwikiImage.currentImageSize = 0;
     94svnwikiImage.imagesList = [];
     95
     96svnwikiImage.tagsDisplayImages = function () {
     97  var filescontainer = document.getElementById('tag-list-files');
     98  filescontainer.style.display = 'none'
     99  if (!svnwikiImage.imagesList.length) {
     100    svnwikiImage.loadImagesList();
     101  }
     102  svnwikiImage.updateImagesDisplay();
     103}
     104
     105svnwikiImage.loadImagesList = function() {
     106  var files = document.getElementById('files-for-tag').getElementsByTagName('li');
     107  svnwikiImage.imagesList = [];
     108  for (i = 0; i < files.length; i ++) {
     109    var fileclasses = files[i].getAttribute('class').split(' ');
     110    for (c = 0; c < fileclasses.length; c ++) {
     111      if (fileclasses[c] == 'image') {
     112        url = files[i].firstChild.getAttribute('href')
     113        name = 'files[i].firstChild.firstChild.textContent';
     114        svnwikiImage.imagesList.push([url, name]);
     115      }
     116    }
     117  }
     118}
     119
     120svnwikiImage.updateImagesDisplay = function() {
     121  document.getElementById('tag-list-images').style.display = 'inline'
     122  var container = document.getElementById('tag-list-images').getElementsByTagName('ul')[1];
     123  svnwikiJavascript.cleanContent(container)
     124  for (i = 0; i < svnwikiImage.imagesList.length; i ++) {
     125    url = svnwikiImage.imagesList[i][0];
     126    name = svnwikiImage.imagesList[i][1];
     127    var img = svnwikiJavascript.makeElement('img');
     128    img.setAttribute('src', url + '-' + svnwikiImage.imageSizes[svnwikiImage.currentImageSize]);
     129    container.appendChild(svnwikiJavascript.makeElement('li', svnwikiJavascript.makeLink(url, img)));
     130  }
     131}
     132
     133svnwikiImage.changeImageSize = function (direction) {
     134  var x = svnwikiImage.currentImageSize + direction;
     135  if (x < 0 || x >= svnwikiImage.imageSizes.length)
     136    return;
     137  svnwikiImage.currentImageSize = x;
     138  svnwikiImage.updateImagesDisplay();
     139}
     140
    67141}
    68142EOF
    69143)
    70144
    71 (define (svnwiki-image-show-sizes-link env random-id)
    72   (let-from-environment env (path)
    73     (format #f "javascript:svnwikiImage.showSizes('~A', [~{[~{~A~^, ~}]~^, ~}], '~A');"
     145(define (svnwiki-image-show-sizes-link env function random-id)
     146  (let-from-environment env (path path-out-real)
     147    (format #f "svnwikiImage.~A('~A', [~{[~{~A~^, ~}]~^, ~}], '~A');"
     148            function
    74149            random-id
    75150            (svnwiki-image-sizes-env env)
    76             (last (string-split path "/")))))
     151            (make-link-url path-out-real path #f))))
    77152
    78153(define (svnwiki-image-default-size env)
     
    91166(define (svnwiki-image-handler-render env)
    92167  (when (svnwiki-image-get-type env)
    93     (let-from-environment env (initial-header-depth path-in path static-url return)
     168    (let-from-environment env (initial-header-depth path-in path static-url return path-out-real)
    94169      (svnwiki-report-progress env "Image generate HTML: ~A~%" path)
    95170      (let ((random-id (random 100000)))
    96171        (return
    97172          (html-stream
    98             (format #f "<h~A>" initial-header-depth)
    99             (svnwiki-get-title-html env)
    100             (format #f "</h~A>" initial-header-depth)
     173            ;(format #f "<h~A>" initial-header-depth)
     174            ;(svnwiki-get-title-html env)
     175            ;(format #f "</h~A>" initial-header-depth)
    101176            ((ul class "svnwiki-image-links")
    102              (li ((a href (svnwiki-image-show-sizes-link env random-id)) "All sizes")))
     177             (li ((a href (format #f "javascript:~A" (svnwiki-image-show-sizes-link env "showSizes" random-id))) "Change view size")))
    103178            ((div id (format #f "svnwikiImageSizes~A" random-id)))
    104             ((img src (format #f "~A-~A" (last (string-split path "/")) (apply max (svnwiki-image-default-size env)))
    105                   id (format #f "svnwikiImage~A" random-id)
    106                   ; TODO: Use svnwiki:title as the alt parameter.
    107                   alt (last (string-split path "/"))))
     179            (let ((src (format #f "~A-~A" (make-link-url path-out-real path #f) (apply max (svnwiki-image-default-size env)))))
     180              (html-stream
     181                ((a href src
     182                    id (format #f "svnwikiImageLink~A" random-id))
     183                 ((img src src
     184                       id (format #f "svnwikiImage~A" random-id)
     185                       ; TODO: Use svnwiki:title as the alt parameter.
     186                       alt (last (string-split path "/")))))
     187                ((script type "text/javascript")
     188                 (svnwiki-image-show-sizes-link env "setInitialImageSize" random-id))))
    108189            (let ((author (get-props-parents-first "svnwiki:image:author" path-in path #f))
    109190                  (author-url (get-props-parents-first "svnwiki:image:author-url" path-in path #f)))
     
    124205                  (p (b "Source:") "\n" ((a href source) source)))
    125206                stream-null))
    126             (get-props-parents-first "svnwiki:image:description" path-in path stream-null)
     207            (let ((description (get-props-parents-first "svnwiki:image:description" path-in path stream-null)))
     208              (if (stream-null? description)
     209                stream-null
     210                (html-stream (p description))))
    127211            (svnwiki-render-file-contents-tail env)))))))
    128212
    129 (define (svnwiki-image-get-size env)
    130   (let-from-environment env (path-in path)
    131     (let* ((input (open-input-pipe
    132                     (format #f "anytopnm ~A | head -2" (svnwiki-path-escape (svnwiki-make-pathname path-in path)))))
    133            ; Skip image type (eg "P4"):
    134            (type (read input))
    135            (x (read input))
    136            (y (read input)))
    137       (close-input-port input)
    138       (values x y))))
     213(define svnwiki-image-get-size
     214  (memoize-proc
     215    100
     216    (lambda (env)
     217      (let-from-environment env (path-in path)
     218        (values
     219          (list path-in path)
     220          (lambda ()
     221            (profile-call
     222              "svnwiki-image-get-size"
     223              (lambda ()
     224                (let* ((input (open-input-pipe
     225                                (format #f "anytopnm ~A | head -2" (svnwiki-path-escape (svnwiki-make-pathname path-in path)))))
     226                       ; Skip image type (eg "P4"):
     227                       (type (read input))
     228                       (x (read input))
     229                       (y (read input)))
     230                  (close-input-port input)
     231                  (values x y))))))))))
    139232
    140233(define *svnwiki-image-sizes-default* 500)
     
    180273(define (svnwiki-image-update env)
    181274  (let-from-environment env (path-in path path-out)
    182     (svnwiki-report-progress env "Image update start: ~A~%" path)
    183275    (and-let* ((subtype (svnwiki-image-get-type env)))
     276      (svnwiki-report-progress env "Image update start: ~A~%" path)
    184277      (receive (size-x size-y)
    185278               (svnwiki-image-get-size env)
     
    227320                                    "svnwiki:title"
    228321                                    (svnwiki-make-pathname path-in file)
     322                                    ; TODO: Maybe use (make-link-url path-out-real file #f) below?
    229323                                    (string->stream (last (string-split file "/"))))))))
    230324                 (link (make-link-url path-out-real file #f))
     
    248342(svnwiki-extension-define 'javascript 'image image-javascript)
    249343(svnwiki-extension-define 'code-break 'image svnwiki-image-code-break)
     344
     345(define (tags-file-class env)
     346  (when (svnwiki-image-get-type env)
     347    (let-from-environment env (return)
     348      (return 'image))))
     349
     350(svnwiki-extension-define 'tags-file-class 'image tags-file-class)
     351
     352(define (tags-view env)
     353  (let-from-environment env (return)
     354    (return
     355      (list
     356        'image
     357        "Images"
     358        "svnwikiImage.tagsDisplayImages()"
     359        (html-stream
     360          ((div id "tag-list-images" style "display:none;")
     361           ((ul class "tags-image-options")
     362            (li ((a href "javascript:svnwikiImage.changeImageSize(-1)") "Zoom out"))
     363            (li ((a href "javascript:svnwikiImage.changeImageSize(1)") "Zoom in")))
     364           (ul)))))))
     365
     366(svnwiki-extension-define 'tags-view 'image tags-view)
Note: See TracChangeset for help on using the changeset viewer.