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.HtmlUtil;
018    import com.liferay.portal.kernel.util.StringBundler;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.kernel.util.Validator;
021    
022    import java.util.Map;
023    
024    /**
025     * @author Shuyang Zhou
026     */
027    public class AUIUtil {
028    
029            /**
030             * @deprecated As of 6.2.0
031             */
032            public static final String BUTTON_INPUT_PREFIX = "btn-input";
033    
034            public static final String BUTTON_PREFIX = "btn";
035    
036            public static final String FIELD_PREFIX = "field";
037    
038            /**
039             * @deprecated As of 6.2.0
040             */
041            public static final String INPUT_PREFIX = "field-input";
042    
043            /**
044             * @deprecated As of 6.2.0
045             */
046            public static final String LABEL_CHOICE_PREFIX = "choice-label";
047    
048            /**
049             * @deprecated As of 6.2.0
050             */
051            public static final String LABEL_FIELD_PREFIX = "field-label";
052    
053            public static String buildControlGroupCss(
054                    boolean inlineField, String inlineLabel, String wrapperCssClass,
055                    String baseType) {
056    
057                    StringBundler sb = new StringBundler(9);
058    
059                    sb.append("control-group");
060    
061                    if (inlineField) {
062                            sb.append(" control-group-inline");
063                    }
064    
065                    if (Validator.isNotNull(inlineLabel)) {
066                            sb.append(" form-inline");
067                    }
068    
069                    if (Validator.isNotNull(wrapperCssClass)) {
070                            sb.append(StringPool.SPACE);
071                            sb.append(wrapperCssClass);
072                    }
073    
074                    if (Validator.isNotNull(baseType)) {
075                            sb.append(StringPool.SPACE);
076                            sb.append("input-");
077                            sb.append(baseType);
078                            sb.append("-wrapper");
079                    }
080    
081                    return sb.toString();
082            }
083    
084            public static String buildCss(
085                    String prefix, boolean disabled, boolean first, boolean last,
086                    String cssClass) {
087    
088                    StringBundler sb = new StringBundler();
089    
090                    sb.append(prefix);
091    
092                    if (disabled) {
093                            sb.append(StringPool.SPACE);
094                            sb.append("disabled");
095                    }
096    
097                    if (first) {
098                            sb.append(StringPool.SPACE);
099                            sb.append(prefix);
100                            sb.append("-first");
101                    }
102                    else if (last) {
103                            sb.append(StringPool.SPACE);
104                            sb.append(prefix);
105                            sb.append("-last");
106                    }
107    
108                    if (Validator.isNotNull(cssClass)) {
109                            sb.append(StringPool.SPACE);
110                            sb.append(cssClass);
111                    }
112    
113                    return sb.toString();
114            }
115    
116            /**
117             * @deprecated As of 6.2.0, replaced by {@link #buildCss(String, boolean,
118             *             boolean, boolean, String)}
119             */
120            public static String buildCss(
121                            String prefix, String baseTypeCss, boolean disabled, boolean first,
122                            boolean last, String cssClass) {
123    
124                    return buildCss(prefix, disabled, first, last, cssClass);
125            }
126    
127            public static String buildData(Map<String, Object> data) {
128                    if ((data == null) || data.isEmpty()) {
129                            return StringPool.BLANK;
130                    }
131    
132                    StringBundler sb = new StringBundler(data.size() * 5);
133    
134                    for (Map.Entry<String, Object> entry : data.entrySet()) {
135                            String dataKey = entry.getKey();
136                            String dataValue = String.valueOf(entry.getValue());
137    
138                            sb.append("data-");
139                            sb.append(dataKey);
140                            sb.append("=\"");
141                            sb.append(HtmlUtil.escapeAttribute(dataValue));
142                            sb.append("\" ");
143                    }
144    
145                    return sb.toString();
146            }
147    
148            public static String buildLabel(
149                    String baseType, boolean inlineField, boolean showForLabel,
150                    String forLabel) {
151    
152                    StringBundler sb = new StringBundler(7);
153    
154                    if (baseType.equals("boolean")) {
155                            baseType = "checkbox";
156                    }
157    
158                    if (baseType.equals("checkbox") || baseType.equals("radio")) {
159                            sb.append("class=\"");
160                            sb.append(baseType);
161    
162                            if (inlineField) {
163                                    sb.append(" inline");
164                            }
165    
166                            sb.append("\" ");
167                    }
168                    else {
169                            sb.append("class=\"control-label\" ");
170                    }
171    
172                    if (showForLabel) {
173                            sb.append("for=\"");
174                            sb.append(HtmlUtil.escapeAttribute(forLabel));
175                            sb.append("\"");
176                    }
177    
178                    return sb.toString();
179            }
180    
181            /**
182             * @deprecated As of 6.2.0, replaced by {@link #buildLabel(String, boolean,
183             *             boolean, String)}
184             */
185            public static String buildLabel(
186                    String inlineLabel, boolean showForLabel, String forLabel,
187                    boolean choiceField) {
188    
189                    return buildLabel(StringPool.BLANK, false, showForLabel, forLabel);
190            }
191    
192    }