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.util.StringBundler;
018    import com.liferay.portal.kernel.util.StringPool;
019    import com.liferay.portal.kernel.util.Validator;
020    
021    import java.util.Map;
022    
023    /**
024     * @author Shuyang Zhou
025     */
026    public class AUIUtil {
027    
028            public static final String BUTTON_INPUT_PREFIX = "aui-button-input";
029    
030            public static final String BUTTON_PREFIX = "aui-button";
031    
032            public static final String FIELD_PREFIX = "aui-field";
033    
034            public static final String INPUT_PREFIX = "aui-field-input";
035    
036            public static final String LABEL_PREFIX = "aui-field-label";
037    
038            public static String buildCss(
039                    String prefix, String baseTypeCss, boolean inlineField,
040                    boolean disabled, boolean choiceField, boolean first, boolean last,
041                    String cssClass) {
042    
043                    StringBundler sb = new StringBundler();
044    
045                    sb.append(prefix);
046    
047                    if (choiceField) {
048                            sb.append(StringPool.SPACE);
049                            sb.append(prefix);
050                            sb.append("-choice");
051                    }
052                    else if (baseTypeCss.equals("button")) {
053                    }
054                    else if (baseTypeCss.equals("password") ||
055                                     baseTypeCss.equals("string") ||
056                                     baseTypeCss.equals("textarea")) {
057    
058                            sb.append(StringPool.SPACE);
059                            sb.append(prefix);
060                            sb.append("-text");
061                    }
062                    else if (baseTypeCss.equals("select")) {
063                            sb.append(StringPool.SPACE);
064                            sb.append(prefix);
065                            sb.append("-select");
066                            sb.append(StringPool.SPACE);
067                            sb.append(prefix);
068                            sb.append("-menu");
069                    }
070                    else {
071                            sb.append(StringPool.SPACE);
072                            sb.append(prefix);
073                            sb.append("-");
074                            sb.append(baseTypeCss);
075                    }
076    
077                    if (inlineField) {
078                            sb.append(StringPool.SPACE);
079                            sb.append(prefix);
080                            sb.append("-inline");
081                    }
082    
083                    if (disabled) {
084                            sb.append(StringPool.SPACE);
085                            sb.append(prefix);
086                            sb.append("-disabled");
087                    }
088    
089                    if (first) {
090                            sb.append(StringPool.SPACE);
091                            sb.append(prefix);
092                            sb.append("-first");
093                    }
094                    else if (last) {
095                            sb.append(StringPool.SPACE);
096                            sb.append(prefix);
097                            sb.append("-last");
098                    }
099    
100                    if (Validator.isNotNull(cssClass)) {
101                            sb.append(StringPool.SPACE);
102                            sb.append(cssClass);
103                    }
104    
105                    return sb.toString();
106            }
107    
108            public static String buildData(Map<String, Object> data) {
109                    if ((data == null) || (data.isEmpty())) {
110                            return StringPool.BLANK;
111                    }
112    
113                    StringBundler sb = new StringBundler(data.size() * 5);
114    
115                    for (Map.Entry<String, Object> entry : data.entrySet()) {
116                            String dataKey = entry.getKey();
117                            String dataValue = String.valueOf(entry.getValue());
118    
119                            sb.append("data-");
120                            sb.append(dataKey);
121                            sb.append("=\"");
122                            sb.append(dataValue);
123                            sb.append("\" ");
124                    }
125    
126                    return sb.toString();
127            }
128    
129            public static String buildLabel(
130                    String inlineLabel, boolean showForLabel, String forLabel) {
131    
132                    StringBundler sb = new StringBundler(4);
133    
134                    sb.append("class=\"" + LABEL_PREFIX);
135    
136                    if (Validator.isNotNull(inlineLabel)) {
137                            sb.append("-inline-label");
138                    }
139    
140                    sb.append("\"");
141    
142                    if (showForLabel) {
143                            sb.append(" for=\"" + forLabel + "\"");
144                    }
145    
146                    return sb.toString();
147            }
148    
149    }