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.renderkit.html.util;
21  
22  import org.apache.commons.lang.StringUtils;
23  import org.apache.myfaces.tobago.component.Attributes;
24  import org.apache.myfaces.tobago.component.Facets;
25  import org.apache.myfaces.tobago.component.UIPopup;
26  import org.apache.myfaces.tobago.event.PopupFacetActionListener;
27  import org.apache.myfaces.tobago.internal.component.AbstractUICommand;
28  import org.apache.myfaces.tobago.internal.component.AbstractUICommandBase;
29  import org.apache.myfaces.tobago.renderkit.util.RenderUtils;
30  import org.apache.myfaces.tobago.util.ComponentUtils;
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  
34  import javax.faces.component.UIComponent;
35  import javax.faces.component.ValueHolder;
36  import javax.faces.context.FacesContext;
37  import java.util.Arrays;
38  
39  /**
40   * @deprecated since 2.0.0. Please use {@link org.apache.myfaces.tobago.renderkit.html.CommandMap}
41   */
42  @Deprecated
43  public class CommandRendererHelper {
44  
45    private static final Logger LOG = LoggerFactory.getLogger(CommandRendererHelper.class);
46  
47    private String onclick;
48    private boolean disabled;
49    private String href;
50    private String target;
51  
52    public CommandRendererHelper(final FacesContext facesContext, final AbstractUICommandBase command) {
53      this(facesContext, command, null);
54    }
55  
56    public CommandRendererHelper(final FacesContext facesContext, final AbstractUICommandBase base, final Tag tag) {
57  
58      if (!(base instanceof AbstractUICommand)) {
59        return;
60      }
61      final AbstractUICommand command = (AbstractUICommand) base;
62      disabled = ComponentUtils.getBooleanAttribute(command, Attributes.DISABLED);
63  
64      if (disabled) {
65        onclick = "";
66        href = "";
67      } else {
68        href = "#"; // this is to make the link "active", needed for focus, cursor, etc.
69        final UIPopup popup = (UIPopup) command.getFacet(Facets.POPUP);
70        if (popup != null) {
71          if (!ComponentUtils.containsPopupActionListener(command)) {
72            command.addActionListener(new PopupFacetActionListener());
73          }
74        }
75  
76        final boolean transition = ComponentUtils.getBooleanAttribute(command, Attributes.TRANSITION);
77  
78        if (StringUtils.isNotEmpty(command.getLink()) || StringUtils.isNotEmpty(command.getResource())) {
79          final String url = RenderUtils.generateUrl(facesContext, command);
80          if (tag == Tag.ANCHOR) {
81            onclick = null;
82            href = url;
83            target = command.getTarget();
84          } else {
85            // TODO target
86            onclick = "Tobago.navigateToUrl('" + url + "');";
87          }
88        } else if (StringUtils.isNotEmpty(command.getOnclick())) {
89          onclick = prepareOnClick(facesContext, command);
90        } else if (command.getRenderedPartially().length > 0) {
91          final String clientId = command.getClientId(facesContext);
92          final String[] componentIds = command.getRenderedPartially();
93  
94          // TODO find a better way
95          final boolean popupAction = ComponentUtils.containsPopupActionListener(command);
96          if (popupAction) {
97            if (componentIds.length != 1) {
98              LOG.warn("more than one partially rendered component is not supported for popup! using first one: "
99                  + Arrays.toString(componentIds));
100           }
101           onclick = "Tobago.Popup.openWithAction(this, '"
102               + ComponentUtils.evaluateClientId(facesContext, command, componentIds[0]) + "', '" + clientId + "');";
103         } else {
104           onclick = "Tobago.reloadComponent(this, '"
105               + HtmlRendererUtils.getComponentIds(facesContext, command, componentIds) + "','" + clientId + "', {});";
106         }
107 
108       } else {
109         final String clientId = command.getClientId(facesContext);
110         final String target = ComponentUtils.getStringAttribute(command, Attributes.TARGET);
111         onclick = HtmlRendererUtils.createSubmitAction(clientId, transition, target, null);
112       }
113 
114       if (command.getAttributes().get(Attributes.POPUP_CLOSE) != null
115           && ComponentUtils.isInPopup(command)) {
116         final String value = (String) command.getAttributes().get(Attributes.POPUP_CLOSE);
117         if (value.equals("immediate")) {
118           onclick = "Tobago.Popup.close(this);";
119         } else if (value.equals("afterSubmit")
120             && command instanceof org.apache.myfaces.tobago.component.UICommand
121             && ((org.apache.myfaces.tobago.component.UICommand) command).getRenderedPartially().length > 0) {
122           onclick = "Tobago.Popup.unlockBehind();" + onclick + "Tobago.Popup.close(this);";
123         }
124 
125       }
126       onclick = appendConfirmationScript(onclick, command);
127     }
128   }
129 
130   private String prepareOnClick(final FacesContext facesContext, final AbstractUICommandBase base) {
131     if (!(base instanceof AbstractUICommand)) {
132       return null;
133     }
134     final AbstractUICommand command = (AbstractUICommand) base;
135     String onclick = command.getOnclick();
136     if (onclick.contains("@autoId")) {
137       onclick = StringUtils.replace(onclick, "@autoId", command.getClientId(facesContext));
138     }
139     return onclick;
140   }
141 
142   private String appendConfirmationScript(String onclick, final UIComponent component) {
143     final ValueHolder confirmation = (ValueHolder) component.getFacet(Facets.CONFIRMATION);
144     if (confirmation != null) {
145       final StringBuilder script = new StringBuilder("return confirm('");
146       script.append(confirmation.getValue());
147       script.append("')");
148       if (onclick != null) {
149         script.append(" && ");
150         script.append(onclick);
151       }
152       onclick = script.toString();
153     }
154     return onclick;
155   }
156 
157   public String getOnclick() {
158     return onclick;
159   }
160 
161   public boolean isDisabled() {
162     return disabled;
163   }
164 
165   public String getHref() {
166     return href;
167   }
168 
169   public String getTarget() {
170     return target;
171   }
172 
173   public static enum Tag {
174     ANCHOR, BUTTON
175   }
176 }