View Javadoc
1   /*
2    * Copyright (c) 2011-2024 PrimeFaces Extensions
3    *
4    *  Permission is hereby granted, free of charge, to any person obtaining a copy
5    *  of this software and associated documentation files (the "Software"), to deal
6    *  in the Software without restriction, including without limitation the rights
7    *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    *  copies of the Software, and to permit persons to whom the Software is
9    *  furnished to do so, subject to the following conditions:
10   *
11   *  The above copyright notice and this permission notice shall be included in
12   *  all copies or substantial portions of the Software.
13   *
14   *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20   *  THE SOFTWARE.
21   */
22  package org.primefaces.extensions.component.creditcard;
23  
24  import java.io.IOException;
25  
26  import javax.faces.component.UIComponent;
27  import javax.faces.context.FacesContext;
28  import javax.faces.context.ResponseWriter;
29  
30  import org.primefaces.extensions.util.Attrs;
31  import org.primefaces.renderkit.CoreRenderer;
32  import org.primefaces.util.HTML;
33  import org.primefaces.util.LangUtils;
34  import org.primefaces.util.WidgetBuilder;
35  
36  /**
37   * Renderer for the {@link CreditCard} component.
38   *
39   * @author Melloware mellowaredev@gmail.com
40   * @since 8.0.1
41   */
42  public class CreditCardRenderer extends CoreRenderer {
43  
44      @Override
45      public void encodeEnd(final FacesContext context, final UIComponent component) throws IOException {
46          final CreditCard creditCard = (CreditCard) component;
47          encodeMarkup(context, creditCard);
48          encodeScript(context, creditCard);
49      }
50  
51      protected void encodeMarkup(final FacesContext context, final CreditCard creditCard) throws IOException {
52          final ResponseWriter writer = context.getResponseWriter();
53          final String clientId = creditCard.getClientId(context);
54          final String widgetVar = creditCard.resolveWidgetVar();
55          final String styleClass = CreditCard.STYLE_CLASS;
56  
57          writer.startElement("div", creditCard);
58          writer.writeAttribute("id", clientId, "id");
59          writer.writeAttribute(HTML.WIDGET_VAR, widgetVar, null);
60          writer.writeAttribute(Attrs.CLASS, styleClass, "styleClass");
61          writer.endElement("div");
62      }
63  
64      protected void encodeScript(final FacesContext context, final CreditCard creditCard) throws IOException {
65          final WidgetBuilder wb = getWidgetBuilder(context);
66          wb.init("ExtCreditCard", creditCard);
67          wb.attr("width", creditCard.getWidth(), 350);
68          wb.attr("formatting", creditCard.isFormatting(), true);
69  
70          wb.nativeAttr("messages", "{validDate:'" + creditCard.getLabelValidDate() + "',"
71                      + "monthYear:'" + creditCard.getLabelMonthYear() + "'}");
72  
73          final StringBuilder placeholder = new StringBuilder(1024);
74          placeholder.append("{name:'").append(creditCard.getPlaceholderName()).append("'");
75          if (!LangUtils.isBlank(creditCard.getPlaceholderNumber())) {
76              placeholder.append(",number:'").append(creditCard.getPlaceholderNumber()).append("'");
77          }
78          if (!LangUtils.isBlank(creditCard.getPlaceholderExpiry())) {
79              placeholder.append(",expiry:'").append(creditCard.getPlaceholderExpiry()).append("'");
80          }
81          if (!LangUtils.isBlank(creditCard.getPlaceholderCvc())) {
82              placeholder.append(",cvc:'").append(creditCard.getPlaceholderCvc()).append("'");
83          }
84          placeholder.append("}");
85          wb.nativeAttr("placeholders", placeholder.toString());
86  
87          wb.finish();
88      }
89  
90  }