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 | 0 | public class DefaultLinkRenderer implements ILinkRenderer |
37 | |
{ |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | 0 | public static final ILinkRenderer SHARED_INSTANCE = new DefaultLinkRenderer(); |
45 | |
|
46 | |
public void renderLink(IMarkupWriter writer, IRequestCycle cycle, ILinkComponent linkComponent) |
47 | |
{ |
48 | 0 | IMarkupWriter wrappedWriter = null; |
49 | |
|
50 | 0 | if (cycle.getAttribute(Tapestry.LINK_COMPONENT_ATTRIBUTE_NAME) != null) |
51 | 0 | throw new ApplicationRuntimeException(LinkMessages.noNesting(), linkComponent, null, null); |
52 | |
|
53 | 0 | cycle.setAttribute(Tapestry.LINK_COMPONENT_ATTRIBUTE_NAME, linkComponent); |
54 | |
|
55 | 0 | boolean hasBody = getHasBody(); |
56 | |
|
57 | 0 | boolean disabled = linkComponent.isDisabled() || cycle.isRewinding(); |
58 | |
|
59 | 0 | if (!disabled) |
60 | |
{ |
61 | 0 | if (hasBody) |
62 | 0 | writer.begin(getElement()); |
63 | |
else |
64 | 0 | writer.beginEmpty(getElement()); |
65 | |
|
66 | 0 | linkComponent.renderAdditionalAttributes(writer, cycle); |
67 | |
|
68 | 0 | writer.attribute(getUrlAttribute(), constructURL(linkComponent, cycle)); |
69 | |
|
70 | 0 | String target = linkComponent.getTarget(); |
71 | |
|
72 | 0 | if (HiveMind.isNonBlank(target)) |
73 | 0 | writer.attribute(getTargetAttribute(), target); |
74 | |
|
75 | 0 | if (DirectLink.class.isInstance(linkComponent)) { |
76 | 0 | DirectLink direct = (DirectLink)linkComponent; |
77 | |
|
78 | 0 | renderAsyncParams(writer, cycle, direct); |
79 | |
} |
80 | |
|
81 | 0 | beforeBodyRender(writer, cycle, linkComponent); |
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | 0 | wrappedWriter = writer.getNestedWriter(); |
88 | 0 | } else |
89 | 0 | wrappedWriter = writer; |
90 | |
|
91 | 0 | if (hasBody) |
92 | 0 | linkComponent.renderBody(wrappedWriter, cycle); |
93 | |
|
94 | 0 | if (!disabled) { |
95 | |
|
96 | 0 | afterBodyRender(writer, cycle, linkComponent); |
97 | |
|
98 | 0 | if (hasBody) { |
99 | 0 | wrappedWriter.close(); |
100 | |
|
101 | |
|
102 | |
|
103 | 0 | writer.end(); |
104 | |
} else |
105 | 0 | writer.closeTag(); |
106 | |
} |
107 | |
|
108 | 0 | cycle.removeAttribute(Tapestry.LINK_COMPONENT_ATTRIBUTE_NAME); |
109 | 0 | } |
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
protected String constructURL(ILinkComponent component, IRequestCycle cycle) |
119 | |
{ |
120 | 0 | ILink link = component.getLink(cycle); |
121 | |
|
122 | 0 | String scheme = component.getScheme(); |
123 | 0 | Integer port = component.getPort(); |
124 | 0 | int portI = (port == null) ? 0 : port.intValue(); |
125 | 0 | String anchor = component.getAnchor(); |
126 | |
|
127 | 0 | 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 | 0 | } |
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 | 0 | } |
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 | 0 | return "a"; |
221 | |
} |
222 | |
|
223 | |
protected String getUrlAttribute() |
224 | |
{ |
225 | 0 | return "href"; |
226 | |
} |
227 | |
|
228 | |
protected String getTargetAttribute() |
229 | |
{ |
230 | 0 | return "target"; |
231 | |
} |
232 | |
|
233 | |
protected boolean getHasBody() |
234 | |
{ |
235 | 0 | return true; |
236 | |
} |
237 | |
} |