From 3fe4f46a94cc311b32fd7764142ca6b436cdb336 Mon Sep 17 00:00:00 2001 From: stephan Date: Mon, 4 Jul 2016 15:24:30 +0200 Subject: [PATCH] Allow solitory DropMenus, and generate more efficient HTML code. --- .../component/dropMenu/DropMenuRenderer.java | 170 ++++++++++-------- .../component/navLink/NavLinkRenderer.java | 4 +- 2 files changed, 98 insertions(+), 76 deletions(-) diff --git a/src/main/java/net/bootsfaces/component/dropMenu/DropMenuRenderer.java b/src/main/java/net/bootsfaces/component/dropMenu/DropMenuRenderer.java index 25d7105db..6163975c4 100644 --- a/src/main/java/net/bootsfaces/component/dropMenu/DropMenuRenderer.java +++ b/src/main/java/net/bootsfaces/component/dropMenu/DropMenuRenderer.java @@ -27,8 +27,14 @@ import javax.faces.context.ResponseWriter; import javax.faces.render.FacesRenderer; +import net.bootsfaces.component.dropButton.DropButton; import net.bootsfaces.component.flyOutMenu.FlyOutMenu; import net.bootsfaces.component.icon.IconRenderer; +import net.bootsfaces.component.listLinks.ListLinks; +import net.bootsfaces.component.navBar.NavBar; +import net.bootsfaces.component.navBarLinks.NavBarLinks; +import net.bootsfaces.component.pillLinks.PillLinks; +import net.bootsfaces.component.tabLinks.TabLinks; import net.bootsfaces.render.CoreRenderer; import net.bootsfaces.render.Responsive; import net.bootsfaces.render.Tooltip; @@ -38,13 +44,11 @@ public class DropMenuRenderer extends CoreRenderer { /** - * This methods generates the HTML code of the current b:dropMenu. - * encodeBegin generates the start of the component. After the, - * the JSF framework calls encodeChildren() to generate the - * HTML code between the beginning and the end of the component. For - * instance, in the case of a panel component the content of the panel is - * generated by encodeChildren(). After that, - * encodeEnd() is called to generate the rest of the HTML code. + * This methods generates the HTML code of the current b:dropMenu. encodeBegin generates the start of + * the component. After the, the JSF framework calls encodeChildren() to generate the HTML code between + * the beginning and the end of the component. For instance, in the case of a panel component the content of the + * panel is generated by encodeChildren(). After that, encodeEnd() is called to generate + * the rest of the HTML code. * * @param context * the FacesContext. @@ -64,70 +68,91 @@ public void encodeBegin(FacesContext context, UIComponent component) throws IOEx boolean isFlyOutMenu = isFlyOutMenu(component); + String htmlTag = determineHtmlTag(component, isFlyOutMenu); + + rw.startElement(htmlTag, dropMenu); + rw.writeAttribute("id", clientId, "id"); + rw.writeAttribute("class", getStyleClass(dropMenu, isFlyOutMenu), "class"); + if (dropMenu.getStyle() != null) { + rw.writeAttribute("style", dropMenu.getStyle(), "style"); + } + Tooltip.generateTooltip(context, dropMenu, rw); + { - rw.startElement("li", dropMenu); - rw.writeAttribute("id", clientId, "id"); - rw.writeAttribute("class", getStyleClass(dropMenu, isFlyOutMenu), "class"); + rw.startElement("a", dropMenu); + rw.writeAttribute("id", "dtL" + clientId, "id"); + if (isFlyOutMenu) + rw.writeAttribute("class", "dropdown-submenu", "class"); + else + rw.writeAttribute("class", "dropdown-toggle", "class"); if (dropMenu.getStyle() != null) { rw.writeAttribute("style", dropMenu.getStyle(), "style"); } - Tooltip.generateTooltip(context, dropMenu, rw); - - { - rw.startElement("a", dropMenu); - rw.writeAttribute("id", "dtL" + clientId, "id"); - if (isFlyOutMenu) - rw.writeAttribute("class", "dropdown-submenu", "class"); - else - rw.writeAttribute("class", "dropdown-toggle", "class"); - if (dropMenu.getStyle() != null) { - rw.writeAttribute("style", dropMenu.getStyle(), "style"); - } - if ("a".equals("button")) { - rw.writeAttribute("type", "button", null); + if ("a".equals("button")) { + rw.writeAttribute("type", "button", null); + } else { + rw.writeAttribute("href", "#", null); + } + rw.writeAttribute("role", "button", null); + if (!isFlyOutMenu) + rw.writeAttribute("data-toggle", "dropdown", null); + + // Encode value + String value = (String) dropMenu.getAttributes().get("value"); + String icon = dropMenu.getIcon(); + String faicon = dropMenu.getIconAwesome(); + boolean fa = false; // flag to indicate wether the selected icon + // set is + // Font Awesome or not. + if (faicon != null) { + icon = faicon; + fa = true; + } + if (icon != null) { + Object ialign = dropMenu.getIconAlign(); + if (ialign != null && ialign.equals("right")) { + rw.writeText(value + " ", null); + IconRenderer.encodeIcon(rw, dropMenu, icon, fa, dropMenu.getIconSize(), dropMenu.getIconRotate(), + dropMenu.getIconFlip(), dropMenu.isIconSpin(), null, null, false, false, false, false); } else { - rw.writeAttribute("href", "#", null); - } - rw.writeAttribute("role", "button", null); - if (!isFlyOutMenu) - rw.writeAttribute("data-toggle", "dropdown", null); - - // Encode value - String value = (String) dropMenu.getAttributes().get("value"); - String icon = dropMenu.getIcon(); - String faicon = dropMenu.getIconAwesome(); - boolean fa = false; // flag to indicate wether the selected icon - // set is - // Font Awesome or not. - if (faicon != null) { - icon = faicon; - fa = true; + IconRenderer.encodeIcon(rw, dropMenu, icon, fa, dropMenu.getIconSize(), dropMenu.getIconRotate(), + dropMenu.getIconFlip(), dropMenu.isIconSpin(), null, null, false, false, false, false); + // !//R.encodeIcon(rw, this, icon, white); + rw.writeText(" " + value, null); } - if (icon != null) { - Object ialign = dropMenu.getIconAlign(); - if (ialign != null && ialign.equals("right")) { - rw.writeText(value + " ", null); - IconRenderer.encodeIcon(rw, dropMenu, icon, fa, dropMenu.getIconSize(), dropMenu.getIconRotate(), dropMenu.getIconFlip(), dropMenu.isIconSpin(), null, null, false, false, false, false); - } else { - IconRenderer.encodeIcon(rw, dropMenu, icon, fa, dropMenu.getIconSize(), dropMenu.getIconRotate(), dropMenu.getIconFlip(), dropMenu.isIconSpin(), null, null, false, false, false, false); - // !//R.encodeIcon(rw, this, icon, white); - rw.writeText(" " + value, null); - } - } else { - rw.writeText(value, null); + } else { + rw.writeText(value, null); + } + // Encode Caret + if ((!isFlyOutMenu) && (!(dropMenu.getParent() instanceof DropMenu))) { + rw.startElement("b", dropMenu); + rw.writeAttribute("class", "caret", "class"); + rw.endElement("b"); + } + } + rw.endElement("a"); + + encodeDropMenuStart(dropMenu, rw, "dtL" + clientId); + } + + private String determineHtmlTag(UIComponent component, boolean isFlyOutMenu) { + String htmlTag = "span"; + if (isFlyOutMenu) { + htmlTag = "li"; + } else { + UIComponent parent = component.getParent(); + if (parent != null) { + if (parent.getClass().getSimpleName().equals("UIRepeat")) { + parent = parent.getParent(); } - // Encode Caret - if ((!isFlyOutMenu) && (!(dropMenu.getParent() instanceof DropMenu))) { - rw.startElement("b", dropMenu); - rw.writeAttribute("class", "caret", "class"); - rw.endElement("b"); + if (parent instanceof DropButton || parent instanceof NavBar || parent instanceof TabLinks + || parent instanceof PillLinks || parent instanceof ListLinks || parent instanceof NavBarLinks + || parent instanceof DropMenu || parent instanceof FlyOutMenu) { + htmlTag = "li"; } } - rw.endElement("a"); - - encodeDropMenuStart(dropMenu, rw, "dtL" + clientId); } - + return htmlTag; } private boolean isFlyOutMenu(UIComponent component) { @@ -143,8 +168,6 @@ private String getStyleClass(DropMenu dropMenu, boolean isFlyOutMenu) { String userClass = dropMenu.getStyleClass(); if (null == userClass) userClass = ""; - else - userClass += " "; userClass += Responsive.getResponsiveStyleClass(dropMenu, false); String direction = dropMenu.getDrop(); if (direction == null) { @@ -156,11 +179,10 @@ private String getStyleClass(DropMenu dropMenu, boolean isFlyOutMenu) { if (isFlyOutMenu) { userClass += "dropdown-submenu" + " "; return userClass; - } - else if (dropMenu.getParent() instanceof DropMenu) { + } else if (dropMenu.getParent() instanceof DropMenu) { userClass += "dropdown-submenu" + " "; } - return userClass + "drop" + direction; + return (userClass + "drop" + direction).trim(); } /** @@ -184,13 +206,11 @@ public static void encodeDropMenuStart(DropMenu c, ResponseWriter rw, String l) } /** - * This methods generates the HTML code of the current b:dropMenu. - * encodeBegin generates the start of the component. After the, - * the JSF framework calls encodeChildren() to generate the - * HTML code between the beginning and the end of the component. For - * instance, in the case of a panel component the content of the panel is - * generated by encodeChildren(). After that, - * encodeEnd() is called to generate the rest of the HTML code. + * This methods generates the HTML code of the current b:dropMenu. encodeBegin generates the start of + * the component. After the, the JSF framework calls encodeChildren() to generate the HTML code between + * the beginning and the end of the component. For instance, in the case of a panel component the content of the + * panel is generated by encodeChildren(). After that, encodeEnd() is called to generate + * the rest of the HTML code. * * @param context * the FacesContext. @@ -208,7 +228,9 @@ public void encodeEnd(FacesContext context, UIComponent component) throws IOExce ResponseWriter rw = context.getResponseWriter(); rw.endElement("ul"); - rw.endElement("li"); + boolean isFlyOutMenu = isFlyOutMenu(component); + String htmlTag = determineHtmlTag(component, isFlyOutMenu); + rw.endElement(htmlTag); Tooltip.activateTooltips(context, dropMenu); } diff --git a/src/main/java/net/bootsfaces/component/navLink/NavLinkRenderer.java b/src/main/java/net/bootsfaces/component/navLink/NavLinkRenderer.java index 2e9104ece..450e013ae 100644 --- a/src/main/java/net/bootsfaces/component/navLink/NavLinkRenderer.java +++ b/src/main/java/net/bootsfaces/component/navLink/NavLinkRenderer.java @@ -125,7 +125,7 @@ public void encodeHeader(FacesContext context, String h, UIComponent navlink) th styleClass = "dropdown-header " + styleClass + " " + Responsive.getResponsiveStyleClass((AbstractNavLink) navlink, false); - writeAttribute(rw, "class", styleClass, "class"); + writeAttribute(rw, "class", styleClass.trim(), "class"); writeAttribute(rw, "style", ((AbstractNavLink) navlink).getStyle(), "style"); writeAttribute(rw, "role", "presentation", null); rw.writeText(h, null); @@ -267,7 +267,7 @@ private String getStyleClasses(AbstractNavLink navlink) { c += " " + Responsive.getResponsiveStyleClass((AbstractNavLink) navlink, false); - return c; + return c.trim(); } private String encodeHref(FacesContext context, AbstractNavLink navlink) {