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){
70 this.response = response;
71 this.failureType = Roo.form.Action.CONNECT_FAILURE;
72 this.form.afterAction(this, false);
75 processResponse : function(response){
76 this.response = response;
77 if(!response.responseText){
80 this.result = this.handleResponse(response);
84 // utility functions used internally
85 getUrl : function(appendParams){
86 var url = this.options.url || this.form.url || this.form.el.dom.action;
88 var p = this.getParams();
90 url += (url.indexOf('?') != -1 ? '&' : '?') + p;
96 getMethod : function(){
97 return (this.options.method || this.form.method || this.form.el.dom.method || 'POST').toUpperCase();
100 getParams : function(){
101 var bp = this.form.baseParams;
102 var p = this.options.params;
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);
110 p = Roo.urlEncode(bp);
115 createCallback : function(){
117 success: this.success,
118 failure: this.failure,
120 timeout: (this.form.timeout*1000),
121 upload: this.form.fileUpload ? this.success : undefined
126 Roo.form.Action.Submit = function(form, options){
127 Roo.form.Action.Submit.superclass.constructor.call(this, form, options);
130 Roo.extend(Roo.form.Action.Submit, Roo.form.Action, {
133 haveProgress : false,
134 uploadComplete : false,
136 // uploadProgress indicator.
137 uploadProgress : function()
139 if (!this.form.progressUrl) {
143 if (!this.haveProgress) {
144 Roo.MessageBox.progress("Uploading", "Uploading");
146 if (this.uploadComplete) {
147 Roo.MessageBox.hide();
151 this.haveProgress = true;
153 var uid = this.form.findField('UPLOAD_IDENTIFIER').getValue();
155 var c = new Roo.data.Connection();
157 url : this.form.progressUrl,
162 success : function(data){
164 if (this.uploadComplete) {
165 Roo.MessageBox.hide();
170 Roo.MessageBox.updateProgress(data.bytes_uploaded/data.bytes_total,
171 Math.floor((data.bytes_total - data.bytes_uploaded)/1000) + 'k remaining'
174 this.uploadProgress.defer(2000,this);
177 failure: function(data) {
178 Roo.log('progress url failed ');
189 // run get Values on the form, so it syncs any secondary forms.
190 this.form.getValues();
192 var o = this.options;
193 var method = this.getMethod();
194 var isPost = method == 'POST';
195 if(o.clientValidation === false || this.form.isValid()){
197 if (this.form.progressUrl) {
198 this.findField('UPLOAD_IDENTIFIER').setValue(
199 (new Date() * 1) + '' + Math.random());
203 Roo.Ajax.request(Roo.apply(this.createCallback(), {
204 form:this.form.el.dom,
205 url:this.getUrl(!isPost),
207 params:isPost ? this.getParams() : null,
208 isUpload: this.form.fileUpload
211 this.uploadProgress();
213 }else if (o.clientValidation !== false){ // client validation failed
214 this.failureType = Roo.form.Action.CLIENT_INVALID;
215 this.form.afterAction(this, false);
219 success : function(response)
221 this.uploadComplete= true;
222 if (this.haveProgress) {
223 Roo.MessageBox.hide();
226 var result = this.processResponse(response);
227 if(result === true || result.success){
228 this.form.afterAction(this, true);
232 this.form.markInvalid(result.errors);
233 this.failureType = Roo.form.Action.SERVER_INVALID;
235 this.form.afterAction(this, false);
237 failure : function(response)
239 this.uploadComplete= true;
240 if (this.haveProgress) {
241 Roo.MessageBox.hide();
244 this.response = response;
245 this.failureType = Roo.form.Action.CONNECT_FAILURE;
246 this.form.afterAction(this, false);
249 handleResponse : function(response){
250 if(this.form.errorReader){
251 var rs = this.form.errorReader.read(response);
254 for(var i = 0, len = rs.records.length; i < len; i++) {
255 var r = rs.records[i];
259 if(errors.length < 1){
263 success : rs.success,
269 ret = Roo.decode(response.responseText);
273 errorMsg: "Failed to read server message: " + (response ? response.responseText : ' - no message'),
283 Roo.form.Action.Load = function(form, options){
284 Roo.form.Action.Load.superclass.constructor.call(this, form, options);
285 this.reader = this.form.reader;
288 Roo.extend(Roo.form.Action.Load, Roo.form.Action, {
292 Roo.Ajax.request(Roo.apply(
293 this.createCallback(), {
294 method:this.getMethod(),
295 url:this.getUrl(false),
296 params:this.getParams()
300 success : function(response){
301 var result = this.processResponse(response);
302 if(result === true || !result.success || !result.data){
303 this.failureType = Roo.form.Action.LOAD_FAILURE;
304 this.form.afterAction(this, false);
307 this.form.clearInvalid();
308 this.form.setValues(result.data);
309 this.form.afterAction(this, true);
312 handleResponse : function(response){
313 if(this.form.reader){
314 var rs = this.form.reader.read(response);
315 var data = rs.records && rs.records[0] ? rs.records[0].data : null;
317 success : rs.success,
321 return Roo.decode(response.responseText);
325 Roo.form.Action.ACTION_TYPES = {
326 'load' : Roo.form.Action.Load,
327 'submit' : Roo.form.Action.Submit