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