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
20 Roo.namespace('Roo.form');
23 // define the action interface
24 Roo.form.Action = function(form, options){
26 this.options = options || {};
29 * Client Validation Failed
32 Roo.form.Action.CLIENT_INVALID = 'client';
34 * Server Validation Failed
37 Roo.form.Action.SERVER_INVALID = 'server';
39 * Connect to Server Failed
42 Roo.form.Action.CONNECT_FAILURE = 'connect';
44 * Reading Data from Server Failed
47 Roo.form.Action.LOAD_FAILURE = 'load';
49 Roo.form.Action.prototype = {
51 failureType : undefined,
56 run : function(options){
61 success : function(response){
66 handleResponse : function(response){
70 // default connection failure
71 failure : function(response){
73 this.response = response;
74 this.failureType = Roo.form.Action.CONNECT_FAILURE;
75 this.form.afterAction(this, false);
78 processResponse : function(response){
79 this.response = response;
80 if(!response.responseText){
83 this.result = this.handleResponse(response);
87 // utility functions used internally
88 getUrl : function(appendParams){
89 var url = this.options.url || this.form.url || this.form.el.dom.action;
91 var p = this.getParams();
93 url += (url.indexOf('?') != -1 ? '&' : '?') + p;
99 getMethod : function(){
100 return (this.options.method || this.form.method || this.form.el.dom.method || 'POST').toUpperCase();
103 getParams : function(){
104 var bp = this.form.baseParams;
105 var p = this.options.params;
107 if(typeof p == "object"){
108 p = Roo.urlEncode(Roo.applyIf(p, bp));
109 }else if(typeof p == 'string' && bp){
110 p += '&' + Roo.urlEncode(bp);
113 p = Roo.urlEncode(bp);
118 createCallback : function(){
120 success: this.success,
121 failure: this.failure,
123 timeout: (this.form.timeout*1000),
124 upload: this.form.fileUpload ? this.success : undefined
129 Roo.form.Action.Submit = function(form, options){
130 Roo.form.Action.Submit.superclass.constructor.call(this, form, options);
133 Roo.extend(Roo.form.Action.Submit, Roo.form.Action, {
136 haveProgress : false,
137 uploadComplete : false,
139 // uploadProgress indicator.
140 uploadProgress : function()
142 if (!this.form.progressUrl) {
146 if (!this.haveProgress) {
147 Roo.MessageBox.progress("Uploading", "Uploading");
149 if (this.uploadComplete) {
150 Roo.MessageBox.hide();
154 this.haveProgress = true;
156 var uid = this.form.findField('UPLOAD_IDENTIFIER').getValue();
158 var c = new Roo.data.Connection();
160 url : this.form.progressUrl,
165 success : function(req){
170 rdata = Roo.decode(req.responseText)
172 Roo.log("Invalid data from server..");
176 if (!rdata || !rdata.success) {
178 Roo.MessageBox.alert(Roo.encode(rdata));
181 var data = rdata.data;
183 if (this.uploadComplete) {
184 Roo.MessageBox.hide();
189 Roo.MessageBox.updateProgress(data.bytes_uploaded/data.bytes_total,
190 Math.floor((data.bytes_total - data.bytes_uploaded)/1000) + 'k remaining'
193 this.uploadProgress.defer(2000,this);
196 failure: function(data) {
197 Roo.log('progress url failed ');
208 // run get Values on the form, so it syncs any secondary forms.
209 this.form.getValues();
211 var o = this.options;
212 var method = this.getMethod();
213 var isPost = method == 'POST';
214 if(o.clientValidation === false || this.form.isValid()){
216 if (this.form.progressUrl) {
217 this.form.findField('UPLOAD_IDENTIFIER').setValue(
218 (new Date() * 1) + '' + Math.random());
223 Roo.Ajax.request(Roo.apply(this.createCallback(), {
224 form:this.form.el.dom,
225 url:this.getUrl(!isPost),
227 params:isPost ? this.getParams() : null,
228 isUpload: this.form.fileUpload
231 this.uploadProgress();
233 }else if (o.clientValidation !== false){ // client validation failed
234 this.failureType = Roo.form.Action.CLIENT_INVALID;
235 this.form.afterAction(this, false);
239 success : function(response)
241 this.uploadComplete= true;
242 if (this.haveProgress) {
243 Roo.MessageBox.hide();
247 var result = this.processResponse(response);
248 if(result === true || result.success){
249 this.form.afterAction(this, true);
253 this.form.markInvalid(result.errors);
254 this.failureType = Roo.form.Action.SERVER_INVALID;
256 this.form.afterAction(this, false);
258 failure : function(response)
260 this.uploadComplete= true;
261 if (this.haveProgress) {
262 Roo.MessageBox.hide();
265 this.response = response;
266 this.failureType = Roo.form.Action.CONNECT_FAILURE;
267 this.form.afterAction(this, false);
270 handleResponse : function(response){
271 if(this.form.errorReader){
272 var rs = this.form.errorReader.read(response);
275 for(var i = 0, len = rs.records.length; i < len; i++) {
276 var r = rs.records[i];
280 if(errors.length < 1){
284 success : rs.success,
290 ret = Roo.decode(response.responseText);
294 errorMsg: "Failed to read server message: " + (response ? response.responseText : ' - no message'),
304 Roo.form.Action.Load = function(form, options){
305 Roo.form.Action.Load.superclass.constructor.call(this, form, options);
306 this.reader = this.form.reader;
309 Roo.extend(Roo.form.Action.Load, Roo.form.Action, {
314 Roo.Ajax.request(Roo.apply(
315 this.createCallback(), {
316 method:this.getMethod(),
317 url:this.getUrl(false),
318 params:this.getParams()
322 success : function(response){
324 var result = this.processResponse(response);
325 if(result === true || !result.success || !result.data){
326 this.failureType = Roo.form.Action.LOAD_FAILURE;
327 this.form.afterAction(this, false);
330 this.form.clearInvalid();
331 this.form.setValues(result.data);
332 this.form.afterAction(this, true);
335 handleResponse : function(response){
336 if(this.form.reader){
337 var rs = this.form.reader.read(response);
338 var data = rs.records && rs.records[0] ? rs.records[0].data : null;
340 success : rs.success,
344 return Roo.decode(response.responseText);
348 Roo.form.Action.ACTION_TYPES = {
349 'load' : Roo.form.Action.Load,
350 'submit' : Roo.form.Action.Submit