Roo/form/Action.js
[roojs1] / Roo / form / Action.js
1 /*
2  * Based on:
3  * Ext JS Library 1.1.1
4  * Copyright(c) 2006-2007, Ext JS, LLC.
5  *
6  * Originally Released Under LGPL - original licence link has changed is not relivant.
7  *
8  * Fork - LGPL
9  * <script type="text/javascript">
10  */
11  
12  /**
13  * @class Roo.form.Action
14  * Internal Class used to handle form actions
15  * @constructor
16  * @param {Roo.form.BasicForm} el The form element or its id
17  * @param {Object} config Configuration options
18  */
19  
20  
21 // define the action interface
22 Roo.form.Action = function(form, options){
23     this.form = form;
24     this.options = options || {};
25 };
26 /**
27  * Client Validation Failed
28  * @const 
29  */
30 Roo.form.Action.CLIENT_INVALID = 'client';
31 /**
32  * Server Validation Failed
33  * @const 
34  */
35  Roo.form.Action.SERVER_INVALID = 'server';
36  /**
37  * Connect to Server Failed
38  * @const 
39  */
40 Roo.form.Action.CONNECT_FAILURE = 'connect';
41 /**
42  * Reading Data from Server Failed
43  * @const 
44  */
45 Roo.form.Action.LOAD_FAILURE = 'load';
46
47 Roo.form.Action.prototype = {
48     type : 'default',
49     failureType : undefined,
50     response : undefined,
51     result : undefined,
52
53     // interface method
54     run : function(options){
55
56     },
57
58     // interface method
59     success : function(response){
60
61     },
62
63     // interface method
64     handleResponse : function(response){
65
66     },
67
68     // default connection failure
69     failure : function(response){
70         this.response = response;
71         this.failureType = Roo.form.Action.CONNECT_FAILURE;
72         this.form.afterAction(this, false);
73     },
74
75     processResponse : function(response){
76         this.response = response;
77         if(!response.responseText){
78             return true;
79         }
80         this.result = this.handleResponse(response);
81         return this.result;
82     },
83
84     // utility functions used internally
85     getUrl : function(appendParams){
86         var url = this.options.url || this.form.url || this.form.el.dom.action;
87         if(appendParams){
88             var p = this.getParams();
89             if(p){
90                 url += (url.indexOf('?') != -1 ? '&' : '?') + p;
91             }
92         }
93         return url;
94     },
95
96     getMethod : function(){
97         return (this.options.method || this.form.method || this.form.el.dom.method || 'POST').toUpperCase();
98     },
99
100     getParams : function(){
101         var bp = this.form.baseParams;
102         var p = this.options.params;
103         if(p){
104             if(typeof p == "object"){
105                 p = Roo.urlEncode(Roo.applyIf(p, bp));
106             }else if(typeof p == 'string' && bp){
107                 p += '&' + Roo.urlEncode(bp);
108             }
109         }else if(bp){
110             p = Roo.urlEncode(bp);
111         }
112         return p;
113     },
114
115     createCallback : function(){
116         return {
117             success: this.success,
118             failure: this.failure,
119             scope: this,
120             timeout: (this.form.timeout*1000),
121             upload: this.form.fileUpload ? this.success : undefined
122         };
123     }
124 };
125
126 Roo.form.Action.Submit = function(form, options){
127     Roo.form.Action.Submit.superclass.constructor.call(this, form, options);
128 };
129
130 Roo.extend(Roo.form.Action.Submit, Roo.form.Action, {
131     type : 'submit',
132
133     run : function()
134     {
135         // run get Values on the form, so it syncs any secondary forms.
136         this.form.getValues();
137         
138         var o = this.options;
139         var method = this.getMethod();
140         var isPost = method == 'POST';
141         if(o.clientValidation === false || this.form.isValid()){
142             if (this.form.findField( 'UPLOAD_IDENTIFIER')) {
143                 // use upload progress bar..
144             }
145             
146             
147             
148             Roo.Ajax.request(Roo.apply(this.createCallback(), {
149                 form:this.form.el.dom,
150                 url:this.getUrl(!isPost),
151                 method: method,
152                 params:isPost ? this.getParams() : null,
153                 isUpload: this.form.fileUpload
154             }));
155
156         }else if (o.clientValidation !== false){ // client validation failed
157             this.failureType = Roo.form.Action.CLIENT_INVALID;
158             this.form.afterAction(this, false);
159         }
160     },
161
162     success : function(response){
163         var result = this.processResponse(response);
164         if(result === true || result.success){
165             this.form.afterAction(this, true);
166             return;
167         }
168         if(result.errors){
169             this.form.markInvalid(result.errors);
170             this.failureType = Roo.form.Action.SERVER_INVALID;
171         }
172         this.form.afterAction(this, false);
173     },
174
175     handleResponse : function(response){
176         if(this.form.errorReader){
177             var rs = this.form.errorReader.read(response);
178             var errors = [];
179             if(rs.records){
180                 for(var i = 0, len = rs.records.length; i < len; i++) {
181                     var r = rs.records[i];
182                     errors[i] = r.data;
183                 }
184             }
185             if(errors.length < 1){
186                 errors = null;
187             }
188             return {
189                 success : rs.success,
190                 errors : errors
191             };
192         }
193         var ret = false;
194         try {
195             ret = Roo.decode(response.responseText);
196         } catch (e) {
197             ret = {
198                 success: false,
199                 errorMsg: "Failed to read server message: " + (response ? response.responseText : ' - no message'),
200                 errors : []
201             };
202         }
203         return ret;
204         
205     }
206 });
207
208
209 Roo.form.Action.Load = function(form, options){
210     Roo.form.Action.Load.superclass.constructor.call(this, form, options);
211     this.reader = this.form.reader;
212 };
213
214 Roo.extend(Roo.form.Action.Load, Roo.form.Action, {
215     type : 'load',
216
217     run : function(){
218         Roo.Ajax.request(Roo.apply(
219                 this.createCallback(), {
220                     method:this.getMethod(),
221                     url:this.getUrl(false),
222                     params:this.getParams()
223         }));
224     },
225
226     success : function(response){
227         var result = this.processResponse(response);
228         if(result === true || !result.success || !result.data){
229             this.failureType = Roo.form.Action.LOAD_FAILURE;
230             this.form.afterAction(this, false);
231             return;
232         }
233         this.form.clearInvalid();
234         this.form.setValues(result.data);
235         this.form.afterAction(this, true);
236     },
237
238     handleResponse : function(response){
239         if(this.form.reader){
240             var rs = this.form.reader.read(response);
241             var data = rs.records && rs.records[0] ? rs.records[0].data : null;
242             return {
243                 success : rs.success,
244                 data : data
245             };
246         }
247         return Roo.decode(response.responseText);
248     }
249 });
250
251 Roo.form.Action.ACTION_TYPES = {
252     'load' : Roo.form.Action.Load,
253     'submit' : Roo.form.Action.Submit
254 };