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  package org.apache.myfaces.custom.effect;
20  
21  import javax.faces.FacesException;
22  import javax.faces.application.ResourceDependencies;
23  import javax.faces.application.ResourceDependency;
24  import javax.faces.component.UIComponent;
25  import javax.faces.component.behavior.ClientBehavior;
26  import javax.faces.component.behavior.ClientBehaviorContext;
27  import javax.faces.render.ClientBehaviorRenderer;
28  
29  import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFClientBehaviorRenderer;
30  
31  @JSFClientBehaviorRenderer(
32          renderKitId = "HTML_BASIC",
33          type = "org.apache.myfaces.custom.effect.EffectToggleBehavior")
34  @ResourceDependencies({
35      @ResourceDependency(library="oam.custom.prototype", name="prototype.js"),
36      @ResourceDependency(library="oam.custom.prototype", name="effects.js")
37  })
38  public class EffectToggleClientBehaviorRenderer extends ClientBehaviorRenderer
39  {
40      /**
41       * Effect.Fade('id_of_element', { duration: 3.0 });
42       */
43      @Override
44      public String getScript(ClientBehaviorContext behaviorContext,
45              ClientBehavior behavior)
46      {
47          AbstractEffectToggleClientBehavior effectBehavior = (AbstractEffectToggleClientBehavior) behavior;
48          
49          UIComponent target = ( effectBehavior.getForId() != null ) ? 
50                  behaviorContext.getComponent().findComponent(effectBehavior.getForId()) : 
51                      behaviorContext.getComponent();
52          if (target == null)
53          {
54              throw new FacesException("Cannot find component set with forId: "+effectBehavior.getForId());
55          }
56          String clientId = target.getClientId();
57          
58          StringBuilder sb = new StringBuilder();
59          
60          sb.append("new Effect.toggle('");
61          sb.append(clientId);
62          sb.append('\'');
63          sb.append(',');
64          sb.append('\'');
65          sb.append(effectBehavior.getMode());
66          sb.append('\'');
67          
68          if (effectBehavior.getDelay() != null || effectBehavior.getDuration() != null)
69          {
70              sb.append(",{");
71              boolean addComma = false;
72              addComma = EffectUtils.addProperty(sb, "delay", effectBehavior.getDelay(), addComma);
73              addComma = EffectUtils.addProperty(sb, "duration", effectBehavior.getDuration(), addComma);
74              sb.append('}');
75          }
76          sb.append(')');
77          
78          if (effectBehavior.getAppendJs() != null && effectBehavior.getAppendJs().length() > 0)
79          {
80              sb.append(';');
81              sb.append(effectBehavior.getAppendJs());
82          }
83  
84          return sb.toString();
85      }
86      
87  }