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.dao.search.DisplayTerms;
018    import com.liferay.portal.kernel.dao.search.SearchContainer;
019    import com.liferay.portal.kernel.util.JavaConstants;
020    import com.liferay.portal.kernel.util.StringBundler;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.util.PortalUtil;
024    import com.liferay.taglib.aui.base.BaseNavBarSearchTag;
025    
026    import javax.portlet.PortletResponse;
027    
028    import javax.servlet.http.HttpServletRequest;
029    import javax.servlet.jsp.JspException;
030    
031    /**
032     * @author Eduardo Lundgren
033     * @author Bruno Basto
034     * @author Nathan Cavanaugh
035     * @author Julio Camarero
036     */
037    public class NavBarSearchTag extends BaseNavBarSearchTag {
038    
039            @Override
040            public int doStartTag() throws JspException {
041                    NavBarTag navBarTag = (NavBarTag)findAncestorWithClass(
042                            this, NavBarTag.class);
043    
044                    if (navBarTag != null) {
045                            StringBundler sb = navBarTag.getResponsiveButtonsSB();
046    
047                            sb.append("<a class=\"btn navbar-btn navbar-toggle");
048    
049                            if (_hasSearchResults()) {
050                                    sb.append(" hide");
051                            }
052    
053                            sb.append("\" id=\"");
054                            sb.append(_getNamespacedId());
055                            sb.append("NavbarBtn\" data-navId=\"");
056                            sb.append(_getNamespacedId());
057                            sb.append("\" tabindex=\"0\">");
058                            sb.append("<i class=\"icon-search\"></i></a>");
059                    }
060    
061                    return super.doStartTag();
062            }
063    
064            @Override
065            protected void cleanUp() {
066                    super.cleanUp();
067    
068                    _namespacedId = null;
069            }
070    
071            @Override
072            protected void setAttributes(HttpServletRequest request) {
073                    super.setAttributes(request);
074    
075                    setNamespacedAttribute(request, "id", _getNamespacedId());
076                    setNamespacedAttribute(request, "searchResults", _hasSearchResults());
077            }
078    
079            private String _getNamespacedId() {
080                    if (Validator.isNotNull(_namespacedId)) {
081                            return _namespacedId;
082                    }
083    
084                    _namespacedId = getId();
085    
086                    HttpServletRequest request =
087                            (HttpServletRequest)pageContext.getRequest();
088    
089                    if (Validator.isNull(_namespacedId)) {
090                            _namespacedId = PortalUtil.getUniqueElementId(
091                                    request, StringPool.BLANK, AUIUtil.normalizeId("navBar"));
092                    }
093    
094                    PortletResponse portletResponse = (PortletResponse)request.getAttribute(
095                            JavaConstants.JAVAX_PORTLET_RESPONSE);
096    
097                    if (portletResponse != null) {
098                            _namespacedId = portletResponse.getNamespace() + _namespacedId;
099                    }
100    
101                    return _namespacedId;
102            }
103    
104            private boolean _hasSearchResults() {
105                    SearchContainer<?> searchContainer = getSearchContainer();
106    
107                    if (searchContainer == null) {
108                            return false;
109                    }
110    
111                    DisplayTerms displayTerms = searchContainer.getDisplayTerms();
112    
113                    String keywords = displayTerms.getKeywords();
114    
115                    if (displayTerms.isAdvancedSearch() ||
116                            !keywords.equals(StringPool.BLANK)) {
117    
118                            return true;
119                    }
120    
121                    return false;
122            }
123    
124            private String _namespacedId;
125    
126    }