4 * Copyright(c) 2006-2007, Ext JS, LLC.
6 * Originally Released Under LGPL - original licence link has changed is not relivant.
9 * <script type="text/javascript">
13 * @class Roo.form.Action
14 * Internal Class used to handle form actions
16 * @param {Roo.form.BasicForm} el The form element or its id
17 * @param {Object} config Configuration options
21 // define the action interface
22 Roo.form.Action = function(form, options){
24 this.options = options || {};
27 * Client Validation Failed
30 Roo.form.Action.CLIENT_INVALID = 'client';
32 * Server Validation Failed
35 Roo.form.Action.SERVER_INVALID = 'server';
37 * Connect to Server Failed
40 Roo.form.Action.CONNECT_FAILURE = 'connect';
42 * Reading Data from Server Failed
45 Roo.form.Action.LOAD_FAILURE = 'load';
47 Roo.form.Action.prototype = {
49 failureType : undefined,
54 run : function(options){
59 success : function(response){
64 handleResponse : function(response){
68 // default connection failure
69 failure : function(response){
71 this.response = response;
72 this.failureType = Roo.form.Action.CONNECT_FAILURE;
73 this.form.afterAction(this, false);
76 processResponse : function(response){
77 this.response = response;
78 if(!response.responseText){
81 this.result = this.handleResponse(response);
85 // utility functions used internally
86 getUrl : function(appendParams){
87 var url = this.options.url || this.form.url || this.form.el.dom.action;
89 var p = this.getParams();
91 url += (url.indexOf('?') != -1 ? '&' : '?') + p;
97 getMethod : function(){
98 return (this.options.method || this.form.method || this.form.el.dom.method || 'POST').toUpperCase();
101 getParams : function(){
102 var bp = this.form.baseParams;
103 var p = this.options.params;
105 if(typeof p == "object"){
106 p = Roo.urlEncode(Roo.applyIf(p, bp));
107 }else if(typeof p == 'string' && bp){
108 p += '&' + Roo.urlEncode(bp);
111 p = Roo.urlEncode(bp);
116 createCallback : function(){
118 success: this.success,
119 failure: this.failure,
121 timeout: (this.form.timeout*1000),
122 upload: this.form.fileUpload ? this.success : undefined
127 Roo.form.Action.Submit = function(form, options){
128 Roo.form.Action.Submit.superclass.constructor.call(this, form, options);
131 Roo.extend(Roo.form.Action.Submit, Roo.form.Action, {
134 haveProgress : false,
135 uploadComplete : false,
137 // uploadProgress indicator.
138 uploadProgress : function()
140 if (!this.form.progressUrl) {
144 if (!this.haveProgress) {
145 Roo.MessageBox.progress("Uploading", "Uploading");
147 if (this.uploadComplete) {
148 Roo.MessageBox.hide();
152 this.haveProgress = true;
154 var uid = this.form.findField('UPLOAD_IDENTIFIER').getValue();
156 var c = new Roo.data.Connection();
158 url : this.form.progressUrl,
163 success : function(req){
168 rdata = Roo.decode(req.responseText)
170 Roo.log("Invalid data from server..");
174 if (!rdata || !rdata.success) {
176 Roo.MessageBox.alert(Roo.encode(rdata));
179 var data = rdata.data;
181 if (this.uploadComplete) {
182 Roo.MessageBox.hide();
187 Roo.MessageBox.updateProgress(data.bytes_uploaded/data.bytes_total,
188 Math.floor((data.bytes_total - data.bytes_uploaded)/1000) + 'k remaining'
191 this.uploadProgress.defer(2000,this);
194 failure: function(data) {
195 Roo.log('progress url failed ');
206 // run get Values on the form, so it syncs any secondary forms.
207 this.form.getValues();
209 var o = this.options;
210 var method = this.getMethod();
211 var isPost = method == 'POST';
212 if(o.clientValidation === false || this.form.isValid()){
214 if (this.form.progressUrl) {
215 this.form.findField('UPLOAD_IDENTIFIER').setValue(
216 (new Date() * 1) + '' + Math.random());
221 Roo.Ajax.request(Roo.apply(this.createCallback(), {
222 form:this.form.el.dom,
223 url:this.getUrl(!isPost),
225 params:isPost ? this.getParams() : null,
226 isUpload: this.form.fileUpload
229 this.uploadProgress();
231 }else if (o.clientValidation !== false){ // client validation failed
232 this.failureType = Roo.form.Action.CLIENT_INVALID;
233 this.form.afterAction(this, false);
237 success : function(response)
239 this.uploadComplete= true;
240 if (this.haveProgress) {
241 Roo.MessageBox.hide();
245 var result = this.processResponse(response);
246 if(result === true || result.success){
247 this.form.afterAction(this, true);
251 this.form.markInvalid(result.errors);
252 this.failureType = Roo.form.Action.SERVER_INVALID;
254 this.form.afterAction(this, false);
256 failure : function(response)
258 this.uploadComplete= true;
259 if (this.haveProgress) {
260 Roo.MessageBox.hide();
263 this.response = response;
264 this.failureType = Roo.form.Action.CONNECT_FAILURE;
265 this.form.afterAction(this, false);
268 handleResponse : function(response){
269 if(this.form.errorReader){
270 var rs = this.form.errorReader.read(response);
273 for(var i = 0, len = rs.records.length; i < len; i++) {
274 var r = rs.records[i];
278 if(errors.length < 1){
282 success : rs.success,
288 ret = Roo.decode(response.responseText);
292 errorMsg: "Failed to read server message: " + (response ? response.responseText : ' - no message'),
302 Roo.form.Action.Load = function(form, options){
303 Roo.form.Action.Load.superclass.constructor.call(this, form, options);
304 this.reader = this.form.reader;
307 Roo.extend(Roo.form.Action.Load, Roo.form.Action, {
312 Roo.Ajax.request(Roo.apply(
313 this.createCallback(), {
314 method:this.getMethod(),
315 url:this.getUrl(false),
316 params:this.getParams()
320 success : function(response){
322 var result = this.processResponse(response);
323 if(result === true || !result.success || !result.data){
324 this.failureType = Roo.form.Action.LOAD_FAILURE;
325 this.form.afterAction(this, false);
328 this.form.clearInvalid();
329 this.form.setValues(result.data);
330 this.form.afterAction(this, true);
333 handleResponse : function(response){
334 if(this.form.reader){
335 var rs = this.form.reader.read(response);
336 var data = rs.records && rs.records[0] ? rs.records[0].data : null;
338 success : rs.success,
342 return Roo.decode(response.responseText);
346 Roo.form.Action.ACTION_TYPES = {
347 'load' : Roo.form.Action.Load,
348 'submit' : Roo.form.Action.Submit