View Javadoc
1   /**
2    * Copyright 2011-2019 PrimeFaces Extensions
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.primefaces.extensions.renderer;
17  
18  import java.io.IOException;
19  import javax.el.ValueExpression;
20  import javax.faces.context.FacesContext;
21  import org.primefaces.component.commandbutton.CommandButton;
22  import org.primefaces.component.commandbutton.CommandButtonRenderer;
23  
24  /**
25   * {@link CommandButton} renderer disabling the button while action is processed for buttons that are using Ajax.
26   *
27   * @author Jasper de Vries <jepsar@gmail.com>
28   * @since 8.0
29   */
30  public class CommandButtonSingleClickRenderer extends CommandButtonRenderer {
31  
32      @Override
33      protected void encodeMarkup(FacesContext context, CommandButton button) throws IOException {
34          if (isEligible(button)) {
35              String widgetVar = button.resolveWidgetVar(context);
36              String onClick = getAttributeValue(context, button, "onclick");
37              String onComplete = getAttributeValue(context, button, "oncomplete");
38              button.setOnclick(prefix(onClick, getToggleJS(widgetVar, false)));
39              button.setOncomplete(prefix(onComplete, getToggleJS(widgetVar, true)));
40          }
41          super.encodeMarkup(context, button);
42      }
43  
44      protected boolean isEligible(final CommandButton button) {
45          return button.isAjax()
46                 && button.isRendered()
47                 && button.getActionExpression() != null
48                 && !button.isDisabled()
49                 && !isConfirmation(button);
50      }
51  
52      protected boolean isConfirmation(final CommandButton button) {
53          String styleClass = button.getStyleClass();
54          return styleClass != null && styleClass.contains("ui-confirmdialog");
55      }
56  
57      protected String getToggleJS(final String widgetVar, final boolean enabled) {
58          return String.format("var w=PF('%s');if(w){w.%sable();};", widgetVar, enabled ? "en" : "dis");
59      }
60  
61      protected String getAttributeValue(final FacesContext context, final CommandButton button, final String attribute) {
62          ValueExpression ve = button.getValueExpression(attribute);
63          return ve == null ? null : (String) ve.getValue(context.getELContext());
64      }
65  
66      protected String prefix(final String base, final String prefix) {
67          return base == null ? prefix : prefix + base;
68      }
69  
70  }