use of jakarta.faces.component.behavior.ClientBehaviorContext in project mojarra by eclipse-ee4j.
the class AjaxBehaviorRenderer method buildAjaxCommand.
private static String buildAjaxCommand(ClientBehaviorContext behaviorContext, AjaxBehavior ajaxBehavior) {
// First things first - if AjaxBehavior is disabled, we are done.
if (ajaxBehavior.isDisabled()) {
return null;
}
UIComponent component = behaviorContext.getComponent();
String eventName = behaviorContext.getEventName();
StringBuilder ajaxCommand = new StringBuilder(256);
Collection<String> execute = ajaxBehavior.getExecute();
Collection<String> render = ajaxBehavior.getRender();
String onevent = ajaxBehavior.getOnevent();
String onerror = ajaxBehavior.getOnerror();
String sourceId = behaviorContext.getSourceId();
String delay = ajaxBehavior.getDelay();
Boolean resetValues = null;
if (ajaxBehavior.isResetValuesSet()) {
resetValues = ajaxBehavior.isResetValues();
}
Collection<ClientBehaviorContext.Parameter> params = behaviorContext.getParameters();
// Needed workaround for SelectManyCheckbox - if execute doesn't have sourceId,
// we need to add it - otherwise, we use the default, which is sourceId:child, which
// won't work.
ClientBehaviorContext.Parameter foundparam = null;
for (ClientBehaviorContext.Parameter param : params) {
if (param.getName().equals("incExec") && (Boolean) param.getValue()) {
foundparam = param;
}
}
if (foundparam != null && !execute.contains(sourceId)) {
execute = new LinkedList<>(execute);
execute.add(component.getClientId());
}
if (foundparam != null) {
try {
// And since this is a hack, we now try to remove the param
params.remove(foundparam);
} catch (UnsupportedOperationException uoe) {
if (logger.isLoggable(Level.FINEST)) {
logger.log(Level.FINEST, "Unsupported operation", uoe);
}
}
}
HtmlCommandScript commandScript = component instanceof HtmlCommandScript ? (HtmlCommandScript) component : null;
if (commandScript != null) {
String name = commandScript.getName();
if (!name.contains(".")) {
ajaxCommand.append("var ");
}
ajaxCommand.append(name).append('=').append("function(o){var o=(typeof o==='object')&&o?o:{};");
for (ClientBehaviorContext.Parameter param : params) {
ajaxCommand.append("o[");
RenderKitUtils.appendQuotedValue(ajaxCommand, param.getName());
ajaxCommand.append("]=");
Object paramValue = param.getValue();
if (paramValue == null) {
ajaxCommand.append("null");
} else {
RenderKitUtils.appendQuotedValue(ajaxCommand, paramValue.toString());
}
ajaxCommand.append(";");
}
params = Collections.singleton(new ClientBehaviorContext.Parameter("o", null));
}
ajaxCommand.append("mojarra.ab(");
if (sourceId == null) {
ajaxCommand.append("this");
} else {
ajaxCommand.append("'");
ajaxCommand.append(sourceId);
ajaxCommand.append("'");
}
ajaxCommand.append(",");
ajaxCommand.append(commandScript == null ? "event" : "null");
ajaxCommand.append(",'");
ajaxCommand.append(eventName);
ajaxCommand.append("',");
appendIds(behaviorContext.getFacesContext(), component, ajaxCommand, execute);
ajaxCommand.append(",");
appendIds(behaviorContext.getFacesContext(), component, ajaxCommand, render);
if (onevent != null || onerror != null || delay != null || resetValues != null || !params.isEmpty()) {
ajaxCommand.append(",{");
if (onevent != null) {
RenderKitUtils.appendProperty(ajaxCommand, "onevent", onevent, false);
}
if (onerror != null) {
RenderKitUtils.appendProperty(ajaxCommand, "onerror", onerror, false);
}
if (delay != null) {
RenderKitUtils.appendProperty(ajaxCommand, "delay", delay, true);
}
if (resetValues != null) {
RenderKitUtils.appendProperty(ajaxCommand, "resetValues", resetValues, false);
}
if (!params.isEmpty()) {
if (commandScript != null) {
RenderKitUtils.appendProperty(ajaxCommand, "params", params.iterator().next().getName(), false);
} else {
RenderKitUtils.appendProperty(ajaxCommand, "params", "{", false);
for (ClientBehaviorContext.Parameter param : params) {
RenderKitUtils.appendProperty(ajaxCommand, param.getName(), param.getValue());
}
ajaxCommand.append("}");
}
}
ajaxCommand.append("}");
}
ajaxCommand.append(")");
if (commandScript != null) {
ajaxCommand.append("}");
if (commandScript.isAutorun()) {
ajaxCommand.append(";mojarra.l(").append(commandScript.getName()).append(")");
}
}
return ajaxCommand.toString();
}
use of jakarta.faces.component.behavior.ClientBehaviorContext in project faces by jakartaee.
the class TestServlet method createClientBehaviorContextNPETest.
// --------------------------------------- test methods
// createClientBehaviorContext() throws NullPointerException
public void createClientBehaviorContextNPETest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
ClientBehaviorContext cbc = new TCKClientBehaviorContext();
HashSet<ClientBehaviorContext.Parameter> params = new HashSet<ClientBehaviorContext.Parameter>();
ClientBehaviorContext.Parameter paramOne = new ClientBehaviorContext.Parameter("one", "alpha");
params.add(paramOne);
// context as null
JSFTestUtil.checkForNPE(cbc, "createClientBehaviorContext", new Class<?>[] { FacesContext.class, UIComponent.class, String.class, String.class, Collection.class }, new Object[] { null, new UIInput(), "abc", "xyz", params }, out);
// component as null
JSFTestUtil.checkForNPE(cbc, "createClientBehaviorContext", new Class<?>[] { FacesContext.class, UIComponent.class, String.class, String.class, Collection.class }, new Object[] { getFacesContext(), null, "abc", "xyz", params }, out);
// eventName as null
JSFTestUtil.checkForNPE(cbc, "createClientBehaviorContext", new Class<?>[] { FacesContext.class, UIComponent.class, String.class, String.class, Collection.class }, new Object[] { getFacesContext(), new UIInput(), null, "xyz", params }, out);
}
use of jakarta.faces.component.behavior.ClientBehaviorContext in project myfaces by apache.
the class ClientBehaviorRendererUtils method getClientBehaviorScript.
/**
* builds the chained behavior script which then can be reused
* in following order by the other script building parts
* <p/>
* user defined event handling script
* behavior script
* renderer default script
*
* @param eventName event name ("onclick" etc...)
* @param config the {@link MyfacesConfig}
* @param uiComponent the component which has the attachement (or should have)
* @param facesContext the facesContext
* @param params params map of params which have to be dragged into the request
* @return a string representation of the javascripts for the attached event behavior,
* an empty string if none is present
*/
private static boolean getClientBehaviorScript(FacesContext facesContext, MyfacesConfig config, UIComponent uiComponent, String sourceId, String eventName, Map<String, List<ClientBehavior>> clientBehaviors, JavascriptContext target, Collection<ClientBehaviorContext.Parameter> params) {
if (!(uiComponent instanceof ClientBehaviorHolder)) {
target.append(RendererUtils.EMPTY_STRING);
return false;
}
boolean renderClientBehavior = clientBehaviors != null && clientBehaviors.size() > 0;
if (!renderClientBehavior) {
target.append(RendererUtils.EMPTY_STRING);
return false;
}
List<ClientBehavior> attachedEventBehaviors = clientBehaviors.get(eventName);
if (attachedEventBehaviors == null || attachedEventBehaviors.isEmpty()) {
target.append(RendererUtils.EMPTY_STRING);
return false;
}
ClientBehaviorContext context = ClientBehaviorContext.createClientBehaviorContext(facesContext, uiComponent, eventName, sourceId, params);
boolean submitting = false;
// jakarta.faces.component.UIComponentBase.addClientBehavior
if (attachedEventBehaviors instanceof RandomAccess) {
for (int i = 0, size = attachedEventBehaviors.size(); i < size; i++) {
ClientBehavior clientBehavior = attachedEventBehaviors.get(i);
submitting = appendClientBehaviourScript(facesContext, target, context, submitting, i < (size - 1), clientBehavior, config);
}
} else {
Iterator<ClientBehavior> clientIterator = attachedEventBehaviors.iterator();
while (clientIterator.hasNext()) {
ClientBehavior clientBehavior = clientIterator.next();
submitting = appendClientBehaviourScript(facesContext, target, context, submitting, clientIterator.hasNext(), clientBehavior, config);
}
}
return submitting;
}
use of jakarta.faces.component.behavior.ClientBehaviorContext in project myfaces by apache.
the class ClientBehaviorRendererUtilsTest method testBuildBehaviorChain.
public void testBuildBehaviorChain() {
Map<String, List<ClientBehavior>> behaviors = new HashMap<String, List<ClientBehavior>>();
// Map<String, String> params = new HashMap<String, String>();
Collection<ClientBehaviorContext.Parameter> params = new ArrayList<ClientBehaviorContext.Parameter>();
UIComponent component = new HtmlInputText();
Assert.assertEquals("", ClientBehaviorRendererUtils.buildBehaviorChain(facesContext, component, component.getClientId(facesContext), ClientBehaviorEvents.CLICK, params, ClientBehaviorEvents.ACTION, params, behaviors, null, null));
Assert.assertEquals("return faces.util.chain(document.getElementById('j_id__v_0'), event,'huhn', 'suppe');", ClientBehaviorRendererUtils.buildBehaviorChain(facesContext, component, component.getClientId(facesContext), ClientBehaviorEvents.CLICK, params, ClientBehaviorEvents.ACTION, params, behaviors, "huhn", "suppe"));
ClientBehavior submittingBehavior = new ClientBehaviorBase() {
@Override
public String getScript(ClientBehaviorContext behaviorContext) {
return "script()";
}
@Override
public Set<ClientBehaviorHint> getHints() {
return EnumSet.allOf(ClientBehaviorHint.class);
}
};
behaviors.put(ClientBehaviorEvents.CLICK, Arrays.asList(submittingBehavior));
Assert.assertEquals("faces.util.chain(document.getElementById('j_id__v_0'), event,'huhn', 'script()', 'suppe'); return false;", ClientBehaviorRendererUtils.buildBehaviorChain(facesContext, component, component.getClientId(facesContext), ClientBehaviorEvents.CLICK, params, ClientBehaviorEvents.ACTION, params, behaviors, "huhn", "suppe"));
}
use of jakarta.faces.component.behavior.ClientBehaviorContext in project myfaces by apache.
the class ClientBehaviorRendererUtilsTest method testBuildBehaviorChain2.
public void testBuildBehaviorChain2() {
Map<String, List<ClientBehavior>> behaviors = new HashMap<String, List<ClientBehavior>>();
// Map<String, String> params = new HashMap<String, String>();
Collection<ClientBehaviorContext.Parameter> params = new ArrayList<ClientBehaviorContext.Parameter>();
UIComponent component = new HtmlInputText();
Assert.assertEquals("", ClientBehaviorRendererUtils.buildBehaviorChain(facesContext, component, ClientBehaviorEvents.CLICK, params, ClientBehaviorEvents.ACTION, params, behaviors, null, null));
Assert.assertEquals("return faces.util.chain(this, event,'huhn', 'suppe');", ClientBehaviorRendererUtils.buildBehaviorChain(facesContext, component, ClientBehaviorEvents.CLICK, params, ClientBehaviorEvents.ACTION, params, behaviors, "huhn", "suppe"));
ClientBehavior submittingBehavior = new ClientBehaviorBase() {
@Override
public String getScript(ClientBehaviorContext behaviorContext) {
return "script()";
}
@Override
public Set<ClientBehaviorHint> getHints() {
return EnumSet.allOf(ClientBehaviorHint.class);
}
};
behaviors.put(ClientBehaviorEvents.CLICK, Arrays.asList(submittingBehavior));
Assert.assertEquals("faces.util.chain(this, event,'huhn', 'script()', 'suppe'); return false;", ClientBehaviorRendererUtils.buildBehaviorChain(facesContext, component, ClientBehaviorEvents.CLICK, params, ClientBehaviorEvents.ACTION, params, behaviors, "huhn", "suppe"));
}
Aggregations