Spinner: add group and point options for improved support for international numeric formats and added test case.

This commit is contained in:
Ca-Phun Ung 2008-10-06 17:20:57 +00:00
parent f02dda5ade
commit 2f8d6547f2
2 changed files with 69 additions and 12 deletions

View File

@ -56,7 +56,7 @@ test("enable", function() {
});
test("defaults", function() {
expect(10);
expect(12);
el = $("#spin").spinner();
equals(el.data("currency.spinner"), false, "currency");
@ -69,6 +69,8 @@ test("defaults", function() {
equals(el.data("decimals.spinner"), 0, "decimals");
equals(el.data("format.spinner"), '%', "format");
equals(el.data("items.spinner"), false, "items");
equals(el.data("group.spinner"), '', "group");
equals(el.data("point.spinner"), '.', "point");
});
@ -240,7 +242,7 @@ test("spin with auto-incremental stepping", function() {
el.simulate("keyup",{keyCode:$.simulate.VK_DOWN});
equals(el.val(), '-1,800', "keydown 210 times (300-100-100*10-10*100)");
equals(el.val(), -1800, "keydown 210 times (300-100-100*10-10*100)");
});
@ -307,6 +309,55 @@ test("callback", function() {
test("mouse wheel on input", function() {
expect(0);
});
test("currency formats", function() {
expect(8);
// default
el = $("#spin").spinner({ currency: 'HK$', stepping: 1500.50, start: 1000 });
equals(el.val(), "HK$1,000.00", "Hong Kong Dollar");
el.simulate("keydown",{keyCode:$.simulate.VK_UP})
.simulate("keyup",{keyCode:$.simulate.VK_UP});
equals(el.val(), "HK$2,500.50", "Hong Kong Dollar step-up once");
// space and comma
el.spinner('destroy').val('').spinner({ currency: '$', group: ' ', point: '.', stepping: 1500.50, start: 1000 });
equals(el.val(), "$1 000.00", "Australian Dollar");
el.simulate("keydown",{keyCode:$.simulate.VK_UP})
.simulate("keyup",{keyCode:$.simulate.VK_UP});
equals(el.val(), "$2 500.50", "Australian Dollar step-up once");
// apos and point
el.spinner('destroy').val('').spinner({ currency: 'Fr ', group: "'", point: '.', stepping: 1500.50, start: 1000 });
equals(el.val(), "Fr 1'000.00", "Swiss Franc");
el.simulate("keydown",{keyCode:$.simulate.VK_UP})
.simulate("keyup",{keyCode:$.simulate.VK_UP});
equals(el.val(), "Fr 2'500.50", "Swiss Franc step-up once");
// point and comma
el.spinner('destroy').val('').spinner({ currency: 'RUB', group: ".", point: ',', stepping: 1.5, start: 1000 });
equals(el.val(), "RUB1.000,00", "Russian Ruble");
el.simulate("keydown",{keyCode:$.simulate.VK_UP})
.simulate("keyup",{keyCode:$.simulate.VK_UP});
equals(el.val(), "RUB1.001,50", "Russian Ruble step-up once");
});

View File

@ -26,7 +26,7 @@ $.widget('ui.spinner', {
// check for decimals in steppinng and set _decimals as internal
this._decimals = parseInt(this.options.decimals, 10);
if (this.options.stepping.toString().indexOf('.') != -1) {
if (this.options.stepping.toString().indexOf('.') != -1 && this._decimals == 0) {
var s = this.options.stepping.toString();
this._decimals = s.slice(s.indexOf('.')+1, s.length).length;
}
@ -283,7 +283,11 @@ $.widget('ui.spinner', {
e.preventDefault();
},
_getValue: function() {
return parseFloat(this.element.val().replace(/[^0-9\-\.]/g, ''));
var val = this.element.val().replace(this.options.point, '.');
if (this.options.group === '.') {
val = val.replace('.','');
}
return parseFloat(val.replace(/[^0-9\-\.]/g, ''));
},
_setValue: function(newVal) {
if (isNaN(newVal)) {
@ -291,8 +295,8 @@ $.widget('ui.spinner', {
}
this.element.val(
this.options.currency ?
$.ui.spinner.format.currency(newVal, this.options.currency) :
$.ui.spinner.format.number(newVal, this._decimals)
$.ui.spinner.format.currency(newVal, this.options.currency, this.options.group, this.options.point) :
$.ui.spinner.format.number(newVal, this._decimals, this.options.group, this.options.point)
);
},
_animate: function(d) {
@ -400,16 +404,18 @@ $.extend($.ui.spinner, {
incremental: true,
currency: false,
format: '%',
items: []
items: [],
group: '',
point: '.'
},
format: {
currency: function(num, sym) {
currency: function(num, sym, group, pt) {
num = isNaN(num) ? 0 : num;
return (num !== Math.abs(num) ? '-' : '') + sym + this.number(Math.abs(num), 2);
return (num !== Math.abs(num) ? '-' : '') + sym + this.number(Math.abs(num), 2, group || ',', pt);
},
number: function(num, dec) {
number: function(num, dec, group, pt) {
var regex = /(\d+)(\d{3})/;
for (num = isNaN(num) ? 0 : parseFloat(num,10).toFixed(dec); regex.test(num); num=num.replace(regex, '$1,$2'));
for (num = isNaN(num) ? 0 : parseFloat(num,10).toFixed(dec), num = num.replace('.', pt); regex.test(num) && group; num=num.replace(regex, '$1'+group+'$2'));
return num;
}
}