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