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