From c2a85acb9ed241704720cbb36fdaf07e145bdd70 Mon Sep 17 00:00:00 2001 From: stephanrauh Date: Tue, 30 Dec 2014 22:35:08 +0100 Subject: [PATCH] fixed issue #31: tabView now remembers state after submit --- src/net/bootsfaces/component/TabView.java | 23 ++++++++++++++++------- src/net/bootsfaces/render/A.java | 2 +- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/net/bootsfaces/component/TabView.java b/src/net/bootsfaces/component/TabView.java index c6d0b990d..975b8e4ac 100644 --- a/src/net/bootsfaces/component/TabView.java +++ b/src/net/bootsfaces/component/TabView.java @@ -47,6 +47,7 @@ import javax.faces.context.FacesContext; import javax.faces.context.ResponseWriter; +import net.bootsfaces.render.A; import net.bootsfaces.render.R; /** @@ -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"); @@ -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); } @@ -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 children, Map attributes, int currentlyActiveIndex) + private static void encodeTabs(FacesContext context, ResponseWriter writer, List children, Map 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); } } } @@ -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 tabAttributes = tab.getAttributes(); @@ -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); } @@ -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 tabAttributes) throws IOException { + private static void encodeTabAnchorTag(ResponseWriter writer, UIComponent tab, Map 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); diff --git a/src/net/bootsfaces/render/A.java b/src/net/bootsfaces/render/A.java index ea9729120..c6351be3e 100644 --- a/src/net/bootsfaces/render/A.java +++ b/src/net/bootsfaces/render/A.java @@ -195,7 +195,7 @@ public static String asString(Map 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);