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.timer;
23  
24  import java.io.IOException;
25  
26  import javax.faces.FacesException;
27  import javax.faces.component.UIComponent;
28  import javax.faces.component.UIForm;
29  import javax.faces.context.FacesContext;
30  import javax.faces.context.ResponseWriter;
31  import javax.faces.event.ActionEvent;
32  import javax.faces.event.PhaseId;
33  
34  import org.primefaces.context.PrimeRequestContext;
35  import org.primefaces.extensions.util.Attrs;
36  import org.primefaces.renderkit.CoreRenderer;
37  import org.primefaces.util.AjaxRequestBuilder;
38  import org.primefaces.util.ComponentTraversalUtils;
39  import org.primefaces.util.Constants;
40  import org.primefaces.util.LangUtils;
41  import org.primefaces.util.WidgetBuilder;
42  
43  /**
44   * Renderer for {@link Timer}
45   *
46   * @author f.strazzullo
47   * @since 3.0.0
48   */
49  public class TimerRenderer extends CoreRenderer {
50  
51      public static final String RENDERER_TYPE = "org.primefaces.extensions.component.TimerRenderer";
52  
53      @Override
54      public void decode(final FacesContext context, final UIComponent component) {
55          final Timer timer = (Timer) component;
56  
57          if (context.getExternalContext().getRequestParameterMap().containsKey(timer.getClientId(context))) {
58              final ActionEvent event = new ActionEvent(timer);
59              if (timer.isImmediate()) {
60                  event.setPhaseId(PhaseId.APPLY_REQUEST_VALUES);
61              }
62              else {
63                  event.setPhaseId(PhaseId.INVOKE_APPLICATION);
64              }
65  
66              timer.queueEvent(event);
67          }
68      }
69  
70      @Override
71      public void encodeEnd(final FacesContext context, final UIComponent component) throws IOException {
72          final Timer timer = (Timer) component;
73  
74          encodeMarkup(context, timer);
75          encodeScript(context, timer);
76  
77      }
78  
79      protected void encodeMarkup(final FacesContext context, final Timer timer) throws IOException {
80          final ResponseWriter writer = context.getResponseWriter();
81  
82          writer.startElement("span", timer);
83          writer.writeAttribute("id", timer.getClientId(), null);
84          writer.writeAttribute("title", timer.getTitle(), null);
85          writer.writeAttribute(Attrs.CLASS, Timer.STYLE_CLASS + " " + timer.getStyleClass(), "styleclass");
86          writer.writeAttribute(Attrs.STYLE,
87                      (!timer.isVisible() ? "display:none;" : Constants.EMPTY_STRING) + timer.getStyle(), Attrs.STYLE);
88          writer.endElement("span");
89      }
90  
91      protected void encodeScript(final FacesContext context, final Timer timer) throws IOException {
92          final String clientId = timer.getClientId(context);
93  
94          final UIForm form = ComponentTraversalUtils.closestForm(timer);
95          if (form == null) {
96              throw new FacesException("Timer:" + clientId + " needs to be enclosed in a form component");
97          }
98  
99          final AjaxRequestBuilder builder = PrimeRequestContext.getCurrentInstance().getAjaxRequestBuilder();
100 
101         String request = null;
102         if (timer.getListener() != null) {
103             request = builder.init()
104                         .source(clientId)
105                         .form(timer, timer, form)
106                         .process(timer, timer.getProcess())
107                         .update(timer, timer.getUpdate())
108                         .async(timer.isAsync())
109                         .global(timer.isGlobal())
110                         .delay(timer.getDelay())
111                         .partialSubmit(timer.isPartialSubmit(), timer.isPartialSubmitSet(), timer.getPartialSubmitFilter())
112                         .resetValues(timer.isResetValues(), timer.isResetValuesSet())
113                         .ignoreAutoUpdate(timer.isIgnoreAutoUpdate())
114                         .onstart(timer.getOnstart())
115                         .onerror(timer.getOnerror())
116                         .onsuccess(timer.getOnsuccess())
117                         .oncomplete(timer.getOncomplete())
118                         .params(timer)
119                         .build();
120         }
121 
122         final WidgetBuilder wb = getWidgetBuilder(context);
123 
124         wb.init("ExtTimer", timer)
125                     .attr("timeout", timer.getTimeout()).attr("interval", timer.getInterval())
126                     .attr("singleRun", timer.isSingleRun()).attr("format", timer.getFormat())
127                     .attr("autoStart", timer.isAutoStart()).attr("forward", timer.isForward())
128                     .attr("locale", timer.calculateLocale().toString());
129 
130         if (request != null) {
131             wb.callback("listener", "function()", request);
132         }
133 
134         if (LangUtils.isNotBlank(timer.getOntimerstep())) {
135             wb.callback("ontimerstep", "function(intervalData)", timer.getOntimerstep());
136         }
137 
138         if (LangUtils.isNotBlank(timer.getFormatFunction())) {
139             wb.callback("formatFunction", "function(value)", timer.getFormatFunction());
140         }
141 
142         if (LangUtils.isNotBlank(timer.getOntimercomplete())) {
143             wb.callback("ontimercomplete", "function()", timer.getOntimercomplete());
144         }
145 
146         wb.finish();
147     }
148 
149 }