1 | /* |
---|
2 | * Ext JS Library 2.2.1 |
---|
3 | * Copyright(c) 2006-2009, Ext JS, LLC. |
---|
4 | * licensing@extjs.com |
---|
5 | * |
---|
6 | * http://extjs.com/license |
---|
7 | */ |
---|
8 | |
---|
9 | /** |
---|
10 | * @class Ext.form.Action |
---|
11 | * <p>The subclasses of this class provide actions to perform upon {@link Ext.form.BasicForm Form}s.</p> |
---|
12 | * <p>Instances of this class are only created by a {@link Ext.form.BasicForm Form} when |
---|
13 | * the Form needs to perform an action such as submit or load. The Configuration options |
---|
14 | * listed for this class are set through the Form's action methods: {@link Ext.form.BasicForm#submit submit}, |
---|
15 | * {@link Ext.form.BasicForm#load load} and {@link Ext.form.BasicForm#doAction doAction}</p> |
---|
16 | * <p>The instance of Action which performed the action is passed to the success |
---|
17 | * and failure callbacks of the Form's action methods ({@link Ext.form.BasicForm#submit submit}, |
---|
18 | * {@link Ext.form.BasicForm#load load} and {@link Ext.form.BasicForm#doAction doAction}), |
---|
19 | * and to the {@link Ext.form.BasicForm#actioncomplete actioncomplete} and |
---|
20 | * {@link Ext.form.BasicForm#actionfailed actionfailed} event handlers.</p> |
---|
21 | */ |
---|
22 | Ext.form.Action = function(form, options){ |
---|
23 | this.form = form; |
---|
24 | this.options = options || {}; |
---|
25 | }; |
---|
26 | |
---|
27 | /** |
---|
28 | * Failure type returned when client side validation of the Form fails |
---|
29 | * thus aborting a submit action. |
---|
30 | * @type {String} |
---|
31 | * @static |
---|
32 | */ |
---|
33 | Ext.form.Action.CLIENT_INVALID = 'client'; |
---|
34 | /** |
---|
35 | * Failure type returned when server side validation of the Form fails |
---|
36 | * indicating that field-specific error messages have been returned in the |
---|
37 | * response's <tt style="font-weight:bold">errors</tt> property. |
---|
38 | * @type {String} |
---|
39 | * @static |
---|
40 | */ |
---|
41 | Ext.form.Action.SERVER_INVALID = 'server'; |
---|
42 | /** |
---|
43 | * Failure type returned when a communication error happens when attempting |
---|
44 | * to send a request to the remote server. |
---|
45 | * @type {String} |
---|
46 | * @static |
---|
47 | */ |
---|
48 | Ext.form.Action.CONNECT_FAILURE = 'connect'; |
---|
49 | /** |
---|
50 | * Failure type returned when no field values are returned in the response's |
---|
51 | * <tt style="font-weight:bold">data</tt> property. |
---|
52 | * @type {String} |
---|
53 | * @static |
---|
54 | */ |
---|
55 | Ext.form.Action.LOAD_FAILURE = 'load'; |
---|
56 | |
---|
57 | Ext.form.Action.prototype = { |
---|
58 | /** |
---|
59 | * @cfg {String} url The URL that the Action is to invoke. |
---|
60 | */ |
---|
61 | /** |
---|
62 | * @cfg {Boolean} reset When set to <tt><b>true</b></tt>, causes the Form to be |
---|
63 | * {@link Ext.form.BasicForm.reset reset} on Action success. If specified, this happens |
---|
64 | * <b>before</b> the {@link #success} callback is called and before the Form's |
---|
65 | * {@link Ext.form.BasicForm.actioncomplete actioncomplete} event fires. |
---|
66 | */ |
---|
67 | /** |
---|
68 | * @cfg {String} method The HTTP method to use to access the requested URL. Defaults to the |
---|
69 | * {@link Ext.form.BasicForm}'s method, or if that is not specified, the underlying DOM form's method. |
---|
70 | */ |
---|
71 | /** |
---|
72 | * @cfg {Mixed} params Extra parameter values to pass. These are added to the Form's |
---|
73 | * {@link Ext.form.BasicForm#baseParams} and passed to the specified URL along with the Form's |
---|
74 | * input fields. |
---|
75 | */ |
---|
76 | /** |
---|
77 | * @cfg {Number} timeout The number of milliseconds to wait for a server response before |
---|
78 | * failing with the {@link #failureType} as {@link #Action.CONNECT_FAILURE}. |
---|
79 | */ |
---|
80 | /** |
---|
81 | * @cfg {Function} success The function to call when a valid success return packet is recieved. |
---|
82 | * The function is passed the following parameters:<ul class="mdetail-params"> |
---|
83 | * <li><b>form</b> : Ext.form.BasicForm<div class="sub-desc">The form that requested the action</div></li> |
---|
84 | * <li><b>action</b> : Ext.form.Action<div class="sub-desc">The Action class. The {@link #result} |
---|
85 | * property of this object may be examined to perform custom postprocessing.</div></li> |
---|
86 | * </ul> |
---|
87 | */ |
---|
88 | /** |
---|
89 | * @cfg {Function} failure The function to call when a failure packet was recieved, or when an |
---|
90 | * error ocurred in the Ajax communication. |
---|
91 | * The function is passed the following parameters:<ul class="mdetail-params"> |
---|
92 | * <li><b>form</b> : Ext.form.BasicForm<div class="sub-desc">The form that requested the action</div></li> |
---|
93 | * <li><b>action</b> : Ext.form.Action<div class="sub-desc">The Action class. If an Ajax |
---|
94 | * error ocurred, the failure type will be in {@link #failureType}. The {@link #result} |
---|
95 | * property of this object may be examined to perform custom postprocessing.</div></li> |
---|
96 | * </ul> |
---|
97 | */ |
---|
98 | /** |
---|
99 | * @cfg {Object} scope The scope in which to call the callback functions (The <tt>this</tt> reference |
---|
100 | * for the callback functions). |
---|
101 | */ |
---|
102 | /** |
---|
103 | * @cfg {String} waitMsg The message to be displayed by a call to {@link Ext.MessageBox#wait} |
---|
104 | * during the time the action is being processed. |
---|
105 | */ |
---|
106 | /** |
---|
107 | * @cfg {String} waitTitle The title to be displayed by a call to {@link Ext.MessageBox#wait} |
---|
108 | * during the time the action is being processed. |
---|
109 | */ |
---|
110 | |
---|
111 | /** |
---|
112 | * The type of action this Action instance performs. |
---|
113 | * Currently only "submit" and "load" are supported. |
---|
114 | * @type {String} |
---|
115 | */ |
---|
116 | type : 'default', |
---|
117 | /** |
---|
118 | * The type of failure detected. See {@link link Ext.form.Action#Action.CLIENT_INVALID CLIENT_INVALID}, |
---|
119 | * {@link link Ext.form.Action#Action.SERVER_INVALID SERVER_INVALID}, |
---|
120 | * {@link #link Ext.form.ActionAction.CONNECT_FAILURE CONNECT_FAILURE}, {@link Ext.form.Action#Action.LOAD_FAILURE LOAD_FAILURE} |
---|
121 | * @property failureType |
---|
122 | * @type {String} |
---|
123 | *//** |
---|
124 | * The XMLHttpRequest object used to perform the action. |
---|
125 | * @property response |
---|
126 | * @type {Object} |
---|
127 | *//** |
---|
128 | * The decoded response object containing a boolean <tt style="font-weight:bold">success</tt> property and |
---|
129 | * other, action-specific properties. |
---|
130 | * @property result |
---|
131 | * @type {Object} |
---|
132 | */ |
---|
133 | |
---|
134 | // interface method |
---|
135 | run : function(options){ |
---|
136 | |
---|
137 | }, |
---|
138 | |
---|
139 | // interface method |
---|
140 | success : function(response){ |
---|
141 | |
---|
142 | }, |
---|
143 | |
---|
144 | // interface method |
---|
145 | handleResponse : function(response){ |
---|
146 | |
---|
147 | }, |
---|
148 | |
---|
149 | // default connection failure |
---|
150 | failure : function(response){ |
---|
151 | this.response = response; |
---|
152 | this.failureType = Ext.form.Action.CONNECT_FAILURE; |
---|
153 | this.form.afterAction(this, false); |
---|
154 | }, |
---|
155 | |
---|
156 | // private |
---|
157 | processResponse : function(response){ |
---|
158 | this.response = response; |
---|
159 | if(!response.responseText){ |
---|
160 | return true; |
---|
161 | } |
---|
162 | this.result = this.handleResponse(response); |
---|
163 | return this.result; |
---|
164 | }, |
---|
165 | |
---|
166 | // utility functions used internally |
---|
167 | getUrl : function(appendParams){ |
---|
168 | var url = this.options.url || this.form.url || this.form.el.dom.action; |
---|
169 | if(appendParams){ |
---|
170 | var p = this.getParams(); |
---|
171 | if(p){ |
---|
172 | url += (url.indexOf('?') != -1 ? '&' : '?') + p; |
---|
173 | } |
---|
174 | } |
---|
175 | return url; |
---|
176 | }, |
---|
177 | |
---|
178 | // private |
---|
179 | getMethod : function(){ |
---|
180 | return (this.options.method || this.form.method || this.form.el.dom.method || 'POST').toUpperCase(); |
---|
181 | }, |
---|
182 | |
---|
183 | // private |
---|
184 | getParams : function(){ |
---|
185 | var bp = this.form.baseParams; |
---|
186 | var p = this.options.params; |
---|
187 | if(p){ |
---|
188 | if(typeof p == "object"){ |
---|
189 | p = Ext.urlEncode(Ext.applyIf(p, bp)); |
---|
190 | }else if(typeof p == 'string' && bp){ |
---|
191 | p += '&' + Ext.urlEncode(bp); |
---|
192 | } |
---|
193 | }else if(bp){ |
---|
194 | p = Ext.urlEncode(bp); |
---|
195 | } |
---|
196 | return p; |
---|
197 | }, |
---|
198 | |
---|
199 | // private |
---|
200 | createCallback : function(opts){ |
---|
201 | var opts = opts || {}; |
---|
202 | return { |
---|
203 | success: this.success, |
---|
204 | failure: this.failure, |
---|
205 | scope: this, |
---|
206 | timeout: (opts.timeout*1000) || (this.form.timeout*1000), |
---|
207 | upload: this.form.fileUpload ? this.success : undefined |
---|
208 | }; |
---|
209 | } |
---|
210 | }; |
---|
211 | |
---|
212 | /** |
---|
213 | * @class Ext.form.Action.Submit |
---|
214 | * @extends Ext.form.Action |
---|
215 | * <p>A class which handles submission of data from {@link Ext.form.BasicForm Form}s |
---|
216 | * and processes the returned response.</p> |
---|
217 | * <p>Instances of this class are only created by a {@link Ext.form.BasicForm Form} when |
---|
218 | * {@link Ext.form.BasicForm#submit submit}ting.</p> |
---|
219 | * <p>A response packet must contain a boolean <tt style="font-weight:bold">success</tt> property, and, optionally |
---|
220 | * an <tt style="font-weight:bold">errors</tt> property. The <tt style="font-weight:bold">errors</tt> property contains error |
---|
221 | * messages for invalid fields.</p> |
---|
222 | * <p>By default, response packets are assumed to be JSON, so a typical response |
---|
223 | * packet may look like this:</p><pre><code> |
---|
224 | { |
---|
225 | success: false, |
---|
226 | errors: { |
---|
227 | clientCode: "Client not found", |
---|
228 | portOfLoading: "This field must not be null" |
---|
229 | } |
---|
230 | }</code></pre> |
---|
231 | * <p>Other data may be placed into the response for processing by the {@link Ext.form.BasicForm}'s callback |
---|
232 | * or event handler methods. The object decoded from this JSON is available in the {@link #result} property.</p> |
---|
233 | * <p>Alternatively, if an {@link #errorReader} is specified as an {@link Ext.data.XmlReader XmlReader}:</p><pre><code> |
---|
234 | errorReader: new Ext.data.XmlReader({ |
---|
235 | record : 'field', |
---|
236 | success: '@success' |
---|
237 | }, [ |
---|
238 | 'id', 'msg' |
---|
239 | ] |
---|
240 | ) |
---|
241 | </code></pre> |
---|
242 | * <p>then the results may be sent back in XML format:</p><pre><code> |
---|
243 | <?xml version="1.0" encoding="UTF-8"?> |
---|
244 | <message success="false"> |
---|
245 | <errors> |
---|
246 | <field> |
---|
247 | <id>clientCode</id> |
---|
248 | <msg><![CDATA[Code not found. <br /><i>This is a test validation message from the server </i>]]></msg> |
---|
249 | </field> |
---|
250 | <field> |
---|
251 | <id>portOfLoading</id> |
---|
252 | <msg><![CDATA[Port not found. <br /><i>This is a test validation message from the server </i>]]></msg> |
---|
253 | </field> |
---|
254 | </errors> |
---|
255 | </message> |
---|
256 | </code></pre> |
---|
257 | * <p>Other elements may be placed into the response XML for processing by the {@link Ext.form.BasicForm}'s callback |
---|
258 | * or event handler methods. The XML document is available in the {@link #errorReader}'s {@link Ext.data.XmlReader#xmlData xmlData} property.</p> |
---|
259 | */ |
---|
260 | Ext.form.Action.Submit = function(form, options){ |
---|
261 | Ext.form.Action.Submit.superclass.constructor.call(this, form, options); |
---|
262 | }; |
---|
263 | |
---|
264 | Ext.extend(Ext.form.Action.Submit, Ext.form.Action, { |
---|
265 | /** |
---|
266 | * @cfg {Ext.data.DataReader} errorReader <b>Optional. JSON is interpreted with no need for an errorReader.</b> |
---|
267 | * <p>A Reader which reads a single record from the returned data. The DataReader's <b>success</b> property specifies |
---|
268 | * how submission success is determined. The Record's data provides the error messages to apply to any invalid form Fields.</p>. |
---|
269 | */ |
---|
270 | /** |
---|
271 | * @cfg {boolean} clientValidation Determines whether a Form's fields are validated |
---|
272 | * in a final call to {@link Ext.form.BasicForm#isValid isValid} prior to submission. |
---|
273 | * Pass <tt>false</tt> in the Form's submit options to prevent this. If not defined, pre-submission field validation |
---|
274 | * is performed. |
---|
275 | */ |
---|
276 | type : 'submit', |
---|
277 | |
---|
278 | // private |
---|
279 | run : function(){ |
---|
280 | var o = this.options; |
---|
281 | var method = this.getMethod(); |
---|
282 | var isGet = method == 'GET'; |
---|
283 | if(o.clientValidation === false || this.form.isValid()){ |
---|
284 | Ext.Ajax.request(Ext.apply(this.createCallback(o), { |
---|
285 | form:this.form.el.dom, |
---|
286 | url:this.getUrl(isGet), |
---|
287 | method: method, |
---|
288 | headers: o.headers, |
---|
289 | params:!isGet ? this.getParams() : null, |
---|
290 | isUpload: this.form.fileUpload |
---|
291 | })); |
---|
292 | }else if (o.clientValidation !== false){ // client validation failed |
---|
293 | this.failureType = Ext.form.Action.CLIENT_INVALID; |
---|
294 | this.form.afterAction(this, false); |
---|
295 | } |
---|
296 | }, |
---|
297 | |
---|
298 | // private |
---|
299 | success : function(response){ |
---|
300 | var result = this.processResponse(response); |
---|
301 | if(result === true || result.success){ |
---|
302 | this.form.afterAction(this, true); |
---|
303 | return; |
---|
304 | } |
---|
305 | if(result.errors){ |
---|
306 | this.form.markInvalid(result.errors); |
---|
307 | this.failureType = Ext.form.Action.SERVER_INVALID; |
---|
308 | } |
---|
309 | this.form.afterAction(this, false); |
---|
310 | }, |
---|
311 | |
---|
312 | // private |
---|
313 | handleResponse : function(response){ |
---|
314 | if(this.form.errorReader){ |
---|
315 | var rs = this.form.errorReader.read(response); |
---|
316 | var errors = []; |
---|
317 | if(rs.records){ |
---|
318 | for(var i = 0, len = rs.records.length; i < len; i++) { |
---|
319 | var r = rs.records[i]; |
---|
320 | errors[i] = r.data; |
---|
321 | } |
---|
322 | } |
---|
323 | if(errors.length < 1){ |
---|
324 | errors = null; |
---|
325 | } |
---|
326 | return { |
---|
327 | success : rs.success, |
---|
328 | errors : errors |
---|
329 | }; |
---|
330 | } |
---|
331 | return Ext.decode(response.responseText); |
---|
332 | } |
---|
333 | }); |
---|
334 | |
---|
335 | |
---|
336 | /** |
---|
337 | * @class Ext.form.Action.Load |
---|
338 | * @extends Ext.form.Action |
---|
339 | * <p>A class which handles loading of data from a server into the Fields of an {@link Ext.form.BasicForm}.</p> |
---|
340 | * <p>Instances of this class are only created by a {@link Ext.form.BasicForm Form} when |
---|
341 | * {@link Ext.form.BasicForm#load load}ing.</p> |
---|
342 | * <p>A response packet <b>must</b> contain a boolean <tt style="font-weight:bold">success</tt> property, and |
---|
343 | * a <tt style="font-weight:bold">data</tt> property. The <tt style="font-weight:bold">data</tt> property |
---|
344 | * contains the values of Fields to load. The individual value object for each Field |
---|
345 | * is passed to the Field's {@link Ext.form.Field#setValue setValue} method.</p> |
---|
346 | * <p>By default, response packets are assumed to be JSON, so a typical response |
---|
347 | * packet may look like this:</p><pre><code> |
---|
348 | { |
---|
349 | success: true, |
---|
350 | data: { |
---|
351 | clientName: "Fred. Olsen Lines", |
---|
352 | portOfLoading: "FXT", |
---|
353 | portOfDischarge: "OSL" |
---|
354 | } |
---|
355 | }</code></pre> |
---|
356 | * <p>Other data may be placed into the response for processing the {@link Ext.form.BasicForm Form}'s callback |
---|
357 | * or event handler methods. The object decoded from this JSON is available in the {@link #result} property.</p> |
---|
358 | */ |
---|
359 | Ext.form.Action.Load = function(form, options){ |
---|
360 | Ext.form.Action.Load.superclass.constructor.call(this, form, options); |
---|
361 | this.reader = this.form.reader; |
---|
362 | }; |
---|
363 | |
---|
364 | Ext.extend(Ext.form.Action.Load, Ext.form.Action, { |
---|
365 | // private |
---|
366 | type : 'load', |
---|
367 | |
---|
368 | // private |
---|
369 | run : function(){ |
---|
370 | Ext.Ajax.request(Ext.apply( |
---|
371 | this.createCallback(this.options), { |
---|
372 | method:this.getMethod(), |
---|
373 | url:this.getUrl(false), |
---|
374 | headers: this.options.headers, |
---|
375 | params:this.getParams() |
---|
376 | })); |
---|
377 | }, |
---|
378 | |
---|
379 | // private |
---|
380 | success : function(response){ |
---|
381 | var result = this.processResponse(response); |
---|
382 | if(result === true || !result.success || !result.data){ |
---|
383 | this.failureType = Ext.form.Action.LOAD_FAILURE; |
---|
384 | this.form.afterAction(this, false); |
---|
385 | return; |
---|
386 | } |
---|
387 | this.form.clearInvalid(); |
---|
388 | this.form.setValues(result.data); |
---|
389 | this.form.afterAction(this, true); |
---|
390 | }, |
---|
391 | |
---|
392 | // private |
---|
393 | handleResponse : function(response){ |
---|
394 | if(this.form.reader){ |
---|
395 | var rs = this.form.reader.read(response); |
---|
396 | var data = rs.records && rs.records[0] ? rs.records[0].data : null; |
---|
397 | return { |
---|
398 | success : rs.success, |
---|
399 | data : data |
---|
400 | }; |
---|
401 | } |
---|
402 | return Ext.decode(response.responseText); |
---|
403 | } |
---|
404 | }); |
---|
405 | |
---|
406 | Ext.form.Action.ACTION_TYPES = { |
---|
407 | 'load' : Ext.form.Action.Load, |
---|
408 | 'submit' : Ext.form.Action.Submit |
---|
409 | }; |
---|