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() { test("defaults", function() {
expect(10); expect(12);
el = $("#spin").spinner(); el = $("#spin").spinner();
equals(el.data("currency.spinner"), false, "currency"); equals(el.data("currency.spinner"), false, "currency");
@ -69,6 +69,8 @@ test("defaults", function() {
equals(el.data("decimals.spinner"), 0, "decimals"); equals(el.data("decimals.spinner"), 0, "decimals");
equals(el.data("format.spinner"), '%', "format"); equals(el.data("format.spinner"), '%', "format");
equals(el.data("items.spinner"), false, "items"); equals(el.data("items.spinner"), false, "items");
equals(el.data("group.spinner"), '', "group");
equals(el.data("point.spinner"), '.', "point");
}); });
@ -195,7 +197,7 @@ test("decimal options", function() {
el.simulate("keyup",{keyCode:$.simulate.VK_UP}); el.simulate("keyup",{keyCode:$.simulate.VK_UP});
equals(el.val(), "7.0", "keydown 11 times"); equals(el.val(), "7.0", "keydown 11 times");
}); });
test("spin without auto-incremental stepping", function() { test("spin without auto-incremental stepping", function() {
@ -240,7 +242,7 @@ test("spin with auto-incremental stepping", function() {
el.simulate("keyup",{keyCode:$.simulate.VK_DOWN}); 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() { test("mouse wheel on input", function() {
expect(0); 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 // check for decimals in steppinng and set _decimals as internal
this._decimals = parseInt(this.options.decimals, 10); 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(); var s = this.options.stepping.toString();
this._decimals = s.slice(s.indexOf('.')+1, s.length).length; this._decimals = s.slice(s.indexOf('.')+1, s.length).length;
} }
@ -283,7 +283,11 @@ $.widget('ui.spinner', {
e.preventDefault(); e.preventDefault();
}, },
_getValue: function() { _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) { _setValue: function(newVal) {
if (isNaN(newVal)) { if (isNaN(newVal)) {
@ -291,8 +295,8 @@ $.widget('ui.spinner', {
} }
this.element.val( this.element.val(
this.options.currency ? this.options.currency ?
$.ui.spinner.format.currency(newVal, this.options.currency) : $.ui.spinner.format.currency(newVal, this.options.currency, this.options.group, this.options.point) :
$.ui.spinner.format.number(newVal, this._decimals) $.ui.spinner.format.number(newVal, this._decimals, this.options.group, this.options.point)
); );
}, },
_animate: function(d) { _animate: function(d) {
@ -400,16 +404,18 @@ $.extend($.ui.spinner, {
incremental: true, incremental: true,
currency: false, currency: false,
format: '%', format: '%',
items: [] items: [],
group: '',
point: '.'
}, },
format: { format: {
currency: function(num, sym) { currency: function(num, sym, group, pt) {
num = isNaN(num) ? 0 : num; 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})/; 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; return num;
} }
} }