001    /**
002     * Copyright (c) 2000-2013 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_CHOICE_PREFIX = "aui-choice-label";
037    
038            public static final String LABEL_FIELD_PREFIX = "aui-field-label";
039    
040            public static String buildCss(
041                    String prefix, String baseTypeCss, boolean inlineField,
042                    boolean disabled, boolean choiceField, boolean first, boolean last,
043                    String cssClass) {
044    
045                    StringBundler sb = new StringBundler();
046    
047                    sb.append(prefix);
048    
049                    if (choiceField) {
050                            sb.append(StringPool.SPACE);
051                            sb.append(prefix);
052                            sb.append("-choice");
053                    }
054                    else if (baseTypeCss.equals("button")) {
055                    }
056                    else if (baseTypeCss.equals("password") ||
057                                     baseTypeCss.equals("string") ||
058                                     baseTypeCss.equals("textarea")) {
059    
060                            sb.append(StringPool.SPACE);
061                            sb.append(prefix);
062                            sb.append("-text");
063                    }
064                    else if (baseTypeCss.equals("select")) {
065                            sb.append(StringPool.SPACE);
066                            sb.append(prefix);
067                            sb.append("-select");
068                            sb.append(StringPool.SPACE);
069                            sb.append(prefix);
070                            sb.append("-menu");
071                    }
072                    else {
073                            sb.append(StringPool.SPACE);
074                            sb.append(prefix);
075                            sb.append("-");
076                            sb.append(baseTypeCss);
077                    }
078    
079                    if (inlineField) {
080                            sb.append(StringPool.SPACE);
081                            sb.append(prefix);
082                            sb.append("-inline");
083                    }
084    
085                    if (disabled) {
086                            sb.append(StringPool.SPACE);
087                            sb.append(prefix);
088                            sb.append("-disabled");
089                    }
090    
091                    if (first) {
092                            sb.append(StringPool.SPACE);
093                            sb.append(prefix);
094                            sb.append("-first");
095                    }
096                    else if (last) {
097                            sb.append(StringPool.SPACE);
098                            sb.append(prefix);
099                            sb.append("-last");
100                    }
101    
102                    if (Validator.isNotNull(cssClass)) {
103                            sb.append(StringPool.SPACE);
104                            sb.append(cssClass);
105                    }
106    
107                    return sb.toString();
108            }
109    
110            public static String buildData(Map<String, Object> data) {
111                    if ((data == null) || data.isEmpty()) {
112                            return StringPool.BLANK;
113                    }
114    
115                    StringBundler sb = new StringBundler(data.size() * 5);
116    
117                    for (Map.Entry<String, Object> entry : data.entrySet()) {
118                            String dataKey = entry.getKey();
119                            String dataValue = String.valueOf(entry.getValue());
120    
121                            sb.append("data-");
122                            sb.append(dataKey);
123                            sb.append("=\"");
124                            sb.append(dataValue);
125                            sb.append("\" ");
126                    }
127    
128                    return sb.toString();
129            }
130    
131            /**
132             * @deprecated {@link #buildLabel(String, boolean, String, boolean)}
133             */
134            public static String buildLabel(
135                    String inlineLabel, boolean showForLabel, String forLabel) {
136    
137                    return buildLabel(inlineLabel, showForLabel, forLabel, false);
138            }
139    
140            public static String buildLabel(
141                    String inlineLabel, boolean showForLabel, String forLabel,
142                    boolean choiceField) {
143    
144                    StringBundler sb = new StringBundler(4);
145    
146                    if (choiceField) {
147                            sb.append("class=\"" + LABEL_CHOICE_PREFIX);
148                    }
149                    else {
150                            sb.append("class=\"" + LABEL_FIELD_PREFIX);
151    
152                            if (Validator.isNotNull(inlineLabel)) {
153                                    sb.append("-inline-label");
154                            }
155                    }
156    
157                    sb.append("\"");
158    
159                    if (showForLabel) {
160                            sb.append(" for=\"" + forLabel + "\"");
161                    }
162    
163                    return sb.toString();
164            }
165    
166    }