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) {
178 var data = rdata.data;
180 if (this.uploadComplete) {
181 Roo.MessageBox.hide();
186 Roo.MessageBox.updateProgress(data.bytes_uploaded/data.bytes_total,
187 Math.floor((data.bytes_total - data.bytes_uploaded)/1000) + 'k remaining'
190 this.uploadProgress.defer(2000,this);
193 failure: function(data) {
194 Roo.log('progress url failed ');
205 // run get Values on the form, so it syncs any secondary forms.
206 this.form.getValues();
208 var o = this.options;
209 var method = this.getMethod();
210 var isPost = method == 'POST';
211 if(o.clientValidation === false || this.form.isValid()){
213 if (this.form.progressUrl) {
214 this.form.findField('UPLOAD_IDENTIFIER').setValue(
215 (new Date() * 1) + '' + Math.random());
220 Roo.Ajax.request(Roo.apply(this.createCallback(), {
221 form:this.form.el.dom,
222 url:this.getUrl(!isPost),
224 params:isPost ? this.getParams() : null,
225 isUpload: this.form.fileUpload
228 this.uploadProgress();
230 }else if (o.clientValidation !== false){ // client validation failed
231 this.failureType = Roo.form.Action.CLIENT_INVALID;
232 this.form.afterAction(this, false);
236 success : function(response)
238 this.uploadComplete= true;
239 if (this.haveProgress) {
240 Roo.MessageBox.hide();
244 var result = this.processResponse(response);
245 if(result === true || result.success){
246 this.form.afterAction(this, true);
250 this.form.markInvalid(result.errors);
251 this.failureType = Roo.form.Action.SERVER_INVALID;
253 this.form.afterAction(this, false);
255 failure : function(response)
257 this.uploadComplete= true;
258 if (this.haveProgress) {
259 Roo.MessageBox.hide();
262 this.response = response;
263 this.failureType = Roo.form.Action.CONNECT_FAILURE;
264 this.form.afterAction(this, false);
267 handleResponse : function(response){
268 if(this.form.errorReader){
269 var rs = this.form.errorReader.read(response);
272 for(var i = 0, len = rs.records.length; i < len; i++) {
273 var r = rs.records[i];
277 if(errors.length < 1){
281 success : rs.success,
287 ret = Roo.decode(response.responseText);
291 errorMsg: "Failed to read server message: " + (response ? response.responseText : ' - no message'),
301 Roo.form.Action.Load = function(form, options){
302 Roo.form.Action.Load.superclass.constructor.call(this, form, options);
303 this.reader = this.form.reader;
306 Roo.extend(Roo.form.Action.Load, Roo.form.Action, {
311 Roo.Ajax.request(Roo.apply(
312 this.createCallback(), {
313 method:this.getMethod(),
314 url:this.getUrl(false),
315 params:this.getParams()
319 success : function(response){
321 var result = this.processResponse(response);
322 if(result === true || !result.success || !result.data){
323 this.failureType = Roo.form.Action.LOAD_FAILURE;
324 this.form.afterAction(this, false);
327 this.form.clearInvalid();
328 this.form.setValues(result.data);
329 this.form.afterAction(this, true);
332 handleResponse : function(response){
333 if(this.form.reader){
334 var rs = this.form.reader.read(response);
335 var data = rs.records && rs.records[0] ? rs.records[0].data : null;
337 success : rs.success,
341 return Roo.decode(response.responseText);
345 Roo.form.Action.ACTION_TYPES = {
346 'load' : Roo.form.Action.Load,
347 'submit' : Roo.form.Action.Submit