1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.myfaces.tobago.internal.renderkit.renderer;
21
22 import org.apache.myfaces.tobago.internal.component.AbstractUIStyle;
23 import org.apache.myfaces.tobago.internal.context.Nonce;
24 import org.apache.myfaces.tobago.internal.util.StringUtils;
25 import org.apache.myfaces.tobago.internal.util.StyleRenderUtils;
26 import org.apache.myfaces.tobago.layout.Display;
27 import org.apache.myfaces.tobago.layout.GridSpan;
28 import org.apache.myfaces.tobago.layout.Measure;
29 import org.apache.myfaces.tobago.layout.Overflow;
30 import org.apache.myfaces.tobago.layout.Position;
31 import org.apache.myfaces.tobago.layout.TextAlign;
32 import org.apache.myfaces.tobago.renderkit.RendererBase;
33 import org.apache.myfaces.tobago.renderkit.css.Styles;
34 import org.apache.myfaces.tobago.renderkit.html.HtmlAttributes;
35 import org.apache.myfaces.tobago.renderkit.html.HtmlElements;
36 import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
37
38 import javax.faces.context.FacesContext;
39 import java.io.IOException;
40
41 public class StyleRenderer<T extends AbstractUIStyle> extends RendererBase<T> {
42
43 @Override
44 public void encodeBeginInternal(final FacesContext facesContext, final T component) throws IOException {
45
46 final TobagoResponseWriter writer = getResponseWriter(facesContext);
47
48 final String file = component.getFile();
49 if (StringUtils.isNotBlank(file)) {
50
51 writer.startElement(HtmlElements.LINK);
52 writer.writeAttribute(HtmlAttributes.REL, "stylesheet", false);
53 writer.writeAttribute(HtmlAttributes.HREF, file, true);
54
55 writer.writeAttribute(HtmlAttributes.TYPE, "text/css", false);
56 writer.endElement(HtmlElements.LINK);
57
58
59
60
61
62
63
64
65
66
67
68
69 } else {
70
71 final Measure width = component.getWidth();
72 final Measure height = component.getHeight();
73 final Measure minWidth = component.getMinWidth();
74 final Measure minHeight = component.getMinHeight();
75 final Measure maxWidth = component.getMaxWidth();
76 final Measure maxHeight = component.getMaxHeight();
77 final Measure left = component.getLeft();
78 final Measure right = component.getRight();
79 final Measure top = component.getTop();
80 final Measure bottom = component.getBottom();
81 final Measure paddingLeft = component.getPaddingLeft();
82 final Measure paddingRight = component.getPaddingRight();
83 final Measure paddingTop = component.getPaddingTop();
84 final Measure paddingBottom = component.getPaddingBottom();
85 final Measure marginLeft = component.getMarginLeft();
86 final Measure marginRight = component.getMarginRight();
87 final Measure marginTop = component.getMarginTop();
88 final Measure marginBottom = component.getMarginBottom();
89 final Overflow overflowX = component.getOverflowX();
90 final Overflow overflowY = component.getOverflowY();
91 final Display display = component.getDisplay();
92 final Position position = component.getPosition();
93 final TextAlign textAlign = component.getTextAlign();
94 final String backgroundImage = component.getBackgroundImage();
95 final Number flexGrow = component.getFlexGrow();
96 final Number flexShrink = component.getFlexShrink();
97 final Measure flexBasis = component.getFlexBasis();
98 final String gridTemplateColumns = component.getGridTemplateColumns();
99 final String gridTemplateRows = component.getGridTemplateRows();
100 final GridSpan gridColumn = component.getGridColumn();
101 final GridSpan gridRow = component.getGridRow();
102
103
104
105 if (width != null
106 || height != null
107 || minWidth != null
108 || minHeight != null
109 || maxWidth != null
110 || maxHeight != null
111 || left != null
112 || right != null
113 || top != null
114 || bottom != null
115 || paddingLeft != null
116 || paddingRight != null
117 || paddingTop != null
118 || paddingBottom != null
119 || marginLeft != null
120 || marginRight != null
121 || marginTop != null
122 || marginBottom != null
123 || overflowX != null
124 || overflowY != null
125 || display != null
126 || position != null
127 || textAlign != null
128 || backgroundImage != null
129 || flexGrow != null
130 || flexShrink != null
131 || flexBasis != null
132 || gridTemplateColumns != null
133 || gridTemplateRows != null
134 || gridColumn != null
135 || gridRow != null) {
136
137 writer.startElement(HtmlElements.STYLE);
138 writer.writeAttribute(HtmlAttributes.NONCE, Nonce.getNonce(facesContext), false);
139 writer.writeIdAttribute(component.getClientId(facesContext));
140 final String selector = component.getSelector();
141 if (selector != null) {
142 StyleRenderUtils.writeSelector(writer, selector);
143 } else {
144 final String parentId = component.getParent().getClientId(facesContext);
145 StyleRenderUtils.writeIdSelector(writer, parentId);
146 }
147 writer.writeText("{");
148 if (width != null) {
149 encodeStyle(writer, Styles.width, width.serialize());
150 }
151 if (height != null) {
152 encodeStyle(writer, Styles.height, height.serialize());
153 }
154 if (minWidth != null) {
155 encodeStyle(writer, Styles.minWidth, minWidth.serialize());
156 }
157 if (minHeight != null) {
158 encodeStyle(writer, Styles.minHeight, minHeight.serialize());
159 }
160 if (maxWidth != null) {
161 encodeStyle(writer, Styles.maxWidth, maxWidth.serialize());
162 }
163 if (maxHeight != null) {
164 encodeStyle(writer, Styles.maxHeight, maxHeight.serialize());
165 }
166 if (left != null) {
167 encodeStyle(writer, Styles.left, left.serialize());
168 }
169 if (right != null) {
170 encodeStyle(writer, Styles.right, right.serialize());
171 }
172 if (top != null) {
173 encodeStyle(writer, Styles.top, top.serialize());
174 }
175 if (bottom != null) {
176 encodeStyle(writer, Styles.bottom, bottom.serialize());
177 }
178 if (paddingLeft != null) {
179 encodeStyle(writer, Styles.paddingLeft, paddingLeft.serialize());
180 }
181 if (paddingRight != null) {
182 encodeStyle(writer, Styles.paddingRight, paddingRight.serialize());
183 }
184 if (paddingTop != null) {
185 encodeStyle(writer, Styles.paddingTop, paddingTop.serialize());
186 }
187 if (paddingBottom != null) {
188 encodeStyle(writer, Styles.paddingBottom, paddingBottom.serialize());
189 }
190 if (marginLeft != null) {
191 encodeStyle(writer, Styles.marginLeft, marginLeft.serialize());
192 }
193 if (marginRight != null) {
194 encodeStyle(writer, Styles.marginRight, marginRight.serialize());
195 }
196 if (marginTop != null) {
197 encodeStyle(writer, Styles.marginTop, marginTop.serialize());
198 }
199 if (marginBottom != null) {
200 encodeStyle(writer, Styles.marginBottom, marginBottom.serialize());
201 }
202 if (overflowX != null) {
203 encodeStyle(writer, Styles.overflowX, overflowX.name());
204 }
205 if (overflowY != null) {
206 encodeStyle(writer, Styles.overflowY, overflowY.name());
207 }
208 if (display != null) {
209 encodeStyle(writer, Styles.display, display.encode());
210 }
211 if (position != null) {
212 encodeStyle(writer, Styles.position, position.name());
213 }
214 if (textAlign != null) {
215 encodeStyle(writer, Styles.textAlign, textAlign.name());
216 }
217 if (backgroundImage != null) {
218 encodeStyle(writer, Styles.backgroundImage, backgroundImage);
219 }
220 if (flexGrow != null) {
221 encodeStyle(writer, Styles.flexGrow, String.valueOf(flexGrow));
222 }
223 if (flexShrink != null) {
224 encodeStyle(writer, Styles.flexShrink, String.valueOf(flexShrink));
225 }
226 if (flexBasis != null) {
227 encodeStyle(writer, Styles.flexBasis, flexBasis.serialize());
228 }
229 if (gridTemplateColumns != null) {
230 encodeStyle(writer, Styles.gridTemplateColumns, gridTemplateColumns);
231 encodeStyle(writer, "-ms-grid-columns", gridTemplateColumns);
232 }
233 if (gridTemplateRows != null) {
234 encodeStyle(writer, Styles.gridTemplateRows, gridTemplateRows);
235 encodeStyle(writer, "-ms-grid-rows", gridTemplateRows);
236 }
237 if (gridColumn != null) {
238 encodeStyle(writer, Styles.gridColumn, gridColumn.encode());
239 encodeStyle(writer, "-ms-grid-column", gridColumn.getStart());
240 encodeStyle(writer, "-ms-grid-column-span", gridColumn.getSpan());
241 }
242 if (gridRow != null) {
243 encodeStyle(writer, Styles.gridRow, gridRow.encode());
244 encodeStyle(writer, "-ms-grid-row", gridRow.getStart());
245 encodeStyle(writer, "-ms-grid-row-span", gridRow.getSpan());
246 }
247 writer.writeText("}");
248
249 writer.endElement(HtmlElements.STYLE);
250 }
251 }
252 }
253
254 private void encodeStyle(
255 final TobagoResponseWriter writer, final Styles name, final String value) throws IOException {
256 writer.writeText(name.getCssName());
257 writer.writeText(":");
258 switch (name) {
259 case backgroundImage:
260 writer.writeText("url(");
261 writer.write("'");
262 writer.writeText(value);
263 writer.write("'");
264 writer.writeText(")");
265 break;
266 default:
267 writer.writeText(value);
268
269 }
270 writer.writeText(";");
271 }
272
273
274 private void encodeStyle(final TobagoResponseWriter writer, final String name, final String value)
275 throws IOException {
276 writer.writeText(name);
277 writer.writeText(":");
278 writer.writeText(value);
279 writer.writeText(";");
280 }
281
282
283 private void encodeStyle(final TobagoResponseWriter writer, final String name, final Integer value)
284 throws IOException {
285 if (value != null) {
286 writer.writeText(name);
287 writer.writeText(":");
288 writer.writeText(value.toString());
289 writer.writeText(";");
290 }
291 }
292 }