Search in sources :

Example 1 with HtmlCommandScript

use of jakarta.faces.component.html.HtmlCommandScript in project myfaces by apache.

the class HtmlCommandScriptRenderer method encodeBegin.

@Override
public void encodeBegin(FacesContext context, UIComponent component) throws IOException {
    super.encodeBegin(context, component);
    HtmlCommandScript commandScript = (HtmlCommandScript) component;
    ResponseWriter writer = context.getResponseWriter();
    ResourceUtils.renderDefaultJsfJsInlineIfNecessary(context, writer);
    writer.startElement(HTML.SPAN_ELEM, component);
    writer.writeAttribute(HTML.ID_ATTR, component.getClientId(context), null);
    writer.startElement(HTML.SCRIPT_ELEM, component);
    HtmlRendererUtils.renderScriptType(context, writer);
    JavascriptContext script = new JavascriptContext();
    // Write content
    String cmdName = commandScript.getName();
    String name;
    if (cmdName != null && cmdName.length() > 0) {
        name = JavascriptUtils.getValidJavascriptName(cmdName, true);
    } else {
        name = JavascriptUtils.getValidJavascriptName(component.getClientId(context), true);
    }
    script.prettyLine();
    script.increaseIndent();
    script.append("var " + name + " = function(o){var o=(typeof o==='object')&&o?o:{};");
    script.prettyLine();
    List<UIParameter> uiParams = HtmlRendererUtils.getValidUIParameterChildren(context, getChildren(commandScript), false, false);
    StringBuilder ajax = SharedStringBuilder.get(context, AJAX_SB, 60);
    AjaxScriptBuilder.build(context, ajax, commandScript, commandScript.getClientId(context), "action", commandScript.getExecute(), commandScript.getRender(), commandScript.getResetValues(), commandScript.getOnerror(), commandScript.getOnevent(), uiParams);
    script.append(ajax.toString());
    script.decreaseIndent();
    script.append("}");
    if (commandScript.isAutorun()) {
        script.append(";");
        script.append("myfaces._impl.core._Runtime.addOnLoad(window,");
        script.append(name);
        script.append(");");
    }
    writer.writeText(script.toString(), null);
}
Also used : UIParameter(jakarta.faces.component.UIParameter) SharedStringBuilder(org.apache.myfaces.core.api.shared.lang.SharedStringBuilder) ResponseWriter(jakarta.faces.context.ResponseWriter) HtmlCommandScript(jakarta.faces.component.html.HtmlCommandScript) JavascriptContext(org.apache.myfaces.renderkit.html.util.JavascriptContext)

Example 2 with HtmlCommandScript

use of jakarta.faces.component.html.HtmlCommandScript 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();
}
Also used : UIComponent(jakarta.faces.component.UIComponent) ClientBehaviorContext(jakarta.faces.component.behavior.ClientBehaviorContext) HtmlCommandScript(jakarta.faces.component.html.HtmlCommandScript)

Example 3 with HtmlCommandScript

use of jakarta.faces.component.html.HtmlCommandScript in project myfaces by apache.

the class AjaxScriptBuilder method build.

