source: trunk/grails-app/taglib/WebAlbumTagLib.groovy @ 967

Last change on this file since 967 was 194, checked in by gav, 14 years ago

Remove the auto inserted login credentials and message in production mode.

File size: 2.9 KB
Line 
1class WebAlbumTagLib {
2
3    static namespace = "wa"
4
5    def pictureAnchor = { attrs, body ->
6        def picture = attrs.remove('picture')
7        def size = attrs.remove('size')
8        out << "<a href=\"${createPictureLink(picture.id, size).encodeAsHTML()}\""
9        attrs.each { key, value ->
10            out << " $key=\"$value\""
11        }
12        out << '>'
13        out << body()
14        out << '</a>'
15    }
16
17    def pictureImg = { attrs ->
18        def picture = attrs.remove('picture')
19        def size = attrs.remove('size')
20        out << createPictureImage(picture, size, attrs)
21    }
22
23    def pictureLightboxAnchor = { attrs ->
24        def picture = attrs.remove('picture')
25        def size = attrs.remove('size')
26        def lightboxSize = attrs.remove('lightboxSize')
27        //def caption = picture.caption
28//         if (!caption) {
29            def caption = 'Show original'
30//         }
31        caption = caption.encodeAsHTML()
32        StringBuilder sb = new StringBuilder(40)
33        sb.append("<a href=\"${createPictureLink(picture.id, Image.Original).encodeAsHTML()}\"")
34        attrs.each { key, value ->
35            sb.append(" $key=\"$value\"")
36        }
37        sb.append('>')
38        sb.append(caption.replaceAll(/'/, '&#39;'))
39        sb.append('</a>')
40        def link = sb.toString().encodeAsHTML()
41        out << "<a href=\"${createPictureLink(picture.id, lightboxSize).encodeAsHTML()}\" rel=\"lightbox[list]\" title=\"${link}\">"
42        out << createPictureImage(picture, size, null)
43        out << '</a>'
44    }
45
46    private def createPictureImage(picture, size, attrs) {
47        Integer width = 0
48        Integer height = 0
49        switch (size) {
50            case Image.Original:
51                width = picture.width
52                height = picture.height
53                break
54            default:
55                width = Image.Widths[size]
56                height = Image.Widths[size]
57                break
58        }
59//         def caption = picture.caption
60//         if (!caption) {
61            def caption = 'Show original'
62//         }
63        def alt = attrs?.remove('alt')
64        if (!alt) {
65            alt = caption
66        }
67        alt = alt.encodeAsHTML()
68        def title = attrs?.remove('title')
69        if (!title) {
70            title = caption
71        }
72        title = title.encodeAsHTML()
73        StringBuilder sb = new StringBuilder(40)
74        sb.append("<img src=\"${createPictureLink(picture.id, size).encodeAsHTML()}\" alt=\"$alt\" title=\"$title\" width=\"$width\" height=\"$height\"")
75        if (attrs) {
76            attrs.each { key, value ->
77                sb.append(" $key=\"$value\"")
78            }
79        }
80        sb.append(' />')
81        sb.toString()
82    }
83
84    private def createPictureLink(id, size) {
85        def params = [ size: size, filename: Image.filename(id, size) ]
86        createLink(url: [ controller: 'pictureDetailed', action: 'view', id: id, params: params ])
87    }
88}
Note: See TracBrowser for help on using the repository browser.