r784 - in trunk: tests ui

r784 - in trunk: tests ui


Author: pazu2k@gmail.com
Date: Mon Oct 6 10:20:57 2008
New Revision: 784
Modified:
trunk/tests/spinner.js
trunk/ui/ui.spinner.js
Log:
Spinner: add group and point options for improved support for international
numeric formats and added test case.
Modified: trunk/tests/spinner.js
==============================================================================
--- trunk/tests/spinner.js    (original)
+++ trunk/tests/spinner.js    Mon Oct 6 10:20:57 2008
@@ -56,7 +56,7 @@
});
test("defaults", function() {
-    expect(10);
+    expect(12);
    el = $("#spin").spinner();
    equals(el.data("currency.spinner"), false, "currency");
@@ -69,6 +69,8 @@
    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");
});
@@ -195,7 +197,7 @@
    el.simulate("keyup",{keyCode:$.simulate.VK_UP});
    equals(el.val(), "7.0", "keydown 11 times");
-
+    
});
test("spin without auto-incremental stepping", function() {
@@ -240,7 +242,7 @@
    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("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");
+    
});
Modified: trunk/ui/ui.spinner.js
==============================================================================
--- trunk/ui/ui.spinner.js    (original)
+++ trunk/ui/ui.spinner.js    Mon Oct 6 10:20:57 2008
@@ -26,7 +26,7 @@
        
        // 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 @@
        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 @@
        }
        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 @@
        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;
        }
    }