1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
26
27
28
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 }