// 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;
  }
Beispiel #2
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);
              }
            }
          }
        }
      }
    }
  }
  // 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;
  }
  private static Map<String, List<ClientBehavior>> getClientBehaviorsMap(UIComponent component) {
    Map<String, List<ClientBehavior>> result;
    if (component instanceof ClientBehaviorHolder) {
      ClientBehaviorHolder clientBehaviorHolder = (ClientBehaviorHolder) component;

      result = clientBehaviorHolder.getClientBehaviors();
    } else {
      result = Collections.emptyMap();
    }
    return result;
  }
  private static List<ClientBehavior> getClientBehaviors(
      UIComponent component, String behaviorEventName) {

    if (component instanceof ClientBehaviorHolder) {
      ClientBehaviorHolder bHolder = (ClientBehaviorHolder) component;
      Map<String, List<ClientBehavior>> behaviors = bHolder.getClientBehaviors();
      if (null != behaviors) {
        return behaviors.get(behaviorEventName);
      }
    }

    return null;
  }
  private List<ClientBehavior> getBehaviorsList(String behaviorName) {
    List<ClientBehavior> behaviors = null;

    if (component instanceof ClientBehaviorHolder) {
      ClientBehaviorHolder clientBehaviorHolder = (ClientBehaviorHolder) component;
      Map<String, List<ClientBehavior>> clientBehaviorsMap =
          clientBehaviorHolder.getClientBehaviors();

      if (clientBehaviorsMap != null) {
        behaviors = clientBehaviorsMap.get(behaviorName);
      }
    }

    return behaviors;
  }
    private void addBehavior(FacesContext context, ClientBehaviorHolder behaviorHolder) {
      String eventName = this.eventName;

      if (eventName == null) {
        eventName = behaviorHolder.getDefaultEventName();

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

      if (shouldAddBehavior(behaviorHolder, eventName)) {
        ClientBehavior behavior = createBehavior(context);

        behaviorHolder.addClientBehavior(eventName, behavior);
      }
    }
    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;
    }
  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;
  }
Beispiel #10
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("}");
    }
  }
Beispiel #11
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();
  }