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