示例#1
0
  public static void decodeClientBehaviors(FacesContext facesContext, UIComponent uiComponent) {

    if (uiComponent instanceof ClientBehaviorHolder) {

      ClientBehaviorHolder clientBehaviorHolder = (ClientBehaviorHolder) uiComponent;
      Map<String, List<ClientBehavior>> clientBehaviorMap =
          clientBehaviorHolder.getClientBehaviors();

      Map<String, String> requestParameterMap =
          facesContext.getExternalContext().getRequestParameterMap();
      String behaviorEvent = requestParameterMap.get(FacesConstants.JAVAX_FACES_BEHAVIOR_EVENT);

      if (behaviorEvent != null) {

        List<ClientBehavior> clientBehaviors = clientBehaviorMap.get(behaviorEvent);

        if (clientBehaviors != null) {
          String source = requestParameterMap.get(FacesConstants.JAVAX_FACES_SOURCE);

          if (source != null) {
            String clientId = uiComponent.getClientId(facesContext);

            if (clientId.startsWith(source)) {

              for (ClientBehavior behavior : clientBehaviors) {
                behavior.decode(facesContext, uiComponent);
              }
            }
          }
        }
      }
    }
  }
  private static boolean appendBehaviorsToChain(
      StringBuilder builder,
      FacesContext context,
      UIComponent component,
      List<ClientBehavior> behaviors,
      String behaviorEventName,
      Collection<ClientBehaviorContext.Parameter> params) {

    if ((behaviors == null) || (behaviors.isEmpty())) {
      return false;
    }

    ClientBehaviorContext bContext =
        createClientBehaviorContext(context, component, behaviorEventName, params);

    boolean submitting = false;

    for (ClientBehavior behavior : behaviors) {
      String script = behavior.getScript(bContext);
      if ((script != null) && (script.length() > 0)) {
        appendScriptToChain(builder, script);

        if (isSubmitting(behavior)) {
          submitting = true;
        }
      }
    }

    return submitting;
  }
  public void addBehaviors(String domEventName, String logicalEventName) {
    String name = domEventName;
    List<ClientBehavior> behaviorsList = getBehaviorsList(domEventName);

    if ((behaviorsList == null) && (logicalEventName != null)) {
      behaviorsList = getBehaviorsList(logicalEventName);
      name = logicalEventName;
    }

    if (behaviorsList == null) {
      return;
    }

    ClientBehaviorContext behaviorContext =
        ClientBehaviorContext.createClientBehaviorContext(
            facesContext,
            component,
            name,
            includeClientId ? component.getClientId(facesContext) : null,
            getParameters());

    for (ClientBehavior clientBehavior : behaviorsList) {
      String behaviorScript = clientBehavior.getScript(behaviorContext);

      if (isNotEmpty(behaviorScript)) {
        if (clientBehavior.getHints().contains(ClientBehaviorHint.SUBMITTING)) {
          hasSubmittingBehavior = true;
        }

        handlers.add(behaviorScript);
      }
    }
  }
示例#4
0
  private static Object createBehaviorsChain(
      Object inlineHandlerValue,
      ClientBehaviorContext behaviorContext,
      List<ClientBehavior> behaviors) {

    boolean isChained = false;
    StringBuilder result = new StringBuilder();

    isChained = chain(result, inlineHandlerValue, isChained);
    for (ClientBehavior behavior : behaviors) {
      isChained = chain(result, behavior.getScript(behaviorContext), isChained);

      if (behavior.getHints().contains(ClientBehaviorHint.SUBMITTING)) {
        break;
      }
    }

    if (result.length() == 0) {
      return null;
    }

    if (isChained) {
      result.insert(0, "return jsf.util.chain(this, event, ");
      result.append(")");
    }

    return result.toString();
  }
  // Decodes Behaviors if any match the behavior source/event.
  // As a convenience, returns component id, but only if it
  // was retrieved.  This allows us to avoid duplicating
  // calls to getClientId(), which can be expensive for
  // deep component trees.
  protected final String decodeBehaviors(FacesContext context, UIComponent component) {

    if (!(component instanceof ClientBehaviorHolder)) {
      return null;
    }

    ClientBehaviorHolder holder = (ClientBehaviorHolder) component;
    Map<String, List<ClientBehavior>> behaviors = holder.getClientBehaviors();
    if (behaviors.isEmpty()) {
      return null;
    }

    String behaviorEvent = BEHAVIOR_EVENT_PARAM.getValue(context);

    if (null != behaviorEvent) {
      List<ClientBehavior> behaviorsForEvent = behaviors.get(behaviorEvent);

      if (behaviorsForEvent != null && behaviorsForEvent.size() > 0) {
        String behaviorSource = BEHAVIOR_SOURCE_PARAM.getValue(context);
        String clientId = component.getClientId();
        if (isBehaviorSource(context, behaviorSource, clientId)) {
          for (ClientBehavior behavior : behaviorsForEvent) {
            behavior.decode(context, component);
          }
        }

        return clientId;
      }
    }

    return null;
  }
