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 if (this.form.maskEl) {
71 this.form.maskEl.unmask();
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) {
180 var data = rdata.data;
182 if (this.uploadComplete) {
183 Roo.MessageBox.hide();
188 Roo.MessageBox.updateProgress(data.bytes_uploaded/data.bytes_total,
189 Math.floor((data.bytes_total - data.bytes_uploaded)/1000) + 'k remaining'
192 this.uploadProgress.defer(2000,this);
195 failure: function(data) {
196 Roo.log('progress url failed ');
207 // run get Values on the form, so it syncs any secondary forms.
208 this.form.getValues();
210 var o = this.options;
211 var method = this.getMethod();
212 var isPost = method == 'POST';
213 if(o.clientValidation === false || this.form.isValid()){
215 if (this.form.progressUrl) {
216 this.form.findField('UPLOAD_IDENTIFIER').setValue(
217 (new Date() * 1) + '' + Math.random());
220 if (this.form.maskEl) {
221 this.form.maskEl.mask("Saving");
224 Roo.Ajax.request(Roo.apply(this.createCallback(), {
225 form:this.form.el.dom,
226 url:this.getUrl(!isPost),
228 params:isPost ? this.getParams() : null,
229 isUpload: this.form.fileUpload
232 this.uploadProgress();
234 }else if (o.clientValidation !== false){ // client validation failed
235 this.failureType = Roo.form.Action.CLIENT_INVALID;
236 this.form.afterAction(this, false);
240 success : function(response)
242 this.uploadComplete= true;
243 if (this.haveProgress) {
244 Roo.MessageBox.hide();
246 if (this.form.maskEl) {
247 this.form.maskEl.unmask();
250 var result = this.processResponse(response);
251 if(result === true || result.success){
252 this.form.afterAction(this, true);
256 this.form.markInvalid(result.errors);
257 this.failureType = Roo.form.Action.SERVER_INVALID;
259 this.form.afterAction(this, false);
261 failure : function(response)
263 this.uploadComplete= true;
264 if (this.haveProgress) {
265 Roo.MessageBox.hide();
267 if (this.form.maskEl) {
268 this.form.maskEl.unmask();
271 this.response = response;
272 this.failureType = Roo.form.Action.CONNECT_FAILURE;
273 this.form.afterAction(this, false);
276 handleResponse : function(response){
277 if(this.form.errorReader){
278 var rs = this.form.errorReader.read(response);
281 for(var i = 0, len = rs.records.length; i < len; i++) {
282 var r = rs.records[i];
286 if(errors.length < 1){
290 success : rs.success,
296 ret = Roo.decode(response.responseText);
300 errorMsg: "Failed to read server message: " + (response ? response.responseText : ' - no message'),
310 Roo.form.Action.Load = function(form, options){
311 Roo.form.Action.Load.superclass.constructor.call(this, form, options);
312 this.reader = this.form.reader;
315 Roo.extend(Roo.form.Action.Load, Roo.form.Action, {
319 if (this.form.maskEl) {
320 this.form.maskEl.mask("Loading");
322 Roo.Ajax.request(Roo.apply(
323 this.createCallback(), {
324 method:this.getMethod(),
325 url:this.getUrl(false),
326 params:this.getParams()
330 success : function(response){
331 if (this.form.maskEl) {
332 this.form.maskEl.unmask();
334 var result = this.processResponse(response);
335 if(result === true || !result.success || !result.data){
336 this.failureType = Roo.form.Action.LOAD_FAILURE;
337 this.form.afterAction(this, false);
340 this.form.clearInvalid();
341 this.form.setValues(result.data);
342 this.form.afterAction(this, true);
345 handleResponse : function(response){
346 if(this.form.reader){
347 var rs = this.form.reader.read(response);
348 var data = rs.records && rs.records[0] ? rs.records[0].data : null;
350 success : rs.success,
354 return Roo.decode(response.responseText);
358 Roo.form.Action.ACTION_TYPES = {
359 'load' : Roo.form.Action.Load,
360 'submit' : Roo.form.Action.Submit