Search in sources :

Example 1 with UIForm

use of javax.faces.component.UIForm in project acs-community-packaging by Alfresco.

the class UIPropertySheet method renderValidationScript.

/**
 * Renders the necessary JavaScript to enforce any constraints the properties
 * have.
 *
 * @param context FacesContext
 */
@SuppressWarnings("unchecked")
protected void renderValidationScript(FacesContext context) throws IOException {
    ResponseWriter out = context.getResponseWriter();
    UIForm form = Utils.getParentForm(context, this);
    // TODO: We need to encode all the JavaScript functions here
    // with the client id of the property sheet so that we
    // can potentially add more than one property sheet to
    // page and have validation function correctly.
    // output the validation.js script
    out.write("\n<script type='text/javascript' src='");
    out.write(context.getExternalContext().getRequestContextPath());
    out.write("/scripts/validation.js");
    out.write("'></script>\n<script type='text/javascript'>\n");
    // output variable to hold flag for which submit button was pressed
    out.write("var finishButtonPressed = false;\n");
    out.write("var nextButtonPressed = false;\n");
    out.write("var transitionButtonPressed = false;\n");
    // output the validate() function
    out.write("function validate()\n{\n   var result = true;\n   ");
    out.write("if ((transitionButtonPressed || finishButtonPressed || nextButtonPressed) && (");
    int numberValidations = this.validations.size();
    List<ClientValidation> realTimeValidations = new ArrayList<ClientValidation>(numberValidations);
    for (int x = 0; x < numberValidations; x++) {
        ClientValidation validation = this.validations.get(x);
        if (validation.RealTimeChecking) {
            realTimeValidations.add(validation);
        }
        renderValidationMethod(out, validation, (x == (numberValidations - 1)), true);
    }
    // return false if validation failed to stop the form submitting
    out.write(")\n   { result = false; }\n\n");
    out.write("   finishButtonPressed = false;\n   nextButtonPressed = false;\n   transitionButtonPressed = false;\n");
    out.write("   return result;\n}\n\n");
    // output the processButtonState() function (if necessary)
    int numberRealTimeValidations = realTimeValidations.size();
    if (numberRealTimeValidations > 0) {
        out.write("function processButtonState()\n{\n   if (");
        for (int x = 0; x < numberRealTimeValidations; x++) {
            renderValidationMethod(out, realTimeValidations.get(x), (x == (numberRealTimeValidations - 1)), false);
        }
        // disable the finish button if validation failed and
        // also the next button if it is present
        out.write("\n   {\n      document.getElementById('");
        out.write(form.getClientId(context));
        out.write(NamingContainer.SEPARATOR_CHAR);
        out.write(getFinishButtonId());
        out.write("').disabled = true; \n");
        if (this.nextButtonId != null && this.nextButtonId.length() > 0) {
            out.write("      document.getElementById('");
            out.write(form.getClientId(context));
            out.write(NamingContainer.SEPARATOR_CHAR);
            out.write(this.nextButtonId);
            out.write("').disabled = true; \n");
        }
        out.write("   }\n");
        out.write("   else\n   {\n      document.getElementById('");
        out.write(form.getClientId(context));
        out.write(NamingContainer.SEPARATOR_CHAR);
        out.write(getFinishButtonId());
        out.write("').disabled = false;");
        if (this.nextButtonId != null && this.nextButtonId.length() > 0) {
            out.write("\n      document.getElementById('");
            out.write(form.getClientId(context));
            out.write(NamingContainer.SEPARATOR_CHAR);
            out.write(this.nextButtonId);
            out.write("').disabled = false;");
        }
        out.write("\n   }\n}\n\n");
    }
    // write out a function to initialise everything
    out.write("function initValidation()\n{\n");
    // register the validate function as the form onsubmit handler
    out.write("   document.getElementById('");
    out.write(form.getClientId(context));
    out.write("').onsubmit = validate;\n");
    // set the flag when the finish button is clicked
    out.write("   document.getElementById('");
    out.write(form.getClientId(context));
    out.write(NamingContainer.SEPARATOR_CHAR);
    out.write(getFinishButtonId());
    out.write("').onclick = function() { finishButtonPressed = true; }\n");
    // transition buttons on the workflow page also need to handle validation
    // so look for submit buttons with ":transition_" in the id
    out.write("   var inputItems = document.getElementsByTagName('input');\n");
    out.write("   for (i in inputItems)\n");
    out.write("   {\n");
    out.write("      if (inputItems[i].type == 'submit' && inputItems[i].id !== undefined && inputItems[i].id.indexOf(':transition_') != -1)\n");
    out.write("      {\n");
    out.write("         inputItems[i].onclick = function() { transitionButtonPressed = true; }\n");
    out.write("      }\n");
    out.write("   }\n");
    // set the flag when the next button is clicked
    if (this.nextButtonId != null && this.nextButtonId.length() > 0) {
        out.write("   document.getElementById('");
        out.write(form.getClientId(context));
        out.write(NamingContainer.SEPARATOR_CHAR);
        out.write(this.nextButtonId);
        out.write("').onclick = function() { nextButtonPressed = true; }\n");
    }
    // perform an initial check at page load time (if we have any real time validations)
    if (numberRealTimeValidations > 0) {
        out.write("   processButtonState();\n");
    }
    // close out the init function
    out.write("}\n\n");
    // setup init function to be called at page load time
    out.write("window.onload=initValidation;\n");
    // close out the script block
    out.write("</script>\n");
}
Also used : ResponseWriter(javax.faces.context.ResponseWriter) ArrayList(java.util.ArrayList) UIForm(javax.faces.component.UIForm)