示例#6
0
  protected void decodeBehaviors(FacesContext context, UIComponent component) {

    if (!(component instanceof ClientBehaviorHolder)) {
      return;
    }

    Map<String, List<ClientBehavior>> behaviors =
        ((ClientBehaviorHolder) component).getClientBehaviors();
    if (behaviors.isEmpty()) {
      return;
    }

    Map<String, String> params = context.getExternalContext().getRequestParameterMap();
    String behaviorEvent = params.get("javax.faces.behavior.event");

    if (null != behaviorEvent) {
      List<ClientBehavior> behaviorsForEvent = behaviors.get(behaviorEvent);

      if (behaviorsForEvent != null && !behaviorsForEvent.isEmpty()) {
        String behaviorSource = params.get("javax.faces.source");
        String clientId = component.getClientId();

        if (behaviorSource != null && clientId.startsWith(behaviorSource)) {
          for (ClientBehavior behavior : behaviorsForEvent) {
            behavior.decode(context, component);
          }
        }
      }
    }
  }
示例#7
0
  // Decodes Behaviors if any match the behavior source/event.
  // As a convenience, returns component id, but only if it
  // was retrieved.  This allows us to avoid duplicating
  // calls to getClientId(), which can be expensive for
  // deep component trees.
  protected final String decodeBehaviors(FacesContext context, UIComponent component) {

    if (!(component instanceof ClientBehaviorHolder)) {
      return null;
    }

    ClientBehaviorHolder holder = (ClientBehaviorHolder) component;
    Map<String, List<ClientBehavior>> behaviors = holder.getClientBehaviors();
    if (behaviors.isEmpty()) {
      return null;
    }

    ExternalContext external = context.getExternalContext();
    Map<String, String> params = external.getRequestParameterMap();
    String behaviorEvent = params.get("javax.faces.behavior.event");

    if (null != behaviorEvent) {
      List<ClientBehavior> behaviorsForEvent = behaviors.get(behaviorEvent);

      if (behaviorsForEvent != null && behaviorsForEvent.size() > 0) {
        String behaviorSource = params.get("javax.faces.source");
        String clientId = component.getClientId();
        if (isBehaviorSource(context, behaviorSource, clientId)) {
          for (ClientBehavior behavior : behaviorsForEvent) {
            behavior.decode(context, component);
          }
        }

        return clientId;
      }
    }

    return null;
  }
示例#8
0
  /**
   * Non-obstrusive way to apply client behaviors. Behaviors are rendered as options to the client
   * side widget and applied by widget to necessary dom element
   */
  protected void encodeClientBehaviors(FacesContext context, ClientBehaviorHolder component)
      throws IOException {
    ResponseWriter writer = context.getResponseWriter();

    // ClientBehaviors
    Map<String, List<ClientBehavior>> behaviorEvents = component.getClientBehaviors();

    if (!behaviorEvents.isEmpty()) {
      String clientId = ((UIComponent) component).getClientId(context);
      List<ClientBehaviorContext.Parameter> params = Collections.emptyList();

      writer.write(",behaviors:{");

      for (Iterator<String> eventIterator = behaviorEvents.keySet().iterator();
          eventIterator.hasNext(); ) {
        String event = eventIterator.next();
        String domEvent = event;

        if (event.equalsIgnoreCase("valueChange")) // editable value holders
        domEvent = "change";
        else if (event.equalsIgnoreCase("action")) // commands
        domEvent = "click";

        writer.write(domEvent + ":");

        writer.write("function(event) {");
        for (Iterator<ClientBehavior> behaviorIter = behaviorEvents.get(event).iterator();
            behaviorIter.hasNext(); ) {
          ClientBehavior behavior = behaviorIter.next();
          ClientBehaviorContext cbc =
              ClientBehaviorContext.createClientBehaviorContext(
                  context, (UIComponent) component, event, clientId, params);
          String script = behavior.getScript(cbc); // could be null if disabled

          if (script != null) {
            writer.write(script);
          }
        }
        writer.write("}");

        if (eventIterator.hasNext()) {
          writer.write(",");
        }
      }

      writer.write("}");
    }
  }
