Skip to content

Commit

Permalink
Allow solitory DropMenus, and generate more efficient HTML code.
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanrauh committed Jul 4, 2016
1 parent 32bfc07 commit 3fe4f46
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 76 deletions.
170 changes: 96 additions & 74 deletions src/main/java/net/bootsfaces/component/dropMenu/DropMenuRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -38,13 +44,11 @@
public class DropMenuRenderer extends CoreRenderer {

/**
* This methods generates the HTML code of the current b:dropMenu.
* <code>encodeBegin</code> generates the start of the component. After the,
* the JSF framework calls <code>encodeChildren()</code> 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 <code>encodeChildren()</code>. After that,
* <code>encodeEnd()</code> is called to generate the rest of the HTML code.
* This methods generates the HTML code of the current b:dropMenu. <code>encodeBegin</code> generates the start of
* the component. After the, the JSF framework calls <code>encodeChildren()</code> 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 <code>encodeChildren()</code>. After that, <code>encodeEnd()</code> is called to generate
* the rest of the HTML code.
*
* @param context
* the FacesContext.
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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();
}

/**
Expand All @@ -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.
* <code>encodeBegin</code> generates the start of the component. After the,
* the JSF framework calls <code>encodeChildren()</code> 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 <code>encodeChildren()</code>. After that,
* <code>encodeEnd()</code> is called to generate the rest of the HTML code.
* This methods generates the HTML code of the current b:dropMenu. <code>encodeBegin</code> generates the start of
* the component. After the, the JSF framework calls <code>encodeChildren()</code> 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 <code>encodeChildren()</code>. After that, <code>encodeEnd()</code> is called to generate
* the rest of the HTML code.
*
* @param context
* the FacesContext.
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 3fe4f46

Please sign in to comment.