-
Notifications
You must be signed in to change notification settings - Fork 6
/
Currency.js
195 lines (166 loc) · 6.79 KB
/
Currency.js
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/* * GNU General Public License Usage
* This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file. Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
*
* http://www.gnu.org/licenses/lgpl.html
*
* @description: This class provide additional format to numbers by extending Ext.form.field.Number
*
* @author: Greivin Britton
* @contributor: Wemerson Januario ([email protected])
* @email: [email protected]
* @version: 2 compatible with ExtJS 4 (And ExtJS 5 - chamacs)
*
*/
Ext.define('Ext.ux.form.field.Currency', {
extend: 'Ext.form.field.Number',//Extending the NumberField
xtype: [
'currencyfield',
'numericfield'
],
alternateClassName: ['Ext.ux.form.field.Numeric', 'Ext.ux.form.NumericField'],
currencySymbol: '$ ',
useThousandSeparator: true,
thousandSeparator: '.',
alwaysDisplayDecimals: true,
// MOD - chamacs
// @private
isCurrency: false,
// MOD - pmiguelmartins
currencySymbolPos: 'left', // left , right
// MOD - chamacs
//fieldStyle: 'text-align: right;',
// MOD - chamacs
allowExponential: false,
/**
* initComponent
*/
initComponent: function () {
if (this.useThousandSeparator && this.decimalSeparator == ',' && this.thousandSeparator == ',') {
this.thousandSeparator = '.';
}
else if (this.allowDecimals && this.thousandSeparator == '.' && this.decimalSeparator == '.') {
this.decimalSeparator = ',';
}
// MOD - chamacs
this.isCurrency = !Ext.isEmpty(this.currencySymbol);
this.callParent(arguments);
},
/**
* setValue
*/
setValue: function (value) {
var me = this,
bind, valueBind;
// This portion of the code is to prevent a binding from stomping over
// the typed value. Say we have decimalPrecision 4 and the user types
// 1.23456. The value of the field will be set as 1.2346 and published to
// the viewmodel, which will trigger the binding to fire and setValue to
// be called on the field, which would then set the value (and rawValue) to
// 1.2346. Instead, if we have focus and the value is the same, just leave
// the rawValue alone
if (me.hasFocus) {
bind = me.getBind();
valueBind = bind && bind.value;
if (valueBind && valueBind.syncing && value === me.value) {
return me;
}
}
// MOD - chamacs
Ext.ux.form.field.Currency.superclass.setValue.apply(this, [value != null ? value.toString().replace('.', this.decimalSeparator) : value]);
this.setRawValue(this.getFormattedValue(this.getValue()));
},
/**
* getFormattedValue
*/
getFormattedValue: function (value) {
if (Ext.isEmpty(value) || !this.hasFormat()) {
return value;
}
else {
var neg = null;
value = (neg = value < 0) ? value * -1 : value;
value = this.allowDecimals && this.alwaysDisplayDecimals ? value.toFixed(this.decimalPrecision) : value;
if (this.useThousandSeparator) {
if (this.useThousandSeparator && Ext.isEmpty(this.thousandSeparator)) {
throw ('NumberFormatException: invalid thousandSeparator, property must has a valid character.');
}
if (this.thousandSeparator == this.decimalSeparator) {
throw ('NumberFormatException: invalid thousandSeparator, thousand separator must be different from decimalSeparator.');
}
value = value.toString();
var ps = value.split('.');
ps[1] = ps[1] ? ps[1] : null;
var whole = ps[0];
var r = /(\d+)(\d{3})/;
var ts = this.thousandSeparator;
while (r.test(whole)) {
whole = whole.replace(r, '$1' + ts + '$2');
}
value = whole + (ps[1] ? this.decimalSeparator + ps[1] : '');
}
// MOD - pmiguelmartins - updated by chamacs
var position1 = this.isCurrency ? this.currencySymbol + ' ' : '';
var position2 = value;
if (this.currencySymbolPos === 'right') {
position1 = value;
position2 = this.isCurrency ? ' ' + this.currencySymbol : '';
}
return Ext.String.format('{0}{1}{2}', position1, (neg ? '-' : ''), position2);
}
},
/**
* overrides parseValue to remove the format applied by this class
*/
parseValue: function (value) {
// MOD - chamacs
//Replace the currency symbol and thousand separator
return Ext.ux.form.field.Currency.superclass.parseValue.apply(this, [this.removeFormat(value)]);
},
/**
* Remove only the format added by this class to let the superclass validate with it's rules.
* @param {Object} value
*/
removeFormat: function (value) {
// MOD - chamacs
if (Ext.isEmpty(value)) {
return '';
}
else if (!this.hasFormat()) {
return value;
}
else {
// MOD - bhaidaya
value = Ext.String.trim(value.toString().replace(this.currencySymbol, ''));
value = this.useThousandSeparator ? value.replace(new RegExp('[' + this.thousandSeparator + ']', 'g'), '') : value;
return value;
}
},
/** * Remove the format before validating the the value.
* @param {Number} value
*/
getErrors: function (value) {
//FIX data binding by antiplaka
value = arguments.length > 0 ? value : this.processRawValue(this.getRawValue());
return this.callParent([this.removeFormat(value)]);
},
/**
* hasFormat
*/
hasFormat: function () {
return this.decimalSeparator != '.' || (this.useThousandSeparator == true && this.getRawValue() != null) || !Ext.isEmpty(this.currencySymbol) || this.alwaysDisplayDecimals;
},
/**
* Display the numeric value with the fixed decimal precision and without the format using the setRawValue, don't need to do a setValue because we don't want a double
* formatting and process of the value because beforeBlur perform a getRawValue and then a setValue.
*/
onFocus: function () {
this.setRawValue(this.removeFormat(this.getRawValue()));
this.callParent(arguments);
},
/**
* MOD - Jeff.Evans
*/
processRawValue: function (value) {
return this.removeFormat(value);
}
});