Esempio n. 1
0
  public AjaxParameters(InputFile inputFile, String clientId, String formClientId) {

    // Default value of execute is "@this" which maps to name and id of the rendered input element.
    this.execute = clientId.concat(StringPool.SPACE).concat(clientId);

    // Default value of render is "@none" which maps to an empty string.
    this.render = StringPool.BLANK;

    // For each Ajax behavior associated with the specified component:
    Map<String, List<ClientBehavior>> clientBehaviorMap = inputFile.getClientBehaviors();
    List<ClientBehavior> clientBehaviors = clientBehaviorMap.get(inputFile.getDefaultEventName());

    for (ClientBehavior clientBehavior : clientBehaviors) {

      if (clientBehavior instanceof AjaxBehavior) {

        // Interpret the value of the f:ajax "execute" attribute.
        AjaxBehavior ajaxBehavior = (AjaxBehavior) clientBehavior;
        Collection<String> executeIds = ajaxBehavior.getExecute();

        if ((executeIds != null) && (executeIds.size() > 0)) {

          boolean foundAllKeyword = false;
          boolean foundNoneKeyword = false;
          StringBuilder buf = new StringBuilder(clientId);

          for (String executeId : executeIds) {

            if ("@all".equals(executeId)) {
              foundAllKeyword = true;

              break;
            } else if ("@none".equals(executeId)) {
              foundNoneKeyword = true;
              this.execute = StringPool.BLANK;

              break;
            } else if (executeId.length() > 0) {

              buf.append(StringPool.SPACE);
              buf.append(executeId);
            }
          }

          if (!foundNoneKeyword) {

            if (foundAllKeyword) {
              this.execute = "@all";
            } else {
              this.execute = buf.toString();
              this.execute = this.execute.replace("@form", formClientId);
              this.execute = this.execute.replace("@this", clientId);

              boolean foundClientId = false;
              String[] executeIdArray = this.execute.split(StringPool.SPACE);

              for (String executeId : executeIdArray) {

                if (clientId.equals(executeId)) {
                  foundClientId = true;

                  break;
                }
              }

              if (!foundClientId) {
                this.execute = clientId.concat(StringPool.SPACE).concat(this.execute);
              }
            }
          }
        }

        // Interpret the value of the f:ajax "render" attribute.
        Collection<String> renderIds = ajaxBehavior.getRender();

        if ((renderIds != null) && (renderIds.size() > 0)) {

          boolean first = true;
          boolean foundAllKeyword = false;
          boolean foundNoneKeyword = false;
          StringBuilder buf = new StringBuilder();

          for (String renderId : renderIds) {

            if ("@all".equals(renderId)) {
              foundAllKeyword = true;

              break;
            } else if ("@none".equals(renderId)) {
              foundNoneKeyword = true;
              this.render = StringPool.BLANK;

              break;
            } else {

              if (renderId.length() > 0) {

                if (first) {
                  first = false;
                } else {
                  buf.append(StringPool.SPACE);
                }

                buf.append(renderId);
              }
            }
          }

          if (!foundNoneKeyword) {

            if (foundAllKeyword) {
              this.render = "@all";
            } else {
              this.render = buf.toString();
              this.render = this.render.replace("@form", formClientId);
              this.render = this.render.replace("@this", clientId);
            }
          }
        }
      }
    }
  }
Esempio n. 2
0
  private static String buildAjaxCommand(
      ClientBehaviorContext behaviorContext,
      AjaxBehavior ajaxBehavior,
      boolean namespaceParameters) {

    // 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);
        }
      }
    }

    ajaxCommand.append("mojarra.ab(");

    if (sourceId == null) {
      ajaxCommand.append("this");
    } else {
      ajaxCommand.append("'");
      ajaxCommand.append(sourceId);
      ajaxCommand.append("'");
    }

    ajaxCommand.append(",event,'");
    ajaxCommand.append(eventName);
    ajaxCommand.append("',");

    appendIds(component, ajaxCommand, execute);
    ajaxCommand.append(",");
    appendIds(component, ajaxCommand, render);

    String namingContainerId = null;
    if (namespaceParameters) {
      FacesContext context = behaviorContext.getFacesContext();
      UIViewRoot viewRoot = context.getViewRoot();
      if (viewRoot instanceof NamingContainer) {
        namingContainerId = viewRoot.getContainerClientId(context);
      }
    }

    if ((namingContainerId != null)
        || (onevent != null)
        || (onerror != null)
        || (delay != null)
        || (resetValues != null)
        || !params.isEmpty()) {

      ajaxCommand.append(",{");

      if (namingContainerId != null) {
        // the literal string must exactly match the corresponding value
        // in jsf.js.
        RenderKitUtils.appendProperty(
            ajaxCommand, "com.sun.faces.namingContainerId", namingContainerId, true);
      }

      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()) {
        for (ClientBehaviorContext.Parameter param : params) {
          RenderKitUtils.appendProperty(ajaxCommand, param.getName(), param.getValue());
        }
      }

      ajaxCommand.append("}");
    }

    ajaxCommand.append(")");

    return ajaxCommand.toString();
  }