Roo.bootstrap.form.NumberField = function(config){
Roo.bootstrap.form.NumberField.superclass.constructor.call(this, config);
};
Roo.extend(Roo.bootstrap.form.NumberField, Roo.bootstrap.form.Input, {
allowDecimals : true,
decimalSeparator : ".",
decimalPrecision : 2,
allowNegative : true,
allowZero: 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",
thousandsDelimiter : false,
valueAlign : "left",
getAutoCreate : function()
{
var hiddenInput = {
tag: 'input',
type: 'hidden',
id: Roo.id(),
cls: 'hidden-number-input'
};
if (this.name) {
hiddenInput.name = this.name;
}
this.name = '';
var cfg = Roo.bootstrap.form.NumberField.superclass.getAutoCreate.call(this);
this.name = hiddenInput.name;
if(cfg.cn.length > 0) {
cfg.cn.push(hiddenInput);
}
return cfg;
},
initEvents : function()
{
Roo.bootstrap.form.NumberField.superclass.initEvents.call(this);
var allowed = "0123456789";
if(this.allowDecimals){
allowed += this.decimalSeparator;
}
if(this.allowNegative){
allowed += "-";
}
if(this.thousandsDelimiter) {
allowed += ",";
}
this.stripCharsRe = new RegExp('[^'+allowed+']', 'gi');
var keyPress = function(e){
var k = e.getKey();
var c = e.getCharCode();
if(
(String.fromCharCode(c) == '.' || String.fromCharCode(c) == '-') &&
allowed.indexOf(String.fromCharCode(c)) === -1
){
e.stopEvent();
return;
}
if(!Roo.isIE && (e.isSpecialKey() || k == e.BACKSPACE || k == e.DELETE)){
return;
}
if(allowed.indexOf(String.fromCharCode(c)) === -1){
e.stopEvent();
}
};
this.el.on("keypress", keyPress, this);
},
validateValue : function(value)
{
if(!Roo.bootstrap.form.NumberField.superclass.validateValue.call(this, value)){
return false;
}
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()
{
var v = this.hiddenEl().getValue();
return this.fixPrecision(this.parseValue(v));
},
parseValue : function(value)
{
if(this.thousandsDelimiter) {
value += "";
r = new RegExp(",", "g");
value = value.replace(r, "");
}
value = parseFloat(String(value).replace(this.decimalSeparator, "."));
return isNaN(value) ? '' : value;
},
fixPrecision : function(value)
{
if(this.thousandsDelimiter) {
value += "";
r = new RegExp(",", "g");
value = value.replace(r, "");
}
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 = String(this.fixPrecision(v)).replace(".", this.decimalSeparator);
this.value = v;
if(this.rendered){
this.hiddenEl().dom.value = (v === null || v === undefined ? '' : v);
this.inputEl().dom.value = (v == '') ? '' :
Roo.util.Format.number(v, this.decimalPrecision, this.thousandsDelimiter || '');
if(!this.allowZero && v === '0') {
this.hiddenEl().dom.value = '';
this.inputEl().dom.value = '';
}
this.validate();
}
},
decimalPrecisionFcn : function(v)
{
return Math.floor(v);
},
beforeBlur : function()
{
var v = this.parseValue(this.getRawValue());
if(v || v === 0 || v === ''){
this.setValue(v);
}
},
hiddenEl : function()
{
return this.el.select('input.hidden-number-input',true).first();
}
});