Ejemplo n.º 1
0
  @Override
  public String getScript(ClientBehaviorContext behaviorContext) {
    FacesContext facesContext = behaviorContext.getFacesContext();
    UIComponent targetComponent = behaviorContext.getComponent().findComponent(target);
    if (targetComponent == null) {
      throw new FacesException("Cannot find component " + target + " in view.");
    }

    return "$(PrimeFaces.escapeClientId('"
        + targetComponent.getClientId(facesContext)
        + "')).jqprint();return false;";
  }
Ejemplo n.º 2
0
  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);
      }
    }
  }
  @Override
  public String getScript(ClientBehaviorContext behaviorContext) {
    String eventName = behaviorContext.getEventName();
    if (!"click".equals(eventName) && !"mousemove".equals(eventName)) {
      return "";
    }

    return String.format(SCRIPT, cssClass);
  }
  private static ClientBehaviorContext createClientBehaviorContext(
      FacesContext context,
      UIComponent component,
      String behaviorEventName,
      Collection<ClientBehaviorContext.Parameter> params) {

    return ClientBehaviorContext.createClientBehaviorContext(
        context, component, behaviorEventName, null, params);
  }
Ejemplo n.º 5
0
  @Override
  public String getScript(ClientBehaviorContext behaviorContext, ClientBehavior behavior) {
    ShowBehavior behav = (ShowBehavior) behavior;
    if (behav.isDisabled()) {
      return null;
    }

    FacesContext fc = behaviorContext.getFacesContext();
    UIComponent component = behaviorContext.getComponent();
    String source = behaviorContext.getSourceId();

    if (source == null) {
      source = component.getClientId(fc);
    }

    String forId = ComponentUtils.findComponentClientId(behav.getForId());
    StringBuilder req = new StringBuilder();
    req.append("$('");
    req.append(forId);
    req.append("').show();");

    return req.toString();
  }
Ejemplo n.º 6
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("}");
    }
  }
Ejemplo n.º 7
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();
  }
Ejemplo n.º 8
0
  // TODO - create special method for event handlers that will return String?
  // TODO - add check for 'disabled'?
  public static Object getAttributeAndBehaviorsValue(
      FacesContext facesContext, UIComponent component, ComponentAttribute componentAttribute) {
    if (facesContext == null) {
      throw new NullPointerException("facesContext");
    }

    if (component == null) {
      throw new NullPointerException("component");
    }

    if (componentAttribute == null) {
      throw new NullPointerException("componentAttribute");
    }

    String componentAttributeName = componentAttribute.getComponentAttributeName();
    Object attributeValue = component.getAttributes().get(componentAttributeName);

    String[] eventNames = componentAttribute.getEventNames();
    if (eventNames.length > 0) {
      Map<String, List<ClientBehavior>> behaviorsMap = getClientBehaviorsMap(component);
      if (behaviorsMap.size() > 0) {
        for (String eventName : eventNames) {
          if (behaviorsMap.containsKey(eventName)) {
            List<ClientBehavior> behaviorsList = behaviorsMap.get(eventName);
            if (!behaviorsList.isEmpty()) {
              // TODO - parameters handling
              ClientBehaviorContext behaviorContext =
                  ClientBehaviorContext.createClientBehaviorContext(
                      facesContext, component, eventName, null, null);
              attributeValue = createBehaviorsChain(attributeValue, behaviorContext, behaviorsList);
            }
            break;
          }
        }
      }
    }
    return attributeValue;
  }
Ejemplo n.º 9
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;
          }
        }
      }
    }
  }
Ejemplo n.º 10
0
  @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);
  }
Ejemplo n.º 11
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();
  }