@Override
  public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
    ResponseWriter writer = context.getResponseWriter();
    CommandLink link = (CommandLink) component;
    String clientId = link.getClientId(context);
    Object label = link.getValue();

    if (!link.isDisabled()) {
      String request;
      boolean ajax = link.isAjax();
      String styleClass = link.getStyleClass();
      styleClass =
          styleClass == null ? CommandLink.STYLE_CLASS : CommandLink.STYLE_CLASS + " " + styleClass;
      RequestContext requestContext = RequestContext.getCurrentInstance();
      boolean csvEnabled =
          requestContext.getApplicationContext().getConfig().isClientSideValidationEnabled()
              && link.isValidateClient();

      StringBuilder onclick = new StringBuilder();
      if (link.getOnclick() != null) {
        onclick.append(link.getOnclick()).append(";");
      }

      String onclickBehaviors = getOnclickBehaviors(context, link);
      if (onclickBehaviors != null) {
        onclick.append(onclickBehaviors);
      }

      writer.startElement("a", link);
      writer.writeAttribute("id", clientId, "id");
      writer.writeAttribute("href", "#", null);
      writer.writeAttribute("class", styleClass, null);
      if (link.getTitle() != null) {
        writer.writeAttribute("aria-label", link.getTitle(), null);
      }

      if (ajax) {
        request = buildAjaxRequest(context, link, null);
      } else {
        UIComponent form = ComponentUtils.findParentForm(context, link);
        if (form == null) {
          throw new FacesException(
              "Commandlink \"" + clientId + "\" must be inside a form component");
        }

        request = buildNonAjaxRequest(context, link, form, clientId, true);
      }

      if (csvEnabled) {
        CSVBuilder csvb = requestContext.getCSVBuilder();
        request =
            csvb.init()
                .source("this")
                .ajax(ajax)
                .process(link, link.getProcess())
                .command(request)
                .build();
      }

      onclick.append(request);

      if (onclick.length() > 0) {
        if (link.requiresConfirmation()) {
          writer.writeAttribute("data-pfconfirmcommand", onclick.toString(), null);
          writer.writeAttribute("onclick", link.getConfirmationScript(), "onclick");
        } else writer.writeAttribute("onclick", onclick.toString(), "onclick");
      }

      renderPassThruAttributes(context, link, HTML.LINK_ATTRS, HTML.CLICK_EVENT);

      if (label != null) writer.writeText(label, "value");
      else renderChildren(context, link);

      writer.endElement("a");
    } else {
      String styleClass = link.getStyleClass();
      styleClass =
          styleClass == null
              ? CommandLink.DISABLED_STYLE_CLASS
              : CommandLink.DISABLED_STYLE_CLASS + " " + styleClass;

      writer.startElement("span", link);
      writer.writeAttribute("id", clientId, "id");
      writer.writeAttribute("class", styleClass, "styleclass");

      if (link.getStyle() != null) writer.writeAttribute("style", link.getStyle(), "style");

      if (label != null) writer.writeText(label, "value");
      else renderChildren(context, link);

      writer.endElement("span");
    }
  }
Exemplo n.º 2
0
  protected void encodeScript(FacesContext facesContext, DataGrid grid) throws IOException {
    ResponseWriter writer = facesContext.getResponseWriter();
    String clientId = grid.getClientId(facesContext);
    String widgetVar = createUniqueWidgetVar(facesContext, grid);

    UIComponent form = ComponentUtils.findParentForm(facesContext, grid);
    if (form == null) {
      throw new FacesException("DataGrid : \"" + clientId + "\" must be inside a form element");
    }

    writer.startElement("script", null);
    writer.writeAttribute("type", "text/javascript", null);

    writer.write(widgetVar + " = new PrimeFaces.widget.DataGrid('" + clientId + "',{");
    writer.write("url:'" + getActionURL(facesContext) + "'");
    writer.write(",formId:'" + form.getClientId(facesContext) + "'");

    if (grid.isPaginator()) {
      writer.write(",paginator:new YAHOO.widget.Paginator({");
      writer.write("rowsPerPage:" + grid.getRows());
      writer.write(",totalRecords:" + grid.getRowCount());
      writer.write(",initialPage:" + grid.getPage());

      if (grid.getPageLinks() != 10) writer.write(",pageLinks:" + grid.getPageLinks());
      if (grid.getPaginatorTemplate() != null)
        writer.write(",template:'" + grid.getPaginatorTemplate() + "'");
      if (grid.getRowsPerPageTemplate() != null)
        writer.write(",rowsPerPageOptions : [" + grid.getRowsPerPageTemplate() + "]");
      if (grid.getFirstPageLinkLabel() != null)
        writer.write(",firstPageLinkLabel:'" + grid.getFirstPageLinkLabel() + "'");
      if (grid.getPreviousPageLinkLabel() != null)
        writer.write(",previousPageLinkLabel:'" + grid.getPreviousPageLinkLabel() + "'");
      if (grid.getNextPageLinkLabel() != null)
        writer.write(",nextPageLinkLabel:'" + grid.getNextPageLinkLabel() + "'");
      if (grid.getLastPageLinkLabel() != null)
        writer.write(",lastPageLinkLabel:'" + grid.getLastPageLinkLabel() + "'");
      if (grid.getCurrentPageReportTemplate() != null)
        writer.write(",pageReportTemplate:'" + grid.getCurrentPageReportTemplate() + "'");
      if (!grid.isPaginatorAlwaysVisible()) writer.write(",alwaysVisible:false");

      String paginatorPosition = grid.getPaginatorPosition();
      String paginatorContainer = null;
      if (paginatorPosition.equals("both"))
        paginatorContainer = clientId + "_paginatorTop','" + clientId + "_paginatorBottom";
      else if (paginatorPosition.equals("top")) paginatorContainer = clientId + "_paginatorTop";
      else if (paginatorPosition.equals("bottom"))
        paginatorContainer = clientId + "_paginatorBottom";

      writer.write(",containers:['" + paginatorContainer + "']");

      writer.write("})");

      if (grid.isEffect()) {
        writer.write(",effect:true");
        writer.write(",effectSpeed:'" + grid.getEffectSpeed() + "'");
      }
    }

    writer.write("});");

    writer.endElement("script");
  }