001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.taglib.aui;
016    
017    import com.liferay.portal.kernel.servlet.BrowserSnifferUtil;
018    import com.liferay.portal.kernel.servlet.taglib.aui.ScriptData;
019    import com.liferay.portal.kernel.util.StringBundler;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.kernel.util.WebKeys;
023    
024    import java.io.IOException;
025    import java.io.Writer;
026    
027    import java.util.Map;
028    import java.util.Set;
029    
030    import javax.servlet.http.HttpServletRequest;
031    
032    /**
033     * @author Shuyang Zhou
034     */
035    public class AUIUtil {
036    
037            public static final String BUTTON_INPUT_PREFIX = "aui-button-input";
038    
039            public static final String BUTTON_PREFIX = "aui-button";
040    
041            public static final String FIELD_PREFIX = "aui-field";
042    
043            public static final String INPUT_PREFIX = "aui-field-input";
044    
045            public static final String LABEL_CHOICE_PREFIX = "aui-choice-label";
046    
047            public static final String LABEL_FIELD_PREFIX = "aui-field-label";
048    
049            public static String buildCss(
050                    String prefix, String baseTypeCss, boolean inlineField,
051                    boolean disabled, boolean choiceField, boolean first, boolean last,
052                    String cssClass) {
053    
054                    StringBundler sb = new StringBundler();
055    
056                    sb.append(prefix);
057    
058                    if (choiceField) {
059                            sb.append(StringPool.SPACE);
060                            sb.append(prefix);
061                            sb.append("-choice");
062                    }
063                    else if (baseTypeCss.equals("button")) {
064                    }
065                    else if (baseTypeCss.equals("password") ||
066                                     baseTypeCss.equals("string") ||
067                                     baseTypeCss.equals("textarea")) {
068    
069                            sb.append(StringPool.SPACE);
070                            sb.append(prefix);
071                            sb.append("-text");
072                    }
073                    else if (baseTypeCss.equals("select")) {
074                            sb.append(StringPool.SPACE);
075                            sb.append(prefix);
076                            sb.append("-select");
077                            sb.append(StringPool.SPACE);
078                            sb.append(prefix);
079                            sb.append("-menu");
080                    }
081                    else {
082                            sb.append(StringPool.SPACE);
083                            sb.append(prefix);
084                            sb.append("-");
085                            sb.append(baseTypeCss);
086                    }
087    
088                    if (inlineField) {
089                            sb.append(StringPool.SPACE);
090                            sb.append(prefix);
091                            sb.append("-inline");
092                    }
093    
094                    if (disabled) {
095                            sb.append(StringPool.SPACE);
096                            sb.append(prefix);
097                            sb.append("-disabled");
098                    }
099    
100                    if (first) {
101                            sb.append(StringPool.SPACE);
102                            sb.append(prefix);
103                            sb.append("-first");
104                    }
105                    else if (last) {
106                            sb.append(StringPool.SPACE);
107                            sb.append(prefix);
108                            sb.append("-last");
109                    }
110    
111                    if (Validator.isNotNull(cssClass)) {
112                            sb.append(StringPool.SPACE);
113                            sb.append(cssClass);
114                    }
115    
116                    return sb.toString();
117            }
118    
119            public static String buildData(Map<String, Object> data) {
120                    if ((data == null) || data.isEmpty()) {
121                            return StringPool.BLANK;
122                    }
123    
124                    StringBundler sb = new StringBundler(data.size() * 5);
125    
126                    for (Map.Entry<String, Object> entry : data.entrySet()) {
127                            String dataKey = entry.getKey();
128                            String dataValue = String.valueOf(entry.getValue());
129    
130                            sb.append("data-");
131                            sb.append(dataKey);
132                            sb.append("=\"");
133                            sb.append(dataValue);
134                            sb.append("\" ");
135                    }
136    
137                    return sb.toString();
138            }
139    
140            /**
141             * @deprecated {@link #buildLabel(String, boolean, String, boolean)}
142             */
143            public static String buildLabel(
144                    String inlineLabel, boolean showForLabel, String forLabel) {
145    
146                    return buildLabel(inlineLabel, showForLabel, forLabel, false);
147            }
148    
149            public static String buildLabel(
150                    String inlineLabel, boolean showForLabel, String forLabel,
151                    boolean choiceField) {
152    
153                    StringBundler sb = new StringBundler(4);
154    
155                    if (choiceField) {
156                            sb.append("class=\"" + LABEL_CHOICE_PREFIX);
157                    }
158                    else {
159                            sb.append("class=\"" + LABEL_FIELD_PREFIX);
160    
161                            if (Validator.isNotNull(inlineLabel)) {
162                                    sb.append("-inline-label");
163                            }
164                    }
165    
166                    sb.append("\"");
167    
168                    if (showForLabel) {
169                            sb.append(" for=\"" + forLabel + "\"");
170                    }
171    
172                    return sb.toString();
173            }
174    
175            public static void outputScriptData(
176                            HttpServletRequest request, Writer writer)
177                    throws IOException {
178    
179                    ScriptData scriptData = (ScriptData)request.getAttribute(
180                            ScriptTag.class.getName());
181    
182                    if (scriptData == null) {
183                            scriptData = (ScriptData)request.getAttribute(
184                                    WebKeys.AUI_SCRIPT_DATA);
185    
186                            if (scriptData != null) {
187                                    request.removeAttribute(WebKeys.AUI_SCRIPT_DATA);
188                            }
189                    }
190    
191                    if (scriptData == null) {
192                            return;
193                    }
194    
195                    writer.write("<script type=\"text/javascript\">\n// <![CDATA[\n");
196    
197                    StringBundler rawSB = scriptData.getRawSB();
198    
199                    rawSB.writeTo(writer);
200    
201                    StringBundler callbackSB = scriptData.getCallbackSB();
202    
203                    if (callbackSB.index() > 0) {
204                            String loadMethod = "use";
205    
206                            if (BrowserSnifferUtil.isIe(request) &&
207                                    (BrowserSnifferUtil.getMajorVersion(request) < 8)) {
208    
209                                    loadMethod = "ready";
210                            }
211    
212                            writer.write("AUI().");
213                            writer.write( loadMethod );
214                            writer.write("(");
215    
216                            Set<String> useSet = scriptData.getUseSet();
217    
218                            for (String use : useSet) {
219                                    writer.write(StringPool.APOSTROPHE);
220                                    writer.write(use);
221                                    writer.write(StringPool.APOSTROPHE);
222                                    writer.write(StringPool.COMMA_AND_SPACE);
223                            }
224    
225                            writer.write("function(A) {");
226    
227                            callbackSB.writeTo(writer);
228    
229                            writer.write("});");
230                    }
231    
232                    writer.write("\n// ]]>\n</script>");
233            }
234    
235    }