Skip to content

Commit

Permalink
#413 add the jQuery events to b:tabView and make them AJAX-aware
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanrauh committed Jun 15, 2016
1 parent 31e9499 commit ef7691b
Show file tree
Hide file tree
Showing 7 changed files with 343 additions and 27 deletions.
8 changes: 4 additions & 4 deletions src/main/java/net/bootsfaces/component/ajax/AJAXRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -497,12 +497,12 @@ private static String encodeClick(IAJAXComponent component) {
* @param context
* @param component
* @param rw
* @param clientId
* @param jQueryExpressionOfTargetElement
* @param additionalEventHandlers
* @throws IOException
*/
public void generateBootsFacesAJAXAndJavaScriptForJQuery(FacesContext context, UIComponent component,
ResponseWriter rw, String clientId, Map<String, String> additionalEventHandlers) throws IOException {
ResponseWriter rw, String jQueryExpressionOfTargetElement, Map<String, String> additionalEventHandlers) throws IOException {
IAJAXComponent ajaxComponent = (IAJAXComponent) component;
Set<String> events = ajaxComponent.getJQueryEvents().keySet();
for (String event : events) {
Expand All @@ -515,8 +515,8 @@ public void generateBootsFacesAJAXAndJavaScriptForJQuery(FacesContext context, U
additionalEventHandler, true, event, code);
if (code.length() > 0) {
rw.startElement("script", component);
String js = "$('#" + clientId + "').on('" + ajaxComponent.getJQueryEvents().get(event)
+ "', function(){" + code.toString() + "});";
String js = "$('" + jQueryExpressionOfTargetElement + "').on('" + ajaxComponent.getJQueryEvents().get(event)
+ "', function(e){" + code.toString() + "});";
rw.writeText(js, null);
rw.endElement("script");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public void encodeEnd(FacesContext context, UIComponent component) throws IOExce
}

rw.endElement("div");
new AJAXRenderer().generateBootsFacesAJAXAndJavaScriptForJQuery(context, component, rw, clientId, null);
new AJAXRenderer().generateBootsFacesAJAXAndJavaScriptForJQuery(context, component, rw, "#"+clientId, null);

Tooltip.activateTooltips(context, carousel);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ public void encodeEnd(FacesContext context, UIComponent component) throws IOExce
+ "').value='false';");
eventHandlers.put("collapse", "document.getElementById('" + hiddenInputFieldID
+ "').value='true';");
new AJAXRenderer().generateBootsFacesAJAXAndJavaScriptForJQuery(context, component, rw, jQueryClientID+"content", eventHandlers);
new AJAXRenderer().generateBootsFacesAJAXAndJavaScriptForJQuery(context, component, rw, "#"+jQueryClientID+"content", eventHandlers);
}
Tooltip.activateTooltips(context, panel);
}
Expand Down
73 changes: 72 additions & 1 deletion src/main/java/net/bootsfaces/component/tabView/TabView.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ public void setValueExpression(String name, ValueExpression binding) {
*/
public Map<String, String> getJQueryEvents() {
Map<String, String> result = new HashMap<String, String>();
result.put("click", "click");
result.put("show", "shown.bs.tab");
result.put("shown", "shown.bs.tab");
result.put("hide", "hide.bs.tab");
result.put("hidden", "hidden.bs.tab");
return result;
}

Expand Down Expand Up @@ -112,6 +115,10 @@ protected enum PropertyKeys {
immediate,
onclick,
oncomplete,
onhidden,
onhide,
onshow,
onshown,
pills,
process,
role,
Expand Down Expand Up @@ -282,6 +289,70 @@ public void setOncomplete(String _oncomplete) {
getStateHelper().put(PropertyKeys.oncomplete, _oncomplete);
}

/**
* JavaScript and/or AJAX code to be executed just after pushing a tab to the back <P>
* @return Returns the value of the attribute, or null, if it hasn't been set by the JSF file.
*/
public String getOnhidden() {
return (String) getStateHelper().eval(PropertyKeys.onhidden);
}

/**
* JavaScript and/or AJAX code to be executed just after pushing a tab to the back <P>
* Usually this method is called internally by the JSF engine.
*/
public void setOnhidden(String _onhidden) {
getStateHelper().put(PropertyKeys.onhidden, _onhidden);
}

/**
* JavaScript and/or AJAX code to be executed just before pushing a tab to the back <P>
* @return Returns the value of the attribute, or null, if it hasn't been set by the JSF file.
*/
public String getOnhide() {
return (String) getStateHelper().eval(PropertyKeys.onhide);
}

/**
* JavaScript and/or AJAX code to be executed just before pushing a tab to the back <P>
* Usually this method is called internally by the JSF engine.
*/
public void setOnhide(String _onhide) {
getStateHelper().put(PropertyKeys.onhide, _onhide);
}

/**
* JavaScript and/or AJAX code to be executed just before bringing a tab to the front <P>
* @return Returns the value of the attribute, or null, if it hasn't been set by the JSF file.
*/
public String getOnshow() {
return (String) getStateHelper().eval(PropertyKeys.onshow);
}

/**
* JavaScript and/or AJAX code to be executed just before bringing a tab to the front <P>
* Usually this method is called internally by the JSF engine.
*/
public void setOnshow(String _onshow) {
getStateHelper().put(PropertyKeys.onshow, _onshow);
}

/**
* JavaScript and/or AJAX code to be executed just after bringing a tab to the front <P>
* @return Returns the value of the attribute, or null, if it hasn't been set by the JSF file.
*/
public String getOnshown() {
return (String) getStateHelper().eval(PropertyKeys.onshown);
}

/**
* JavaScript and/or AJAX code to be executed just after bringing a tab to the front <P>
* Usually this method is called internally by the JSF engine.
*/
public void setOnshown(String _onshown) {
getStateHelper().put(PropertyKeys.onshown, _onshown);
}

/**
* Change the rendering of tab to pills mode. Default false <P>
* @return Returns the value of the attribute, or null, if it hasn't been set by the JSF file.
Expand Down
41 changes: 21 additions & 20 deletions src/main/java/net/bootsfaces/component/tabView/TabViewRenderer.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* Copyright 2014-16 by Riccardo Massera (TheCoder4.Eu) and Stephan Rauh (http://www.beyondjava.net).
*
*
* This file is part of BootsFaces.
*
*
* BootsFaces is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
Expand Down Expand Up @@ -38,7 +38,7 @@
/** This class generates the HTML code of &lt;b:tabView /&gt;. */
@FacesRenderer(componentFamily = "net.bootsfaces.component", rendererType = "net.bootsfaces.component.tabView.TabView")
public class TabViewRenderer extends CoreRenderer {

@Override
public void encodeChildren(FacesContext context, UIComponent component) throws IOException {
// TODO Auto-generated method stub
Expand All @@ -52,7 +52,7 @@ public void encodeChildren(FacesContext context, UIComponent component) throws I
* default implementation simply stores the input value in the list of
* submitted values. If the validation checks are passed, the values in the
* <code>submittedValues</code> list are store in the backend bean.
*
*
* @param context
* the FacesContext.
* @param component
Expand Down Expand Up @@ -82,7 +82,7 @@ public void decode(FacesContext context, UIComponent component) {

/**
* This methods generates the HTML code of the current b:tabView.
*
*
* @param context
* the FacesContext.
* @param component
Expand Down Expand Up @@ -112,14 +112,14 @@ public void encodeBegin(FacesContext context, UIComponent component) throws IOEx
final String tabPosition = tabView.getTabPosition();
String wrapperClass = "tab-panel";
if("bottom".equalsIgnoreCase(tabPosition)) wrapperClass += " tabs-below";

writer.startElement("div", tabView);
writer.writeAttribute("class", wrapperClass, "class");
writer.writeAttribute("role", "tabpanel", "class");
writer.writeAttribute("dir", tabView.getDir(), "dir");

final List<UIComponent> tabs = getTabs(tabView);

if("bottom".equalsIgnoreCase(tabPosition)) {
encodeTabContentPanes(context, writer, tabView, tabView.getActiveIndex(), tabs);
encodeTabLinks(context, writer, tabView, tabView.getActiveIndex(), tabs, clientId, hiddenInputFieldID);
Expand Down Expand Up @@ -147,27 +147,28 @@ public void encodeBegin(FacesContext context, UIComponent component) throws IOEx
encodeTabLinks(context, writer, tabView, tabView.getActiveIndex(), tabs, clientId, hiddenInputFieldID);
encodeTabContentPanes(context, writer, tabView, tabView.getActiveIndex(), tabs);
}

writer.endElement("div");
new AJAXRenderer().generateBootsFacesAJAXAndJavaScriptForJQuery(context, component, writer, "#" + clientId + " > li > a[data-toggle=\"tab\"]", null);
Tooltip.activateTooltips(context, tabView);
}

/**
* Draw a clear div
* @param writer
* @param tabView
* @throws IOException
*/
private static void drawClearDiv(ResponseWriter writer, UIComponent tabView)
private static void drawClearDiv(ResponseWriter writer, UIComponent tabView)
throws IOException {
writer.startElement("div", tabView);
writer.writeAttribute("style", "clear:both;", "style");
writer.endElement("div");
}

/**
* Encode the list of links that render the tabs
*
*
* @param context
* @param writer
* @param tabView
Expand All @@ -178,7 +179,7 @@ private static void drawClearDiv(ResponseWriter writer, UIComponent tabView)
* @throws IOException
*/
private static void encodeTabLinks(FacesContext context, ResponseWriter writer, TabView tabView,
int currentlyActiveIndex, List<UIComponent> tabs, String clientId, String hiddenInputFieldID)
int currentlyActiveIndex, List<UIComponent> tabs, String clientId, String hiddenInputFieldID)
throws IOException {
writer.startElement("ul", tabView);
writer.writeAttribute("id", clientId, "id");
Expand All @@ -189,7 +190,7 @@ private static void encodeTabLinks(FacesContext context, ResponseWriter writer,
} else {
classes = classes + (tabView.isPills() ? " nav-pills" : " nav-tabs");
}

if (tabView.getStyleClass() != null) {
classes += " ";
classes += tabView.getStyleClass();
Expand All @@ -214,7 +215,7 @@ private static void encodeTabLinks(FacesContext context, ResponseWriter writer,
* Essentially, getTabs() does the same as getChildren(), but it filters
* everything that's not a tab. In particular, comments are ignored. See
* issue 77 (https://github.com/TheCoder4eu/BootsFaces-OSP/issues/77).
*
*
* @return
*/
private List<UIComponent> getTabs(TabView tabView) {
Expand All @@ -229,7 +230,7 @@ private List<UIComponent> getTabs(TabView tabView) {

/**
* Generates the HTML of the tab panes.
*
*
* @param context
* the current FacesContext
* @param writer
Expand Down Expand Up @@ -315,7 +316,7 @@ private static void encodeTabPane(FacesContext context, ResponseWriter writer, U

/**
* Generates the HTML of the tabs.
*
*
* @param context
* the current FacesContext
* @param writer
Expand Down Expand Up @@ -363,7 +364,7 @@ private static void encodeTab(FacesContext context, ResponseWriter writer, UICom
writer.writeAttribute("role", "presentation", "role");

Tooltip.generateTooltip(context, tab, writer);

String classes = isActive ? "active" : "";
if (tab.getStyleClass() != null) {
classes += " ";
Expand All @@ -372,7 +373,7 @@ private static void encodeTab(FacesContext context, ResponseWriter writer, UICom
if (classes.length() > 0) {
writer.writeAttribute("class", classes, "class");
}

if (tab.getStyle() != null) {
writer.writeAttribute("style", tab.getStyle(), "style");
}
Expand Down
Loading

0 comments on commit ef7691b

Please sign in to comment.