示例#9
0
    private boolean shouldAddBehavior(ClientBehaviorHolder behaviorHolder, String eventName) {
      if (!behaviorHolder.getEventNames().contains(eventName)) {
        return false;
      }

      Map<String, List<ClientBehavior>> clientBehaviorsMap = behaviorHolder.getClientBehaviors();
      List<ClientBehavior> clientBehaviors = clientBehaviorsMap.get(eventName);

      if (clientBehaviors == null || clientBehaviors.isEmpty()) {
        return true;
      }

      for (ClientBehavior behavior : clientBehaviors) {
        Set<ClientBehaviorHint> hints = behavior.getHints();

        if (hints.contains(ClientBehaviorHint.SUBMITTING)) {
          return false;
        }
      }

      return true;
    }
示例#10
0
  protected String getOnclickBehaviors(FacesContext context, ClientBehaviorHolder cbh) {
    List<ClientBehavior> behaviors = cbh.getClientBehaviors().get("action");
    StringBuilder sb = new StringBuilder();

    if (behaviors != null && !behaviors.isEmpty()) {
      UIComponent component = (UIComponent) cbh;
      String clientId = component.getClientId(context);
      List<ClientBehaviorContext.Parameter> params = Collections.emptyList();

      for (Iterator<ClientBehavior> behaviorIter = behaviors.iterator(); behaviorIter.hasNext(); ) {
        ClientBehavior behavior = behaviorIter.next();
        ClientBehaviorContext cbc =
            ClientBehaviorContext.createClientBehaviorContext(
                context, component, "action", clientId, params);
        String script = behavior.getScript(cbc);

        if (script != null) sb.append(script).append(";");
      }
    }

    return sb.length() == 0 ? null : sb.toString();
  }
示例#11
0
  protected void setConfirmationScript(FacesContext context, MenuItem item) {
    if (item instanceof ClientBehaviorHolder) {
      Map<String, List<ClientBehavior>> behaviors =
          ((ClientBehaviorHolder) item).getClientBehaviors();
      List<ClientBehavior> clickBehaviors = (behaviors == null) ? null : behaviors.get("click");

      if (clickBehaviors != null && !clickBehaviors.isEmpty()) {
        for (int i = 0; i < clickBehaviors.size(); i++) {
          ClientBehavior clientBehavior = clickBehaviors.get(i);
          if (clientBehavior instanceof ConfirmBehavior) {
            ClientBehaviorContext cbc =
                ClientBehaviorContext.createClientBehaviorContext(
                    context,
                    (UIComponent) item,
                    "click",
                    item.getClientId(),
                    Collections.EMPTY_LIST);
            clientBehavior.getScript(cbc);
            break;
          }
        }
      }
    }
  }
