Ext.define('Ext.form.field.Field', {
isFormField : true,
disabled : false,
submitValue: true,
validateOnChange: true,
suspendCheckChange: 0,
initField: function() {
this.addEvents(
'change',
'validitychange',
'dirtychange'
);
this.initValue();
},
initValue: function() {
var me = this;
me.value = me.transformOriginalValue(me.value);
me.originalValue = me.lastValue = me.value;
me.suspendCheckChange++;
me.setValue(me.value);
me.suspendCheckChange--;
},
transformOriginalValue: function(value){
return value;
},
getName: function() {
return this.name;
},
getValue: function() {
return this.value;
},
setValue: function(value) {
var me = this;
me.value = value;
me.checkChange();
return me;
},
isEqual: function(value1, value2) {
return String(value1) === String(value2);
},
isEqualAsString: function(value1, value2){
return String(Ext.value(value1, '')) === String(Ext.value(value2, ''));
},
getSubmitData: function() {
var me = this,
data = null;
if (!me.disabled && me.submitValue && !me.isFileUpload()) {
data = {};
data[me.getName()] = '' + me.getValue();
}
return data;
},
getModelData: function() {
var me = this,
data = null;
if (!me.disabled && !me.isFileUpload()) {
data = {};
data[me.getName()] = me.getValue();
}
return data;
},
reset : function(){
var me = this;
me.beforeReset();
me.setValue(me.originalValue);
me.clearInvalid();
delete me.wasValid;
},
beforeReset: Ext.emptyFn,
resetOriginalValue: function() {
this.originalValue = this.getValue();
this.checkDirty();
},
checkChange: function() {
if (!this.suspendCheckChange) {
var me = this,
newVal = me.getValue(),
oldVal = me.lastValue;
if (!me.isEqual(newVal, oldVal) && !me.isDestroyed) {
me.lastValue = newVal;
me.fireEvent('change', me, newVal, oldVal);
me.onChange(newVal, oldVal);
}
}
},
onChange: function(newVal, oldVal) {
if (this.validateOnChange) {
this.validate();
}
this.checkDirty();
},
isDirty : function() {
var me = this;
return !me.disabled && !me.isEqual(me.getValue(), me.originalValue);
},
checkDirty: function() {
var me = this,
isDirty = me.isDirty();
if (isDirty !== me.wasDirty) {
me.fireEvent('dirtychange', me, isDirty);
me.onDirtyChange(isDirty);
me.wasDirty = isDirty;
}
},
onDirtyChange: Ext.emptyFn,
getErrors: function(value) {
return [];
},
isValid : function() {
var me = this;
return me.disabled || Ext.isEmpty(me.getErrors());
},
validate : function() {
var me = this,
isValid = me.isValid();
if (isValid !== me.wasValid) {
me.wasValid = isValid;
me.fireEvent('validitychange', me, isValid);
}
return isValid;
},
batchChanges: function(fn) {
try {
this.suspendCheckChange++;
fn();
} catch(e){
throw e;
} finally {
this.suspendCheckChange--;
}
this.checkChange();
},
isFileUpload: function() {
return false;
},
extractFileInput: function() {
return null;
},
markInvalid: Ext.emptyFn,
clearInvalid: Ext.emptyFn
});