1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.myfaces.custom.inputAjax;
21
22 import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlTextRendererBase;
23 import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlRendererUtils;
24 import org.apache.myfaces.shared_tomahawk.renderkit.html.HTML;
25 import org.apache.myfaces.shared_tomahawk.renderkit.RendererUtils;
26 import org.apache.myfaces.renderkit.html.util.AddResource;
27 import org.apache.myfaces.custom.ajax.api.AjaxRenderer;
28 import org.apache.myfaces.custom.ajax.util.AjaxRendererUtils;
29 import org.apache.myfaces.renderkit.html.util.AddResourceFactory;
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32
33 import javax.faces.context.FacesContext;
34 import javax.faces.context.ResponseWriter;
35 import javax.faces.component.UIComponent;
36 import java.io.IOException;
37
38
39
40
41
42
43
44
45
46
47
48
49 public class HtmlInputTextAjaxRenderer extends HtmlTextRendererBase implements AjaxRenderer
50 {
51 private static final Log log = LogFactory.getLog(HtmlInputTextAjaxRenderer.class);
52 private static final String JAVASCRIPT_ENCODED = "org.apache.myfaces.custom.inputAjax.HtmlInputTextAjax.JAVASCRIPT_ENCODED";
53
54
55
56
57
58
59
60
61
62
63
64 private void encodeJavascript(FacesContext context, UIComponent component) throws IOException
65 {
66 HtmlInputTextAjax comp = (HtmlInputTextAjax) component;
67
68 AddResource addResource = AddResourceFactory.getInstance(context);
69
70 AjaxRendererUtils.addPrototypeScript(context, component, addResource);
71
72 ResponseWriter out = context.getResponseWriter();
73 AjaxRendererUtils.writeAjaxScript(context, out, comp);
74
75 context.getExternalContext().getRequestMap().put(JAVASCRIPT_ENCODED, Boolean.TRUE);
76 }
77
78
79
80
81 public void encodeEnd(FacesContext context, UIComponent component) throws IOException
82 {
83 log.debug("encodeEnd in HtmlInputTextAjaxRenderer");
84 RendererUtils.checkParamValidity(context, component, HtmlInputTextAjax.class);
85
86 if (HtmlRendererUtils.isDisplayValueOnly(component) || isDisabled(context, component))
87 {
88 super.encodeEnd(context, component);
89 return;
90 }
91
92 String clientId = component.getClientId(context);
93 String submitFunctionStart = AjaxRendererUtils.JS_MYFACES_NAMESPACE + "ajaxSubmit1('" + clientId + "');";
94 HtmlInputTextAjax comp = (HtmlInputTextAjax) component;
95 String loadingStyleClass = AjaxRendererUtils.STYLECLASS_LOADER;
96
97 String styleClass = comp.getStyleClass();
98 if(styleClass != null && styleClass.length() > 0)
99 {
100 comp.setStyleClass(loadingStyleClass + " " + styleClass);
101 }
102 else
103 {
104 comp.setStyleClass(loadingStyleClass);
105 }
106
107 if(!comp.getShowOkButton().booleanValue()){
108
109
110 String onchange = comp.getOnchange();
111 if(onchange != null && onchange.length() > 0)
112 {
113 if(!onchange.endsWith(";"))
114 {
115 onchange += ";";
116 }
117 comp.setOnchange(submitFunctionStart+onchange);
118 }
119 else
120 {
121 comp.setOnchange(submitFunctionStart);
122 }
123 }
124 this.encodeJavascript(context, component);
125 super.encodeEnd(context, component);
126
127 ResponseWriter writer = context.getResponseWriter();
128 if(comp.getShowOkButton() != null && comp.getShowOkButton().booleanValue()){
129
130 writer.startElement(HTML.INPUT_ELEM, comp);
131 writer.writeAttribute(HTML.TYPE_ATTR, HTML.INPUT_TYPE_BUTTON, null);
132 String okText = comp.getOkText() != null ? comp.getOkText() : "Ok";
133 writer.writeAttribute(HTML.VALUE_ATTR, okText, null);
134 writer.writeAttribute(HTML.ONCLICK_ATTR, submitFunctionStart, null);
135 writer.endElement(HTML.INPUT_TYPE_BUTTON);
136 if(comp.getShowCancelButton() != null && comp.getShowCancelButton().booleanValue()){
137
138 writer.write(" ");
139 writer.startElement(HTML.ANCHOR_ELEM, comp);
140 writer.writeAttribute(HTML.HREF_ATTR, "javascript:" + AjaxRendererUtils.JS_MYFACES_NAMESPACE + "clearById('" + clientId + "');", null);
141 String cancelText = comp.getCancelText() != null ? comp.getCancelText() : "cancel";
142 writer.writeText(cancelText, null);
143 writer.endElement(HTML.ANCHOR_ELEM);
144 }
145 }
146
147
148
149
150
151
152
153
154 }
155
156 public void encodeAjax(FacesContext context, UIComponent component) throws IOException
157 {
158 log.debug("encodeAjax in HtmlInputTextAjaxRenderer");
159 AjaxRendererUtils.encodeAjax(context, component);
160 }
161 }