Roo.form.NumberField = function(config){
Roo.form.NumberField.superclass.constructor.call(this, config);
};
Roo.extend(Roo.form.NumberField, Roo.form.TextField, {
fieldClass: "x-form-field x-form-num-field",
allowDecimals : true,
decimalSeparator : ".",
thousandSeparator : "",
decimalPrecision : 2,
allowNegative : true,
minValue : Number.NEGATIVE_INFINITY,
maxValue : Number.MAX_VALUE,
minText : "The minimum value for this field is {0}",
maxText : "The maximum value for this field is {0}",
nanText : "{0} is not a valid number",
hiddenField : false,
onRender : function(ct, position)
{
Roo.form.TextField.superclass.onRender.call(this, ct, position);
Roo.log("Changing name?");
if (this.thousandSeparator != '') {
this.el.dom.setAttribute('name', this.name + '____hidden___' );
this.hiddenField = this.el.insertSibling({ tag:'input', type:'hidden', name: this.name },
'before', true);
this.hiddenField.value = this.value ? this.parseValue(this.value) : '';
this.el.on('blur', this.onBlur, this);
}
},
onBlur : function(){
this.beforeBlur();
if(!Roo.isOpera && this.focusClass){ this.el.removeClass(this.focusClass);
}
this.hasFocus = false;
if(this.validationEvent !== false && this.validateOnBlur && this.validationEvent != "blur"){
this.validate();
}
var v = this.getValue();
if(String(v) !== String(this.startValue)){
this.setValue( this.parseValue(v));
this.fireEvent('change', this, v, this.startValue);
}
this.fireEvent("blur", this);
},
getName: function(){
if (this.thousandSeparator != '') {
return this.name;
}
return Roo.form.TextField.superclass.getName.call(this);
},
initEvents : function(){
var allowed = "0123456789";
if(this.allowDecimals){
allowed += this.decimalSeparator;
}
allowed += this.thousandSeparator;
if(this.allowNegative){
allowed += "-";
}
this.stripCharsRe = new RegExp('[^'+allowed+']', 'gi');
var keyPress = function(e){
var k = e.getKey();
if(!Roo.isIE && (e.isSpecialKey() || k == e.BACKSPACE || k == e.DELETE)){
return;
}
var c = e.getCharCode();
if(allowed.indexOf(String.fromCharCode(c)) === -1){
e.stopEvent();
}
};
this.el.on("keypress", keyPress, this);
},
validateValue : function(value){
if(!Roo.form.NumberField.superclass.validateValue.call(this, value)){
return false;
}
if(value.length < 1){ return true;
}
var num = this.parseValue(value);
if(isNaN(num)){
this.markInvalid(String.format(this.nanText, value));
return false;
}
if(num < this.minValue){
this.markInvalid(String.format(this.minText, this.minValue));
return false;
}
if(num > this.maxValue){
this.markInvalid(String.format(this.maxText, this.maxValue));
return false;
}
return true;
},
getValue : function(){
return this.fixPrecision(this.parseValue(Roo.form.NumberField.superclass.getValue.call(this)));
},
parseValue : function(value){
value = parseFloat(String(value).replace(this.decimalSeparator, ".").split(this.thousandSeparator).join(''));
return isNaN(value) ? '' : value;
},
fixPrecision : function(value){
var nan = isNaN(value);
if(!this.allowDecimals || this.decimalPrecision == -1 || nan || !value){
return nan ? '' : value;
}
return parseFloat(value).toFixed(this.decimalPrecision);
},
setValue : function(v){
v = this.fixPrecision(v);
if(this.thousandSeparator != ''){
v = Roo.util.Format.number(v, this.decimalPrecision, this.thousandSeparator);
}
Roo.form.NumberField.superclass.setValue.call(this, String(v).replace(".", this.decimalSeparator));
if (this.hiddenField !== false) {
this.hiddenField.value = v ? this.parseValue(v) : '';
}
},
decimalPrecisionFcn : function(v){
return Math.floor(v);
},
beforeBlur : function(){
var v = this.parseValue(this.getRawValue());
if(v){
this.setValue(v);
}
}
});