Example 2 with UIForm

use of javax.faces.component.UIForm in project acs-community-packaging by Alfresco.

the class Utils method generateFormSubmit.

/**
 * Generate the JavaScript to submit set the specified hidden Form field to the
 * supplied value and submit the parent Form.
 *
 * NOTE: the supplied hidden field name is added to the Form Renderer map for output.
 *
 * @param context       FacesContext
 * @param component     UIComponent to generate JavaScript for
 * @param fieldId       Hidden field id to set value for
 * @param fieldValue    Hidden field value to set hidden field too on submit
 * @param valueIsParam  Determines whether the fieldValue parameter should be treated
 *                      as a parameter in the generated JavaScript, false will treat
 *                      the value i.e. surround it with single quotes
 * @param params        Optional map of param name/values to output
 *
 * @return JavaScript event code
 */
public static String generateFormSubmit(FacesContext context, UIComponent component, String fieldId, String fieldValue, boolean valueIsParam, Map<String, String> params) {
    UIForm form = Utils.getParentForm(context, component);
    if (form == null) {
        throw new IllegalStateException("Must nest components inside UIForm to generate form submit!");
    }
    String formClientId = form.getClientId(context);
    StringBuilder buf = new StringBuilder(200);
    buf.append("document.forms['");
    buf.append(formClientId);
    buf.append("']['");
    buf.append(fieldId);
    buf.append("'].value=");
    if (valueIsParam == false) {
        buf.append("'");
    }
    buf.append(Utils.encode(fieldValue));
    if (valueIsParam == false) {
        buf.append("'");
    }
    buf.append(";");
    if (params != null) {
        for (String name : params.keySet()) {
            buf.append("document.forms['");
            buf.append(formClientId);
            buf.append("']['");
            buf.append(name);
            buf.append("'].value='");
            String val = params.get(name);
            if (val != null) {
                val = Utils.encode(val);
            }
            // encode escape character
            val = replace(val, "\\", "\\\\");
            // encode single quote as we wrap string with that
            val = replace(val, "'", "\\'");
            buf.append(val);
            buf.append("';");
            // weak, but this seems to be the way Sun RI do it...
            // FormRenderer.addNeededHiddenField(context, name);
            HtmlFormRendererBase.addHiddenCommandParameter(context, form, name);
        }
    }
    buf.append("document.forms['");
    buf.append(formClientId);
    buf.append("'].submit();");
    if (valueIsParam == false) {
        buf.append("return false;");
    }
    // weak, but this seems to be the way Sun RI do it...
    // FormRenderer.addNeededHiddenField(context, fieldId);
    HtmlFormRendererBase.addHiddenCommandParameter(context, form, fieldId);
    return buf.toString();
}
Also used : UIForm(javax.faces.component.UIForm)

Example 3 with UIForm

use of javax.faces.component.UIForm in project acs-community-packaging by Alfresco.

the class UIPanel method getTitleComponent.

/**
 * Return the UI Component to be displayed on the right of the panel title area
 *
 * @return UIComponent
 */
public UIComponent getTitleComponent() {
    UIComponent titleComponent = null;
    // attempt to find a component with the specified ID
    String facetsId = getFacetsId();
    if (facetsId != null) {
        UIForm parent = Utils.getParentForm(FacesContext.getCurrentInstance(), this);
        UIComponent facetsComponent = parent.findComponent(facetsId);
        if (facetsComponent != null) {
            // get the 'title' facet from the component
            titleComponent = facetsComponent.getFacet("title");
        }
    }
    return titleComponent;
}
Also used : UIComponent(javax.faces.component.UIComponent) UIForm(javax.faces.component.UIForm)

Example 4 with UIForm

use of javax.faces.component.UIForm in project acs-community-packaging by Alfresco.

the class Utils method generateFormSubmit.

/**
 * Generate the JavaScript to submit the parent Form.
 *
 * @param context       FacesContext
 * @param component     UIComponent to generate JavaScript for
 *
 * @return JavaScript event code
 */
public static String generateFormSubmit(FacesContext context, UIComponent component) {
    UIForm form = Utils.getParentForm(context, component);
    if (form == null) {
        throw new IllegalStateException("Must nest components inside UIForm to generate form submit!");
    }
    String formClientId = form.getClientId(context);
    StringBuilder buf = new StringBuilder(48);
    buf.append("document.forms['");
    buf.append(formClientId);
    buf.append("'].submit()");
    buf.append(";return false;");
    return buf.toString();
}
Also used : UIForm(javax.faces.component.UIForm)

Aggregations

UIForm (javax.faces.component.UIForm)4 ArrayList (java.util.ArrayList)1 UIComponent (javax.faces.component.UIComponent)1 ResponseWriter (javax.faces.context.ResponseWriter)1