Skip to content

Instantly share code, notes, and snippets.

@lfryc
Created June 22, 2012 13:43
Show Gist options
  • Select an option

  • Save lfryc/2972803 to your computer and use it in GitHub Desktop.

Select an option

Save lfryc/2972803 to your computer and use it in GitHub Desktop.
<b:commandButton value="Launch Modal" onclick="#{b:show('modal')}" />
<b:modal id="modal">
<f:facet name="header">
<h3>Facet Header</h3>
</f:facet>
<f:facet name="footer">
<b:commandButton value="Close" onclick="#{b:hide('modal')}" />
<b:commandButton value="Save Changes" severity="primary" onclick="#{b:hide('modal')}" />
</f:facet>
<p>One can define body...</p>
</b:modal>
package org.richfaces.bootstrap.function;
import java.text.MessageFormat;
import javax.faces.component.UIComponent;
import org.ajax4jsf.javascript.ScriptUtils;
import org.richfaces.bootstrap.component.Toggleable;
import org.richfaces.bootstrap.javascript.BootstrapJSPlugin;
import org.richfaces.cdk.annotations.Function;
import org.richfaces.function.RichFunction;
/**
* @author Lukas Fryc
*/
public final class BootstrapFunction {
@Function
public static String toggle(String target) {
return bootstrapCall(target, Toggleable.class, "toggle");
}
@Function
public static String hide(String target) {
return bootstrapCall(target, Hideable.class, "hide");
}
@Function
public static String show(String target) {
return bootstrapCall(target, Showable.class, "show");
}
private static String bootstrapCall(String target, Class<?> type, String operationName) {
UIComponent component = RichFunction.findComponent(target);
BootstrapOperation operation = new BootstrapOperation(component, operationName);
operation.verifyComponent(type);
String call = operation.getCall();
return call;
}
private static class BootstrapOperation {
private static String CALL = "$('#{0}').{1}('{2}');";
private static String ESCAPED_CALL = CALL.replace("'", "''");
private UIComponent component;
private String operation;
public BootstrapOperation(UIComponent component, String operation) {
this.component = component;
this.operation = operation;
}
public String getCall() {
String clientId = component.getClientId();
String pluginName = getPluginName();
String escapedClientId = ScriptUtils.escapeCSSMetachars(clientId);
return MessageFormat.format(ESCAPED_CALL, escapedClientId, pluginName, operation);
}
private void verifyComponent(Class<?> type) {
if (component == null) {
throw new IllegalArgumentException("no such target component with identified was found");
}
if (component.getClass().isInstance(type)) {
// TODO unified API for reporting meaningful exceptions (componentID, etc.)
throw new IllegalArgumentException("the target component " + component + " is not " + type.getName());
}
}
private String getPluginName() {
BootstrapJSPlugin annotation = component.getClass().getSuperclass().getAnnotation(BootstrapJSPlugin.class);
if (annotation == null) {
throw new IllegalArgumentException("given component does not have Bootstrap JavaScript plugin binding defined");
}
return annotation.value();
}
}
}
@pauldijou

Copy link
Copy Markdown
  • "String escapedClientId = ScriptUtils.escapeCSSMetachars(clientId);" => you need to escape twice to make it works
  • ... or you can use RichFunction.jQuerySelector(target)
  • ... or remove "$('#{0}')" and replace it with "RichFunction.jQuery(target)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment