1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
package org.apache.tapestry.link; |
16 |
|
|
17 |
|
import org.apache.hivemind.ApplicationRuntimeException; |
18 |
|
import org.apache.hivemind.HiveMind; |
19 |
|
import org.apache.tapestry.*; |
20 |
|
import org.apache.tapestry.components.ILinkComponent; |
21 |
|
import org.apache.tapestry.engine.ILink; |
22 |
|
import org.apache.tapestry.util.ScriptUtils; |
23 |
|
|
24 |
|
import java.util.HashMap; |
25 |
|
import java.util.List; |
26 |
|
import java.util.Map; |
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
10 |
public class DefaultLinkRenderer implements ILinkRenderer |
37 |
|
{ |
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
1 |
public static final ILinkRenderer SHARED_INSTANCE = new DefaultLinkRenderer(); |
45 |
|
|
46 |
|
public void renderLink(IMarkupWriter writer, IRequestCycle cycle, ILinkComponent linkComponent) |
47 |
|
{ |
48 |
9 |
IMarkupWriter wrappedWriter = null; |
49 |
|
|
50 |
9 |
if (cycle.getAttribute(Tapestry.LINK_COMPONENT_ATTRIBUTE_NAME) != null) |
51 |
1 |
throw new ApplicationRuntimeException(LinkMessages.noNesting(), linkComponent, null, null); |
52 |
|
|
53 |
8 |
cycle.setAttribute(Tapestry.LINK_COMPONENT_ATTRIBUTE_NAME, linkComponent); |
54 |
|
|
55 |
8 |
boolean hasBody = getHasBody(); |
56 |
|
|
57 |
8 |
boolean disabled = linkComponent.isDisabled() || cycle.isRewinding(); |
58 |
|
|
59 |
8 |
if (!disabled) |
60 |
|
{ |
61 |
4 |
if (hasBody) |
62 |
3 |
writer.begin(getElement()); |
63 |
|
else |
64 |
1 |
writer.beginEmpty(getElement()); |
65 |
|
|
66 |
4 |
linkComponent.renderAdditionalAttributes(writer, cycle); |
67 |
|
|
68 |
4 |
writer.attribute(getUrlAttribute(), constructURL(linkComponent, cycle)); |
69 |
|
|
70 |
4 |
String target = linkComponent.getTarget(); |
71 |
|
|
72 |
4 |
if (HiveMind.isNonBlank(target)) |
73 |
3 |
writer.attribute(getTargetAttribute(), target); |
74 |
|
|
75 |
5 |
if (DirectLink.class.isInstance(linkComponent)) { |
76 |
0 |
DirectLink direct = (DirectLink)linkComponent; |
77 |
|
|
78 |
0 |
renderAsyncParams(writer, cycle, direct); |
79 |
|
} |
80 |
|
|
81 |
4 |
beforeBodyRender(writer, cycle, linkComponent); |
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
|
87 |
4 |
wrappedWriter = writer.getNestedWriter(); |
88 |
4 |
} else |
89 |
4 |
wrappedWriter = writer; |
90 |
|
|
91 |
8 |
if (hasBody) |
92 |
6 |
linkComponent.renderBody(wrappedWriter, cycle); |
93 |
|
|
94 |
8 |
if (!disabled) { |
95 |
|
|
96 |
4 |
afterBodyRender(writer, cycle, linkComponent); |
97 |
|
|
98 |
4 |
if (hasBody) { |
99 |
3 |
wrappedWriter.close(); |
100 |
|
|
101 |
|
|
102 |
|
|
103 |
3 |
writer.end(); |
104 |
|
} else |
105 |
1 |
writer.closeTag(); |
106 |
|
} |
107 |
|
|
108 |
8 |
cycle.removeAttribute(Tapestry.LINK_COMPONENT_ATTRIBUTE_NAME); |
109 |
8 |
} |
110 |
|
|
111 |
|
|
112 |
|
|
113 |
|
|
114 |
|
|
115 |
|
|
116 |
|
|
117 |
|
|
118 |
|
protected String constructURL(ILinkComponent component, IRequestCycle cycle) |
119 |
|
{ |
120 |
4 |
ILink link = component.getLink(cycle); |
121 |
|
|
122 |
4 |
String scheme = component.getScheme(); |
123 |
4 |
Integer port = component.getPort(); |
124 |
4 |
int portI = (port == null) ? 0 : port.intValue(); |
125 |
4 |
String anchor = component.getAnchor(); |
126 |
|
|
127 |
4 |
return link.getURL(scheme, null, portI, anchor, true); |
128 |
|
} |
129 |
|
|
130 |
|
|
131 |
|
|
132 |
|
|
133 |
|
|
134 |
|
|
135 |
|
|
136 |
|
|
137 |
|
|
138 |
|
|
139 |
|
|
140 |
|
|
141 |
|
|
142 |
|
|
143 |
|
|
144 |
|
|
145 |
|
protected void beforeBodyRender(IMarkupWriter writer, IRequestCycle cycle, ILinkComponent link) |
146 |
|
{ |
147 |
2 |
} |
148 |
|
|
149 |
|
|
150 |
|
|
151 |
|
|
152 |
|
|
153 |
|
|
154 |
|
|
155 |
|
|
156 |
|
|
157 |
|
|
158 |
|
|
159 |
|
|
160 |
|
|
161 |
|
|
162 |
|
|
163 |
|
|
164 |
|
|
165 |
|
protected void afterBodyRender(IMarkupWriter writer, IRequestCycle cycle, ILinkComponent link) |
166 |
|
{ |
167 |
2 |
} |
168 |
|
|
169 |
|
|
170 |
|
|
171 |
|
|
172 |
|
|
173 |
|
|
174 |
|
|
175 |
|
|
176 |
|
|
177 |
|
|
178 |
|
|
179 |
|
|
180 |
|
|
181 |
|
|
182 |
|
|
183 |
|
|
184 |
|
|
185 |
|
protected void renderAsyncParams(IMarkupWriter writer, IRequestCycle cycle, DirectLink link) |
186 |
|
{ |
187 |
0 |
List comps = link.getUpdateComponents(); |
188 |
|
|
189 |
0 |
if (!link.isAsync() && !link.isJson() |
190 |
|
&& (comps == null |
191 |
|
|| comps.size() <= 0)) |
192 |
0 |
return; |
193 |
|
|
194 |
0 |
if (!link.isParameterBound("onclick") && !link.isParameterBound("onClick")) { |
195 |
0 |
writer.attribute("onclick", |
196 |
|
"return tapestry.linkOnClick(this.href,'" + link.getClientId() + "', " + link.isJson() + ")"); |
197 |
0 |
return; |
198 |
|
} |
199 |
|
|
200 |
0 |
PageRenderSupport prs = TapestryUtils.getPageRenderSupport(cycle, link); |
201 |
|
|
202 |
0 |
if (prs == null) |
203 |
0 |
return; |
204 |
|
|
205 |
0 |
Map parms = new HashMap(); |
206 |
|
|
207 |
0 |
parms.put("component", link); |
208 |
0 |
parms.put("json", Boolean.valueOf(link.isJson())); |
209 |
0 |
parms.put("key", ScriptUtils.functionHash("onclick" + link.hashCode())); |
210 |
|
|
211 |
|
|
212 |
|
|
213 |
0 |
link.getScript().execute(link, cycle, prs, parms); |
214 |
0 |
} |
215 |
|
|
216 |
|
|
217 |
|
|
218 |
|
protected String getElement() |
219 |
|
{ |
220 |
2 |
return "a"; |
221 |
|
} |
222 |
|
|
223 |
|
protected String getUrlAttribute() |
224 |
|
{ |
225 |
2 |
return "href"; |
226 |
|
} |
227 |
|
|
228 |
|
protected String getTargetAttribute() |
229 |
|
{ |
230 |
1 |
return "target"; |
231 |
|
} |
232 |
|
|
233 |
|
protected boolean getHasBody() |
234 |
|
{ |
235 |
4 |
return true; |
236 |
|
} |
237 |
|
} |