3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
4 <title>The source code</title>
5 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
6 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
8 <body onload="prettyPrint();">
9 <pre class="prettyprint lang-js">/*!
10 * Ext JS Library 3.3.1
11 * Copyright(c) 2006-2010 Sencha Inc.
12 * licensing@sencha.com
13 * http://www.sencha.com/license
15 <div id="cls-Ext.form.Action"></div>/**
16 * @class Ext.form.Action
17 * <p>The subclasses of this class provide actions to perform upon {@link Ext.form.BasicForm Form}s.</p>
18 * <p>Instances of this class are only created by a {@link Ext.form.BasicForm Form} when
19 * the Form needs to perform an action such as submit or load. The Configuration options
20 * listed for this class are set through the Form's action methods: {@link Ext.form.BasicForm#submit submit},
21 * {@link Ext.form.BasicForm#load load} and {@link Ext.form.BasicForm#doAction doAction}</p>
22 * <p>The instance of Action which performed the action is passed to the success
23 * and failure callbacks of the Form's action methods ({@link Ext.form.BasicForm#submit submit},
24 * {@link Ext.form.BasicForm#load load} and {@link Ext.form.BasicForm#doAction doAction}),
25 * and to the {@link Ext.form.BasicForm#actioncomplete actioncomplete} and
26 * {@link Ext.form.BasicForm#actionfailed actionfailed} event handlers.</p>
28 Ext.form.Action = function(form, options){
30 this.options = options || {};
33 <div id="prop-Ext.form.Action-CLIENT_INVALID"></div>/**
34 * Failure type returned when client side validation of the Form fails
35 * thus aborting a submit action. Client side validation is performed unless
36 * {@link #clientValidation} is explicitly set to <tt>false</tt>.
40 Ext.form.Action.CLIENT_INVALID = 'client';
41 <div id="prop-Ext.form.Action-SERVER_INVALID"></div>/**
42 * <p>Failure type returned when server side processing fails and the {@link #result}'s
43 * <tt style="font-weight:bold">success</tt> property is set to <tt>false</tt>.</p>
44 * <p>In the case of a form submission, field-specific error messages may be returned in the
45 * {@link #result}'s <tt style="font-weight:bold">errors</tt> property.</p>
49 Ext.form.Action.SERVER_INVALID = 'server';
50 <div id="prop-Ext.form.Action-CONNECT_FAILURE"></div>/**
51 * Failure type returned when a communication error happens when attempting
52 * to send a request to the remote server. The {@link #response} may be examined to
53 * provide further information.
57 Ext.form.Action.CONNECT_FAILURE = 'connect';
58 <div id="prop-Ext.form.Action-LOAD_FAILURE"></div>/**
59 * Failure type returned when the response's <tt style="font-weight:bold">success</tt>
60 * property is set to <tt>false</tt>, or no field values are returned in the response's
61 * <tt style="font-weight:bold">data</tt> property.
65 Ext.form.Action.LOAD_FAILURE = 'load';
67 Ext.form.Action.prototype = {
68 <div id="cfg-Ext.form.Action-url"></div>/**
69 * @cfg {String} url The URL that the Action is to invoke.
71 <div id="cfg-Ext.form.Action-reset"></div>/**
72 * @cfg {Boolean} reset When set to <tt><b>true</b></tt>, causes the Form to be
73 * {@link Ext.form.BasicForm.reset reset} on Action success. If specified, this happens
74 * <b>before</b> the {@link #success} callback is called and before the Form's
75 * {@link Ext.form.BasicForm.actioncomplete actioncomplete} event fires.
77 <div id="cfg-Ext.form.Action-method"></div>/**
78 * @cfg {String} method The HTTP method to use to access the requested URL. Defaults to the
79 * {@link Ext.form.BasicForm}'s method, or if that is not specified, the underlying DOM form's method.
81 <div id="cfg-Ext.form.Action-params"></div>/**
82 * @cfg {Mixed} params <p>Extra parameter values to pass. These are added to the Form's
83 * {@link Ext.form.BasicForm#baseParams} and passed to the specified URL along with the Form's
85 * <p>Parameters are encoded as standard HTTP parameters using {@link Ext#urlEncode}.</p>
87 <div id="cfg-Ext.form.Action-timeout"></div>/**
88 * @cfg {Number} timeout The number of seconds to wait for a server response before
89 * failing with the {@link #failureType} as {@link #Action.CONNECT_FAILURE}. If not specified,
90 * defaults to the configured <tt>{@link Ext.form.BasicForm#timeout timeout}</tt> of the
91 * {@link Ext.form.BasicForm form}.
93 <div id="cfg-Ext.form.Action-success"></div>/**
94 * @cfg {Function} success The function to call when a valid success return packet is recieved.
95 * The function is passed the following parameters:<ul class="mdetail-params">
96 * <li><b>form</b> : Ext.form.BasicForm<div class="sub-desc">The form that requested the action</div></li>
97 * <li><b>action</b> : Ext.form.Action<div class="sub-desc">The Action class. The {@link #result}
98 * property of this object may be examined to perform custom postprocessing.</div></li>
101 <div id="cfg-Ext.form.Action-failure"></div>/**
102 * @cfg {Function} failure The function to call when a failure packet was recieved, or when an
103 * error ocurred in the Ajax communication.
104 * The function is passed the following parameters:<ul class="mdetail-params">
105 * <li><b>form</b> : Ext.form.BasicForm<div class="sub-desc">The form that requested the action</div></li>
106 * <li><b>action</b> : Ext.form.Action<div class="sub-desc">The Action class. If an Ajax
107 * error ocurred, the failure type will be in {@link #failureType}. The {@link #result}
108 * property of this object may be examined to perform custom postprocessing.</div></li>
111 <div id="cfg-Ext.form.Action-scope"></div>/**
112 * @cfg {Object} scope The scope in which to call the callback functions (The <tt>this</tt> reference
113 * for the callback functions).
115 <div id="cfg-Ext.form.Action-waitMsg"></div>/**
116 * @cfg {String} waitMsg The message to be displayed by a call to {@link Ext.MessageBox#wait}
117 * during the time the action is being processed.
119 <div id="cfg-Ext.form.Action-waitTitle"></div>/**
120 * @cfg {String} waitTitle The title to be displayed by a call to {@link Ext.MessageBox#wait}
121 * during the time the action is being processed.
124 <div id="cfg-Ext.form.Action-submitEmptyText"></div>/**
125 * @cfg {Boolean} submitEmptyText If set to <tt>true</tt>, the emptyText value will be sent with the form
126 * when it is submitted. Defaults to <tt>true</tt>.
129 <div id="prop-Ext.form.Action-type"></div>/**
130 * The type of action this Action instance performs.
131 * Currently only "submit" and "load" are supported.
135 <div id="prop-Ext.form.Action-failureType"></div>/**
136 * The type of failure detected will be one of these: {@link #CLIENT_INVALID},
137 * {@link #SERVER_INVALID}, {@link #CONNECT_FAILURE}, or {@link #LOAD_FAILURE}. Usage:
139 var fp = new Ext.form.FormPanel({
145 if(fp.getForm().isValid()){
146 fp.getForm().submit({
147 url: 'form-submit.php',
148 waitMsg: 'Submitting your data...',
149 success: function(form, action){
150 // server responded with success = true
151 var result = action.{@link #result};
153 failure: function(form, action){
154 if (action.{@link #failureType} === Ext.form.Action.{@link #CONNECT_FAILURE}) {
155 Ext.Msg.alert('Error',
156 'Status:'+action.{@link #response}.status+': '+
157 action.{@link #response}.statusText);
159 if (action.failureType === Ext.form.Action.{@link #SERVER_INVALID}){
160 // server responded with success = false
161 Ext.Msg.alert('Invalid', action.{@link #result}.errormsg);
170 fp.getForm().reset();
174 * @property failureType
177 <div id="prop-Ext.form.Action-response"></div>/**
178 * The XMLHttpRequest object used to perform the action.
182 <div id="prop-Ext.form.Action-result"></div>/**
183 * The decoded response object containing a boolean <tt style="font-weight:bold">success</tt> property and
184 * other, action-specific properties.
190 run : function(options){
195 success : function(response){
200 handleResponse : function(response){
204 // default connection failure
205 failure : function(response){
206 this.response = response;
207 this.failureType = Ext.form.Action.CONNECT_FAILURE;
208 this.form.afterAction(this, false);
212 // shared code among all Actions to validate that there was a response
213 // with either responseText or responseXml
214 processResponse : function(response){
215 this.response = response;
216 if(!response.responseText && !response.responseXML){
219 this.result = this.handleResponse(response);
223 // utility functions used internally
224 getUrl : function(appendParams){
225 var url = this.options.url || this.form.url || this.form.el.dom.action;
227 var p = this.getParams();
229 url = Ext.urlAppend(url, p);
236 getMethod : function(){
237 return (this.options.method || this.form.method || this.form.el.dom.method || 'POST').toUpperCase();
241 getParams : function(){
242 var bp = this.form.baseParams;
243 var p = this.options.params;
245 if(typeof p == "object"){
246 p = Ext.urlEncode(Ext.applyIf(p, bp));
247 }else if(typeof p == 'string' && bp){
248 p += '&' + Ext.urlEncode(bp);
251 p = Ext.urlEncode(bp);
257 createCallback : function(opts){
258 var opts = opts || {};
260 success: this.success,
261 failure: this.failure,
263 timeout: (opts.timeout*1000) || (this.form.timeout*1000),
264 upload: this.form.fileUpload ? this.success : undefined
269 <div id="cls-Ext.form.Action.Submit"></div>/**
270 * @class Ext.form.Action.Submit
271 * @extends Ext.form.Action
272 * <p>A class which handles submission of data from {@link Ext.form.BasicForm Form}s
273 * and processes the returned response.</p>
274 * <p>Instances of this class are only created by a {@link Ext.form.BasicForm Form} when
275 * {@link Ext.form.BasicForm#submit submit}ting.</p>
276 * <p><u><b>Response Packet Criteria</b></u></p>
277 * <p>A response packet may contain:
278 * <div class="mdetail-params"><ul>
279 * <li><b><code>success</code></b> property : Boolean
280 * <div class="sub-desc">The <code>success</code> property is required.</div></li>
281 * <li><b><code>errors</code></b> property : Object
282 * <div class="sub-desc"><div class="sub-desc">The <code>errors</code> property,
283 * which is optional, contains error messages for invalid fields.</div></li>
285 * <p><u><b>JSON Packets</b></u></p>
286 * <p>By default, response packets are assumed to be JSON, so a typical response
287 * packet may look like this:</p><pre><code>
291 clientCode: "Client not found",
292 portOfLoading: "This field must not be null"
295 * <p>Other data may be placed into the response for processing by the {@link Ext.form.BasicForm}'s callback
296 * or event handler methods. The object decoded from this JSON is available in the
297 * {@link Ext.form.Action#result result} property.</p>
298 * <p>Alternatively, if an {@link #errorReader} is specified as an {@link Ext.data.XmlReader XmlReader}:</p><pre><code>
299 errorReader: new Ext.data.XmlReader({
307 * <p>then the results may be sent back in XML format:</p><pre><code>
308 <?xml version="1.0" encoding="UTF-8"?>
309 <message success="false">
312 <id>clientCode</id>
313 <msg><![CDATA[Code not found. <br /><i>This is a test validation message from the server </i>]]></msg>
316 <id>portOfLoading</id>
317 <msg><![CDATA[Port not found. <br /><i>This is a test validation message from the server </i>]]></msg>
322 * <p>Other elements may be placed into the response XML for processing by the {@link Ext.form.BasicForm}'s callback
323 * or event handler methods. The XML document is available in the {@link #errorReader}'s {@link Ext.data.XmlReader#xmlData xmlData} property.</p>
325 Ext.form.Action.Submit = function(form, options){
326 Ext.form.Action.Submit.superclass.constructor.call(this, form, options);
329 Ext.extend(Ext.form.Action.Submit, Ext.form.Action, {
330 <div id="cfg-Ext.form.Action.Submit-errorReader"></div>/**
331 * @cfg {Ext.data.DataReader} errorReader <p><b>Optional. JSON is interpreted with
332 * no need for an errorReader.</b></p>
333 * <p>A Reader which reads a single record from the returned data. The DataReader's
334 * <b>success</b> property specifies how submission success is determined. The Record's
335 * data provides the error messages to apply to any invalid form Fields.</p>
337 <div id="cfg-Ext.form.Action.Submit-clientValidation"></div>/**
338 * @cfg {boolean} clientValidation Determines whether a Form's fields are validated
339 * in a final call to {@link Ext.form.BasicForm#isValid isValid} prior to submission.
340 * Pass <tt>false</tt> in the Form's submit options to prevent this. If not defined, pre-submission field validation
347 var o = this.options,
348 method = this.getMethod(),
349 isGet = method == 'GET';
350 if(o.clientValidation === false || this.form.isValid()){
351 if (o.submitEmptyText === false) {
352 var fields = this.form.items,
354 setupEmptyFields = function(f){
355 if (f.el.getValue() == f.emptyText) {
359 if(f.isComposite && f.rendered){
360 f.items.each(setupEmptyFields);
364 fields.each(setupEmptyFields);
366 Ext.Ajax.request(Ext.apply(this.createCallback(o), {
367 form:this.form.el.dom,
368 url:this.getUrl(isGet),
371 params:!isGet ? this.getParams() : null,
372 isUpload: this.form.fileUpload
374 if (o.submitEmptyText === false) {
375 Ext.each(emptyFields, function(f) {
376 if (f.applyEmptyText) {
381 }else if (o.clientValidation !== false){ // client validation failed
382 this.failureType = Ext.form.Action.CLIENT_INVALID;
383 this.form.afterAction(this, false);
388 success : function(response){
389 var result = this.processResponse(response);
390 if(result === true || result.success){
391 this.form.afterAction(this, true);
395 this.form.markInvalid(result.errors);
397 this.failureType = Ext.form.Action.SERVER_INVALID;
398 this.form.afterAction(this, false);
402 handleResponse : function(response){
403 if(this.form.errorReader){
404 var rs = this.form.errorReader.read(response);
407 for(var i = 0, len = rs.records.length; i < len; i++) {
408 var r = rs.records[i];
412 if(errors.length < 1){
416 success : rs.success,
420 return Ext.decode(response.responseText);
425 <div id="cls-Ext.form.Action.Load"></div>/**
426 * @class Ext.form.Action.Load
427 * @extends Ext.form.Action
428 * <p>A class which handles loading of data from a server into the Fields of an {@link Ext.form.BasicForm}.</p>
429 * <p>Instances of this class are only created by a {@link Ext.form.BasicForm Form} when
430 * {@link Ext.form.BasicForm#load load}ing.</p>
431 * <p><u><b>Response Packet Criteria</b></u></p>
432 * <p>A response packet <b>must</b> contain:
433 * <div class="mdetail-params"><ul>
434 * <li><b><code>success</code></b> property : Boolean</li>
435 * <li><b><code>data</code></b> property : Object</li>
436 * <div class="sub-desc">The <code>data</code> property contains the values of Fields to load.
437 * The individual value object for each Field is passed to the Field's
438 * {@link Ext.form.Field#setValue setValue} method.</div></li>
440 * <p><u><b>JSON Packets</b></u></p>
441 * <p>By default, response packets are assumed to be JSON, so for the following form load call:<pre><code>
442 var myFormPanel = new Ext.form.FormPanel({
443 title: 'Client and routing info',
445 fieldLabel: 'Client',
448 fieldLabel: 'Port of loading',
449 name: 'portOfLoading'
451 fieldLabel: 'Port of discharge',
452 name: 'portOfDischarge'
455 myFormPanel.{@link Ext.form.FormPanel#getForm getForm}().{@link Ext.form.BasicForm#load load}({
456 url: '/getRoutingInfo.php',
458 consignmentRef: myConsignmentRef
460 failure: function(form, action) {
461 Ext.Msg.alert("Load failed", action.result.errorMessage);
465 * a <b>success response</b> packet may look like this:</p><pre><code>
469 clientName: "Fred. Olsen Lines",
470 portOfLoading: "FXT",
471 portOfDischarge: "OSL"
474 * while a <b>failure response</b> packet may look like this:</p><pre><code>
477 errorMessage: "Consignment reference not found"
479 * <p>Other data may be placed into the response for processing the {@link Ext.form.BasicForm Form}'s
480 * callback or event handler methods. The object decoded from this JSON is available in the
481 * {@link Ext.form.Action#result result} property.</p>
483 Ext.form.Action.Load = function(form, options){
484 Ext.form.Action.Load.superclass.constructor.call(this, form, options);
485 this.reader = this.form.reader;
488 Ext.extend(Ext.form.Action.Load, Ext.form.Action, {
494 Ext.Ajax.request(Ext.apply(
495 this.createCallback(this.options), {
496 method:this.getMethod(),
497 url:this.getUrl(false),
498 headers: this.options.headers,
499 params:this.getParams()
504 success : function(response){
505 var result = this.processResponse(response);
506 if(result === true || !result.success || !result.data){
507 this.failureType = Ext.form.Action.LOAD_FAILURE;
508 this.form.afterAction(this, false);
511 this.form.clearInvalid();
512 this.form.setValues(result.data);
513 this.form.afterAction(this, true);
517 handleResponse : function(response){
518 if(this.form.reader){
519 var rs = this.form.reader.read(response);
520 var data = rs.records && rs.records[0] ? rs.records[0].data : null;
522 success : rs.success,
526 return Ext.decode(response.responseText);
532 <div id="cls-Ext.form.Action.DirectLoad"></div>/**
533 * @class Ext.form.Action.DirectLoad
534 * @extends Ext.form.Action.Load
535 * <p>Provides Ext.direct support for loading form data.</p>
536 * <p>This example illustrates usage of Ext.Direct to <b>load</b> a form through Ext.Direct.</p>
538 var myFormPanel = new Ext.form.FormPanel({
539 // configs for FormPanel
540 title: 'Basic Information',
541 renderTo: document.body,
542 width: 300, height: 160,
545 // configs apply to child items
546 defaults: {anchor: '100%'},
547 defaultType: 'textfield',
555 fieldLabel: 'Company',
559 // configs for BasicForm
561 // The server-side method to call for load() requests
562 load: Profile.getBasicInfo,
563 // The server-side must mark the submit handler as a 'formHandler'
564 submit: Profile.updateBasicInfo
566 // specify the order for the passed params
567 paramOrder: ['uid', 'foo']
571 myFormPanel.getForm().load({
572 // pass 2 arguments to server side getBasicInfo method (len=2)
579 * The data packet sent to the server will resemble something like:
583 "action":"Profile","method":"getBasicInfo","type":"rpc","tid":2,
584 "data":[34,"bar"] // note the order of the params
588 * The form will process a data packet returned by the server that is similar
589 * to the following format:
593 "action":"Profile","method":"getBasicInfo","type":"rpc","tid":2,
597 "name":"Fred Flintstone",
598 "company":"Slate Rock and Gravel",
599 "email":"fred.flintstone@slaterg.com"
606 Ext.form.Action.DirectLoad = Ext.extend(Ext.form.Action.Load, {
607 constructor: function(form, opts) {
608 Ext.form.Action.DirectLoad.superclass.constructor.call(this, form, opts);
613 var args = this.getParams();
614 args.push(this.success, this);
615 this.form.api.load.apply(window, args);
618 getParams : function() {
619 var buf = [], o = {};
620 var bp = this.form.baseParams;
621 var p = this.options.params;
623 var paramOrder = this.form.paramOrder;
625 for(var i = 0, len = paramOrder.length; i < len; i++){
626 buf.push(o[paramOrder[i]]);
628 }else if(this.form.paramsAsHash){
633 // Direct actions have already been processed and therefore
634 // we can directly set the result; Direct Actions do not have
635 // a this.response property.
636 processResponse : function(result) {
637 this.result = result;
641 success : function(response, trans){
642 if(trans.type == Ext.Direct.exceptions.SERVER){
645 Ext.form.Action.DirectLoad.superclass.success.call(this, response);
649 <div id="cls-Ext.form.Action.DirectSubmit"></div>/**
650 * @class Ext.form.Action.DirectSubmit
651 * @extends Ext.form.Action.Submit
652 * <p>Provides Ext.direct support for submitting form data.</p>
653 * <p>This example illustrates usage of Ext.Direct to <b>submit</b> a form through Ext.Direct.</p>
655 var myFormPanel = new Ext.form.FormPanel({
656 // configs for FormPanel
657 title: 'Basic Information',
658 renderTo: document.body,
659 width: 300, height: 160,
664 myFormPanel.getForm().submit({
673 // configs apply to child items
674 defaults: {anchor: '100%'},
675 defaultType: 'textfield',
683 fieldLabel: 'Company',
687 // configs for BasicForm
689 // The server-side method to call for load() requests
690 load: Profile.getBasicInfo,
691 // The server-side must mark the submit handler as a 'formHandler'
692 submit: Profile.updateBasicInfo
694 // specify the order for the passed params
695 paramOrder: ['uid', 'foo']
698 * The data packet sent to the server will resemble something like:
701 "action":"Profile","method":"updateBasicInfo","type":"rpc","tid":"6",
705 "extAction":"Profile","extMethod":"updateBasicInfo",
706 "extType":"rpc","extTID":"6","extUpload":"false",
707 "name":"Aaron Conran","email":"aaron@extjs.com","company":"Ext JS, LLC"
712 * The form will process a data packet returned by the server that is similar
715 // sample success packet (batched requests)
718 "action":"Profile","method":"updateBasicInfo","type":"rpc","tid":3,
725 // sample failure packet (one request)
727 "action":"Profile","method":"updateBasicInfo","type":"rpc","tid":"6",
730 "email":"already taken"
737 * Also see the discussion in {@link Ext.form.Action.DirectLoad}.
739 Ext.form.Action.DirectSubmit = Ext.extend(Ext.form.Action.Submit, {
740 constructor : function(form, opts) {
741 Ext.form.Action.DirectSubmit.superclass.constructor.call(this, form, opts);
743 type : 'directsubmit',
744 // override of Submit
746 var o = this.options;
747 if(o.clientValidation === false || this.form.isValid()){
748 // tag on any additional params to be posted in the
750 this.success.params = this.getParams();
751 this.form.api.submit(this.form.el.dom, this.success, this);
752 }else if (o.clientValidation !== false){ // client validation failed
753 this.failureType = Ext.form.Action.CLIENT_INVALID;
754 this.form.afterAction(this, false);
758 getParams : function() {
760 var bp = this.form.baseParams;
761 var p = this.options.params;
765 // Direct actions have already been processed and therefore
766 // we can directly set the result; Direct Actions do not have
767 // a this.response property.
768 processResponse : function(result) {
769 this.result = result;
773 success : function(response, trans){
774 if(trans.type == Ext.Direct.exceptions.SERVER){
777 Ext.form.Action.DirectSubmit.superclass.success.call(this, response);
781 Ext.form.Action.ACTION_TYPES = {
782 'load' : Ext.form.Action.Load,
783 'submit' : Ext.form.Action.Submit,
784 'directload' : Ext.form.Action.DirectLoad,
785 'directsubmit' : Ext.form.Action.DirectSubmit