View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.internal.renderkit.renderer;
21  
22  import org.apache.myfaces.tobago.internal.component.AbstractUIStars;
23  import org.apache.myfaces.tobago.internal.util.HtmlRendererUtils;
24  import org.apache.myfaces.tobago.renderkit.css.BootstrapClass;
25  import org.apache.myfaces.tobago.renderkit.css.TobagoClass;
26  import org.apache.myfaces.tobago.renderkit.html.HtmlAttributes;
27  import org.apache.myfaces.tobago.renderkit.html.HtmlElements;
28  import org.apache.myfaces.tobago.renderkit.html.HtmlInputTypes;
29  import org.apache.myfaces.tobago.util.ComponentUtils;
30  import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
31  
32  import javax.faces.context.FacesContext;
33  import java.io.IOException;
34  
35  public class StarsRenderer<T extends AbstractUIStars> extends MessageLayoutRendererBase<T> {
36  
37    @Override
38    protected boolean isOutputOnly(T component) {
39      return component.isDisabled() || component.isReadonly();
40    }
41  
42    @Override
43    public HtmlElements getComponentTag() {
44      return HtmlElements.TOBAGO_STARS;
45    }
46  
47    @Override
48    protected void encodeBeginField(FacesContext facesContext, T component) throws IOException {
49  
50      final TobagoResponseWriter writer = getResponseWriter(facesContext);
51      final String clientId = component.getClientId(facesContext);
52      final String fieldId = component.getFieldId(facesContext);
53      final String hiddenInputId = clientId + ComponentUtils.SUB_SEPARATOR + "input";
54      final String sliderId = clientId + ComponentUtils.SUB_SEPARATOR + "slider";
55      final int value = component.getRangeValue();
56      final int max = component.getRangeMax();
57      final Double placeholder = component.getPlaceholder();
58      final boolean readonly = component.isReadonly();
59      final boolean disabled = component.isDisabled();
60      final boolean required = component.isRequired();
61      final String title = HtmlRendererUtils.getTitleFromTipAndMessages(facesContext, component);
62  
63      final String sliderValue = component.getSubmittedValue() != null
64          ? (String) component.getSubmittedValue() : String.valueOf(value);
65      final String hiddenInputValue = required && "0".equals(sliderValue) ? null : sliderValue;
66  
67      writer.startElement(HtmlElements.DIV);
68      writer.writeIdAttribute(fieldId);
69      writer.writeClassAttribute(
70          TobagoClass.STARS,
71          TobagoClass.STARS.createMarkup(component.getMarkup()),
72          component.getCustomClass());
73  
74      // The hidden input must be used to submit the rating. The 'required' attribute is not allowed on slider component.
75      writer.startElement(HtmlElements.INPUT);
76      writer.writeIdAttribute(hiddenInputId);
77      writer.writeNameAttribute(clientId);
78      writer.writeAttribute(HtmlAttributes.TYPE, HtmlInputTypes.HIDDEN);
79      writer.writeAttribute(HtmlAttributes.VALUE, hiddenInputValue, true);
80      writer.writeAttribute(HtmlAttributes.READONLY, readonly);
81      writer.writeAttribute(HtmlAttributes.DISABLED, disabled);
82      writer.writeAttribute(HtmlAttributes.REQUIRED, required);
83      writer.endElement(HtmlElements.INPUT);
84  
85      writer.startElement(HtmlElements.SPAN);
86      writer.writeClassAttribute(TobagoClass.STARS__CONTAINER);
87  
88      writer.startElement(HtmlElements.INPUT);
89      writer.writeIdAttribute(sliderId);
90      writer.writeNameAttribute(clientId);
91      writer.writeClassAttribute(TobagoClass.STARS__SLIDER);
92      writer.writeAttribute(HtmlAttributes.TYPE, readonly || disabled ? HtmlInputTypes.HIDDEN : HtmlInputTypes.RANGE);
93      writer.writeAttribute(HtmlAttributes.MIN, required ? 1 : 0);
94      writer.writeAttribute(HtmlAttributes.MAX, max);
95      writer.writeAttribute(HtmlAttributes.VALUE, sliderValue, true);
96      if (placeholder != null) {
97        writer.writeAttribute(HtmlAttributes.PLACEHOLDER, placeholder.toString(), true);
98      }
99      writer.writeAttribute(HtmlAttributes.READONLY, readonly);
100     writer.writeAttribute(HtmlAttributes.DISABLED, disabled);
101     writer.writeAttribute(HtmlAttributes.REQUIRED, required);
102     renderFocus(clientId, component.isFocus(), component.isError(), facesContext, writer);
103     writer.writeAttribute(HtmlAttributes.TABINDEX, component.getTabIndex());
104     writer.writeAttribute(HtmlAttributes.TITLE, title, true);
105     writer.endElement(HtmlElements.INPUT);
106 
107     encodeBehavior(writer, facesContext, component);
108 
109     writer.startElement(HtmlElements.DIV);
110     writer.writeClassAttribute(TobagoClass.STARS__FOCUS_BOX);
111     writer.endElement(HtmlElements.DIV);
112     writer.startElement(HtmlElements.SPAN);
113     writer.writeClassAttribute(TobagoClass.STARS__TOOLTIP, BootstrapClass.FADE);
114     writer.endElement(HtmlElements.SPAN);
115     writer.startElement(HtmlElements.SPAN);
116     writer.writeClassAttribute(TobagoClass.STARS__SELECTED);
117     writer.endElement(HtmlElements.SPAN);
118     writer.startElement(HtmlElements.SPAN);
119     writer.writeClassAttribute(TobagoClass.STARS__UNSELECTED);
120     writer.endElement(HtmlElements.SPAN);
121     writer.startElement(HtmlElements.SPAN);
122     writer.writeClassAttribute(TobagoClass.STARS__PRESELECTED);
123     writer.endElement(HtmlElements.SPAN);
124 
125     writer.endElement(HtmlElements.SPAN);
126   }
127 
128   @Override
129   protected void encodeEndField(FacesContext facesContext, T component) throws IOException {
130     final TobagoResponseWriter writer = getResponseWriter(facesContext);
131     writer.endElement(HtmlElements.DIV);
132   }
133 
134   @Override
135   protected String getFieldId(FacesContext facesContext, T component) {
136     return component.getFieldId(facesContext);
137   }
138 }