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.behavior.javascript;
23  
24  import java.util.Collection;
25  
26  import javax.faces.component.UIComponent;
27  import javax.faces.component.UIParameter;
28  import javax.faces.component.behavior.ClientBehavior;
29  import javax.faces.component.behavior.ClientBehaviorContext;
30  import javax.faces.context.FacesContext;
31  import javax.faces.render.ClientBehaviorRenderer;
32  
33  import org.primefaces.component.api.ClientBehaviorRenderingMode;
34  
35  /**
36   * {@link ClientBehaviorRenderer} implementation for the {@link JavascriptBehavior}.
37   *
38   * @author Thomas Andraschko / last modified by $Author$
39   * @version $Revision$
40   * @since 0.2
41   */
42  public class JavascriptBehaviorRenderer extends ClientBehaviorRenderer {
43  
44      @Override
45      public String getScript(final ClientBehaviorContext behaviorContext, final ClientBehavior behavior) {
46          final JavascriptBehavior javascriptBehavior = (JavascriptBehavior) behavior;
47          if (javascriptBehavior.isDisabled()) {
48              return null;
49          }
50  
51          final FacesContext context = behaviorContext.getFacesContext();
52          final UIComponent component = behaviorContext.getComponent();
53          String source = behaviorContext.getSourceId();
54          if (source == null) {
55              source = component.getClientId(context);
56          }
57  
58          final StringBuilder script = new StringBuilder();
59          script.append("PrimeFacesExt.behavior.Javascript({");
60          script.append("source:'").append(source).append("'");
61          script.append(",event:'").append(behaviorContext.getEventName()).append("'");
62          script.append(",execute:function(source,event,params,ext){");
63          script.append(javascriptBehavior.getExecute()).append(";}");
64  
65          // params
66          boolean paramWritten = false;
67  
68          for (int i = 0; i < component.getChildCount(); i++) {
69              final UIComponent child = component.getChildren().get(i);
70              if (child instanceof UIParameter) {
71                  final UIParameter parameter = (UIParameter) child;
72  
73                  if (paramWritten) {
74                      script.append(",");
75                  }
76                  else {
77                      paramWritten = true;
78                      script.append(",params:{");
79                  }
80  
81                  script.append("'");
82                  script.append(parameter.getName()).append("':'").append(parameter.getValue());
83                  script.append("'");
84              }
85          }
86  
87          if (paramWritten) {
88              script.append("}");
89          }
90  
91          ClientBehaviorRenderingMode renderingMode = getClientBehaviorRenderingMode(behaviorContext);
92  
93          if (ClientBehaviorRenderingMode.UNOBSTRUSIVE.equals(renderingMode)) {
94              script.append("},ext);");
95          }
96          else {
97              script.append("});");
98          }
99  
100         return script.toString();
101     }
102 
103     protected static ClientBehaviorRenderingMode getClientBehaviorRenderingMode(final ClientBehaviorContext behaviorContext) {
104         final Collection<ClientBehaviorContext.Parameter> behaviorParameters = behaviorContext.getParameters();
105         if (behaviorParameters == null || behaviorParameters.isEmpty()) {
106             return null;
107         }
108         for (final ClientBehaviorContext.Parameter behaviorParameter : behaviorParameters) {
109             if (behaviorParameter.getValue() instanceof ClientBehaviorRenderingMode) {
110                 return (ClientBehaviorRenderingMode) behaviorParameter.getValue();
111             }
112         }
113         return null;
114     }
115 }