This repository was archived by the owner on Sep 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumber_field.js
More file actions
91 lines (86 loc) · 2.58 KB
/
number_field.js
File metadata and controls
91 lines (86 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*globals Ext, SWorks, console */
/*jslint glovar: true, undef: true, nomen: true */
// This allows you to override the parseValue function to allow
// things like '12 ft' -> 144. 'baseChars' would need to be set
// appropriately too.
Ext.override(Ext.form.NumberField, {
validateValue : function(value){
if(!Ext.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;
}
});
Ext.form.NumberField.fractionRe = /^\s*(\d+)\s*\/\s*(\d+)\s*$/;
Ext.form.NumberField.conversions = [
{ re: /^\s*(.+)\s*ft?\s*$/, multi: 12 },
{ re: /^\s*(.+)\s*hr?\s*$/, multi: 60 }
];
Ext.override(Ext.form.NumberField, {
baseChars: "0123456789 fthr",
initEvents : function(){
Ext.form.NumberField.superclass.initEvents.call(this);
var allowed = this.baseChars+'';
if(this.allowDecimals){
allowed += this.decimalSeparator;
allowed += '/'; // This is the only line added in this function
}
if(this.allowNegative){
allowed += "-";
}
this.stripCharsRe = new RegExp('[^'+allowed+']', 'gi');
var keyPress = function(e){
var k = e.getKey();
if(!Ext.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);
},
parseValue: function(value) {
var con = this.conversions || Ext.form.NumberField.conversions;
var multi = 1;
for(var i=0;i<con.length;i++) {
var set = con[i];
var match = set.re.exec(value);
if(match) {
value = match[1];
multi = set.multi;
break;
}
}
var fracMatch = Ext.form.NumberField.fractionRe.exec(value);
if(fracMatch) {
if(!isNaN(fracMatch[1]) && !isNaN(fracMatch[2]) &&
(fracMatch[1] !== 0) && (fracMatch[2] !== 0)) {
value = fracMatch[1] / fracMatch[2];
}
}
value = parseFloat(String(value).replace(this.decimalSeparator, "."));
if(isNaN(value)) {
return '';
} else {
return (value * multi);
}
}
});