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.ui;
016    
017    import com.liferay.portal.kernel.language.LanguageUtil;
018    import com.liferay.portal.kernel.servlet.taglib.aui.ScriptData;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.IntegerWrapper;
021    import com.liferay.portal.kernel.util.PropsKeys;
022    import com.liferay.portal.kernel.util.PropsUtil;
023    import com.liferay.portal.kernel.util.ServerDetector;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.kernel.util.Validator;
026    import com.liferay.portal.kernel.util.WebKeys;
027    import com.liferay.portal.theme.PortletDisplay;
028    import com.liferay.portal.theme.ThemeDisplay;
029    import com.liferay.portal.util.PortalUtil;
030    import com.liferay.taglib.BaseBodyTagSupport;
031    import com.liferay.taglib.FileAvailabilityUtil;
032    import com.liferay.taglib.aui.ScriptTag;
033    import com.liferay.taglib.util.PortalIncludeUtil;
034    
035    import javax.servlet.ServletContext;
036    import javax.servlet.http.HttpServletRequest;
037    import javax.servlet.jsp.JspException;
038    import javax.servlet.jsp.JspWriter;
039    import javax.servlet.jsp.tagext.BodyTag;
040    
041    /**
042     * @author Brian Wing Shun Chan
043     * @author Shuyang Zhou
044     */
045    public class IconMenuTag extends BaseBodyTagSupport implements BodyTag {
046    
047            @Override
048            public int doAfterBody() {
049                    HttpServletRequest request =
050                            (HttpServletRequest)pageContext.getRequest();
051    
052                    IntegerWrapper iconCount = (IntegerWrapper)request.getAttribute(
053                            "liferay-ui:icon-menu:icon-count");
054    
055                    Boolean singleIcon = (Boolean)request.getAttribute(
056                            "liferay-ui:icon-menu:single-icon");
057    
058                    if ((iconCount != null) && (iconCount.getValue() == 1) &&
059                            (singleIcon == null)) {
060    
061                            bodyContent.clearBody();
062    
063                            ScriptData scriptData = (ScriptData)request.getAttribute(
064                                    WebKeys.AUI_SCRIPT_DATA);
065    
066                            if (scriptData != null) {
067                                    scriptData.reset();
068                            }
069    
070                            request.setAttribute(
071                                    "liferay-ui:icon-menu:single-icon", Boolean.TRUE);
072    
073                            return EVAL_BODY_AGAIN;
074                    }
075                    else {
076                            return SKIP_BODY;
077                    }
078            }
079    
080            @Override
081            public int doEndTag() throws JspException {
082                    try {
083                            return processEndTag();
084                    }
085                    catch (Exception e) {
086                            throw new JspException(e);
087                    }
088                    finally {
089                            if (!ServerDetector.isResin()) {
090                                    _cssClass = null;
091                                    _direction = "left";
092                                    _endPage = null;
093                                    _extended = true;
094                                    _icon = null;
095                                    _id = null;
096                                    _localizeMessage = true;
097                                    _maxDisplayItems = _DEFAULT_MAX_DISPLAY_ITEMS;
098                                    _message = "actions";
099                                    _scroll = false;
100                                    _select = false;
101                                    _showArrow = true;
102                                    _showExpanded = false;
103                                    _showWhenSingleIcon = false;
104                                    _startPage = null;
105                                    _triggerCssClass = null;
106                            }
107                    }
108            }
109    
110            @Override
111            public int doStartTag() {
112                    HttpServletRequest request =
113                            (HttpServletRequest)pageContext.getRequest();
114    
115                    ScriptData scriptData = (ScriptData)request.getAttribute(
116                            WebKeys.AUI_SCRIPT_DATA);
117    
118                    if (scriptData != null) {
119                            scriptData.mark();
120                    }
121    
122                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
123                            WebKeys.THEME_DISPLAY);
124    
125                    if (_direction == null) {
126                            _direction = "left";
127                    }
128    
129                    if (_icon == null) {
130                            _icon = "../aui/cog";
131                    }
132    
133                    if (Validator.isNull(_id)) {
134                            _id = (String)request.getAttribute(
135                                    "liferay-ui:search-container-row:rowId");
136    
137                            if (Validator.isNull(_id)) {
138                                    _id = PortalUtil.generateRandomKey(
139                                            request, IconMenuTag.class.getName());
140                            }
141    
142                            _id = _id.concat("_menu");
143                    }
144    
145                    request.setAttribute("liferay-ui:icon-menu:id", _id);
146    
147                    PortletDisplay portletDisplay = themeDisplay.getPortletDisplay();
148    
149                    String namespace = portletDisplay.getNamespace();
150    
151                    _id = namespace.concat(_id);
152    
153                    request.setAttribute(
154                            "liferay-ui:icon-menu:icon-count", new IntegerWrapper());
155                    request.setAttribute(
156                            "liferay-ui:icon-menu:showWhenSingleIcon",
157                            String.valueOf(_showWhenSingleIcon));
158    
159                    return EVAL_BODY_BUFFERED;
160            }
161    
162            public void setCssClass(String cssClass) {
163                    _cssClass = cssClass;
164            }
165    
166            public void setDirection(String direction) {
167                    _direction = direction;
168            }
169    
170            public void setDisabled(boolean disabled) {
171                    _disabled = disabled;
172            }
173    
174            public void setEndPage(String endPage) {
175                    _endPage = endPage;
176            }
177    
178            public void setExtended(boolean extended) {
179                    _extended = extended;
180            }
181    
182            public void setIcon(String icon) {
183                    _icon = icon;
184            }
185    
186            public void setId(String id) {
187                    _id = id;
188            }
189    
190            public void setLocalizeMessage(boolean localizeMessage) {
191                    _localizeMessage = localizeMessage;
192            }
193    
194            public void setMarkupView(String markupView) {
195                    _markupView = markupView;
196            }
197    
198            public void setMaxDisplayItems(int maxDisplayItems) {
199                    if (maxDisplayItems <= 0) {
200                            maxDisplayItems = _DEFAULT_MAX_DISPLAY_ITEMS;
201                    }
202    
203                    _maxDisplayItems = maxDisplayItems;
204            }
205    
206            public void setMessage(String message) {
207                    if (message != null) {
208                            _message = message;
209                    }
210            }
211    
212            public void setScroll(boolean scroll) {
213                    _scroll = scroll;
214            }
215    
216            public void setSelect(boolean select) {
217                    _select = select;
218            }
219    
220            public void setShowArrow(boolean showArrow) {
221                    _showArrow = showArrow;
222            }
223    
224            public void setShowExpanded(boolean showExpanded) {
225                    _showExpanded = showExpanded;
226            }
227    
228            public void setShowWhenSingleIcon(boolean showWhenSingleIcon) {
229                    _showWhenSingleIcon = showWhenSingleIcon;
230            }
231    
232            public void setStartPage(String startPage) {
233                    _startPage = startPage;
234            }
235    
236            public void setTriggerCssClass(String triggerCssClass) {
237                    _triggerCssClass = triggerCssClass;
238            }
239    
240            public void setUseIconCaret(boolean useIconCaret) {
241                    _useIconCaret = useIconCaret;
242            }
243    
244            protected String getEndPage() {
245                    if (Validator.isNotNull(_endPage)) {
246                            return _endPage;
247                    }
248    
249                    if (Validator.isNotNull(_markupView)) {
250                            return "/html/taglib/ui/icon_menu/" + _markupView + "/end.jsp";
251                    }
252    
253                    return "/html/taglib/ui/icon_menu/end.jsp";
254            }
255    
256            protected String getStartPage() {
257                    if (Validator.isNotNull(_startPage)) {
258                            return _startPage;
259                    }
260    
261                    if (Validator.isNotNull(_markupView)) {
262                            return "/html/taglib/ui/icon_menu/" + _markupView + "/start.jsp";
263                    }
264    
265                    return "/html/taglib/ui/icon_menu/start.jsp";
266            }
267    
268            protected int processEndTag() throws Exception {
269                    HttpServletRequest request =
270                            (HttpServletRequest)pageContext.getRequest();
271    
272                    IntegerWrapper iconCount = (IntegerWrapper)request.getAttribute(
273                            "liferay-ui:icon-menu:icon-count");
274    
275                    request.removeAttribute("liferay-ui:icon-menu:icon-count");
276                    request.removeAttribute("liferay-ui:icon-menu:id");
277    
278                    Boolean singleIcon = (Boolean)request.getAttribute(
279                            "liferay-ui:icon-menu:single-icon");
280    
281                    request.removeAttribute("liferay-ui:icon-menu:single-icon");
282    
283                    JspWriter jspWriter = pageContext.getOut();
284    
285                    if ((iconCount != null) && (iconCount.getValue() >= 1) &&
286                            ((singleIcon == null) || _showWhenSingleIcon)) {
287    
288                            if (!FileAvailabilityUtil.isAvailable(
289                                            (ServletContext)request.getAttribute(WebKeys.CTX),
290                                            getStartPage())) {
291    
292                                    if (_showExpanded) {
293                                            jspWriter.write("<ul class=\"lfr-menu-expanded ");
294                                            jspWriter.write("lfr-menu-list");
295    
296                                            if (Validator.isNotNull(_cssClass)) {
297                                                    jspWriter.write(StringPool.SPACE);
298                                                    jspWriter.write(_cssClass);
299                                            }
300    
301                                            jspWriter.write("\" id=\"");
302                                            jspWriter.write(_id);
303                                            jspWriter.write("\">");
304                                    }
305                                    else {
306                                            jspWriter.write("<div class=\"btn-group lfr-icon-menu");
307    
308                                            if (Validator.isNotNull(_cssClass)) {
309                                                    jspWriter.write(StringPool.SPACE);
310                                                    jspWriter.write(_cssClass);
311                                            }
312    
313                                            if (_direction.equals("up")) {
314                                                    jspWriter.write(" dropup");
315                                            }
316    
317                                            jspWriter.write("\"><a class=\"dropdown-toggle direction-");
318                                            jspWriter.write(_direction);
319                                            jspWriter.write(" max-display-items-");
320                                            jspWriter.write(String.valueOf(_maxDisplayItems));
321    
322                                            if (_disabled) {
323                                                    jspWriter.write(" disabled");
324                                            }
325    
326                                            if (_extended) {
327                                                    jspWriter.write(" btn btn-default");
328                                            }
329    
330                                            if (_select) {
331                                                    jspWriter.write(" select");
332                                            }
333    
334                                            if (Validator.isNotNull(_triggerCssClass)) {
335                                                    jspWriter.write(StringPool.SPACE + _triggerCssClass);
336                                            }
337    
338                                            String message = _message;
339    
340                                            if (_localizeMessage) {
341                                                    message = LanguageUtil.get(request, _message);
342                                            }
343    
344                                            jspWriter.write("\" href=\"javascript:;\" id=\"");
345                                            jspWriter.write(_id);
346                                            jspWriter.write("\" title=\"");
347                                            jspWriter.write(message);
348                                            jspWriter.write("\">");
349    
350                                            if (_showArrow && _direction.equals("left")) {
351                                                    String caret = "caret";
352    
353                                                    if (_useIconCaret) {
354                                                            caret = "icon-caret-left";
355                                                    }
356    
357                                                    jspWriter.write("<i class=\"lfr-icon-menu-arrow ");
358                                                    jspWriter.write(caret);
359                                                    jspWriter.write("\"></i> ");
360                                            }
361    
362                                            boolean auiImage = false;
363    
364                                            if (Validator.isNotNull(_icon)) {
365                                                    auiImage = _icon.startsWith(_AUI_PATH);
366    
367                                                    if (auiImage) {
368                                                            jspWriter.write(" <i class=\"icon-");
369                                                            jspWriter.write(
370                                                                    _icon.substring(_AUI_PATH.length()));
371                                                            jspWriter.write(" lfr-icon-menu-icon");
372                                                            jspWriter.write("\"></i> ");
373                                                    }
374                                                    else {
375                                                            jspWriter.write(
376                                                                    "<img alt=\"\" class=\"lfr-icon-menu-icon\" ");
377                                                            jspWriter.write("src=\"");
378                                                            jspWriter.write(_icon);
379                                                            jspWriter.write("\" /> ");
380                                                    }
381                                            }
382    
383                                            if (Validator.isNotNull(message)) {
384                                                    jspWriter.write("<span class=\"lfr-icon-menu-text\">");
385                                                    jspWriter.write(message);
386                                                    jspWriter.write("</span>");
387                                            }
388    
389                                            if (_showArrow && !_direction.equals("left")) {
390                                                    String caret = "caret";
391    
392                                                    if (_useIconCaret) {
393                                                            caret = "icon-caret-" + _direction;
394                                                    }
395    
396                                                    jspWriter.write(" <i class=\"lfr-icon-menu-arrow ");
397                                                    jspWriter.write(caret);
398                                                    jspWriter.write("\"></i> ");
399                                            }
400    
401                                            jspWriter.write("</a>");
402    
403                                            ScriptTag.doTag(
404                                                    null, null, "liferay-menu",
405                                                    "Liferay.Menu.register('" + _id + "');", bodyContent,
406                                                    pageContext);
407    
408                                            jspWriter.write("<ul class=\"dropdown-menu lfr-menu-list");
409                                            jspWriter.write(" direction-");
410                                            jspWriter.write(_direction);
411                                            jspWriter.write("\">");
412                                    }
413                            }
414                            else {
415                                    setAttributes();
416    
417                                    PortalIncludeUtil.include(pageContext, getStartPage());
418                            }
419                    }
420    
421                    writeBodyContent(jspWriter);
422    
423                    if ((iconCount != null) && (iconCount.getValue() >= 1) &&
424                            ((singleIcon == null) || _showWhenSingleIcon)) {
425    
426                            if (!FileAvailabilityUtil.isAvailable(
427                                            (ServletContext)request.getAttribute(WebKeys.CTX),
428                                            getEndPage())) {
429    
430                                    jspWriter.write("</ul>");
431    
432                                    if (_showExpanded) {
433                                            ScriptTag.doTag(
434                                                    null, null, "liferay-menu",
435                                                    "Liferay.Menu.handleFocus('#" + _id + "menu');",
436                                                    bodyContent, pageContext);
437                                    }
438                                    else {
439                                            jspWriter.write("</div>");
440                                    }
441                            }
442                            else {
443                                    PortalIncludeUtil.include(pageContext, getEndPage());
444                            }
445                    }
446    
447                    request.removeAttribute("liferay-ui:icon-menu:showWhenSingleIcon");
448    
449                    return EVAL_PAGE;
450            }
451    
452            protected void setAttributes() {
453                    HttpServletRequest request =
454                            (HttpServletRequest)pageContext.getRequest();
455    
456                    request.setAttribute("liferay-ui:icon-menu:cssClass", _cssClass);
457                    request.setAttribute("liferay-ui:icon-menu:direction", _direction);
458    
459                    String message = _message;
460    
461                    if (_localizeMessage) {
462                            message = LanguageUtil.get(request, _message);
463                    }
464    
465                    request.setAttribute("liferay-ui:icon-menu:message", message);
466                    request.setAttribute("liferay-ui:icon-menu:scroll", _scroll);
467                    request.setAttribute(
468                            "liferay-ui:icon-menu:triggerCssClass", _triggerCssClass);
469            }
470    
471            private static final String _AUI_PATH = "../aui/";
472    
473            private static final int _DEFAULT_MAX_DISPLAY_ITEMS = GetterUtil.getInteger(
474                    PropsUtil.get(PropsKeys.MENU_MAX_DISPLAY_ITEMS));
475    
476            private String _cssClass;
477            private String _direction = "left";
478            private boolean _disabled;
479            private String _endPage;
480            private boolean _extended = true;
481            private String _icon;
482            private String _id;
483            private boolean _localizeMessage = true;
484            private String _markupView;
485            private int _maxDisplayItems = _DEFAULT_MAX_DISPLAY_ITEMS;
486            private String _message = "actions";
487            private boolean _scroll;
488            private boolean _select;
489            private boolean _showArrow = true;
490            private boolean _showExpanded;
491            private boolean _showWhenSingleIcon;
492            private String _startPage;
493            private String _triggerCssClass;
494            private boolean _useIconCaret;
495    
496    }