Skip to content

Commit

Permalink
fixed issue #31: tabView now remembers state after submit
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanrauh committed Dec 30, 2014
1 parent c8c497b commit c2a85ac
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
23 changes: 16 additions & 7 deletions src/net/bootsfaces/component/TabView.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;

import net.bootsfaces.render.A;
import net.bootsfaces.render.R;

/**
Expand Down Expand Up @@ -124,7 +125,9 @@ public void encodeBegin(FacesContext context) throws IOException {
String clientId = getClientId(context);
writer.startElement("input", this);
writer.writeAttribute("type", "hidden", null);
writer.writeAttribute("name", clientId + "_activeIndex", "name");
final String hiddenInputFieldID = clientId + "_activeIndex";
writer.writeAttribute("name", hiddenInputFieldID, "name");
writer.writeAttribute("id", hiddenInputFieldID, "id");
writer.writeAttribute("value", determineActiveIndex(attributes, currentlyActiveIndex), "value");
writer.endElement("input");

Expand All @@ -145,7 +148,7 @@ public void encodeBegin(FacesContext context) throws IOException {

writer.writeAttribute(ROLE, role, ROLE);

encodeTabs(context, writer, getChildren(), attributes, currentlyActiveIndex);
encodeTabs(context, writer, getChildren(), attributes, currentlyActiveIndex, hiddenInputFieldID);
writer.endElement("ul");
encodeTabContentPanes(context, writer, this, attributes, currentlyActiveIndex);
}
Expand Down Expand Up @@ -255,12 +258,12 @@ private static void encodeTabPane(FacesContext context, ResponseWriter writer, U
* @throws IOException
* only thrown if something's wrong with the response writer
*/
private static void encodeTabs(FacesContext context, ResponseWriter writer, List<UIComponent> children, Map<String, Object> attributes, int currentlyActiveIndex)
private static void encodeTabs(FacesContext context, ResponseWriter writer, List<UIComponent> children, Map<String, Object> attributes, int currentlyActiveIndex, String hiddenInputFieldID)
throws IOException {
if (null != children) {
int activeIndex = determineActiveIndex(attributes,currentlyActiveIndex);
for (int index = 0; index < children.size(); index++) {
encodeTab(context, writer, children.get(index), index == activeIndex);
encodeTab(context, writer, children.get(index), index == activeIndex, hiddenInputFieldID, index);
}
}
}
Expand All @@ -280,7 +283,7 @@ private static void encodeTabs(FacesContext context, ResponseWriter writer, List
* @throws IOException
* only thrown if something's wrong with the response writer
*/
private static void encodeTab(FacesContext context, ResponseWriter writer, UIComponent tab, boolean isActive) throws IOException {
private static void encodeTab(FacesContext context, ResponseWriter writer, UIComponent tab, boolean isActive, String hiddenInputFieldID, int tabIndex) throws IOException {
writer.startElement(LI, tab);
writer.writeAttribute(ROLE, "presentation", ROLE);
Map<String, Object> tabAttributes = tab.getAttributes();
Expand All @@ -291,7 +294,7 @@ private static void encodeTab(FacesContext context, ResponseWriter writer, UICom
}
if (classes.length()>0)
writer.writeAttribute(CLASS, classes, CLASS);
encodeTabAnchorTag(writer, tab, tabAttributes);
encodeTabAnchorTag(writer, tab, tabAttributes, hiddenInputFieldID, tabIndex);
writer.endElement(LI);
}

Expand All @@ -307,11 +310,17 @@ private static void encodeTab(FacesContext context, ResponseWriter writer, UICom
* @throws IOException
* only thrown if something's wrong with the response writer
*/
private static void encodeTabAnchorTag(ResponseWriter writer, UIComponent tab, Map<String, Object> tabAttributes) throws IOException {
private static void encodeTabAnchorTag(ResponseWriter writer, UIComponent tab, Map<String, Object> tabAttributes, String hiddenInputFieldID, int tabindex) throws IOException {
writer.startElement(A, tab);
writer.writeAttribute(ROLE, "tab", ROLE);
writer.writeAttribute("data-toggle", "tab", "data-toggle");
writer.writeAttribute(HREF, "#" + tab.getClientId().replace(":", "_"), HREF);
String onclick="document.getElementById('"+hiddenInputFieldID+"').value='"+String.valueOf(tabindex)+"';";
String userClick=(String)tabAttributes.get("onclick");
if (null != userClick && userClick.trim().length()>0) {
onclick+=userClick;
}
writer.writeAttribute("onclick", onclick, "onclick");
R.encodeHTML4DHTMLAttrs(writer, tabAttributes, TAB_ATTRS);
writer.writeText(tabAttributes.get("title"), null);
writer.endElement(A);
Expand Down
2 changes: 1 addition & 1 deletion src/net/bootsfaces/render/A.java
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public static String asString(Map<String, Object> attrs, Object o) {
public static final String[] ALLBUTTON_ATTRS = A.concatAll( H.ALLBUTTON, E.DBLCLICK, E.FOCUS, E.MOUSE);
public static final String[] CHECKBOX_ATTRS = A.concatAll( H.CHECKBOX, E.CLICK, E.FOCUS, E.MOUSE);
public static final String[] INPUT_TEXT_ATTRS = A.concatAll( H.INPUT_TEXT, E.CLICK, E.FOCUS, E.MOUSE);
public static final String[] TAB_ATTRS = A.concatAll( H.TAB, E.CLICK, E.FOCUS, E.MOUSE);
public static final String[] TAB_ATTRS = A.concatAll( H.TAB, new String[]{"ondblclick"}, E.FOCUS, E.MOUSE);
public static final String[] TAB_VIEW_ATTRS = A.concatAll( H.TAB_VIEW, E.CLICK, E.FOCUS, E.MOUSE);
public static final String[] SELECT_ONE_MENU_ATTRS = A.concatAll( H.SELECT_ONE_MENU, E.CLICK, E.FOCUS, E.MOUSE);

Expand Down

0 comments on commit c2a85ac

Please sign in to comment.