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.inputplace;
23  
24  import java.io.IOException;
25  import java.util.Locale;
26  
27  import javax.faces.component.UIComponent;
28  import javax.faces.context.FacesContext;
29  import javax.faces.context.ResponseWriter;
30  
31  import org.primefaces.renderkit.InputRenderer;
32  import org.primefaces.util.ComponentUtils;
33  import org.primefaces.util.HTML;
34  import org.primefaces.util.LangUtils;
35  import org.primefaces.util.WidgetBuilder;
36  
37  /**
38   * Renderer for the {@link InputPlace} component.
39   *
40   * @since 14.0.0
41   */
42  public class InputPlaceRenderer extends InputRenderer {
43  
44      @Override
45      public void decode(FacesContext context, UIComponent component) {
46          InputPlace inputPlace = (InputPlace) component;
47  
48          if (!shouldDecode(inputPlace)) {
49              return;
50          }
51  
52          decodeBehaviors(context, inputPlace);
53  
54          String clientId = inputPlace.getClientId(context);
55          String submittedValue = context.getExternalContext().getRequestParameterMap().get(clientId);
56  
57          if (submittedValue != null) {
58              int maxlength = inputPlace.getMaxlength();
59              if (maxlength > 0 && submittedValue.length() > maxlength) {
60                  submittedValue = LangUtils.substring(submittedValue, 0, maxlength);
61              }
62              inputPlace.setSubmittedValue(submittedValue);
63          }
64      }
65  
66      @Override
67      public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
68          InputPlace inputPlace = (InputPlace) component;
69  
70          encodeMarkup(context, inputPlace);
71          encodeScript(context, inputPlace);
72      }
73  
74      protected void encodeScript(FacesContext context, InputPlace inputPlace) throws IOException {
75          WidgetBuilder wb = getWidgetBuilder(context);
76          wb.init("ExtInputPlace", inputPlace)
77                      .attr("apiType", inputPlace.getApiType().toLowerCase(Locale.ROOT))
78                      .attr("apiKey", inputPlace.getApiKey(), null)
79                      .attr("restrictTypes", inputPlace.getRestrictTypes(), null)
80                      .attr("restrictCountries", inputPlace.getRestrictCountries(), null)
81                      .callback("onPlaceChanged", "function(place)", inputPlace.getOnplacechanged());
82  
83          encodeClientBehaviors(context, inputPlace);
84          wb.finish();
85      }
86  
87      protected void encodeMarkup(FacesContext context, InputPlace inputPlace) throws IOException {
88          ResponseWriter writer = context.getResponseWriter();
89          String clientId = inputPlace.getClientId(context);
90  
91          writer.startElement("input", inputPlace);
92          writer.writeAttribute("id", clientId, null);
93          writer.writeAttribute("name", clientId, null);
94          writer.writeAttribute("type", "text", null);
95  
96          String valueToRender = ComponentUtils.getValueToRender(context, inputPlace);
97          if (valueToRender != null) {
98              writer.writeAttribute("value", valueToRender, null);
99          }
100 
101         if (inputPlace.getStyle() != null) {
102             writer.writeAttribute("style", inputPlace.getStyle(), null);
103         }
104 
105         writer.writeAttribute("class", createStyleClass(inputPlace, InputPlace.STYLE_CLASS), "styleClass");
106 
107         renderAccessibilityAttributes(context, inputPlace);
108         renderRTLDirection(context, inputPlace);
109         renderPassThruAttributes(context, inputPlace, HTML.INPUT_TEXT_ATTRS_WITHOUT_EVENTS);
110         renderDomEvents(context, inputPlace, HTML.INPUT_TEXT_EVENTS);
111         renderValidationMetadata(context, inputPlace);
112 
113         writer.endElement("input");
114     }
115 
116 }