public static void build(FacesContext context, StringBuilder sb, UIComponent component, String sourceId, String eventName, String execute, String render, String delay, String resetValues, String onerror, String onevent, Collection<ClientBehaviorContext.Parameter> params, List<UIParameter> uiParams) {
    // CHECKSTYLE:ON
    HtmlCommandScript commandScript = (component instanceof HtmlCommandScript) ? (HtmlCommandScript) component : null;
    sb.append("myfaces.ab(");
    if (sourceId == null) {
        sb.append("this");
    } else {
        sb.append('\'');
        sb.append(sourceId);
        sb.append('\'');
        if (!sourceId.trim().equals(component.getClientId(context))) {
            // Check if sourceId is not a clientId and there is no execute set
            UIComponent ref = (component.getParent() == null) ? component : component.getParent();
            UIComponent instance = null;
            try {
                instance = ref.findComponent(sourceId);
            } catch (IllegalArgumentException e) {
            // No Op
            }
            if (instance == null && execute == null) {
                // set the clientId of the component so the behavior can be decoded later,
                // otherwise the behavior will fail
                execute = component.getClientId(context);
            }
        }
    }
    sb.append(',');
    sb.append(commandScript == null ? "event" : "null");
    sb.append(",'");
    sb.append(eventName);
    sb.append("',");
    SearchExpressionHandler seHandler = null;
    SearchExpressionContext seContext = null;
    if (StringUtils.isNotBlank(execute) || StringUtils.isNotBlank(render)) {
        seHandler = context.getApplication().getSearchExpressionHandler();
        seContext = SearchExpressionContext.createSearchExpressionContext(context, component, MyFacesSearchExpressionHints.SET_RESOLVE_CLIENT_SIDE_RESOLVE_SINGLE_COMPONENT, null);
    }
    appendIds(sb, execute, seHandler, seContext);
    sb.append(',');
    appendIds(sb, render, seHandler, seContext);
    if (onevent != null || onerror != null || delay != null || resetValues != null || (params != null && !params.isEmpty()) || (uiParams != null && !uiParams.isEmpty())) {
        sb.append(",{");
        if (onevent != null) {
            appendProperty(sb, "onevent", onevent, false);
        }
        if (onerror != null) {
            appendProperty(sb, "onerror", onerror, false);
        }
        if (delay != null) {
            appendProperty(sb, "delay", delay, true);
        }
        if (resetValues != null) {
            appendProperty(sb, "resetValues", resetValues, false);
        }
        if ((params != null && !params.isEmpty()) || (uiParams != null && !uiParams.isEmpty())) {
            if (params != null && !params.isEmpty()) {
                if (params instanceof RandomAccess) {
                    List<ClientBehaviorContext.Parameter> list = (List<ClientBehaviorContext.Parameter>) params;
                    for (int i = 0, size = list.size(); i < size; i++) {
                        ClientBehaviorContext.Parameter param = list.get(i);
                        appendProperty(sb, param.getName(), param.getValue(), true);
                    }
                } else {
                    for (ClientBehaviorContext.Parameter param : params) {
                        appendProperty(sb, param.getName(), param.getValue(), true);
                    }
                }
            }
            if (uiParams != null && uiParams.size() > 0) {
                for (int i = 0, size = uiParams.size(); i < size; i++) {
                    UIParameter param = uiParams.get(i);
                    appendProperty(sb, param.getName(), param.getValue(), true);
                }
            }
        }
        sb.append('}');
    }
    sb.append(')');
}
Also used : UIParameter(jakarta.faces.component.UIParameter) SearchExpressionContext(jakarta.faces.component.search.SearchExpressionContext) UIComponent(jakarta.faces.component.UIComponent) RandomAccess(java.util.RandomAccess) ClientBehaviorContext(jakarta.faces.component.behavior.ClientBehaviorContext) SearchExpressionHandler(jakarta.faces.component.search.SearchExpressionHandler) HtmlCommandScript(jakarta.faces.component.html.HtmlCommandScript) UIParameter(jakarta.faces.component.UIParameter) List(java.util.List)

Example 4 with HtmlCommandScript

use of jakarta.faces.component.html.HtmlCommandScript in project myfaces by apache.

the class HtmlCommandScriptRenderer method decode.

