1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 package org.equanda.tapestry5.components;
26
27 import org.apache.tapestry5.*;
28 import org.apache.tapestry5.annotations.*;
29 import org.apache.tapestry5.corelib.mixins.DiscardBody;
30 import org.apache.tapestry5.dom.Element;
31 import org.apache.tapestry5.ioc.annotations.Inject;
32 import org.apache.tapestry5.ioc.services.SymbolSource;
33 import org.apache.tapestry5.ioc.services.TypeCoercer;
34
35
36
37
38
39
40 @IncludeStylesheet( "classpath:/org/equanda/tapestry5/resources/truncate.css" )
41 @IncludeJavaScriptLibrary( { "${tapestry.scriptaculous}/prototype.js",
42 "classpath:/org/equanda/tapestry5/resources/t5equanda.js" } )
43 @SupportsInformalParameters
44 public class Truncate
45 implements ClientElement
46 {
47
48 @Parameter( required = true, defaultPrefix = BindingConstants.PROP )
49 private String text;
50
51
52 @Parameter( defaultPrefix = BindingConstants.LITERAL )
53 private Integer length;
54
55
56 @Parameter( defaultPrefix = BindingConstants.LITERAL )
57 private String suffix;
58
59
60 @Inject
61 private SymbolSource symbolSource;
62
63
64 @Inject
65 private TypeCoercer typeCoercer;
66
67
68
69
70
71
72 public Integer defaultLength()
73 {
74 return typeCoercer.coerce( symbolSource.valueForSymbol( "equanda.truncate.default.length" ), Integer.class );
75 }
76
77
78
79
80
81
82 public String defaultSuffix()
83 {
84 return symbolSource.valueForSymbol( "equanda.truncate.default.suffix" );
85 }
86
87
88 @Mixin
89 @SuppressWarnings( "unused" )
90 private DiscardBody discardBody;
91
92 @Environmental
93 private RenderSupport renderSupport;
94
95 @Inject
96 private ComponentResources resources;
97
98 private String assignedClientId;
99
100
101
102
103
104
105 public String getTruncatedText()
106 {
107 int len = length;
108 return text != null && text.length() > len ? ( text.substring( 0, len - suffix.length() ) + suffix ) : text;
109 }
110
111 public String getClientId()
112 {
113 return assignedClientId;
114 }
115
116 @SetupRender
117 void setupRender()
118 {
119 assignedClientId = renderSupport.allocateClientId( resources.getId() ).toLowerCase();
120 }
121
122
123
124
125
126
127 @BeginRender
128 void doBeginRender( MarkupWriter writer )
129 {
130 String text = getTruncatedText();
131 if ( null == text ) text = "";
132 writer.element( resources.getElementName() != null ? resources.getElementName() : "span", "id",
133 assignedClientId ).text( text );
134 resources.renderInformalParameters( writer );
135 writer.end();
136 }
137
138
139
140
141
142
143 @AfterRender
144 void doAfterRender( MarkupWriter writer )
145 {
146 if ( text != null && text.length() > length )
147 {
148 Element root = writer.getDocument().getRootElement();
149 Element body = null;
150 if ( root != null )
151 {
152 if ( root.getName().equals( "html" ) )
153 {
154 body = root.find( "body" );
155 }
156 }
157 String[] args = new String[]{ "class", "eqTrunCt", "id", assignedClientId + "_text",
158 "style", "visibility: hidden; top: -1000px; left: 0;" };
159 if ( body != null )
160 {
161 Element element = body.element( "div", args );
162 element.text( text );
163 }
164 else
165 {
166 Element element = writer.element( "div", (Object[]) args );
167 element.text( text );
168 writer.end();
169 }
170 renderSupport.addScript( "eqTruncInit('%s'); ", assignedClientId );
171 }
172 }
173
174 }