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