示例#12
0
  public static String decodeBehaviors(FacesContext context, UIComponent component) {
    if (!(component instanceof ClientBehaviorHolder)) {
      return null;
    }

    ClientBehaviorHolder holder = (ClientBehaviorHolder) component;
    Map<String, List<ClientBehavior>> behaviors = holder.getClientBehaviors();

    if (behaviors == null || behaviors.isEmpty()) {
      return null;
    }

    ExternalContext externalContext = context.getExternalContext();
    Map<String, String> parametersMap = externalContext.getRequestParameterMap();
    String behaviorEvent = parametersMap.get(BEHAVIOR_EVENT_NAME);

    if (behaviorEvent == null) {
      return null;
    }

    List<ClientBehavior> behaviorsForEvent = behaviors.get(behaviorEvent);
    String behaviorSource = parametersMap.get(BEHAVIOR_SOURCE_ID);
    String clientId = component.getClientId(context);

    if (behaviorSource != null && behaviorSource.equals(clientId)) {
      if (behaviorsForEvent != null && !behaviorsForEvent.isEmpty()) {
        for (ClientBehavior behavior : behaviorsForEvent) {
          behavior.decode(context, component);
        }

        return behaviorEvent;
      }
    }

    return null;
  }
  private static String getSingleBehaviorHandler(
      FacesContext context,
      UIComponent component,
      ClientBehavior behavior,
      Collection<ClientBehaviorContext.Parameter> params,
      String behaviorEventName) {

    ClientBehaviorContext bContext =
        createClientBehaviorContext(context, component, behaviorEventName, params);

    String script = behavior.getScript(bContext);

    boolean preventDefault =
        ((isSubmitting(behavior))
            && ("action".equals(behaviorEventName) || "click".equals(behaviorEventName)));

    if (preventDefault) {
      script = script + ";return false";
    }

    return script;
  }
 private static boolean isSubmitting(ClientBehavior behavior) {
   return behavior.getHints().contains(ClientBehaviorHint.SUBMITTING);
 }
  @Override
  public void encodeBegin(FacesContext facesContext, UIComponent uiComponent) throws IOException {

    // Encode the starting <div> element that represents the rich text editor.
    ResponseWriter responseWriter = facesContext.getResponseWriter();
    responseWriter.startElement(StringPool.DIV, uiComponent);

    String clientId = uiComponent.getClientId();
    char separatorChar = UINamingContainer.getSeparatorChar(facesContext);
    String escapedEditorName = clientId.replace(separatorChar, '_').concat("_jsptag");
    responseWriter.writeAttribute(StringPool.ID, clientId, null);
    RendererUtil.encodeStyleable(responseWriter, (Styleable) uiComponent);

    // Encode the starting <textarea> element.
    InputRichText inputRichText = (InputRichText) uiComponent;

    responseWriter.startElement("textarea", uiComponent);
    responseWriter.writeAttribute(StringPool.ID, clientId + "_input", null);
    responseWriter.writeAttribute(StringPool.NAME, escapedEditorName, null);
    responseWriter.writeAttribute(Styleable.STYLE, "display:none;", null);

    // Encode the onblur/onchange/onfocus attributes and any associated client behavior scripts.
    String onblur = inputRichText.getOnblur();
    String onchange = inputRichText.getOnchange();
    String onfocus = inputRichText.getOnfocus();
    Map<String, List<ClientBehavior>> clientBehaviorMap = inputRichText.getClientBehaviors();

    for (String eventName : inputRichText.getEventNames()) {
      List<ClientBehavior> clientBehaviorsForEvent = clientBehaviorMap.get(eventName);

      if (clientBehaviorsForEvent != null) {

        for (ClientBehavior clientBehavior : clientBehaviorsForEvent) {

          ClientBehaviorContext clientBehaviorContext =
              ClientBehaviorContext.createClientBehaviorContext(
                  facesContext, inputRichText, eventName, clientId, null);
          String clientBehaviorScript = clientBehavior.getScript(clientBehaviorContext);

          if (clientBehaviorScript != null) {

            if ("valueChange".equals(eventName) || "change".equals(eventName)) {

              if (onchange != null) {
                clientBehaviorScript = onchange.concat(";").concat(clientBehaviorScript);
                onchange = null;
              }

              responseWriter.writeAttribute("onchange", clientBehaviorScript, null);
            } else if ("blur".equals(eventName)) {

              if (onblur != null) {
                clientBehaviorScript = onblur.concat(";").concat(clientBehaviorScript);
                onblur = null;
              }

              responseWriter.writeAttribute("onblur", clientBehaviorScript, null);
            } else if ("focus".equals(eventName)) {

              if (onfocus != null) {
                clientBehaviorScript = onfocus.concat(";").concat(clientBehaviorScript);
                onfocus = null;
              }

              responseWriter.writeAttribute("onfocus", clientBehaviorScript, null);
            }
          }
        }
      }
    }

    if (onblur != null) {
      responseWriter.writeAttribute("onblur", onblur, null);
    }

    if (onchange != null) {
      responseWriter.writeAttribute("onchange", onchange, null);
    }

    if (onfocus != null) {
      responseWriter.writeAttribute("onfocus", onfocus, null);
    }

    // Encode the value of the component as a child of the textarea element.
    Object value = inputRichText.getValue();

    if (value != null) {
      responseWriter.writeText(value, null);
    }

    // Encode the closing </textarea> element.
    responseWriter.endElement("textarea");

    // Encode the script that contains functions with names specific to this component, so that they
    // can be
    // invoked directly by the JavaScript generated by the JSP tag.
    String formattedTemplate = wysiwygTemplate.format(facesContext, inputRichText);
    responseWriter.startElement(StringPool.SCRIPT, uiComponent);
    responseWriter.writeAttribute(StringPool.TYPE, ContentTypes.TEXT_JAVASCRIPT, null);
    responseWriter.write(formattedTemplate);
    responseWriter.endElement(StringPool.SCRIPT);

    // Begin the JSP tag lifecycle and write the output to the response.
    super.encodeBegin(facesContext, uiComponent);
  }