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">
12 // as we use this in bootstrap.
13 Roo.namespace('Roo.form');
15 * @class Roo.form.Action
16 * Internal Class used to handle form actions
18 * @param {Roo.form.BasicForm} el The form element or its id
19 * @param {Object} config Configuration options
24 // define the action interface
25 Roo.form.Action = function(form, options){
27 this.options = options || {};
30 * Client Validation Failed
33 Roo.form.Action.CLIENT_INVALID = 'client';
35 * Server Validation Failed
38 Roo.form.Action.SERVER_INVALID = 'server';
40 * Connect to Server Failed
43 Roo.form.Action.CONNECT_FAILURE = 'connect';
45 * Reading Data from Server Failed
48 Roo.form.Action.LOAD_FAILURE = 'load';
50 Roo.form.Action.prototype = {
52 failureType : undefined,
57 run : function(options){
62 success : function(response){
67 handleResponse : function(response){
71 // default connection failure
72 failure : function(response){
74 this.response = response;
75 this.failureType = Roo.form.Action.CONNECT_FAILURE;
76 this.form.afterAction(this, false);
79 processResponse : function(response){
80 this.response = response;
81 if(!response.responseText){
84 this.result = this.handleResponse(response);
88 // utility functions used internally
89 getUrl : function(appendParams){
90 var url = this.options.url || this.form.url || this.form.el.dom.action;
92 var p = this.getParams();
94 url += (url.indexOf('?') != -1 ? '&' : '?') + p;
100 getMethod : function(){
101 return (this.options.method || this.form.method || this.form.el.dom.method || 'POST').toUpperCase();
104 getParams : function(){
105 var bp = this.form.baseParams;
106 var p = this.options.params;
108 if(typeof p == "object"){
109 p = Roo.urlEncode(Roo.applyIf(p, bp));
110 }else if(typeof p == 'string' && bp){
111 p += '&' + Roo.urlEncode(bp);
114 p = Roo.urlEncode(bp);
119 createCallback : function(){
121 success: this.success,
122 failure: this.failure,
124 timeout: (this.form.timeout*1000),
125 upload: this.form.fileUpload ? this.success : undefined
130 Roo.form.Action.Submit = function(form, options){
131 Roo.form.Action.Submit.superclass.constructor.call(this, form, options);
134 Roo.extend(Roo.form.Action.Submit, Roo.form.Action, {
137 haveProgress : false,
138 uploadComplete : false,
140 // uploadProgress indicator.
141 uploadProgress : function()
143 if (!this.form.progressUrl) {
147 if (!this.haveProgress) {
148 Roo.MessageBox.progress("Uploading", "Uploading");
150 if (this.uploadComplete) {
151 Roo.MessageBox.hide();
155 this.haveProgress = true;
157 var uid = this.form.findField('UPLOAD_IDENTIFIER').getValue();
159 var c = new Roo.data.Connection();
161 url : this.form.progressUrl,
166 success : function(req){
171 rdata = Roo.decode(req.responseText)
173 Roo.log("Invalid data from server..");
177 if (!rdata || !rdata.success) {
179 Roo.MessageBox.alert(Roo.encode(rdata));
182 var data = rdata.data;
184 if (this.uploadComplete) {
185 Roo.MessageBox.hide();
190 Roo.MessageBox.updateProgress(data.bytes_uploaded/data.bytes_total,
191 Math.floor((data.bytes_total - data.bytes_uploaded)/1000) + 'k remaining'
194 this.uploadProgress.defer(2000,this);
197 failure: function(data) {
198 Roo.log('progress url failed ');
209 // run get Values on the form, so it syncs any secondary forms.
210 this.form.getValues();
212 var o = this.options;
213 var method = this.getMethod();
214 var isPost = method == 'POST';
216 if(o.clientValidation === false || this.form.isValid()){
218 if (this.form.progressUrl) {
219 this.form.findField('UPLOAD_IDENTIFIER').setValue(
220 (new Date() * 1) + '' + Math.random());
225 Roo.Ajax.request(Roo.apply(this.createCallback(), {
226 form:this.form.el.dom,
227 url:this.getUrl(!isPost),
229 params:isPost ? this.getParams() : null,
230 isUpload: this.form.fileUpload
233 this.uploadProgress();
235 }else if (o.clientValidation !== false){ // client validation failed
236 this.failureType = Roo.form.Action.CLIENT_INVALID;
237 this.form.afterAction(this, false);
241 success : function(response)
243 this.uploadComplete= true;
244 if (this.haveProgress) {
245 Roo.MessageBox.hide();
249 var result = this.processResponse(response);
250 if(result === true || result.success){
251 this.form.afterAction(this, true);
255 this.form.markInvalid(result.errors);
256 this.failureType = Roo.form.Action.SERVER_INVALID;
258 this.form.afterAction(this, false);
260 failure : function(response)
262 this.uploadComplete= true;
263 if (this.haveProgress) {
264 Roo.MessageBox.hide();
267 this.response = response;
268 this.failureType = Roo.form.Action.CONNECT_FAILURE;
269 this.form.afterAction(this, false);
272 handleResponse : function(response){
273 if(this.form.errorReader){
274 var rs = this.form.errorReader.read(response);
277 for(var i = 0, len = rs.records.length; i < len; i++) {
278 var r = rs.records[i];
282 if(errors.length < 1){
286 success : rs.success,
292 ret = Roo.decode(response.responseText);
296 errorMsg: "Failed to read server message: " + (response ? response.responseText : ' - no message'),
306 Roo.form.Action.Load = function(form, options){
307 Roo.form.Action.Load.superclass.constructor.call(this, form, options);
308 this.reader = this.form.reader;
311 Roo.extend(Roo.form.Action.Load, Roo.form.Action, {
316 Roo.Ajax.request(Roo.apply(
317 this.createCallback(), {
318 method:this.getMethod(),
319 url:this.getUrl(false),
320 params:this.getParams()
324 success : function(response){
326 var result = this.processResponse(response);
327 if(result === true || !result.success || !result.data){
328 this.failureType = Roo.form.Action.LOAD_FAILURE;
329 this.form.afterAction(this, false);
332 this.form.clearInvalid();
333 this.form.setValues(result.data);
334 this.form.afterAction(this, true);
337 handleResponse : function(response){
338 if(this.form.reader){
339 var rs = this.form.reader.read(response);
340 var data = rs.records && rs.records[0] ? rs.records[0].data : null;
342 success : rs.success,
346 return Roo.decode(response.responseText);
350 Roo.form.Action.ACTION_TYPES = {
351 'load' : Roo.form.Action.Load,
352 'submit' : Roo.form.Action.Submit