@Override
public void decode(FacesContext facesContext, UIComponent component) {
    super.decode(facesContext, component);
    HtmlCommandScript commandScript = (HtmlCommandScript) component;
    if (HtmlRendererUtils.isDisabled(component) || !commandScript.isRendered()) {
        return;
    }
    Map<String, String> paramMap = facesContext.getExternalContext().getRequestParameterMap();
    String behaviorEventName = paramMap.get(ClientBehaviorContext.BEHAVIOR_EVENT_PARAM_NAME);
    if (behaviorEventName != null) {
        String sourceId = paramMap.get(ClientBehaviorContext.BEHAVIOR_SOURCE_PARAM_NAME);
        String componentClientId = component.getClientId(facesContext);
        String clientId = sourceId;
        if (sourceId.startsWith(componentClientId) && sourceId.length() > componentClientId.length()) {
            String item = sourceId.substring(componentClientId.length() + 1);
            // component, because that could conflict with the clientId naming convention.
            if (StringUtils.isInteger(item)) {
                clientId = componentClientId;
            }
        }
        if (component.getClientId(facesContext).equals(clientId)) {
            boolean disabled = HtmlRendererUtils.isDisabled(component);
            UIForm form = ComponentUtils.findClosest(UIForm.class, component);
            boolean activateActionEvent = false;
            if (form != null && !disabled) {
                String reqValue = (String) facesContext.getExternalContext().getRequestParameterMap().get(HtmlRendererUtils.getHiddenCommandLinkFieldName(form, facesContext));
                activateActionEvent = reqValue != null && reqValue.equals(clientId) || HtmlRendererUtils.isPartialOrBehaviorSubmit(facesContext, clientId);
            }
            if (activateActionEvent) {
                component.queueEvent(new ActionEvent(component));
            }
        }
    }
    if (component instanceof ClientBehaviorHolder && !HtmlRendererUtils.isDisabled(component)) {
        ClientBehaviorRendererUtils.decodeClientBehaviors(facesContext, component);
    }
}
Also used : ActionEvent(jakarta.faces.event.ActionEvent) HtmlCommandScript(jakarta.faces.component.html.HtmlCommandScript) UIForm(jakarta.faces.component.UIForm) ClientBehaviorHolder(jakarta.faces.component.behavior.ClientBehaviorHolder)

Example 5 with HtmlCommandScript

use of jakarta.faces.component.html.HtmlCommandScript in project mojarra by eclipse-ee4j.

the class CommandScriptRenderer method encodeBegin.

@Override
public void encodeBegin(FacesContext context, UIComponent component) throws IOException {
    rendererParamsNotNull(context, component);
    if (!shouldEncode(component)) {
        return;
    }
    HtmlCommandScript commandScript = (HtmlCommandScript) component;
    String clientId = commandScript.getClientId(context);
    if (RenderKitUtils.getForm(commandScript, context) == null) {
        throw new IllegalArgumentException("commandScript ID " + clientId + " must be placed in UIForm");
    }
    String name = commandScript.getName();
    if (name == null || !PATTERN_NAME.matcher(name).matches()) {
        throw new IllegalArgumentException("commandScript ID " + clientId + " has an illegal name: '" + name + "'");
    }
    RenderKitUtils.renderFacesJsIfNecessary(context);
    ResponseWriter writer = context.getResponseWriter();
    assert writer != null;
    writer.startElement("span", commandScript);
    writer.writeAttribute("id", clientId, "id");
    writer.startElement("script", commandScript);
    if (!RenderKitUtils.isOutputHtml5Doctype(context)) {
        writer.writeAttribute("type", ScriptRenderer.DEFAULT_CONTENT_TYPE, "type");
    }
    RenderKitUtils.renderFunction(context, component, getBehaviorParameters(commandScript), clientId);
}
Also used : ResponseWriter(jakarta.faces.context.ResponseWriter) HtmlCommandScript(jakarta.faces.component.html.HtmlCommandScript)

Aggregations

HtmlCommandScript (jakarta.faces.component.html.HtmlCommandScript)5 UIComponent (jakarta.faces.component.UIComponent)2 UIParameter (jakarta.faces.component.UIParameter)2 ClientBehaviorContext (jakarta.faces.component.behavior.ClientBehaviorContext)2 ResponseWriter (jakarta.faces.context.ResponseWriter)2 UIForm (jakarta.faces.component.UIForm)1 ClientBehaviorHolder (jakarta.faces.component.behavior.ClientBehaviorHolder)1 SearchExpressionContext (jakarta.faces.component.search.SearchExpressionContext)1 SearchExpressionHandler (jakarta.faces.component.search.SearchExpressionHandler)1 ActionEvent (jakarta.faces.event.ActionEvent)1 List (java.util.List)1 RandomAccess (java.util.RandomAccess)1 SharedStringBuilder (org.apache.myfaces.core.api.shared.lang.SharedStringBuilder)1 JavascriptContext (org.apache.myfaces.renderkit.html.util.JavascriptContext)1