Roo.namespace('Roo.form');
Roo.form.Action = function(form, options){
this.form = form;
this.options = options || {};
};
Roo.form.Action.CLIENT_INVALID = 'client';
Roo.form.Action.SERVER_INVALID = 'server';
Roo.form.Action.CONNECT_FAILURE = 'connect';
Roo.form.Action.LOAD_FAILURE = 'load';
Roo.form.Action.prototype = {
type : 'default',
failureType : undefined,
response : undefined,
result : undefined,
run : function(options){
},
success : function(response){
},
handleResponse : function(response){
},
failure : function(response){
this.response = response;
this.failureType = Roo.form.Action.CONNECT_FAILURE;
this.form.afterAction(this, false);
},
processResponse : function(response){
this.response = response;
if(!response.responseText){
return true;
}
this.result = this.handleResponse(response);
return this.result;
},
getUrl : function(appendParams){
var url = this.options.url || this.form.url || this.form.el.dom.action;
if(appendParams){
var p = this.getParams();
if(p){
url += (url.indexOf('?') != -1 ? '&' : '?') + p;
}
}
return url;
},
getMethod : function(){
return (this.options.method || this.form.method || this.form.el.dom.method || 'POST').toUpperCase();
},
getParams : function(){
var bp = this.form.baseParams;
var p = this.options.params;
if(p){
if(typeof p == "object"){
p = Roo.urlEncode(Roo.applyIf(p, bp));
}else if(typeof p == 'string' && bp){
p += '&' + Roo.urlEncode(bp);
}
}else if(bp){
p = Roo.urlEncode(bp);
}
return p;
},
createCallback : function(){
return {
success: this.success,
failure: this.failure,
scope: this,
timeout: (this.form.timeout*1000),
upload: this.form.fileUpload ? this.success : undefined
};
}
};
Roo.form.Action.Submit = function(form, options){
Roo.form.Action.Submit.superclass.constructor.call(this, form, options);
};
Roo.extend(Roo.form.Action.Submit, Roo.form.Action, {
type : 'submit',
haveProgress : false,
uploadComplete : false,
uploadProgress : function()
{
if (!this.form.progressUrl) {
return;
}
if (!this.haveProgress) {
Roo.MessageBox.progress("Uploading", "Uploading");
}
if (this.uploadComplete) {
Roo.MessageBox.hide();
return;
}
this.haveProgress = true;
var uid = this.form.findField('UPLOAD_IDENTIFIER').getValue();
var c = new Roo.data.Connection();
c.request({
url : this.form.progressUrl,
params: {
id : uid
},
method: 'GET',
success : function(req){
var rdata = false;
var edata;
try {
rdata = Roo.decode(req.responseText)
} catch (e) {
Roo.log("Invalid data from server..");
Roo.log(edata);
return;
}
if (!rdata || !rdata.success) {
Roo.log(rdata);
Roo.MessageBox.alert(Roo.encode(rdata));
return;
}
var data = rdata.data;
if (this.uploadComplete) {
Roo.MessageBox.hide();
return;
}
if (data){
Roo.MessageBox.updateProgress(data.bytes_uploaded/data.bytes_total,
Math.floor((data.bytes_total - data.bytes_uploaded)/1000) + 'k remaining'
);
}
this.uploadProgress.defer(2000,this);
},
failure: function(data) {
Roo.log('progress url failed ');
Roo.log(data);
},
scope : this
});
},
run : function()
{
this.form.getValues();
var o = this.options;
var method = this.getMethod();
var isPost = method == 'POST';
if(o.clientValidation === false || this.form.isValid()){
if (this.form.progressUrl) {
this.form.findField('UPLOAD_IDENTIFIER').setValue(
(new Date() * 1) + '' + Math.random());
}
Roo.Ajax.request(Roo.apply(this.createCallback(), {
form:this.form.el.dom,
url:this.getUrl(!isPost),
method: method,
params:isPost ? this.getParams() : null,
isUpload: this.form.fileUpload,
formData : this.form.formData
}));
this.uploadProgress();
}else if (o.clientValidation !== false){ this.failureType = Roo.form.Action.CLIENT_INVALID;
this.form.afterAction(this, false);
}
},
success : function(response)
{
this.uploadComplete= true;
if (this.haveProgress) {
Roo.MessageBox.hide();
}
var result = this.processResponse(response);
if(result === true || result.success){
this.form.afterAction(this, true);
return;
}
if(result.errors){
this.form.markInvalid(result.errors);
this.failureType = Roo.form.Action.SERVER_INVALID;
}
this.form.afterAction(this, false);
},
failure : function(response)
{
this.uploadComplete= true;
if (this.haveProgress) {
Roo.MessageBox.hide();
}
this.response = response;
this.failureType = Roo.form.Action.CONNECT_FAILURE;
this.form.afterAction(this, false);
},
handleResponse : function(response){
if(this.form.errorReader){
var rs = this.form.errorReader.read(response);
var errors = [];
if(rs.records){
for(var i = 0, len = rs.records.length; i < len; i++) {
var r = rs.records[i];
errors[i] = r.data;
}
}
if(errors.length < 1){
errors = null;
}
return {
success : rs.success,
errors : errors
};
}
var ret = false;
try {
ret = Roo.decode(response.responseText);
} catch (e) {
ret = {
success: false,
errorMsg: "Failed to read server message: " + (response ? response.responseText : ' - no message'),
errors : []
};
}
return ret;
}
});
Roo.form.Action.Load = function(form, options){
Roo.form.Action.Load.superclass.constructor.call(this, form, options);
this.reader = this.form.reader;
};
Roo.extend(Roo.form.Action.Load, Roo.form.Action, {
type : 'load',
run : function(){
Roo.Ajax.request(Roo.apply(
this.createCallback(), {
method:this.getMethod(),
url:this.getUrl(false),
params:this.getParams()
}));
},
success : function(response){
var result = this.processResponse(response);
if(result === true || !result.success || !result.data){
this.failureType = Roo.form.Action.LOAD_FAILURE;
this.form.afterAction(this, false);
return;
}
this.form.clearInvalid();
this.form.setValues(result.data);
this.form.afterAction(this, true);
},
handleResponse : function(response){
if(this.form.reader){
var rs = this.form.reader.read(response);
var data = rs.records && rs.records[0] ? rs.records[0].data : null;
return {
success : rs.success,
data : data
};
}
return Roo.decode(response.responseText);
}
});
Roo.form.Action.ACTION_TYPES = {
'load' : Roo.form.Action.Load,
'submit' : Roo.form.Action.Submit
};